<template>
	<view>
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="作业编码" :labelWidth='80' name="pwoJobCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" type="text" v-model="formData.pwoJobCode"
							@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="ptNoTar">
						<uni-easyinput disabled type="text" v-model="formData.ptNoTar" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="产品名称" :labelWidth='80' name="ptTitleTar">
						<uni-easyinput disabled type="text" v-model="formData.ptTitleTar" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="设备编号" :labelWidth='80' name="machineId">
						<uni-easyinput disabled type="text" v-model="formData.machineId" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="批号" :labelWidth='80' name="batchNo">
						<uni-easyinput disabled type="text" v-model="formData.batchNo" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="制程编码" :labelWidth='80' name="opCode">
						<uni-easyinput disabled type="text" v-model="formData.opCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="制程名称" :labelWidth='80' name="opTitle">
						<uni-easyinput disabled disabled type="text" v-model="formData.opTitle" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="用时(s)" :labelWidth='80' name="timeTake">
						<uni-easyinput type="number" v-model="formData.timeTake" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="报工类型" :labelWidth='80' name="type">
						<uni-easyinput disabled disabled type="text" v-model="formData.type" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="报工数量" :labelWidth='80' name="reportNumber">
						<uni-easyinput type="number" :placeholder="'可报工数量：'+reportableNum"
							v-model="formData.reportNumber" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="损耗原因" :labelWidth='80' name="reason">
						<uni-data-select v-model="formData.reason" :localdata="reasonOptions"></uni-data-select>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="生产员工编码" :labelWidth='80' name="createByCode">
						<uni-easyinput type="text" suffixIcon="scan" @iconClick="scanBar1" @confirm="scanBarCreateBy"
							v-model="formData.createByCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="生产员工" :labelWidth='80' name="createByName">
						<uni-easyinput disabled type="text" v-model="formData.createByName" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="开工时间" :labelWidth='90'>
						<view class="example-body">
							<uni-datetime-picker type="datetime" v-model="formData.startTime" />
						</view>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="完工时间" :labelWidth='90' name="endTime">
						<view class="example-body">
							<uni-datetime-picker type="datetime" v-model="formData.endTime" />
						</view>
					</uni-forms-item>
				</uni-col>
			</uni-row>
		</uni-forms>
		<u-button type="primary" @click="submit" v-if="!isPwoRoute">提交</u-button>
	</view>
</template>

<script>
	import {
		listPwoJob,
		addReport,
		getEquipment,
		addReportEnterPiece
	} from "@/api/mes/jobReport.js";
	import {
		listEmpEqpHistory,
		listEmployee,
		listConversion
	} from "@/api/mes/jobIn.js";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";

	export default {
		onLoad: function(option) {
			// 获取当前时间
			var currentDate = new Date();
			// 格式化为字符串（YYYY-MM-DD HH:mm:ss）
			var year = currentDate.getFullYear();
			var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
			var day = ("0" + currentDate.getDate()).slice(-2);
			var hours = ("0" + currentDate.getHours()).slice(-2);
			var minutes = ("0" + currentDate.getMinutes()).slice(-2);
			var seconds = ("0" + currentDate.getSeconds()).slice(-2);
			var formattedDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
			// 将当前时间赋值给 formData.endTime
			this.formData.endTime = formattedDate;
			this.formData.way = '人工报工';
			this.formData.type = '损耗';
			if (option) {
				this.formData = JSON.parse(decodeURIComponent(option.formData));
				this.isPwoRoute = true;
			}
			getDicts("mes_job_report_loss").then(res => {
				this.reasonOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			// this.formData.createByName = this.$store.state.user.name;
		},
		data() {
			return {
				isPwoRoute: false,
				reasonOptions: [],
				formData: {
					pwoJobCode: null,
					pwoCode: null,
					ptNoTar: null,
					ptTitleTar: null,
					machineId: null,
					batchNo: null,
					createByName: null,
					createByCode: null,
					pieceNo: null,
				},
				radio: 0,
				reportableNum: 0,
				pieceIn: [{
						text: '是',
						value: 1
					},
					{
						text: '否',
						value: 0
					}
				],
				rules: {
					pwoJobCode: {
						rules: [{
							required: true,
							errorMessage: '请输入作业编码!'
						}]
					},
					createByCode: {
						rules: [{
							required: true,
							errorMessage: '请输入生产员工编码!'
						}]
					},
					reportNumber: {
						rules: [{
							required: true,
							errorMessage: '请输入报工数量!'
						}]
					},
					reason: {
						rules: [{
							required: true,
							errorMessage: '请输入不良原因!'
						}]
					},
					endTime: {
						rules: [{
							required: true,
							errorMessage: '请输入完工时间!'
						}]
					},
				}
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					//判断是否选择录入件号
					if (this.radio == 1) {
						//判断是否输入件号
						if (this.formData.pieceNo && this.formData.pieceNo != "") {
							this.$modal.loading('提交中')
							addReportEnterPiece(this.formData).then(res => {
								this.$modal.closeLoading();
								this.$modal.msgSuccess("报工成功！");
								setTimeout(() => {
									this.$tab.switchTab("/pages/work/index");
								}, 500);
							})
						} else {
							this.$modal.msg("请输入件号！");
						}
					} else {
						this.$modal.loading('提交中')
						addReport(this.formData).then(res => {
							this.$modal.closeLoading();
							this.$modal.msgSuccess("报工成功！");
							setTimeout(() => {
								this.$tab.switchTab("/pages/work/index");
							}, 500);
						})
					}
				})
			},
			scanBarCode() {
				if (this.formData.pwoJobCode) {
					let obj = {
						code: this.formData.pwoJobCode
					}
					listPwoJob(obj).then(async res => {
						let jobs = res.rows;
						if (jobs.length == 0) {
							this.$modal.msg("未检索到该作业编码相关的信息！");
						} else {
							let job = jobs[0];
							this.formData.pwoCode = job.pwoCode;
							this.formData.ptNoTar = job.ptNoTar;
							this.formData.ptTitleTar = job.ptTitleTar;
							this.formData.opCode = job.opCode;
							this.formData.opTitle = job.opTitle;
							this.reportableNum = job.targetNum - job.finishQty;
							if (job.eqpId) {
								await getEquipment(job.eqpId).then(res => {
									this.formData.machineId = res.data.equipmentCode;
								})

								//调取最后一条转入记录的操作人
								let code = {
									powJobCode: this.formData.pwoJobCode
								}
								listConversion(code).then(async res => {
									if (res.rows[0].operateBy && res.rows[0].operateBy != "") {
										this.formData.createByName = res.rows[0].operateBy;
										let arry = {
											name: this.formData.createByName
										}
										listEmployee(arry).then(async res => {
											this.formData.createByCode = res.rows[0]
												.empCode;
										})
									}
								})
							}
							this.formData.batchNo = job.batchNo;
						}
					})
				}
			},
			scanBarCreateBy() {
				if (this.formData.createByCode) {
					let obj = {
						empCode: this.formData.createByCode
					}
					listEmployee(obj).then(async res => {
						if (res.rows.length != 0) {
							this.formData.createByName = res.rows[0].name;
						} else {
							this.$modal.msg("未查询到该人员信息！");
						}
					});
				}
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.pwoJobCode = res.result;
						_this.scanBarCode(_this.formData.pwoJobCode);
					}
				});
			},
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.createByCode = res.result;
						_this.scanBarCreateBy();
					}
				});
			},
			scanBarPieceNo() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.pieceNo = res.result;
					}
				});
			}
		}
	}
</script>