<template>
	<el-container>
		<el-main class="orderMain" v-loading="loading" element-loading-text="加载中...">
			<el-scrollbar>
				<detailView :data="info">
					<template #review>
						<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: "preview-order",
	components:{
		detailView
	},
	data(){
		return{
			size:'small',
			loading:false,
			confirmLoading:false,
			cancelLoading:false,
			opinion:"",
			params:{
				order_id:Number(this.$route.query.id)
			},
			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;
			}
			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) {
				await this.getData();
			}
			this.confirmLoading = false;
			this.cancelLoading = false;
		}
	}
}
</script>

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