<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header class="header">
			<div class="left-panel">
				<el-button type="primary" v-auth="'repairOrderConfirm'" :size="size" icon="sc-icon-ConfirmOrder" @click="confirmOrder">确认工单</el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<div class="flowPath">
				<flow :list="flowList"/>
			</div>
			<div class="fieldsSwitchBox searchConditionItem" v-if="!fieldsShow">
				<div class="leftBox"></div>
				<div class="rightBox" @click="searchClick">
					<el-icon class="icon"><sc-icon-SwitchQuery /></el-icon>
					<span class="name">切换查询方式</span>
				</div>
			</div>
			<div class="searchMain searchMainNoTop" v-if="!fieldsShow">
				<scTableSearch ref="scSearch" :searchList="list.column" :paramsData="params" :searchShow="searchShow" @fetchSelectData="getSelectData" @changeHeaderData="getHeaderData"></scTableSearch>

				<div class="searchItem searchBtn" v-if="searchHeaderShow">
					<el-button :size="size" :icon="searchShow?'el-icon-ArrowUpBold':'el-icon-ArrowDownBold'" @click="searchShowClick">{{searchShow?'收起':'更多'}}</el-button>
					<el-button :size="size" type="primary" icon="el-icon-search" @click="upSearch">查询</el-button>
					<el-button :size="size" type="info" icon="el-icon-RefreshRight" @click="reset">重置</el-button>
				</div>
			</div>
			<div class="searchMain searchMainMarginNone searchMainNoTop" v-if="fieldsShow">
				<herderMultipleSearch @fieldsSwitch="fieldsSwitch" @fieldsSearch="fieldsSearch" :fieldsShow="fieldsShow" :searchList="list.fields"></herderMultipleSearch>
			</div>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" :params="params" row-key="id" stripe border :size="size" @selection-change="selectionChange" @columnBack="columnBack">
				<el-table-column type="selection" align="center" width="40"></el-table-column>
				<template #repair_date="scope">{{scope.row.repair_order && scope.row.repair_order.repair_date?scope.row.repair_order.repair_date:''}}</template>
				<template #schedule_date="scope">{{scope.row.repair_order && scope.row.repair_order.schedule_date?scope.row.repair_order.schedule_date:''}}</template>
				<template #business_status="scope">
					<span v-if="scope.row.business_status">
						<span v-for="(item,ind) in setMap.statusList" :key="ind">
							<span :style="{color:item.value==1?`var(--el-order-color-1)`:item.value==2?`var(--el-order-color-2)`:item.value==3?`var(--el-order-color-3)`:item.value==4?`var(--el-order-color-4)`:`var(--el-order-color-100)`}"
								  v-if="item.value == scope.row.business_status">{{item.label}}</span>
						</span>
					</span>
					<span v-else :style="{color:`var(--el-order-color-1)`}">待确认</span>
				</template>
				<el-table-column label="操作" fixed="right" align="center" width="150">
					<template #default="scope">
						<el-dropdown>
							<el-button class="noBorderBtn" icon="el-icon-more" :size="size"></el-button>
							<template #dropdown>
								<el-dropdown-menu>
									<div v-auth="'repairOrderConfirm'">
										<el-dropdown-item  icon="sc-icon-ConfirmOrder" @click="confirmOrderClick(scope.row)">确认工单</el-dropdown-item>
									</div>
									<div v-auth="'order_plan_detail'">
										<el-dropdown-item @click="table_show(scope.row, 'see')" icon="sc-icon-See">工单详情</el-dropdown-item>
									</div>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<confirm-dialog v-if="dialog.confirm" ref="confirmDialog" @success="handleSaveSuccess" @closed="dialog.confirm=false"></confirm-dialog>

</template>

<script>
import flow from '../orderList/components/flow'
import confirmDialog from './confirmOrder'
import {eventBus} from "@/utils/eventBus";

