<template>
	<el-container>
		<el-main class="orderMain" v-loading="loading" element-loading-text="加载中...">
			<el-scrollbar>
				<detailView :data="info">
					<template #review>
						<div class="cardItem cardItemNoBorder">
							<div class="title">维保部件信息</div>
							<div class="cardBody">
								<el-row>
									<el-col :span="24" :lg="24">
										<span class="label">验证ERP库存：</span>
										<span class="text">{{info.is_verify_erp_inventory?'是':'否'}}</span>
									</el-col>
								</el-row>
							</div>
							<div class="cardBody">
								<sc-table ref="table" :data="info.component_info" style="width: 100%;" :hidePagination="true" :hideDo="true" :hideEmpty="true" :size="size">
									<sc-table-column prop="component_serial_no" label="部件SN/Sp" :show-overflow-tooltip="true" width="160"></sc-table-column>
									<sc-table-column prop="component_manufacturer" label="部件厂商" :show-overflow-tooltip="true" width="130"></sc-table-column>
									<sc-table-column prop="component_model" label="部件型号" :show-overflow-tooltip="true" width="160"></sc-table-column>
									<sc-table-column prop="component_pn" label="部件PN/Sp" :show-overflow-tooltip="true" width="150"></sc-table-column>
									<sc-table-column prop="bd_backup_count" label="甚于BD数" :show-overflow-tooltip="true" width="120"></sc-table-column>
								</sc-table>
							</div>
						</div>
						<div class="reviewBox">
							<el-input type="textarea" v-model="opinion" min="4" placeholder="请填写具体原因"></el-input>
							<div class="btnView">
								<el-button type="primary" :size="size" @click="save(1)" :loading="confirmLoading">审核通过</el-button>
								<el-button type="danger" :size="size" @click="save(2)" :loading="cancelLoading">审核拒绝</el-button>
							</div>
						</div>
					</template>
				</detailView>
			</el-scrollbar>
		</el-main>
	</el-container>
</template>

<script>
import detailView from "@/views/order/orderList/detailView";
export default {
	name: "",
	components:{
		detailView
	},
	data(){
		return{
			size:'small',
			loading:false,
			confirmLoading:false,
			cancelLoading:false,
			opinion:"",
			params:{
				order_id:Number(this.$route.query.id)
			},
			info:{
				is_verify_erp_inventory:false,
				component_info:[]
			},
		}
	},
	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
						}
					]
				}
			}
			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;
		},
		async save(type) {
			let params = {
				order_ids: [this.params.order_id],
				status: type == 1?true:false,
				opinion: this.opinion
			}
			if (type == 1) {
				delete params.opinion
				this.confirmLoading = true;
			}else{
				this.cancelLoading = true;
			}
			const res = await this.$API.orders.order.maintenance.review.post(params);
			if (res.code == 200) {
				this.$message.success('审核成功');
				await this.getData();
			}
			this.confirmLoading = false;
			this.cancelLoading = false;
		}
	}
}
</script>

<style scoped lang="scss">
	.btnView{
		margin-top: 15px;
		display: flex;justify-content: flex-start;
	}
	.orderMain .footerCard .cardItemNoBorder{
		border-bottom: 0;
	}
</style>
