<template>
	<view>
		<uni-collapse>
			<uni-forms ref="form" :modelValue="formData" :rules="rules">
				<uni-forms-item label="工单" :labelWidth='90' name="pwoCode">
					<uni-easyinput v-model="formData.pwoCode" suffixIcon="scan" @iconClick="scanBarpwoCode"
						type="text" />
				</uni-forms-item>
				<uni-forms-item label="批号" :labelWidth='90' name="batchNo">
					<uni-easyinput v-model="formData.batchNo" suffixIcon="scan" @iconClick="scanBarbatchNo"
						type="text" />
				</uni-forms-item>
				<u-button type="primary" @click="submit">查询</u-button>
				<uni-collapse-item title="工单追溯" :open="true" v-if="pwoList.length>0">
					<uni-card :is-shadow="false" v-for="(item,index) in pwoList" :key="index" is-full>
						<view><strong>生产工单作业编码</strong> : {{item.code}}</view>
						<view><strong>生产工单</strong> : {{item.pwoCode}}</view>
						<view><strong>数量</strong> : {{item.num}}</view>
						<view><strong>单位</strong> : {{item.unitId}}</view>
						<view><strong>生产版本</strong> : {{item.productionVersion}}</view>
						<view><strong>目标产品编码</strong> : {{item.ptNoTar}}</view>
						<view><strong>目标产品名称</strong> : {{item.ptTitleTar}}</view>
						<view><strong>工单完成时间</strong> : {{item.pwoAchieveTime}}</view>
						<view><strong>转入时间</strong> : {{item.pieceTransferInTime}}</view>
						<view><strong>完成时间</strong> : {{item.pieceFinishTime}}</view>
						<view><strong>接收仓库</strong> : {{item.destWhId}}</view>
						<view><strong>排程类型</strong> : {{item.schdMode}}</view>
						<view><strong>投料状态</strong> : {{item.feedStatus}}</view>
						<view><strong>工单状态</strong> : {{item.status}}</view>
						<view><strong>工单打印状态</strong> : {{item.prtStatus}}</view>
						<view><strong>自动关闭</strong> : {{item.autoClosed}}</view>
						<view><strong>备注</strong> : {{item.remark}}</view>
					</uni-card>
				</uni-collapse-item>
				<uni-collapse-item title="原料追溯-工单领料总数" :open="true" v-if="mesPwoJobCvDetailTotal.length>0">
					<uni-card :is-shadow="false" v-for="(item,index) in mesPwoJobCvDetailTotal" :key="index" is-full>
						<view><strong>物料</strong> : {{item.materialCode}}</view>
						<view><strong>批号</strong> : {{item.materialBatchNo}}</view>
						<view><strong>出库数量</strong> : {{item.number}}</view>
						<view><strong>单位</strong> : {{item.unitCode}}</view>
						<view><strong>附件</strong> : {{item.enclosure}}</view>
						<view><strong>备注</strong> : {{item.remark}}</view>
					</uni-card>
				</uni-collapse-item>
				<uni-collapse-item title="原料追溯-工单领料单" :open="true" v-if="conversionList.length>0">
					<uni-card :is-shadow="false" v-for="(item,index) in conversionList" :key="index" is-full>
						<view><strong>车间物料转移单编码</strong> : {{item.jobCvCode}}</view>
						<view><strong>工单编码</strong> : {{item.pwoCode}}</view>
						<view><strong>作业编码</strong> : {{item.powJobCode}}</view>
						<view><strong>转移数量</strong> : {{item.num}}</view>
						<view><strong>单位</strong> : {{item.unitId}}</view>
						<view><strong>单据状态</strong> : {{item.status}}</view>
						<view><strong>转移单类型</strong> : {{item.type}}</view>
						<view><strong>预计领料时间</strong> : {{item.estimateDrawTime}}</view>
						<view><strong>领料员</strong> : {{item.drawBy}}</view>
						<view><strong>运输员</strong> : {{item.transportBy}}</view>
						<view><strong>接收员</strong> : {{item.receiveBy}}</view>
						<view><strong>仓库编码</strong> : {{item.whCode}}</view>
						<view><strong>接收时间</strong> : {{item.cvTime}}</view>
						<uni-row>
							<uni-col :span="18">
								<view><strong>附件</strong> : {{item.enclosure}}</view>
								<view><strong>备注</strong> : {{item.remark}}</view>
							</uni-col>
							<uni-col :span="6"> <u-button type="primary"
									@click="conversionDetail(item)">详情</u-button></uni-col>
						</uni-row>
					</uni-card>
				</uni-collapse-item>
				<uni-collapse-item title="工装追溯" :open="true" v-if="OpTranInOptions.length>0">
					<uni-card :is-shadow="false" v-for="(item,index) in OpTranInOptions" :key="index" is-full>
						<view><strong>作业编码</strong> : {{item.code}}</view>
						<view><strong>工单编码</strong> : {{item.pwoCode}}</view>
						<view><strong>制程序号</strong> : {{item.opNo}}</view>
						<view><strong>制程编码</strong> : {{item.opCode}}</view>
						<view><strong>制程名称</strong> : {{item.opTitle}}</view>
						<view><strong>目标产品编码</strong> : {{item.ptNoTar}}</view>
						<view><strong>目标产品名称</strong> : {{item.ptTitleTar}}</view>
						<view><strong>设备编码</strong> : {{item.targetNum3}}</view>
						<view><strong>开工时间</strong> : {{item.planStartTime}}</view>
						<view><strong>完工时间</strong> : {{item.planEndTime}}</view>
						<view><strong>备注</strong> : {{item.remark}}</view>
					</uni-card>
				</uni-collapse-item>
				<uni-collapse-item title="报工追溯" :open="true" v-if="reportList.length>0">
					<uni-card :is-shadow="false" v-for="(item,index) in reportList" :key="index" is-full>
						<view><strong>报工单编码</strong> : {{item.jobReportCode}}</view>
						<view><strong>工单编码</strong> : {{item.pwoCode}}</view>
						<view><strong>作业编码</strong> : {{item.pwoJobCode}}</view>
						<view><strong>目标产品编码</strong> : {{item.ptNoTar}}</view>
						<view><strong>目标产品名称</strong> : {{item.ptTitleTar}}</view>
						<view><strong>设备编码</strong> : {{item.machineId}}</view>
						<view><strong>制程编码</strong> : {{item.opCode}}</view>
						<view><strong>制程名称</strong> : {{item.opTitle}}</view>
						<view><strong>批号</strong> : {{item.batchNo}}</view>
						<view><strong>件号</strong> : {{item.pieceNo}}</view>
						<view><strong>用时</strong> : {{item.timeTake}}</view>
						<view><strong>用时单位</strong> : {{item.timeUnitId}}</view>
						<view><strong>报工类型</strong> : <uni-tag :text="item.type" type="primary"
								v-if="item.type"></uni-tag></view>
						<view><strong>投料总数</strong> : {{item.pickNum}}</view>
						<view><strong>报工数量</strong> : {{item.reportNumber}}</view>
						<view><strong>报工数量单位</strong> : {{item.reportNumUnit}}</view>
						<view><strong>报工原因</strong> : <uni-tag :text="item.reasonText" type="primary"
								v-if="item.reasonText"></uni-tag></view>
						<view><strong>初始产量</strong> : {{item.startNum}}</view>
						<view><strong>完工产量</strong> : {{item.endNum}}</view>
						<view><strong>状态</strong> : <uni-tag :text="item.statusText" type="warning"></uni-tag></view>
						<view><strong>加工人员</strong> : {{item.createByName}}</view>
						<view><strong>开工时间</strong> : {{item.startTime}}</view>
						<view><strong>完工时间</strong> : {{item.endTime}}</view>
						<view><strong>创建时间</strong> : {{item.createTime}}</view>
						<view><strong>报工人</strong> : {{item.reportBy}}</view>
						<view><strong>更新人</strong> : {{item.updateByName}}</view>
						<uni-row>
							<uni-col :span="18">
								<view><strong>修改时间</strong> : {{item.updateTime}}</view>
								<view><strong>备注</strong> : {{item.remark}}</view>
							</uni-col>
							<uni-col :span="6"> <u-button type="primary"
									@click="reportDetail(item)">详情</u-button></uni-col>
						</uni-row>
					</uni-card>
				</uni-collapse-item>
			</uni-forms>

		</uni-collapse>
	</view>
