<template>
	<el-container class="mainBox mainBoxHeaderNoBorder">
		<el-main class="nopadding">
			<div class="searchMain">
				<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>
					<el-button type="danger" v-auth="'deleteOutStockImportMesInfo'" plain :size="size" icon="el-icon-Delete" :disabled="selection.length==0" @click="batch_del"></el-button>
					<scExport :size="size" @exportData="exportData" @updateShow="exportChangeShow" :show="exportShow" type="47">
						<el-button :size="size" v-auth="'getOutStockExportMesInfo'" icon="sc-icon-Download" :disabled="exportShow" @click="exportData">导出</el-button>
					</scExport>
				</div>
			</div>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" 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 #logo="scope">
					<el-image class="logoCell" :src="scope.row.logo" preview-teleported :preview-src-list="[scope.row.logo]" fit="contain">
						<template #error>
							<div class="image-slot" style="text-align: center;font-size: 20px;">
								<el-icon><el-icon-Picture /></el-icon>
							</div>
						</template>
					</el-image>
				</template>
			</scTable>
		</el-main>
	</el-container>

</template>

<script>

import {eventBus} from "@/utils/eventBus";

export default {
	name:"",
	components: {

	},
	data() {
		return {
			size:'small',
			dialog: {
				save: false,
				show: false,
			},
			list: {
				apiObj: this.$API.shipment.out.stockBom,
				column: [],
			},
			selection: [],
			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
		},
	},
	methods: {
		/** 检索开始 */
		columnBack(val){
			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.shipment.out.bomField.post(searchParams);
			if (res.code == 200) {
				if (res.data && res.data.length > 0) {
					res.data.forEach(item => {
						item.label = item[data.prop];
						item.value = item[data.prop];
					})
				}
				this.list.column.forEach(item => {
					if (item.prop == data.prop) {
						item.data = res.data;
					}
				})
			}
		},
		/** 检索结束 */

		// 删除
		batch_del(){
			this.$confirm(`确定删除选中 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: this.selection.map(em=>em.id)};
				const res = await this.$API.shipment.out.deleteBom.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;
		},
		// 下载导出
		exportChangeShow(params){
			if(params.type == 47){
				this.exportShow = params.status==0?true:false
			}
		},
		async exportData() {
			if(this.exportShow) return
			const res = await this.$API.shipment.out.bomExport.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();
		},
		handleSaveSuccess(){
			this.$refs.table.refresh();
		},
	}
}
</script>

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