export default {
	name:"orderPlan",
	components: {
		flow,
		confirmDialog,
	},
	data() {
		return {
			fieldsShow:false,
			size:'small',
			flowList:[
				{name:'确认工单',left:false,right:true},
				{name:'下发维保',left:true,right:false},
			],
			setMap:{
				statusList:[]
			},
			dialog: {
				confirm: false,
				show: false,
			},
			list: {
				apiObj: this.$API.orders.order.plan.list,
				column: [],
				fields:{}
			},
			selection: [],
			exportShow:false,
			searchShow:false,
			searchHeaderShow:false,
			filterMap:{
				data:{}
			},
			params: {
				business_status:{
					operator:"in",
					value:[3]
				}
			},
		}
	},
	provide(){
		return{
			filterUploadClick:this.filterClick,
			filterUploadParams:this.filterParams,
			filterTagClose:this.tagClose,
			filterModelParams:this.filterModelParams,
		}
	},
	watch:{
		'list.column':{
			handler(val){
				this.searchHeaderShow = val.length>0 && val.some(em=>em.is_search);
			},
			immediate:false,
			deep:true
		}
	},
	computed:{
		filterModelParams(){
			return this.filterMap
		},
	},
	mounted() {
		this.getStatusList();
	},
	methods: {
		async getStatusList() {
			const res = await this.$API.orders.order.maintenance.status.post();
			if (res.code == 200) {
				this.setMap.statusList = res.data;
			}
		},
		/** 检索开始 */
		searchClick(){
			this.fieldsShow = true;
		},
		fieldsSwitch(val){
			this.fieldsShow = val;
		},
		fieldsSearch(params){
			this.params = params;
			this.upSearch();
		},

		columnBack(val,fieldsData){
			val.forEach(item=>{
				if(item.prop === "business_status"){
					item.data = this.setMap.statusList;
				}
			})
			this.list.column = val;
			this.list.fields = fieldsData;
		},
		async filterClick(item) {
			let {data, params} = item;
			let filterParams = Object.assign(this.params,params);
			this.filterMap.data = filterParams;
			let searchParams = this.$TOOL.objCopy(this.params);
			searchParams.field = ""
			if (typeof data.prop === 'string') {
				searchParams.field = data.prop;
			} else {
				searchParams.field = data.prop[0];
			}
			if (data.search_type == 'select' || data.search_type == 'checkbox') {
				await this.getField(data,searchParams);
			}
		},
		filterParams(params){
			let filterParams = Object.assign(this.params,params);
			this.filterMap.data = filterParams;
			this.upSearch();
		},
		tagClose(params){
			let filterParams = Object.assign(this.params,params);
			this.filterMap.data = filterParams;
		},
		searchShowClick(){
			this.searchShow = !this.searchShow;
			eventBus.$emit('close-all-popovers');
		},
		getHeaderData(params){
			this.params = params;
			this.filterMap.data = params;
		},
		async getSelectData(item) {
			let {data, params} = item;
			this.params = params;          // 列表需要的参数
			this.filterMap.data = params;  // 表头组件需要转 才能传的参数
			let searchParams = this.$TOOL.objCopy(params);
			searchParams.field = ""
			if (typeof data.prop === 'string') {
				searchParams.field = data.prop;
			} else {
				searchParams.field = data.prop[0];
			}
			if (data.search_type == 'select' || data.search_type == 'checkbox') {
				await this.getField(data,searchParams)
			}
		},
		async getField(data,searchParams) {
			const res = await this.$API.orders.order.maintenance.field.post(searchParams);
			if (res.code == 200) {
				if (res.data && res.data.length > 0) {
					res.data.forEach(item => {
						if (data.prop === 'business_status') {
							this.setMap.statusList.forEach(em => {
								if (em.value === item[data.prop]) {
									item.label = em.label;
									item.value = em.value;
								}
							})
						} else {
							item.label = item[data.prop];
							item.value = item[data.prop];
						}
					})
				}
				this.list.column.forEach(item => {
					if (item.prop == data.prop) {
						item.data = res.data;
					}
				})
			}
		},
		/** 检索结束 */

		confirmOrder(){
			this.dialog.confirm = true;
			this.$nextTick(() => {
				this.$refs.confirmDialog.open('confirm').setData(this.selection)
			})
		},
		// 数据确认
		confirmOrderClick(row){
			this.dialog.confirm = true;
			this.$nextTick(() => {
				this.$refs.confirmDialog.open('confirm').setData([row])
			})
		},
		//查看
		table_show(row){
			eventBus.$emit('tagClose','/order/order-info',{id:row.id});
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		upSearch(){
			this.$refs.table.upData(this.params);
		},
		reset(){
			this.params = {
				business_status:{
					operator:"in",
					value:[3]
				}
			};
			this.filterMap.data = {};
			this.$refs.scSearch.reload();
			eventBus.$emit('reset-popovers');
			this.$refs.table.reload(this.params);
		},
		handleSaveSuccess(){
			this.$refs.table.refresh();
		},
	}
}
</script>

<style lang="scss" scoped>
.mainBox{
	padding: 0;
	.header{
		padding: 0 10px;
	}
	.searchMain{
		margin-left: 10px;
		margin-right: 10px;
	}
	.scTable{
		padding: 0 10px;
	}
}
.logoCell{
	display: flex;
	align-items: center;
	height: 20px;
	margin: 0 auto;
}
</style>
