<template>
	<uni-card :is-shadow="false" is-full>
		<h2 style="margin-bottom: 10px;">报工信息</h2>
		<view><strong>工单编码：</strong>{{formData.pwoCode}}</view>
		<view><strong>作业编码：</strong>{{formData.pwoJobCode}}</view>
		<view><strong>物料编码：</strong>{{formData.ptNoTar}}</view>
		<view><strong>物料名称：</strong>{{formData.ptTitleTar}}</view>
		<view><strong>物料规格：</strong>{{formData.specification1}}</view>
		<view><strong>制程：</strong>{{formData.opCode+':'+formData.opTitle}}</view>
		<view><strong>设备编码：</strong>{{formData.eqpCode}}</view>
		<view><strong>报工数量：</strong>{{formData.reportNumber}}</view>
		<view><strong>加工人员：</strong>{{formData.createByName}}</view>
		<view><strong>报工时间：</strong>{{formData.reportcreateTime}}</view>
		<h2 style="margin-top: 10px;">不良原因</h2>
		<view v-for="item in reasonOptions">
			<uni-row style="margin-top: 10px;">
				<uni-col :span="12">
					<span>{{item.defectReason}}</span>
				</uni-col>
				<uni-col :span="12">
					<u-number-box button-size="36" inputWidth="120" v-model="item.number" min="0"></u-number-box>
				</uni-col>
			</uni-row>
		</view>
		<u-button type="primary" @click="submit">提交</u-button>
	</uni-card>
</template>

<script>
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		listMesPwoRecordrecord,
		getMesPwoRecordrecord,
		delMesPwoRecordrecord,
		addMesPwoRecordrecord,
		updateMesPwoRecordrecord
	} from "@/api/mes/mesPwoRecordrecord";
	import {
		listMesPwoRecordDetail,
		getMesPwoRecordDetail,
		delMesPwoRecordDetail,
		addMesPwoRecordDetail,
		updateMesPwoRecordDetail,
	} from "@/api/mes/mesPwoRecordDetail";
	import {
		listEmployee
	} from "@/api/mes/jobIn.js";
	export default {
		onLoad: function(option) {
			getDicts("mes_pwo_record_defect_reason").then(res => {
				this.reasonOptions = res.data.map(dict => {
					return {
						defectReason: dict.dictLabel,
						number: 0,
						diasble: false
					}
				});
			})
			listEmployee().then(res => {
				this.empList = res.rows
			});
			if (option) {
				this.formData = JSON.parse(decodeURIComponent(
					option.reportForm));
				console.log(this.formData)
				// this.formData = option.badRecord
			}
		},
		mounted() {},
		data() {
			return {
				empList: [],
				reasonOptions: [],
				formData: {
					pwoCode: null,
					pwoJobCode: null,
					ptNoTar: null,
					ptTitleTar: null,
					specification1: null,
					reportNumber: null,
					createByName: null,
					createTime: null,
					eqpCode: null,
					qualityCode: null
				}
			}
		},
		methods: {
			submit() {
				this.reasonOptions.filter(item => item.number > 0).map(item => {
					item.ptNoTar = this.formData.ptNoTar.split("@")[0];
					item.ptTitleTar = this.formData.ptTitleTar.split("@")[0];
					item.pwoCode = this.formData.pwoCode;
					item.opCode = this.formData.opCode;
					item.pwoJobCode = this.formData.pwoJobCode;
					item.specification1 = this.formData.specification1;
					item.eqpCode = this.formData.eqpCode;
					let obj = this.empList.find(obj => obj.name == this.formData.createByName)
					if (obj) {
						item.empCode = obj.empCode
					}
					item.dateTime = this.formData.reportcreateTime;
					return item
				})
				addMesPwoRecordrecord({
					pwoRecordName: '不良记录单',
					relOrderCode: this.formData.qualityCode
				}).then(res => {
					for (var i in this.reasonOptions) {
						this.reasonOptions[i] = JSON.stringify(this.reasonOptions[i])
						addMesPwoRecordDetail({
							pwoRecordCode: res.data.pwoRecordCode,
							pwoRecordId: res.data.id,
							dataInfo: this.reasonOptions[i]
						}).then(() => {
							this.$modal.msgSuccess("新增成功!");
						}).catch(() => {});
					}
					this.$tab.navigateBack();
				}).catch(() => {});
			}
		}
	}
</script>

<style>
</style>