<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="pwoJobCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" disabled type="text" @confirm="scanJobCode"
							v-model="jobInForm.pwoJobCode"></uni-easyinput>
					</uni-forms-item>
					<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="stationCode">
						<uni-easyinput suffixIcon="scan" type="text" v-model="stationCode"
							@iconClick="scanBarStationCode" @input="scanStationCode"></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-forms-item v-show="show" :labelWidth='90' style="text-align: center;" name="">
						<button type="primary" size="mini" style=" text-align: center;font-size: 18px;"
							@click="eqpBind">绑定</button>
					</uni-forms-item>
					<uni-forms-item label="选择状态" :labelWidth='90'>
						<u-radio-group v-model="radiovalue1" placement="row">
							<u-radio v-for="(item, index) in radiolist1" :key="index" :label="item.name"
								:name="item.name">
							</u-radio>
						</u-radio-group>
					</uni-forms-item>
					<uni-forms-item :label="radiovalue1+'时间'" :labelWidth='90'>
						<view class="example-body">
							<uni-datetime-picker type="datetime" v-model="value1" />
						</view>
					</uni-forms-item>
					<uni-forms-item label="操作人编码" :labelWidth='90' name="operateByCode">
						<uni-easyinput suffixIcon="scan" @confirm="scanBarOperateBy" @iconClick="scanBar2" type="text"
							v-model="operateByCode"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="操作人" :labelWidth='90' name="operateBy">
						<uni-easyinput disabled type="text" v-model="operateBy"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="转移单类型" :labelWidth='90' name="type">
						<uni-easyinput type="text" disabled v-model="jobInForm.type"></uni-easyinput>
					</uni-forms-item>
					<uni-forms-item label="备注" :labelWidth='90' name="remark">
						<uni-easyinput autoHeight type="textarea" v-model="jobInForm.remark"></uni-easyinput>
					</uni-forms-item>

				</uni-collapse-item>
				<uni-collapse-item title="上机转入单明细" :open="true">
					<uni-swipe-action>
						<uni-swipe-action-item :rightOptions="rightOptions"
							v-for="(item, index) in jobInForm.mesPwoJobCvDetailList" :key="index"
							@click="(data) => clickDetail(index,data)" @change="swipChange">
							<!-- 							<uni-badge :text="index+1" type="primary"><u-tag text="未核销" size="large" type="error" plain
									plainFill></u-tag></uni-badge> -->
							<!-- <div><u-tag text="未核销" size="mini" type="error" plain plainFill></u-tag></div> -->
							<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 +'.materialName'">
								<uni-easyinput type="text" disabled v-model="item.materialName"></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-forms-item label="可转入数量" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.availableNum'">
								<uni-easyinput type="text" disabled v-model="item.availableNum"></uni-easyinput>
							</uni-forms-item>
							<uni-forms-item label="转移数量" :labelWidth='90'
								:name="'mesPwoJobCvDetailList.'+ index +'.number'">
								<uni-easyinput type="text" v-model="item.number"></uni-easyinput>
							</uni-forms-item>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
		<view style="text-align: center;margin: 10px 0;" v-show="jobInForm.mesPwoJobCvDetailList.length>0">
			<button type="primary" size="mini" style=" text-align: center;font-size: 18px;"
				@click="byDetailAdd">照单领</button>
		</view>
		<u-button type="primary" @click="jobInSubmit">提交</u-button>
	</view>
</template>

