<template>
	<view style="background-color: #fff;">
		<uni-forms ref="form" :modelValue="formData" :rules="rules">
			<uni-row>
				<uni-col :span="24">
					<uni-forms-item label="开始作业编码" :labelWidth='80' name="startPwoJobCode">
						<uni-easyinput suffixIcon="scan" @iconClick="scanBar" type="text"
							v-model="formData.startPwoJobCode" @confirm="scanBarStartCode" />
					</uni-forms-item>
				</uni-col>
				<uni-col :span="24">
					<uni-forms-item label="结束作业编码" :labelWidth='80' name="endPwoJobCode">
						<uni-combox-re labelKey="code" valueKey="id" :candidates="endPwoJobOptions" emptyTips="无"
							@input="scanBarEndCode" v-model="formData.endPwoJobCode"></uni-combox-re>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="合格数量" :labelWidth='80' name="passNum">
						<u-number-box button-size="36" v-model="formData.passNum" min="0"></u-number-box>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="缺陷数量" :labelWidth='80' name="defectNum">
						<u-number-box button-size="36" v-model="formData.defectNum" min="0"></u-number-box>
					</uni-forms-item>
				</uni-col>
				<uni-col :span="12">
					<uni-forms-item label="报废数量" :labelWidth='80' name="scrapNum">
						<u-number-box button-size="36" v-model="formData.scrapNum" min="0"></u-number-box>
					</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-row>
		</uni-forms>
		<uni-card :is-shadow="false" is-full v-for="item in jobList" :key="item.id">
			<div><strong>工单</strong>:{{item.pwoCode}}</div>
			<div><strong>作业编码</strong>:{{item.code}}</div>
			<div><strong>目标产品编码</strong>:{{item.ptNoTar}}</div>
			<div><strong>目标产品名称</strong>:{{item.ptTitleTar}}</div>
			<div><strong>制程名称</strong>:{{item.opTitle}}</div>
			<div><strong>目标数量</strong>:{{item.planNum}}</div>
			<div><strong>完成数量</strong>:{{item.finishQty}}</div>
			<div><strong>开工状态</strong>:
				<uni-tag :text="jobtag(item.status)" type="success"></uni-tag>
			</div>
		</uni-card>
		<u-button type="primary" @click="submit">提交</u-button>
	</view>
</template>

<script>
	import {
		listPwoJob,
		addReport,
		getEquipment,
		addPipelineReport
	} from "@/api/mes/jobReport.js";
	import {
		listEmpEqpHistory,
		listEmployee,
		listConversion
	} from "@/api/mes/jobIn.js";
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import uniComboxRe from "../../../uni_modules/uni-combox/components/uni-combox/uni-combox-re.vue"
	export default {
		components: {
			uniComboxRe
		},
		data() {
			return {
				formData: {
					startPwoJobCode: null,
					endPwoJobCode: null,
					startPwoJobId: null,
					endPwoJobId: null,
					passNum: 0,
					defectNum: 0,
					scrapNum: 0,
					createByName: null,
					createByCode: null
				},
				endPwoJobOptions: [],
				pwojobList: [],
				jobList: [],
				rules: {
					startPwoJobCode: {
						rules: [{
							required: true,
							errorMessage: '请输入开始作业编码!'
						}]
					},
				},
			}
		},
		mounted() {
			listPwoJob().then((response) => {
				this.pwojobList = response.rows;
			});
		},
		methods: {
			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("未查询到该人员信息！");
						}
					});
				}
			},
			scanBar1() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.createByCode = res.result;
						_this.scanBarCreateBy();
					}
				});
			},
			jobtag(status) {
				switch (Number(status)) {
					case 1:
						return '计划中';
						break;
					case 2:
						return '材料转入';
						break;
					case 3:
						return '作业开工';
						break;
					case 4:
						return '作业暂停';
						break;
					case 5:
						return '作业完工';
						break;
					case 6:
						return '完成品转出';
						break;
					case 7:
						return '取消';
						break;
					default:
						return '无';
						break;
				}

			},
			submit() {
				this.$refs.form.validate().then(res => {
					if (this.jobList.length == 0) {
						this.$modal.mesError("没有选择作业！");
					} else {
						this.$modal.loading('提交中')
						addPipelineReport(this.formData).then((resp) => {
							this.$modal.closeLoading();
							this.$modal.msgSuccess("报工成功！");
							setTimeout(() => {
								this.$tab.switchTab("/pages/work/index");
							}, 500);
						});
					}

				})
			},
			scanBarStartCode() {
				this.endPwoJobOptions = [];
				this.jobList = [];

				listPwoJob({
					code: this.formData.startPwoJobCode
				}).then((response) => {
					if (response.total > 0) {
						//开始作业的工单id
						var pwoId = response.rows[0].pwoId;
						//开始作业id
						let id = this.formData.startPwoJobId = response.rows[0].id;
						for (let item of this.pwojobList) {
							if (pwoId == item.pwoId && id > item.id) {
								this.endPwoJobOptions.push(item);
							}
						}
					}
				});

			},
			//选择结束作业后表单自动填充作业信息
			scanBarEndCode(e) {
				var startJobId = this.formData.startPwoJobId;
				this.formData.endPwoJobId = e;
				this.jobList = [];
				if (startJobId == null || e == null || e == "") {
					this.jobList = [];
					return;
				}
				for (let item of this.pwojobList) {
					if (item.id <= startJobId && item.id >= e) {
						this.jobList.push(item);
					}
				}
			},

			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.startPwoJobCode = res.result;
						_this.scanBarStartCode(_this.formData.startPwoJobCode);
					}
				});
			}
		}
	}
</script>