<template>
	<el-container class="mainBox mainBoxHeaderNoBorder">
		<el-main class="nopadding">
			<div class="searchMain logSearchMain">
				<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 class="exportBtn">
					<scExport :size="size" @exportData="exportData" @updateShow="exportChangeShow" :show="exportShow" type="8">
						<el-button v-auth="'sysLogExport'" :size="size" icon="sc-icon-Download" :disabled="exportShow" @click="exportData">导出</el-button>
					</scExport>
				</div>
			</div>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" stripe border :size="size" highlightCurrentRow @row-click="rowClick" @columnBack="columnBack">
				<template #type="scope">
					<span v-for="(item,ind) in setMap.typeList" :key="ind">
						<span v-if="item.type == scope.row.type">{{item.type_text}}</span>
					</span>
				</template>
			</scTable>
		</el-main>
	</el-container>

	<el-drawer v-model="infoDrawer" title="日志详情" :size="600" destroy-on-close>
		<info ref="info"></info>
	</el-drawer>
</template>

<script>
import info from '../info'
import {eventBus} from "@/utils/eventBus";
export default {
	components: {
		info,
	},
	data() {
		return {
			size:"small",
			setMap:{
				name:[],
				typeList:[]
			},
			infoDrawer: false,
			date: [],
			list:{
				apiObj: this.$API.system.log.list,
				column:[]
			},
			exportShow:false,
			searchShow:false,
			searchHeaderShow:false,
			filterMap:{
				data:{}
			},
			params: {},
		}
	},
	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.getTypeList();
	},
	methods: {
		/**
		 * 表格检索开始
		 * */
		columnBack(val){
			val.forEach(item=>{
				if(item.prop === "type"){
					item.data = this.setMap.typeList;
				}
			})
			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.creator_id = {
				operator:"in",
				value:[this.$TOOL.data.get('USER_INFO').id]
			};
			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.creator_id = {
				operator:"in",
				value:[this.$TOOL.data.get('USER_INFO').id]
			};
			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.system.log.operation.post(searchParams);
			if (res.code == 200) {
				if (res.data && res.data.length > 0) {
					res.data.forEach(item => {
						if (data.prop === 'type') {
							item.label = item[data.prop+'_text'];
							item.value = item[data.prop];
						} 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;
					}
				})
			}
		},
		/** 表格检索结束 */

		async getTypeList() {
			const res = await this.$API.system.log.operation.post();
			if(res.code == 200){
				if(res.data.length>0){
					res.data.forEach(item=>{
						item.value = item.type;
						item.label = item.type_text;
					})
				}
				this.setMap.typeList = res.data;
			}
		},
		// 下载导出
		exportChangeShow(params){
			if(params.type == 8){
				this.exportShow = params.status==0?true:false;
			}
		},
		async exportData() {
			if(this.exportShow) return
			const res = await this.$API.system.log.export.post(this.params);
			if(res.code == 200){
				this.exportShow = true;
				this.$message.success('开始导出');
			}
		},

		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();
		},
		rowClick(row){
			this.infoDrawer = true
			this.$nextTick(() => {
				this.$refs.info.setData(row)
			})
		}
	}
}
</script>

<style>
</style>
