<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-collapse-item title="销售出库检单" :open="true">
					<uni-forms-item label="质检单" :labelWidth='90' name="qualityCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode"
							v-model="formData.qualityCode" type="text" />
					</uni-forms-item>
					<!-- 						<uni-forms-item label="来源单" :labelWidth='90' name="sourceCode">
							<uni-easyinput suffixIcon="scan" @iconClick="scanBar1" @confirm="scanBarCode" v-model="formData.sourceCode" type="text"  />
						</uni-forms-item> -->
					<uni-forms-item label="仓库编码" :labelWidth='90' name="warehouseCode">
						<uni-easyinput type="text" v-model="formData.warehouseCode" />
					</uni-forms-item>
				</uni-collapse-item>
				<uni-collapse-item title="销售出库检单明细" :open="true">
					<uni-swipe-action>
						<uni-swipe-action-item :rightOptions="rightOptions" :key="index"
							v-for="(item, index) in formData.qcQualityBillDetailList"
							@click="(data) => clickDetail(index,data)" @change="swipChange">
							<view>
								<uni-badge :text="index+1" class="uni-badge-left-margin" type="primary"></uni-badge>
								<button @click="open(index)" size="mini" type="primary">编辑</button>
							</view>
							<uni-forms-item label="物料编码" :labelWidth='80'
								:name="'qcQualityBillDetailList.'+ index +'.materialCode'">
								<uni-easyinput type="text" disabled v-model="item.materialCode"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="制程编码" :labelWidth='80'
								:name="'qcQualityBillDetailList.'+ index +'.opCode'">
								<uni-easyinput type="text" disabled v-model="item.opCode"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料批号" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'.materialBatchNo'">
								<uni-easyinput disabled type="text" v-model="item.materialBatchNo" />
							</uni-forms-item>
							<uni-forms-item label="物料箱号" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'.materialLotNo'">
								<uni-easyinput disabled type="text" v-model="item.materialLotNo" />
							</uni-forms-item>
							<uni-forms-item label="质检员" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'qualityBy'">
								<uni-easyinput disabled type="text" v-model="item.qualityBy" />
							</uni-forms-item>
							<uni-forms-item label="质检方案编码" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'.qualityTypeCode'">
								<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBar3"
									v-model="item.qualityTypeCode" />
							</uni-forms-item>
							<uni-forms-item label="质检标准编码" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'.qualityStandardCode'">
								<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBar4(index)"
									v-model="item.qualityStandardCode" />
							</uni-forms-item>
							<uni-row>
								<uni-col :span="24">
									<uni-forms-item label="计划质检数量" :labelWidth='80'
										name="'qcQualityBillDetailList.'+ index +'.planNumber'">
										<u-number-box inputWidth="120" button-size="36" v-model="item.planNumber"
											min="0"></u-number-box>
									</uni-forms-item>
								</uni-col>
								<uni-col :span="24">
									<uni-forms-item label="实收数量" :labelWidth='80'
										name="'qcQualityBillDetailList.'+ index +'.actualNumber'">
										<u-number-box inputWidth="120" button-size="36" v-model="item.actualNumber"
											min="0"></u-number-box>
									</uni-forms-item>
								</uni-col>
								<uni-col :span="24">
									<uni-forms-item label="质检数量" :labelWidth='80'
										name="'qcQualityBillDetailList.'+ index +'.number'">
										<u-number-box inputWidth="120" button-size="36" v-model="item.number"
											min="0"></u-number-box>
									</uni-forms-item>
								</uni-col>
								<uni-col :span="24">
									<uni-forms-item label="质检合格数量" :labelWidth='80'
										name="'qcQualityBillDetailList.'+ index +'.passNumber'">
										<u-number-box inputWidth="120" button-size="36" v-model="item.passNumber"
											min="0"></u-number-box>
									</uni-forms-item>
								</uni-col>
							</uni-row>
							<uni-forms-item label="质检不通过原因" :labelWidth='80'
								name="'qcQualityBillDetailList.'+ index +'.failReason'">
								<uni-easyinput type="textarea" v-model="item.failReason" />
							</uni-forms-item>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listQualityBill,
		addQualityBill,
		getQualityBill,
		updateQualityBill,
		listQualityBillByBatchNo,
		getStandardList,
		listStandardList
	} from "@/api/qc/qc.js";
	export default {
		mounted() {
			listStandardList().then(async res => {
				this.StandardList = res.rows;
			})
		},
		data() {
			return {
				formData: {
					qualityCode: null,
					warehouseCode: null,
					qcQualityBillDetailList: [],
				},
				StandardList: null,
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					qualityCode: {
						rules: [{
							required: true,
							errorMessage: '请输入质检单!'
						}]
					},
				}
			}
		},

		methods: {
			otherFun(object) {
				console.log(object)
				this.formData.qcQualityBillDetailList.find(item => item.id == object.did).number = object.number;
				this.formData.qcQualityBillDetailList.find(item => item.id == object.did).passNumber = object.passNumber;
			},
			open(index) {
				let qscid = null;
				if (this.formData.qcQualityBillDetailList[index].qualityStandardCode && this.formData
					.qcQualityBillDetailList[index].qualityStandardCode != "") {
					const result = this.StandardList.find(item => item.qualityStandardCode == this.formData
						.qcQualityBillDetailList[index].qualityStandardCode);
					if (result !== undefined) {
						// 找到了匹配项，进行后续逻辑
						qscid = result.id
						let did = this.formData.qcQualityBillDetailList[index].id;
						let mbn = this.formData.qcQualityBillDetailList[index].materialBatchNo;
						let mc = this.formData.qcQualityBillDetailList[index].materialCode;

						console.log(qscid + ' ' + mbn + ' ' + mc);
						uni.navigateTo({
							url: '/pages/qc/updateStandardList?qscid=' + qscid +
								'&materialBatchNo=' + mbn +
								'&materialCode=' + mc +
								'&did=' + did
						});
					} else {
						console.log(result)
						this.$modal.msg("未检索到该质检标准编码相关信息!");
					}
				} else {
					this.$modal.msg("请输入质检标准编码!");
				}
			},
			test() {},
			change(e) {
				console.log('当前模式：' + e.type + ',状态：' + e.show);
			},
			scanBarCode() {
				if (this.formData.qualityCode && this.formData.qualityCode != "") {
					let q = {
						qualityCode: this.formData.qualityCode
					}
					listQualityBill(q).then(async res => {
						console.log(res);
						if (res.rows != null && res.rows.length > 0) {
							let did = res.rows[0].id
							//查询质检单明细
							getQualityBill(did).then(async response => {
								this.formData = response.data;
								this.formData.qcQualityBillDetailList =
									response.data.qcQualityBillDetailList.map((item) => {
										item.qualityStandardCode = this.StandardList.find(i =>
												i.id == item.qualityStandardCode)
											.qualityStandardCode
										item.qualityTypeCode = Number(item.qualityTypeCode);
										return item;
									});
							});
						} else {
							this.$modal.msg("未检索到该质检单相关信息!");
						}
					});
				} else {
					this.$modal.msg("请输入质检单!");
				}
			},
			//质检单
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.qualityCode = res.result;
						_this.scanBarCode(_this.formData.qualityCode);
					}
				});
			},
			//仓库编码
			scanBar2() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.warehouseCode = res.result;
						_this.scanBarCode(_this.formData.warehouseCode);
					}
				});
			},
			//质检方案编码
			scanBar3() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.qcQualityBillDetailList[0].qualityTypeCode = res.result;
						_this.scanBarCode(_this.qcQualityBillDetailList[0].qualityTypeCode);
					}
				});
			},
			//质检标准编码
			scanBar4(index) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.qcQualityBillDetailList[index].qualityStandardCode = res.result;
						_this.scanBarCode(_this.qcQualityBillDetailList[index].qualityStandardCode);
					}
				});
			},
			submit() {
				const _this = this;
				_this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定完成该质检吗？',
						success: function(res) {
							if (res.confirm) {
								for (var i in _this.formData.qcQualityBillDetailList) {
									if (_this.formData.qcQualityBillDetailList[i]
										.qualityStandardCode && _this.formData.qcQualityBillDetailList[
											i].qualityStandardCode != "") {
										//查询质检标准编码的id
										const res = _this.StandardList.find(item => item
											.qualityStandardCode == _this.formData
											.qcQualityBillDetailList[i].qualityStandardCode);
										if (res !== undefined) {
											_this.formData.qcQualityBillDetailList[i]
												.qualityStandardCode = res.id;
										} else {
											_this.$modal.msg("请填写正确的质检标准编码!");
										}
									} else {
										_this.formData.qcQualityBillDetailList[i].qualityStandardCode =
											null;
									}
								}
								_this.$modal.loading('提交中')
								updateQualityBill(_this.formData).then(async res => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("检验成功!");
									setTimeout(() => {
										_this.$tab.switchTab("/pages/work/index");
									}, 500);
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			}
		}
	}
</script>

<style>


</style>