<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header class="header">
			<div class="left-panel">
				<el-button type="primary" :disabled="selection.length!==1" :size="size" icon="sc-icon-MaintenanceSetup" @click="upkeep">执行维修</el-button>
			</div>
			<div class="right-panel">

			</div>
		</el-header>
		<el-main class="nopadding">
			<div class="flowPath">
				<flow :list="flowList"/>
			</div>
			<div class="searchMain searchMainNoTop">
				<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>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" stripe border :size="size" :hideTotal="false" @selection-change="selectionChange" @columnBack="columnBack">
				<el-table-column type="selection" align="center" width="40"></el-table-column>
				<template #repair_status="scope">
					<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.repair_status">{{item.label}}</span>
					</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>
									<el-dropdown-item icon="sc-icon-MaintenanceSetup" @click="table_upkeep(scope.row)">执行维修</el-dropdown-item>
									<el-dropdown-item icon="sc-icon-See" @click="table_show(scope.row, 'see')">工单详情</el-dropdown-item>
									<el-dropdown-item icon="sc-icon-OrderLog" @click="table_logs(scope.row)" divided>工单日志</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
				<template #total>
					<div class="countView">
						<div class="countItem" v-for="(item,index) in countParams" :key="index">
							<span class="name">{{item.title}}:</span>
							<span class="num">{{item.value}}</span>
						</div>
					</div>
				</template>
			</scTable>
		</el-main>
	</el-container>

</template>

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

export default {
	name:"repairList",
	components: {
		flow,
	},
	data() {
		return {
			size:'small',
			flowList:[
				{name:'维修确认',left:false,right:true},
				{name:'维修中',left:true,right:true},
				{name:'维修完成',left:true,right:false},
			],
			dialog: {
				save: false,
				show: false,
			},
			setMap:{
				statusList:[],
			},
			list: {
				apiObj: this.$API.orders.order.repair.list,
				column: [],
			},
			selection: [],
			exportShow:false,
			searchShow:false,
			searchHeaderShow:false,
			filterMap:{
				data:{}
			},
			params: {},
			countParams:this.$store.state.msg.repair_count
		}
	},
	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();
		// 获取新消息
		eventBus.$on('sockBack', this.getWsResult);
	},
	unmounted() {
		// 获取新消息
		eventBus.$off('sockBack', this.getWsResult);
	},
	methods: {
		getWsResult(res){
			if(res.data && res.data.type == 23){
				this.$store.commit('set_repair_count',res.data.list_repair_info.repair_count);
				this.countParams = res.data.list_repair_info.repair_count;
			}
		},

		async getStatusList() {
			const res = await this.$API.orders.order.repair.status.post();
			if (res.code == 200) {
				this.setMap.statusList = res.data;
			}
		},

		/**
		 * 表格检索开始
		 * */
		columnBack(val){
			val.forEach(item=>{
				if(item.prop === "repair_status"){
					item.data = this.setMap.statusList;
				}
			})
			this.list.column = val;
		},
		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.repair.field.post(searchParams);
			if (res.code == 200) {
				if (res.data && res.data.length > 0) {
					res.data.forEach(item => {
						if (data.prop === 'repair_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;
					}
				})
			}
		},
		/** 表格检索结束 */

		//添加
		add(){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		upkeep(){
			const row = this.selection[0];
			eventBus.$emit('tagClose','/order/repair-action',{id:row.id,order_id:row.order_id});
		},
		// 执行维修
		table_upkeep(row){
			eventBus.$emit('tagClose','/order/repair-action',{id:row.id,order_id:row.order_id});
		},
		//查看
		table_show(row){
			eventBus.$emit('tagClose','/order/order-info',{id:row.order_id});
		},
		// 工单日志
		table_logs(row){
			eventBus.$emit('tagClose','/order/order-logs',{id:row.id,type:2});
		},
		// 删除
		async table_del(row){
			this.$confirm(`确定删除 ${row.name} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {id: row.id};
				const res = await this.$API.system.company.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					await this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(()=>{})
		},

		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		upSearch(){
			this.$refs.table.upData(this.params);
		},
		reset(){
			this.params = {};
			this.filterMap.data = {};
			this.$refs.scSearch.reload();
			eventBus.$emit('reset-popovers');
			this.$refs.table.reload();
		},
		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>
