<template>
	<view style="background-color: #fff;">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="作业编码" :labelWidth='80' name="sourceCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" type="text" v-model="formData.sourceCode"
							@confirm="scanBarCode" />
					</uni-forms-item>
				</uni-col>
				<!-- 				<uni-col :span="24">
					<uni-forms-item label="工单" :labelWidth='80' name="pwoCode">
						<uni-easyinput disabled type="text" v-model="formData.pwoCode" />
					</uni-forms-item>
				</uni-col> -->
				<uni-col :span="24">
					<uni-forms-item label="物料编码" :labelWidth='80' name="materialCode">
						<uni-easyinput disabled type="text" v-model="formData.qcQualityBillDetailList.materialCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="物料名称" :labelWidth='80' name="materialName">
						<uni-easyinput disabled type="text" v-model="formData.qcQualityBillDetailList.materialName" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="制程编码" :labelWidth='80' name="opCode">
						<uni-easyinput type="text" v-model="formData.qcQualityBillDetailList.opCode" />
						<uni-data-select v-model="formData.qcQualityBillDetailList.opCode" :localdata="pPbomOptions" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="质检类型" :labelWidth='80' name="type">
						<uni-data-select v-model="formData.type" :localdata="typeOptions" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="质检标准" :labelWidth='80' name="qualityStandardCode">
						<uni-data-select v-model="formData.qcQualityBillDetailList.qualityStandardCode"
							:localdata="standardOptions" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="实收数量" :labelWidth='80' name="actualNumber">
						<uni-easyinput type="number" v-model="formData.qcQualityBillDetailList.actualNumber" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="质检数量" :labelWidth='80' name="number">
						<uni-easyinput type="number" v-model="formData.qcQualityBillDetailList.number" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="" :labelWidth='80'>
						<uni-data-checkbox v-model="radio" multiple :localdata="qcchecked" @change="qcAllCheck"
							style="transform:scale(1.4);"></uni-data-checkbox>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="质检员" :labelWidth='90' name="qualityBy">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBarQualityBy" @confirm="confirmQualityBy"
							v-model="formData.qualityBy" type="text" style="margin-bottom: 5px;" />
						<uni-data-select v-model="formData.qualityBy" :localdata="empList" />
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listQualityBill,
		addQualityBill,
		getQualityBill,
		updateQualityBill,
		listQualityBillByBatchNo,
		getStandardList,
		listStandardList,
		listStandardApply
	} from "@/api/qc/qc.js";
	import {
		listPwoJob
	} from "@/api/mes/jobReport.js"
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		listPbom
	} from "@/api/qc/ppom";
	import {
		listEmployee
	} from "@/api/mes/jobIn";
	export default {
		mounted() {
			getDicts("qc_bill_type").then(res => {
				this.typeOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			listEmployee().then((res) => {
				this.empList = res.rows.map(item => {
					return {
						text: item.empCode + ':' + item.name,
						value: item.empCode,
						diasble: false
					}
				});
			})
		},
		data() {
			return {
				empList: [],
				radio: [],
				qcchecked: [{
					text: '全选',
					value: 0
				}],
				typeOptions: [],
				pPbomOptions: [],
				standardOptions: [],
				formData: {
					qcQualityBillDetailList: {
						qualityBy: null,
						materialCode: null,
						materialName: null,
						qualityStandardCode: null,
						opCode: null,
						actualNumber: null,
						number: null,
					},
					type: null,
					pwoCode: null,
					sourceCode: null,
					qualityBy: null
				},
				rules: {
					pwoJobCode: {
						rules: [{
							required: true,
							errorMessage: '请输入作业编码!'
						}]
					},
				},
			}
		},
		watch: {
			'formData.qcQualityBillDetailList.materialCode': {
				handler: function(newVal, oldVal) {
					if (newVal != null) {
						this.selectStandardOptions();
					}
				}
			},
			'formData.qcQualityBillDetailList.opCode': {
				handler: function(newVal, oldVal) {
					if (newVal != null) {
						this.selectStandardOptions();
					}

				}
			},
			'formData.type': {
				handler: function(newVal, oldVal) {
					if (newVal != null) {
						this.selectStandardOptions();
					}
				}
			},
		},
		methods: {
			scanBarQualityBy() {
				const _this = this;
				uni.scanCode({
					scanType: ['qrCode', 'barCode'],
					success: function(res) {
						_this.formData.qualityBy = res.result;
						_this.formData.qcQualityBillDetailList.qualityBy = res.result;
						_this.confirmQualityBy()
					}
				});
			},
			confirmQualityBy() {
				listEmployee({
					empCode: this.formData.qualityBy
				}).then(async res => {
					if (res.rows.length == 0) {
						this.$modal.msg("未查询到该人员，请重新输入！")
						this.formData.qualityBy = null;
						this.formData.qcQualityBillDetailList.qualityBy = null;
					}
				})
			},
			qcAllCheck(e) {
				if (e.detail.data.length > 0) {
					this.formData.qcQualityBillDetailList.number = this.formData.qcQualityBillDetailList
						.actualNumber
				} else {
					this.formData.qcQualityBillDetailList.number = null;
				}
			},
			submit() {
				this.$refs.form.validate().then(res => {
					this.formData.qcQualityBillDetailList.qualityBy = this.formData.qualityBy
					this.formData.qcQualityBillDetailList = [this.formData.qcQualityBillDetailList]
					if (this.formData && this.formData.qcQualityBillDetailList.length > 0) {
						//默认新建质检单结果为通过,状态为新建
						this.formData.status = '0'
						// this.qcform.result = '1'
						console.log(this.formData)
						this.$modal.loading('提交中')
						addQualityBill(this.formData).then(res => {
							this.$modal.closeLoading();
							this.$modal.msgSuccess("创建成功！");
							setTimeout(() => {
								this.$tab.switchTab("/pages/work/index");
							}, 500);
						});
					} else {
						this.$message.error("请输入明细！");
					}
				})
			},
			scanBarCode() {
				if (this.formData.sourceCode) {
					listPwoJob({
						code: this.formData.sourceCode
					}).then(async res => {
						let jobs = res.rows;
						if (jobs.length == 0) {
							this.$modal.msg("未检索到该作业编码相关的信息！");
						} else {
							let pwoJob = jobs[0];
							// 根据物料编码找制程
							this.selectMaterial(pwoJob.ptNoTar);
							// 添加明细
							this.formData.qcQualityBillDetailList.materialCode = pwoJob.ptNoTar;
							this.formData.qcQualityBillDetailList.materialName = pwoJob.ptTitleTar;
							this.formData.qcQualityBillDetailList.materialBatchNo = pwoJob.batchNo;
							this.formData.qcQualityBillDetailList.materialLotNo = pwoJob.lotNo;
							this.formData.qcQualityBillDetailList.opCode = pwoJob.opCode;
							this.formData.qcQualityBillDetailList.planNumber = pwoJob.targetNum;
							this.formData.qcQualityBillDetailList.qualityTypeCode = "";
							this.formData.qcQualityBillDetailList.qualityBy = "";
							this.formData.qcQualityBillDetailList.qualityStandardCode = "";
							this.formData.qcQualityBillDetailList.planNumber = 0;
							this.formData.qcQualityBillDetailList.actualNumber = pwoJob.targetNum;
							this.formData.qcQualityBillDetailList.number = 0;
							this.formData.qcQualityBillDetailList.unitId = "";
							this.formData.qcQualityBillDetailList.secondActualNumber = "";
							this.formData.qcQualityBillDetailList.secondNumber = "";
							this.formData.qcQualityBillDetailList.secondUnit = "";
							this.formData.qcQualityBillDetailList.passNumber = 0;
							this.formData.qcQualityBillDetailList.passUnit = "";
							this.formData.qcQualityBillDetailList.secondPassNumber = "";
							this.formData.qcQualityBillDetailList.secondPassUnit = "";
							this.formData.qcQualityBillDetailList.failReason = "";
							this.formData.qcQualityBillDetailList.enclosure = "";
							this.formData.qcQualityBillDetailList.remark = "";
						}
					})
				}
			},
			selectMaterial(materialCode) {
				let ppbom = {};
				ppbom.concatPnCode = materialCode;
				ppbom.filterSearch = true;
				if (ppbom.concatPnCode != "" && ppbom.concatPnCode) {
					listPbom(ppbom).then((response) => {

						this.pPbomOptions = response.rows.map(item => {
							item.text = item.opCode + ":" + item.opTitle;
							item.value = item.opCode;
							return item
						});
					});
				}
			},
			selectStandardOptions() {
				let materialCode = this.formData.qcQualityBillDetailList.materialCode;
				let opCode = this.formData.qcQualityBillDetailList.opCode;
				let type = this.formData.type;
				let standardOptions = [];
				if (materialCode != "" && materialCode && opCode != "" && opCode && type != "" && type) {
					listStandardList().then((res) => {
						standardOptions = res.rows;
					});
					let mt = null;
					if (materialCode.includes('@')) {
						mt = materialCode.split('@')[0];
					}
					listStandardApply({
						materialCode: mt,
						opCode: opCode,
						billType: type
					}).then((response) => {
						console.log(response)
						if (response.total > 0) {
							let array = response.rows.map(item => ({
								id: item.qualityStandardId
							}))
							array.forEach(aitem => {
								standardOptions.forEach(sitem => {
									if (sitem.id == aitem.id) {
										sitem.text = sitem.qualityStandardCode +
											":" + sitem.qualityStandardName;
										sitem.value = sitem.qualityStandardCode;
										this.standardOptions.push(sitem)
									}
								});
							})

							console.log(this.standardOptions)
						}
						// this.pPbomOptions = response.rows;
					});
				}
			},

			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.sourceCode = res.result;
						_this.scanBarCode(_this.formData.sourceCode);
					}
				});
			},

		}
	}
</script>