<template>
	<el-container>
		<el-main class="orderMain" v-loading="loading" element-loading-text="加载中...">
			<el-scrollbar>
				<detailView :data="info">
					<template #submit>
						<div class="cardItem">
							<div class="title titleFlex">
								<span class="name">维保部件信息</span>
							</div>
							<div class="cardBody">
								<el-row>
									<el-col :span="24" :lg="24" class="colFlex">
										<span class="label labelText">验证ERP库存：</span>
										<span class="text">{{info.is_verify_erp_inventory?'是':'否'}}</span>
									</el-col>
								</el-row>
							</div>
							<div class="cardBody">
								<template v-if="info.business_status<5">
									<sc-table ref="table" :data="info.component_info" style="width: 100%;" :hidePagination="true" :hideDo="true" :hideEmpty="true" :size="size">
										<sc-table-column align="center" prop="component_serial_no" label="部件SN/Sp" :show-overflow-tooltip="true" width="160"></sc-table-column>
										<sc-table-column align="center" prop="component_manufacturer" label="部件厂商" :show-overflow-tooltip="true" width="130"></sc-table-column>
										<sc-table-column align="center" prop="component_model" label="部件型号" :show-overflow-tooltip="true" width="160"></sc-table-column>
										<sc-table-column align="center" prop="component_pn" label="部件PN/Sp" :show-overflow-tooltip="true" width="150"></sc-table-column>
										<sc-table-column align="center" prop="bd_backup_count" label="甚于BD数" :show-overflow-tooltip="true" width="120"></sc-table-column>
									</sc-table>
								</template>
								<template v-else>
									<sc-table ref="table" :data="repair_records" style="width: 100%;" :hidePagination="true" :hideDo="true" :hideEmpty="true" :size="size">
										<sc-table-column align="center" prop="work_order_number" label="维修单号" :show-overflow-tooltip="true" width="160"></sc-table-column>
										<sc-table-column align="center" prop="machine_sn" label="整机SN" :show-overflow-tooltip="true" width="160"></sc-table-column>
										<sc-table-column align="center" prop="fault_type" label="故障类型" :show-overflow-tooltip="true" width="160"></sc-table-column>
										<sc-table-column align="center" prop="part_sn_two" label="新件SN" :show-overflow-tooltip="true" width="130"></sc-table-column>
										<sc-table-column align="center" prop="part_mpn_two" label="新件MPN" :show-overflow-tooltip="true" width="130"></sc-table-column>
										<sc-table-column align="center" prop="part_sn_one" label="旧件SN" :show-overflow-tooltip="true" width="130"></sc-table-column>
										<sc-table-column align="center" prop="part_mpn_one" label="旧件MPN" :show-overflow-tooltip="true" width="130"></sc-table-column>
										<sc-table-column align="center" prop="confirmed_defects" label="不良现象" :show-overflow-tooltip="true" width="150"></sc-table-column>
										<sc-table-column align="center" prop="analysis_steps" label="现场步骤分析" :show-overflow-tooltip="true" width="120"></sc-table-column>
										<sc-table-column align="center" prop="sla_non_compliance_reason" label="SLA不达标原因" :show-overflow-tooltip="true" width="120"></sc-table-column>
										<sc-table-column align="center" prop="remark" label="备注" :show-overflow-tooltip="true" ></sc-table-column>
									</sc-table>
								</template>
							</div>
						</div>
					</template>
				</detailView>
			</el-scrollbar>
		</el-main>
	</el-container>
</template>

<script>
import detailView from "@/views/order/orderList/detailView";
export default {
	name: "order-info",
	components:{
		detailView
	},
	data(){
		return{
			size:'small',
			loading:false,
			params:{
				order_id:Number(this.$route.query.id)
			},
			info:{
				is_verify_erp_inventory:false,
				component_info:[]
			},
			repair_records:[],
		}
	},
	mounted() {
		this.getData();
	},
	methods:{
		async getData() {
			this.loading = true;
			const res = await this.$API.orders.order.maintenance.info.post(this.params);
			if(res.code == 200){
				if(res.data.process_engine){
					let obj = this.processList(res.data.process_engine);
					res.data.process_engine = obj.processedList;
					res.data.process_num = obj.num;
				}
				this.info = res.data;
				if(res.data.component_info.length==0){
					this.info.component_info = [
						{
							component_serial_no:res.data.component_serial_no,
							component_manufacturer:res.data.component_manufacturer,
							component_model:res.data.component_model,
							component_pn:res.data.component_pn,
							bd_backup_count:res.data.bd_backup_count
						}
					]
				}
				// res.data.business_status>4
				if(res.data.repair_records && res.data.repair_records.length>0){
					// 取数组 m_ascription 为 1, 2 的最后一条数据
					res.data.repair_records.forEach(item=>{
						item.fault_type = res.data.fault_type;
					})
					let m_ascription_one = res.data.repair_records.filter(item=>item.m_ascription === 1).pop();
					let m_ascription_two = res.data.repair_records.filter(item=>item.m_ascription === 2).pop();

					if(m_ascription_one){
						m_ascription_one.part_sn_one = m_ascription_one.part_sn;
						m_ascription_one.part_mpn_one = m_ascription_one.part_mpn;
					}
					if(m_ascription_two){
						m_ascription_one.part_sn_two = m_ascription_two.part_sn;
						m_ascription_one.part_mpn_two = m_ascription_two.part_mpn;
					}

					this.repair_records = [m_ascription_one].filter(item=>item);
				}
			}
			this.loading = false;
		},
		processList(list) {
			let obj = {
				num:0,
				processedList:this.$TOOL.objCopy(list)
			}
			let foundNodeTrue = false;
			for (let i = 0; i < obj.processedList.length; i++) {
				if (obj.processedList[i].node === true) {
					foundNodeTrue = true;
					obj.processedList[i].status = 'finish';
					obj.num = i;
				} else if (!foundNodeTrue) {
					obj.processedList[i].status = 'success';
				}
				if (foundNodeTrue && obj.processedList[i].node !== true) break;
			}
			return obj;
		}
	}
}
</script>

<style scoped lang="scss">

</style>
