<template>
	<view>
		<uni-collapse>
			<uni-forms :modelValue="jobInForm" ref="jobInForm" :rules="rules">
				<uni-collapse-item title="生产任务单" :open="true">
					<uni-forms-item label="工单" :labelWidth='90' name="pwoCode">
						<uni-easyinput type="text" disabled v-model="jobInForm.pwoCode"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="生产入库任务单" :labelWidth='90' name="pwoJobCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" type="text" @confirm="scanJobCode"
							v-model="jobInForm.pwoJobCode"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="仓库编码" :labelWidth='90' name="equipmentCode">
						<uni-easyinput suffixIcon="scan" :disabled=flag ref="equipmentCode" @iconClick="scanBar1"
							type="text" v-model="equipmentCode">
						</uni-easyinput>
					</uni-forms-item>
				</uni-collapse-item>
				<uni-collapse-item title="生产任务单单明细" :open="true">
					<!-- <u-button type="primary" @click="addDetail">新增</u-button>
			  <u-button type="primary" @click="submit">确认</u-button> -->
					<uni-swipe-action>
						<uni-swipe-action-item :rightOptions="rightOptions" :key="index"
							v-for="(item, index) in jobInForm.mesPwoJobCvDetailList"
							@click="(data) => clickDetail(index,data)" @change="swipChange">
							<uni-badge :text="index+1" type="primary"></uni-badge>
							<uni-forms-item label="物料" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.materialCode'">
								<uni-easyinput type="text" disabled v-model="item.materialCode"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料批号" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.materialBatch'">
								<uni-easyinput type="text" disabled v-model="item.materialBatch"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="物料箱号" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.whCodeDest'">
								<uni-easyinput type="text" disabled v-model="item.whCodeDest"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="数量" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.locCodeDest'">
								<uni-easyinput type="text" disabled v-model="item.locCodeDest"></uni-easyinput>
							</uni-forms-item>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>

		</uni-collapse>
		<u-button type="primary" @click="jobInSubmit">提交</u-button>
	</view>
</template>

