<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData">
				<uni-forms-item label="作业编码" :labelWidth='90' name="sourceCode">
					<uni-easyinput v-model="formData.sourceCode" suffixIcon="scan" @iconClick="scanBarSourceCode"
						@confirm="submit" type="text" />
				</uni-forms-item>
				<uni-forms-item label="检验状态" :labelWidth='90' name="status">
					<u-radio-group v-model="formData.status" placement="row">
						<u-radio v-for="(item, index) in statusOptions" :key="index" :label="item.text"
							:name="item.value">
						</u-radio>
					</u-radio-group>
				</uni-forms-item>
				<u-button type="primary" @click="submit">查询</u-button>
				<uni-collapse-item title="查询结果" :open="true">
					<uni-card :is-shadow="false" v-for="(item,index) in qcList" :key="index" is-full>
						<view><strong>制程</strong> : {{item.opTitle}}</view>
						<view><strong>物料编码</strong> : {{item.ptNoTar}}</view>
						<view><strong>物料名称</strong> : {{item.ptTitleTar}}</view>
						<view><strong>质检结果</strong> : {{item.result}}</view>
						<uni-row>
							<uni-col :span="18">
								<view><strong>物料规格</strong> : {{item.specification1}}</view>
								<view><strong>报工数量</strong> : {{item.reportNumber}}</view>
							</uni-col>
							<uni-col :span="6">
								<u-button type="primary" @click="badRecordList(item)" style="font-size: 14px;"
									v-if="item.isRecord">不良记录</u-button>
								<u-button type="primary" @click="badRecordForm(item)"
									style="font-size: 14px;margin-top: 10px;" v-else>不良记录新增</u-button>

							</uni-col>
						</uni-row>
						<uni-row>
							<uni-col :span="18">
								<view><strong>加工人员</strong> : {{item.createByName}}</view>
								<view><strong>报工时间</strong> :{{item.reportcreateTime}}</view>
							</uni-col>
							<uni-col :span="6"> <u-button type="success" @click="handleFinish(item)"
									style="font-size: 14px;margin-top: 10px;">通过</u-button></uni-col>
						</uni-row>
					</uni-card>
				</uni-collapse-item>
			</uni-forms>
		</uni-collapse>
	</view>
</template>

<script>
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	import {
		listQualityBill,
		addQualityBill,
		getQualityBill,
		updateQualityBill,
		listQualityBillByBatchNo,
		getStandardList,
		listStandardList
	} from "@/api/qc/qc.js";
	import {
		listPwoJob,
		addReport,
		getEquipment,
		addReportEnterPiece,
		listEquipment
	} from "@/api/mes/jobReport.js";
	import {
		listMesPwoRecordrecord,
		getMesPwoRecordrecord,
		delMesPwoRecordrecord,
		addMesPwoRecordrecord,
		updateMesPwoRecordrecord
	} from "@/api/mes/mesPwoRecordrecord";
	import {
		listMesPwoRecordDetail,
		getMesPwoRecordDetail,
		delMesPwoRecordDetail,
		addMesPwoRecordDetail,
		updateMesPwoRecordDetail,
	} from "@/api/mes/mesPwoRecordDetail";
	export default {
		mounted() {
			getDicts("qc_bill_status").then(res => {
				this.statusOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			getDicts("qc_bill_result").then(res => {
				this.resultOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			listMesPwoRecordrecord().then(resp => {
				this.recordList = resp.rows
			})
		},
		data() {
			return {
				recordList: [],
				statusOptions: [],
				resultOptions: [],
				qcList: [],
				formData: {
					sourceCode: null,
					status: "0",
					type: 3
				},
			}
		},
		methods: {
			badRecordList(item) {
				listMesPwoRecordrecord({
					relOrderCode: item.qualityCode
				}).then(res => {
					if (res.rows.length > 0) {
						listMesPwoRecordDetail({
							pwoRecordId: res.rows[0].id
						}).then(response => {
							console.log(response)
							let mesPwoRecordDetailList = response.rows.map((obj) => {
								obj.dataInfo = JSON.parse(obj.dataInfo);
								return obj;
							});
							console.log(mesPwoRecordDetailList)
							let obj = {
								pwoRecordName: res.rows[0].pwoRecordName,
								pwoRecordCode: res.rows[0].pwoRecordCode,
								mesPwoRecordDetailList: mesPwoRecordDetailList
							}
							uni.navigateTo({
								url: '/pages/mes/pwoRecord/badRecordList?formData=' +
									encodeURIComponent(
										JSON.stringify(obj))
							});
						});
					} else {
						this.$modal.msg("未查询到记录单相关信息！")
					}
				})
			},
			badRecordForm(item) {
				uni.navigateTo({
					url: '/pages/mes/pwoRecord/badRecord?reportForm=' +
						encodeURIComponent(
							JSON.stringify(item))
				});
			},
			scanBarSourceCode(code) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.sourceCode = res.result;
						_this.submit()
					}
				});
			},
			submit() {
				listQualityBill(this.formData).then(res => {
					if (res.rows.length > 0) {
						res.rows.map(item => {
							let obj = this.recordList.find(r => r.relOrderCode == item.qualityCode)
							if (obj) {
								item.isRecord = true
							} else {
								item.isRecord = false
							}
							item.additionalJson = JSON.parse(item.additionalJson);
							item.ptNoTar = item.additionalJson.ptNoTar;
							item.opTitle = item.additionalJson.opTitle;
							item.ptTitleTar = item.additionalJson.ptTitleTar;
							item.specification1 = item.additionalJson.specification1;
							item.reportNumber = item.additionalJson.reportNumber;
							item.createByName = item.additionalJson.createByName;
							item.reportcreateTime = item.additionalJson.createTime;
							item.pwoCode = item.additionalJson.pwoCode;
							item.pwoJobCode = item.additionalJson.pwoJobCode;
							item.reportId = item.additionalJson.id;
							item.eqpCode = item.additionalJson.machineId;
							item.opCode = item.additionalJson.opCode;
							return item
						})
						console.log(res.rows)
						this.qcList = res.rows;
						console.log(this.qcList)
					} else {
						this.$modal.msg("未查询到相关信息，请重新输入！")
						this.formData.sourceCode = null;
					}
				})
			},
			handleFinish(item) {
				let obj = this.resultOptions.find(i => i.text == '通过')
				if (obj) {
					item.additionalJson = JSON.stringify(item.additionalJson)
					item.result = obj.value
					getQualityBill(item.id).then(res => {
						item.qcQualityBillDetailList = res.data.qcQualityBillDetailList
						updateQualityBill(item).then(res => {
							this.$modal.msgSuccess("修改成功!");
						})
					})

				}

			}
		}
	}
</script>

<style>
	.text {
		text-align: center;
	}
</style>