</template>

<script>
	import {
		listPwo,
		listReport
	} from "@/api/mes/pwoDraw";
	import {
		listConversion,
		getConversion,
		calculateCvTotalNum
	} from "@/api/mes/jobIn";

	import {
		updatePwoJob,
		listEquipment,
		getPwoJob,
		listPwoJob,
		getReport
	} from "@/api/mes/jobReport.js"
	import {
		getDicts
	} from "@/api/system/dict/dictData.js";
	export default {
		mounted() {
			listEquipment().then((res) => {
				let map = new Map();
				for (let i = 0; i < res.rows.length; i++) {
					map.set(res.rows[i].id, res.rows[i]);
				}
				this.eqpMap = new Map(map);
			});
			getDicts("mes_job_report_defect").then(res => {
				this.defectOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			getDicts("mes_job_report_scrap").then(res => {
				this.scrapOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})
			getDicts("mes_job_report_loss").then(res => {
				this.lossOptions = res.data.map(dict => {
					return {
						text: dict.dictLabel,
						value: dict.dictValue,
						diasble: false
					}
				});
			})

			getDicts("mes_job_report_status").then(res => {
				this.statusOptions = res.data.map(dict => {
					let a = Number(dict.dictValue)
					return {
						text: dict.dictLabel,
						value: a,
						diasble: false
					}
				});
				console.log(this.statusOptions)
			})
		},
		data() {
			return {
				reportList: [],
				statusOptions: [],
				lossOptions: [],
				scrapOptions: [],
				defectOptions: [],
				eqpMap: new Map(),
				OpTranInOptions: [],
				formData: {
					isListAll: true,
					orderByColumn: "id",
					isAsc: "desc",
					batchNo: null,
					pwoCode: null,
				},
				pwoList: [],
				mesPwoJobCvDetailTotal: [],
				conversionList: [],
				rules: {},
			}
		},
		methods: {
			//工单
			scanBarpwoCode(code) {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.pwoCode = res.result;
					}
				});
			},
			//批号
			scanBarbatchNo() {
				const _this = this;
				uni.scanCode({
					scanType: ['barCode', 'qrCode'],
					success: function(res) {
						_this.formData.batchNo = res.result;
					}
				});
			},
			submit() {
				const _this = this;
				if (_this.formData.pwoCode && _this.formData.pwoCode != "" || _this.formData.batchNo && _this.formData
					.batchNo != "") {
					_this.$modal.loading('提交中')
					listPwo(_this.formData).then(async res => {
						_this.$modal.closeLoading();
						_this.pwoList = res.rows;
						let objConversion = {
							pwoCode: res.rows[0].pwoCode,
							type: 1,
						};
						listConversion(objConversion).then((response) => {
							_this.conversionList = response.rows;
						});
						calculateCvTotalNum(objConversion).then((response) => {
							_this.mesPwoJobCvDetailTotal = response.rows;
						});
					});
					let obj = {
						batchNo: this.formData.batchNo,
						isListAll: true,
						pwoCode: this.formData.pwoCode,
					};
					listPwoJob(obj).then((res) => {
						let OpTranInOptions = res.rows;
						OpTranInOptions = OpTranInOptions.map((job) => {
							let item = {
								id: job.id,
								code: job.code,
								pwoCode: job.pwoCode,
								opNo: job.opNo,
								opCode: job.opCode,
								opTitle: job.opTitle,
								ptNoTar: job.ptNoTar,
								ptTitleTar: job.ptTitleTar,
								// targetNum: '12101101',
								// targetNum1: '通用三爪',
								targetNum3: job.eqpId != null ?
									this.eqpMap.get(job.eqpId).equipmentTitle : null,
								targetNum4: job.eqpId != null ?
									this.eqpMap.get(job.eqpId).equipmentCode : null,
							};
							return item;
						});
						this.OpTranInOptions = OpTranInOptions;
					});
					listReport(this.formData).then((response) => {
						this.reportList = response.rows.map(item => {
							console.log(item.type)
							if (item.type == '不良品') {
								item.reasonText = this.defectOptions.find(tp => tp.value == item.reason)
									.text
							} else if (item.type == '报废品') {
								item.reasonText = this.scrapOptions.find(tp => tp.value == item.reason)
									.text
							} else if (item.type == '损耗') {
								item.reasonText = this.lossOptions.find(tp => tp.value == item.reason).text
							}
							if (item.status) {
								let obj = this.statusOptions.find(tp => tp.value == item.status)
								if (obj) item.statusText = obj.text
							}
							return item
						});
					});
				}
			},
			conversionDetail(item) {

				const id = item.id;
				getConversion(id).then((response) => {
					let type = response.data.type
					let mesPwoJobCvDetailList = response.data.mesPwoJobCvDetailList
					uni.navigateTo({
						url: '/pages/mes/jobCv/pwoDraw?pwoCode=' +
							this.formData.pwoCode + '&type=' + type + '&mesPwoJobCvDetailList=' +
							encodeURIComponent(JSON.stringify(
								mesPwoJobCvDetailList))
					});
				});
			},
			reportDetail(item) {
				const id = item.id;
				getReport(id).then((response) => {
					let formData = response.data
					if (item.type == '合格品') {
						uni.navigateTo({
							url: '/pages/mes/jobReport/pass?formData=' +
								encodeURIComponent(JSON.stringify(
									formData))
						});
					} else if (item.type == '不良品') {
						uni.navigateTo({
							url: '/pages/mes/jobReport/defect?formData=' +
								encodeURIComponent(JSON.stringify(
									formData))
						});
					} else if (item.type == '报废品') {
						uni.navigateTo({
							url: '/pages/mes/jobReport/scrap?formData=' +
								encodeURIComponent(JSON.stringify(
									formData))
						});
					} else if (item.type == '损耗品') {
						uni.navigateTo({
							url: '/pages/mes/jobReport/loss?formData=' +
								encodeURIComponent(JSON.stringify(
									formData))
						});
					}

				});
			}
		}
	}
</script>

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