<script>
	import {
		handleConvert,
		getEquipment,
		addConversion,
		listEmpEqpHistory,
		listEmployee,
		listConversion,
		pwoStart,
		pwoStop
	} from "@/api/mes/jobIn.js";
	import {
		updatePwoJob,
		listEquipment,
		getPwoJob,
		listPwoJob
	} from "@/api/mes/jobReport.js"
	import {
		listStation,
		addCustomizeEsop
	} from "@/api/esop/esop.js";
	export default {
		data() {
			return {
				value1: null,
				radiolist1: [{
						name: '开工',
						disabled: false
					},
					{
						name: '暂停',
						disabled: false
					}
				],
				radiovalue1: '开工',
				stationCode: null,
				jobInForm: {
					mesPwoJobCvDetailList: [],
				},
				stationId: null,
				number: null,
				equipmentCode: null,
				flag: true,
				show: false,
				operateBy: null,
				operateByCode: null,
				rightOptions: [{
					text: '删除',
					style: {
						backgroundColor: '#ff2a17'
					}
				}, ],
				rules: {
					pwoJobCode: {
						rules: [{
							required: true,
							errorMessage: '请输入作业编码！'
						}]
					},
					// materialName: [{
					// 	required: false,
					// 	errorMessage: '请输入物料名称!'
					// }],
					// number: [{
					// 	required: true,
					// 	errorMessage: '请输入转移数量!'
					// }],
					// equipmentCode: {
					// 	rules: [{
					// 		pattern: '^[A-Z0-9]+$',
					// 		errorMessage: '请输入正确格式的设备编码！',
					// 		trigger: 'blur',
					// 	}]
					// }
				}
			}
		},
		mounted() {
			listEquipment().then(response => {
				this.equipmentList = response.rows
			});
		},
		methods: {
			//照单领
			byDetailAdd() {
				if (this.jobInForm.mesPwoJobCvDetailList.length > 0) {
					for (var i in this.jobInForm.mesPwoJobCvDetailList) {
						this.jobInForm.mesPwoJobCvDetailList[i].number = this.jobInForm.mesPwoJobCvDetailList[i]
							.availableNum;
					}
				}
			},
			deleteDetail(index) {
				this.jobInForm.mesPwoJobCvDetailList.splice(index, 1);
			},
			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));
				//判断是否有该设备
				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
							}
							this.$modal.loading('提交中')
							updatePwoJob(data).then(res => {
								this.$modal.closeLoading();
								this.$modal.msgSuccess("绑定成功!");
								this.show = false;
								this.flag = true;
								this.scanJobCode(this.jobInForm.pwoJobCode);

							})
						} else {
							this.$modal.msg("未查询到该设备编码！");
						}
					});
				} else {
					this.$modal.msg("请输入设备编码！");
				}

			},
			scanJobCode(code) {
				if (code) {
					this.reset(code);
					// let id = Number(code.substring(4));
					listPwoJob({
						code: code
					}).then(async res => {
						if (res.rows.length != 0) {
							if (!res.rows[0].eqpId) {
								//未绑定设备，设备编码框可用，绑定按钮显示
								this.$modal.msg("该作业未绑定设备，请绑定设备！");
								this.flag = false;
								this.show = true;
							} else {
								//已绑定设备，
								handleConvert(res.rows[0].id).then(async response => {
									this.equipmentCode = this.equipmentList.find(item => item.id ==
										response.jobInfo.eqpId).equipmentCode; //赋值设备编码
									listEmpEqpHistory({
										equipmentCode: this.equipmentCode
									}).then(async res => {
										if (res.rows.length != 0) {
											//取上机且未下机的人员
											if (res.rows[res.rows.length -
													1].startTime && !res.rows[res.rows
													.length -
													1].endTime) {
												this.operateBy = res.rows[res.rows
													.length -
													1].empName;
												this.operateByCode = res.rows[res.rows
													.length - 1].empCode;
											}
										}
									});
									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";
											obj.number = null;
											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("未查询到该人员信息！");
					}
				});
			},
			//工位编码
			scanBarStationCode() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.stationCode = res.result;
						_this.scanStationCode(_this.stationCode);
					}
				});
			},
			scanStationCode(code) {
				let obj = {
					stationCode: code
				}
				listStation(obj).then(res => {
					if (res.rows.length > 0) {
						this.stationId = res.rows[0].id
					} else {
						this.$modal.msg("未查询到该工位，无法同步ESOP！")
					}
				});
			},
			jobInSubmit() {
				//判断是否输入工位，并进行同步esop
				if (this.stationCode && this.stationCode != '' && this.stationId && this.stationId != '') {
					addCustomizeEsop({
						pwoJobId: this.jobInForm.pwoJobId,
						stationId: this.stationId,
						stationCode: this.stationCode,
						pwoJobCode: this.jobInForm.pwoJobCode
					}).then(resp => {
						this.$modal.msgSuccess("ESOP同步成功!");
					});
				}
				//判断是否输入时间
				if (this.value1 && this.value1 != '') {
					switch (this.radiovalue1) {
						case '开工':
							pwoStart(this.jobInForm.pwoJobId, this.value1).then(res => {
								this.$modal.msgSuccess("作业开工成功!");
							});
							break;
						case '暂停':
							pwoStop(this.jobInForm.pwoJobId, this.value1).then(res => {
								this.$modal.msgSuccess("作业暂停成功!");
							});
							break;
					}
				}
				this.jobInForm.operateBy = this.operateBy;
				this.$refs["jobInForm"].validate().then(valid => {
					//作业转入
					this.$modal.loading('提交中')
					addConversion(this.jobInForm).then(response => {
						this.$modal.closeLoading();
						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>