<script>
	import {
		handleConvert,
		getEquipment,
		addConversion,
		listEmpEqpHistory,
		listEmployee,
		listConversion
	} from "@/api/mes/jobIn.js";
	import {
		updatePwoJob,
		listEquipment,
		getPwoJob,
		listPwoJob
	} from "@/api/mes/jobReport.js"

	export default {
		data() {
			return {
				jobInForm: {
					mesPwoJobCvDetailList: [],
				},
				equipmentCode: null,
				flag: true,
				show: false,
				operateBy: null,
				operateByCode: null,
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					materialCode: [{
						required: true,
						errorMessage: '请输入物料编码!'
					}],
					materialName: [{
						required: false,
						errorMessage: '请输入物料名称!'
					}],
					number: [{
						required: true,
						errorMessage: '请输入转移数量!'
					}],
					equipmentCode: [{
						required: true,
						errorMessage: '请输入设备编码!'
					}]
				}
			}
		},
		mounted() {},
		methods: {
			addDetail() {
				this.jobInForm.mesPwoJobCvDetailList.push({});
			},
			deleteDetail(index) {
				console.log(index);
				console.log(this.mesPwoJobCvDetailList);
				this.jobInForm.mesPwoJobCvDetailList.splice(index, 1);
				console.log(this.mesPwoJobCvDetailList);
			},
			clickDetail(itemIndex, {
				position,
				index
			}) {
				if (index == 0) {
					this.deleteDetail(itemIndex);
				}
			},
			reset(code) {
				this.jobInForm = {
					pwoJobCode: code,
					mesPwoJobCvDetailList: [],
				};
				this.equipmentCode = null;
				this.operateBy = null;
				this.operateByCode = null;
			},
			eqpBind() {

				let id = Number(this.jobInForm.pwoJobCode.substring(4));
				console.log(id);
				//判断是否有该设备
				if (this.equipmentCode != "" && this.equipmentCode) {
					let query = {
						equipmentCode: this.equipmentCode
					}
					listEquipment(query).then(async res => {
						if (res.rows.length != 0) {
							//绑定设备
							let data = {
								id: this.jobInForm.pwoJobCode.slice(4),
								eqpId: res.rows[0].id
							}

							// await getEquipment(response.jobInfo.eqpId).then(response => {
							// 	this.equipmentCode = response.data.equipmentCode; //赋值设备编码
							// 	console.log(response);
							// })
							updatePwoJob(data).then(res => {
								this.$modal.msgSuccess("绑定成功!");
								this.show = false;
								this.flag = true;
								this.scanJobCode(this.jobInForm.pwoJobCode);

							})
						} else {
							this.$modal.msg("未查询到该设备编码！");
						}
					});
					// listEmpEqpHistory(query).then(async res=>{
					// 	if(res.rows.length!=0){
					// 		this.operateBy = res.rows[res.rows.length-1].empName;
					// 		this.operateByCode = res.rows[res.rows.length-1].empCode;
					// 	}
					// });
					// handleConvert(id).then(async response => {
					// 	console.log(response);
					// 	this.jobInForm.pwoJobId = response.jobInfo.id;
					// 	this.jobInForm.powJobCode = response.jobInfo.code;
					// 	this.jobInForm.pwoId = response.jobInfo.pwoId;
					// 	this.jobInForm.pwoCode = response.jobInfo.pwoCode;
					// 	this.jobInForm.type = 2;
					// 	console.log(this.jobInForm);
					// 	if (response.materials != null && response.materials.length > 0){
					// 		for (let i in response.materials){
					// 		  let obj = {};
					// 		  obj.materialCode = response.materials[i].cPtNo;
					// 		  obj.materialName = response.materials[i].cTitle;
					// 		  obj.whCodeDest = "op-" + response.jobInfo.opCode;
					// 		  obj.locCodeDest = this.equipmentCode + "-01";
					// 		  this.jobInForm.mesPwoJobCvDetailList.push(obj);
					// 		}
					// 	}else{
					// 		this.$modal.msg("未查询到可转入的原材料！");
					// 	}
					// });
				} else {
					this.$modal.msg("请输入设备编码！");
				}

			},
			scanJobCode(code) {
				if (code) {
					this.reset(code);
					let id = Number(code.substring(4));
					// getPwoJob(id).then(async res =>{
					//  console.log(res);
					// })
					let obj = {
						code: code
					}
					listPwoJob(obj).then(async res => {
						console.log(res);
						if (res.rows.length != 0) {
							if (!res.rows[0].eqpId) { //未绑定设备，设备编码框可用，绑定按钮显示
								console.log('no');
								this.$modal.msg("该作业未绑定设备，请绑定设备！");
								this.flag = false;
								this.show = true;
							} else { //已绑定设备，
								handleConvert(id).then(async response => {
									await getEquipment(response.jobInfo.eqpId).then(response => {
										this.equipmentCode = response.data
											.equipmentCode; //赋值设备编码
										console.log(response);
									});
									let query = {
										equipmentCode: this.equipmentCode
									}
									listEmpEqpHistory(query).then(async res => {
										if (res.rows.length != 0) {
											this.operateBy = res.rows[res.rows.length -
												1].empName;
											this.operateByCode = res.rows[res.rows
												.length - 1].empCode;
										}
									});
									console.log(response);
									this.jobInForm.pwoJobId = response.jobInfo.id;
									this.jobInForm.powJobCode = response.jobInfo.code;
									this.jobInForm.pwoId = response.jobInfo.pwoId;
									this.jobInForm.pwoCode = response.jobInfo.pwoCode;
									this.jobInForm.type = 2;

									if (response.materials != null && response.materials.length >
										0) {
										for (let i in response.materials) {
											let obj = {};
											obj.materialCode = response.materials[i].cPtNo;
											obj.materialName = response.materials[i].cTitle;
											obj.materialBatch = response.materials[i].batchNo;
											obj.whCodeDest = "op-" + response.jobInfo.opCode;
											obj.availableNum = response.materials[i].availableNum;
											obj.locCodeDest = this.equipmentCode + "-01";
											this.jobInForm.mesPwoJobCvDetailList.push(obj);
										}
									} else {
										this.$modal.msg("未查询到可转入的原材料！");
									}
								});
							}
						}
					});
				}
			},
			scanBarOperateBy() {
				let obj = {
					empCode: this.operateByCode
				}
				listEmployee(obj).then(async res => {
					if (res.rows.length != 0) {
						this.operateBy = res.rows[0].name;
					} else {
						this.$modal.msg("未查询到该人员信息！");
					}
				});
			},
			jobInSubmit() {
				this.jobInForm.operateBy = this.operateBy
				console.log(this.jobInForm);
				this.$refs["jobInForm"].validate().then(valid => {
					//作业转入

					this.jobInForm.operateBy = this.operateBy
					console.log(this.jobInForm);
					addConversion(this.jobInForm).then(response => {
						this.$modal.msgSuccess("转入成功!");
						setTimeout(() => {
							this.$tab.switchTab("/pages/work/index");
						}, 500);
					});
				});
			},
			//作业编码
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.jobInForm.pwoJobCode = res.result;
						_this.scanJobCode(_this.jobInForm.pwoJobCode);
					}
				});
			},
			//设备编码
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.equipmentCode = res.result;
					}
				});
			},
			//操作人编码
			scanBar2() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.operateByCode = res.result;
						_this.scanBarOperateBy();
					}
				});
			},
		}
	}
</script>

<style>
</style>