<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-row>
					<uni-col :span="24">
						<uni-forms-item label="工单" :labelWidth='80' name="pwoCode">
							<uni-easyinput suffixIcon="scan" @iconClick="scanBar" v-model="formData.pwoCode"
								@confirm="scanBarCode" />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="开始作业" :labelWidth='80' name="startPwoJobCode">
							<uni-easyinput disabled type="text" v-model="formData.startPwoJobCode" />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="结束作业" :labelWidth='80' name="endPwoJobCode">
							<uni-easyinput disabled type="text" v-model="formData.endPwoJobCode" />
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="操作员" :labelWidth='80' name="operator">
							<uni-easyinput suffixIcon="scan" @iconClick="scanBarOperator" type="text"
								v-model="formData.operator" />
						</uni-forms-item>
					</uni-col>
					<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 :key="index" v-for="(item, index) in jobList"
								@click="(data) => clickDetail(index,data)" @change="swipChange">
								<uni-badge :text="index+1" type="primary"></uni-badge>
								<uni-forms-item label="作业编码" :labelWidth='90' :name="'jobList.'+ index +'.code'">
									<uni-easyinput type="text" v-model="item.code"></uni-easyinput>
								</uni-forms-item>
								<uni-forms-item label="目标产品编码" :labelWidth='90' :name="'jobList.'+ index +'.ptNoTar'">
									<uni-easyinput type="text" v-model="item.ptNoTar"></uni-easyinput>
								</uni-forms-item>
								<uni-forms-item label="目标产品名称" :labelWidth='90'
									:name="'jobList.'+ index +'.ptTitleTar'">
									<uni-easyinput type="text" v-model="item.ptTitleTar"></uni-easyinput>
								</uni-forms-item>
								<uni-forms-item label="制程名称" :labelWidth='90' :name="'jobList.'+ index +'.opTitle'">
									<uni-easyinput type="text" v-model="item.opTitle"></uni-easyinput>
								</uni-forms-item>
								<uni-forms-item label="目标数量" :labelWidth='90' :name="'jobList.'+ index +'.planNum'">
									<uni-easyinput type="text" v-model="item.planNum"></uni-easyinput>
								</uni-forms-item>
								<uni-forms-item label="完成数量" :labelWidth='90' :name="'jobList.'+ index +'.finishQty'">
									<uni-easyinput type="text" v-model="item.finishQty"></uni-easyinput>
								</uni-forms-item>
							</uni-swipe-action-item>
						</uni-swipe-action>
					</uni-collapse-item>

					<uni-col :span="24">
						<uni-forms-item label="合格数量" :labelWidth='80' name="passNum">
							<u-number-box inputWidth="120" button-size="36" v-model="formData.passNum"
								min="0"></u-number-box>
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="不良数量" :labelWidth='80' name="defectNum">
							<u-number-box inputWidth="120" button-size="36" v-model="formData.defectNum"
								min="0"></u-number-box>
						</uni-forms-item>
					</uni-col>
					<uni-col :span="24">
						<uni-forms-item label="报废数量" :labelWidth='80' name="scrapNum">
							<u-number-box inputWidth="120" button-size="36" v-model="formData.scrapNum"
								min="0"></u-number-box>
						</uni-forms-item>
					</uni-col>
				</uni-row>
			</uni-forms>
			<!-- <uni-table ref="table"  border stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th width="100" align="center">作业编码</uni-th>
						<uni-th width="150" align="center">产品编码</uni-th>
						<uni-th width="50" align="center">数量</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in jobList" :key="index">
						<uni-td align="center">{{ item.code }}</uni-td>
						<uni-td align="center">{{ item.ptNoTar }}</uni-td>
						<uni-td align="center">{{ item.planNum }}</uni-td>
					</uni-tr>
				</uni-table> -->

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

<script>
	import {
		listPwoJob,
		addReport,
		getEquipment,
		addReportOnce,
		pwoReport
	} from "@/api/mes/jobReport.js";
	import {
		listEmpEqpHistory,
		listEmployee,
		listConversion
	} from "@/api/mes/jobIn.js";
	export default {
		data() {
			return {
				formData: {
					pwoJobCode: null,
					pwoCode: null,
					startPwoJobId: null,
					startPwoJobCode: null,
					endPwoJobId: null,
					endPwoJobCode: null,
					operator: null,
					passNum: 0,
					defectNum: 0,
					scrapNum: 0,
				},
				jobList: [],
				rules: {
					pwoCode: {
						rules: [{
							required: true,
							errorMessage: '请输入工单!'
						}]
					},
					operator: {
						rules: [{
							required: true,
							errorMessage: '请输入操作员工编码!'
						}]
					},
					passNum: {
						rules: [{
							required: true,
							errorMessage: '请输入合格数量!'
						}]
					},
					defectNum: {
						rules: [{
							required: true,
							errorMessage: '请输入不良数量!'
						}]
					},
					scrapNum: {
						rules: [{
							required: true,
							errorMessage: '请输入报废数量!'
						}]
					}
					// machineId: {
					//   rules: [
					// 			  { required: true, errorMessage:'请输入设备编码!' }
					// 			]
					// },
				}
			}
		},
		methods: {
			submit() {
				this.$refs.form.validate().then(res => {
					if (this.jobList.length == 0) {
						this.$modal.mesError("没有作业可报工！");
					} else {
						//员工
						let emp = {
							empCode: this.formData.operator
						}
						listEmployee(emp).then(res => {
							if (res.rows.length != 0) {
								this.formData.operator = res.rows[0].id;
							}
							this.$modal.loading('提交中')
							pwoReport(this.formData).then(resp => {
								this.$modal.closeLoading();
								this.$modal.msgSuccess("报工成功！");
								setTimeout(() => {
									this.$tab.switchTab("/pages/work/index");
								}, 500);
							})
						})

					}
				})
			},
			scanBarCode() {
				if (this.formData.pwoCode) {
					var queryParams = {
						pwoCode: this.formData.pwoCode,
					};
					listPwoJob(queryParams).then(response => {
						// this.pwoJobOptionsDraft = response.rows;
						console.log(response);
						let total = response.total;
						if (total != 0) {
							this.jobList = response.rows;
							//编码用于前端显示
							this.formData.startPwoJobCode = this.jobList[0].code;
							this.formData.endPwoJobCode = this.jobList[total - 1].code;
							//id用于提交时传回给后端
							this.formData.startPwoJobId = this.jobList[0].id;
							this.formData.endPwoJobId = this.jobList[total - 1].id;
							console.log(this.formData);
						} else {
							this.jobList = response.rows;
							//编码用于前端显示
							this.formData.startPwoJobCode = "";
							this.formData.endPwoJobCode = "";
						}
					});
				}
			},
			scanBar() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.pwoCode = res.result;
						_this.scanBarCode();
					}
				});
			},
			scanBarOperator() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.operator = res.result;
						_
					}
				});
			},
		}
	}
</script>