<template>
	<view>
		<uni-card>
			<text>{{ '工单号:' + workOrder }}</text>
			<ScanInput @scanConfirm="scanConfirm"></ScanInput>
		</uni-card>
		<uni-card v-for="(item, index) in materialList" :key="index" :title="item.title">
			<uni-forms ref="formRef" :modelValue="item">
				<uni-forms-item label="已扫箱" required name="cases">
					<uni-easyinput type="number" :clearable="false" v-model.number="item.cases" />
				</uni-forms-item>
				<uni-forms-item label="零件数" required name="quantity">
					<uni-easyinput type="number" :clearable="false" v-model.number="item.quantity" />
				</uni-forms-item>
			</uni-forms>
			<!-- <uni-list style="width: 100%">
				<uni-list-item  :title="item.materialCode" :rightText="item.materialName"></uni-list-item>
			</uni-list> -->
		</uni-card>
		<uni-row :gutter="20">
			<uni-col :span="12">
				<button type="default" @click="clear">清空</button>
			</uni-col>
			<uni-col :span="12">
				<button type="primary" @click="submit()">提交</button>
			</uni-col>
		</uni-row>
	</view>
</template>

<script>
import { analysisScanCode } from '@/api/scan/index';
import { generateIngredientTask } from '@/api/preparationTask/index.js';
import { warn } from 'vue';
export default {
	onLoad: function (option) {
		this.workOrder = option.workOrder;
		this.$nextTick(() => {});
	},
	data() {
		return {
			formRef: null,
			workOrder: '',
			clearMaterialList: [],
			materialList: []
		};
	},
	methods: {
		// 扫码结果
		scanConfirm(val) {
			analysisScanCode({ materialCode: val }).then((res) => {
				if (res.code === 200) {
					console.log(res.data);
					const _data = res.data;
					let options = {
						title: `${_data.partnumber} ${_data.materialName}`,
						materialCode: _data.partnumber,
						materialName: _data.materialName,
						specification: _data.specification,
						color: _data.color,
						unit: _data.unit,
						cases: 1,
						quantity: 0
					};
					let _index = this.hasMaterial(options.materialCode);
					if (_index === -1) {
						this.materialList.push(options);
					} else {
						this.materialList[_index].cases += 1;
					}
				}
			});
		},
		// 是否已存在此零件
		hasMaterial(materialCode) {
			let index = 0;
			const list = this.materialList;
			for (index; index < list.length; index++) {
				if (list[index].materialCode === materialCode) {
					return index;
				}
			}
			return -1;
		},
		clear() {
			this.materialList = JSON.parse(JSON.stringify(this.clearMaterialList));
		},
		submit(ref) {
			if (!this.formValidate()) {
				return;
			}
			const postData = {
				Ingredient_task: this.materialList,
				workorder: this.workOrder
			};
			generateIngredientTask(postData).then((res) => {
				if (res.code === 200) {
					uni.showToast({
						icon: 'success',
						title: '提交成功'
					});
					this.clear();
				}
			});
		},
		// 手动校验
		formValidate() {
			if(this.materialList.length === 0){
				uni.showToast({
					icon: 'none',
					title: '无零件'
				});
				return false;
			}
			for (let item of this.materialList) {
				if (item.quantity === 0) {
					uni.showToast({
						icon: 'none',
						title: item.materialCode + '零件号不可为0'
					});
					return false;
				}
				if (item.quantity === '') {
					uni.showToast({
						icon: 'none',
						title: item.materialCode + '零件号不可为空'
					});
					return false;
				}
			}
			return true;
		}
	}
};
</script>

<style></style>
