<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="productReveiveCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" @confirm="scanBarCode"
							v-model="formData.productReveiveCode" type="text" />
					</uni-forms-item>
					
					<uni-forms-item label="收货方式" :labelWidth='90'>
						<u-radio-group v-model="value" iconPlacement="left">
							<u-radio label="正常" name="正常"></u-radio>
							<u-radio label="扫物料标签" name="扫物料标签" style="margin-left: 10px;"></u-radio>
						</u-radio-group>
					</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.pdcInQualityDetailList"
							@click="(data) => clickDetail(index,data)" @change="swipChange">
							<uni-badge :text="index+1" type="primary"></uni-badge>
							<uni-forms-item label="物料编码" :name="'pdcInQualityDetailList.'+ index +'.materialCode'">
								<uni-easyinput type="text" disabled v-model="item.materialCode"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料名称" :name="'pdcInQualityDetailList.'+ index +'.materialName'">
								<uni-easyinput type="text" disabled v-model="item.materialName"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料批号" :labelWidth='90'
								name="'pdcInQualityDetailList.'+ index +'.materialBatchNo'">
								<uni-easyinput disabled type="text" v-model="item.materialBatchNo" />
							</uni-forms-item>
							<uni-forms-item label="实收数量" :labelWidth='90'
								name="'pdcInQualityDetailList.'+ index +'.actualNumber'">
								<uni-easyinput disabled type="number" v-model="item.actualNumber" />
							</uni-forms-item>
							<uni-forms-item label="质检数量" :labelWidth='90'
								name="'pdcInQualityDetailList.'+ index +'number'">
								<uni-easyinput disabled type="number" v-model="item.number" />
							</uni-forms-item>
							<uni-forms-item label="合格数量" :labelWidth='90'
								name="'pdcInQualityDetailList.'+ index +'.passNumber'">
								<u-number-box inputWidth="120" button-size="36" v-model="item.passNumber"
									min="0"></u-number-box>
							</uni-forms-item>
							<!-- 							<uni-forms-item label="不良数量" :labelWidth='90' name="'pdcInQualityDetailList.'+ index +'.blNum'">
								<uni-easyinput type="number" v-model="item.blNum"/>	
							</uni-forms-item> -->
							<uni-forms-item label="不良原因" :labelWidth='90'
								name="'pdcInQualityDetailList.'+ 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 {
		addReveive,
		listReceive,
		getReceive,
		listTask,
		getTask,
		listQuality,
		getQuality,
		addQuality
	} from "@/api/wms/pdcIn.js";
	import {
		listMaterial
	} from "@/api/wms/request.js";

	export default {
		mounted() {
			this.test();
		},
		data() {
			return {
				formData: {
					pdcInQualityDetailList: [],
				},
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					productReveiveCode: {
						rules: [{
							required: true,
							errorMessage: '请输入产品收货单!'
						}]
					},
					materialCode: {
						rules: [{
							required: true,
							errorMessage: '请输入物料编码!'
						}]
					},
				}
			}
		},
		methods: {
			clickDetail(itemIndex, {
				position,
				index
			}) {
				//  if (index == 0){
				// this.deleteDetail(itemIndex);
				//  }
			},
			reset(code) {
				this.formData = {
					productReveiveCode: code,
					pdcInQualityDetailList: [],
				};
			},
			test() {
				listQuality().then(async res => {
					console.log(res);
				});

				// getQuality('284').then(async res => {
				// 		console.log(res);
				// });
			},
			scanBarCode(code) {
				if (code) {
					this.reset(code);
				}
				if (this.formData.productReveiveCode) {
					let q = {
						productReveiveCode: this.formData.productReveiveCode
					}
					listReceive(q).then(async res => {
						console.log(res);
						if (res.rows != null && res.rows.length > 0) {
							let did = res.rows[0].id
							getReceive(did).then(async res => {
								for (let i in res.data.wmsProductReceiveDetailList) {
									let obj = {};
									obj.materialBatchNo = res.data.wmsProductReceiveDetailList[i]
										.materialBatchNo;
									obj.materialCode = res.data.wmsProductReceiveDetailList[i]
										.materialCode;
									obj.materialName = res.data.wmsProductReceiveDetailList[i]
										.materialName;
									obj.actualNumber = res.data.wmsProductReceiveDetailList[i]
										.actualNumber;
									obj.number = res.data.wmsProductReceiveDetailList[i]
										.actualNumber
									obj.unitId = res.data.wmsProductReceiveDetailList[i]
										.unitId
									this.formData.pdcInQualityDetailList.push(obj);

								}
							});
						}
					});
				}
			},
			//采购任务单
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.productReveiveCode = res.result;
						_this.scanBarCode(_this.formData.productReveiveCode);
					}
				});
			},
			submit() {
				const _this = this;
				// this.$refs["jobInForm"].validate().then(valid => {
				this.$refs.form.validate().then(res => {
					uni.showModal({
						title: '提示',
						content: '您确定完成该质检吗？',
						success: function(res) {
							if (res.confirm) {
								// let psNum = Number()
								let wmsProductQualityDetailList = [];
								for (let i in _this.formData.pdcInQualityDetailList) {
									let obj = {};
									obj.materialCode = _this.formData.pdcInQualityDetailList[i]
										.materialCode;
									obj.materialName = _this.formData.pdcInQualityDetailList[i]
										.materialName;
									obj.materialBatchNo = _this.formData.pdcInQualityDetailList[i]
										.materialBatchNo;
									obj.number = _this.formData.pdcInQualityDetailList[i].number;
									obj.unitId = Number(_this.formData.pdcInQualityDetailList[i]
										.unitId);
									obj.secondNumber = _this.formData.pdcInQualityDetailList[i].number;
									obj.passNumber = Number(_this.formData.pdcInQualityDetailList[i]
										.passNumber);
									obj.secondPassNumber = Number(_this.formData
										.pdcInQualityDetailList[i]
										.secondPassNumber);
									obj.secondUnitId = Number(_this.formData.pdcInQualityDetailList[i]
										.unitId);
									obj.failReason = _this.formData.pdcInQualityDetailList[i]
										.failReason;
									wmsProductQualityDetailList.push(obj);
								}
								console.log(wmsProductQualityDetailList);
								let data = {
									productReceiveCode: _this.formData.productReveiveCode,
									status: '1',
									wmsProductQualityDetailList: wmsProductQualityDetailList
								}
								console.log(data);

								_this.$modal.loading('提交中')
								addQuality(data).then(response => {
									_this.$modal.closeLoading();
									_this.$modal.msgSuccess("质检成功!");
									_this.$tab.switchTab('/pages/work/index');
								});
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				});
			}
		}
	}
</script>

<style>
</style>