<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'addCost'" :size="size" icon="el-icon-plus" @click="add">新增</el-button>
				<el-button type="danger" plain v-auth="'deleteCost'" :disabled="selection.length>0?false:true" :size="size" icon="el-icon-delete" @click="all_del"></el-button>
				<scImport ref="scImport" :size="size" :httpDisabled="httpDisabled" type="18" title="批量导入费用流水" @parentParams="importUpload" @importSuccess="importSuccess">
					<template #header>
						<el-button v-auth="'costFlowImport'" type="primary" :size="size" plain @click="importFile">批量导入</el-button>
					</template>
					<template #download>
						<div v-auth="'costFlowImportTemplate'" @click="importTemplate">下载导入模版</div>
					</template>
				</scImport>
			</div>
			<div class="right-panel">
				<scExport :size="size" @exportData="exportData" @updateShow="exportChangeShow" :show="exportShow" type="19">
					<el-button v-auth="'costFlowExport'" :size="size" icon="sc-icon-Download" :disabled="exportShow" @click="exportData">导出</el-button>
				</scExport>
			</div>
		</el-header>
		<el-main class="nopadding">
			<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" @selection-change="selectionChange" @columnBack="columnBack" :hideTotal="false">
				<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>
				<template #active_status="scope">
					<el-switch :size="size" v-model="scope.row.active_status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="true" :inactive-value="false"></el-switch>
				</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="'addCost'">
										<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑费用</el-dropdown-item>
									</div>
									<div v-auth="'deleteCost'">
										<el-dropdown-item @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete">删除费用</el-dropdown-item>
									</div>
									<el-dropdown-item @click="table_show(scope.row, 'see')" icon="sc-icon-See" divided>查看详情</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
				<template #total="{params}">
					<div class="totalView">
						<span class="name">汇总合计：</span>
						<div class="total-item">
							<span class="text">数量</span>
							<span class="num">{{params.total_quantity}}单</span>
						</div>
						<div class="total-item">
							<span class="text">金额</span>
							<span class="num">{{params.total_cost}}</span>
						</div>
					</div>
				</template>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'
import {eventBus} from "@/utils/eventBus";
export default {
	name:"costFlow",
	components: {
		saveDialog
	},
	data() {
		return {
			size:'small',
			httpDisabled:false,
			dialog: {
				save: false,
				show: false,
			},
			list: {
				apiObj: this.$API.finance.cost.list,
				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.finance.cost.field.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;
					}
				})
			}
		},
		/** 检索结束 */

		//添加
		add(){
			this.dialog.save = true;
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},

		// 批量导入
		importFile(){
			this.$nextTick(()=>{
				this.$refs.scImport.importFile();
			})
		},
		async importTemplate() {
			const res = await this.$API.finance.cost.template.post();
			const blob = new Blob([res]);
			const text = new Date().getTime();
			const eLink = document.createElement('a');
			eLink.download = "费用流水导入模版_"+text+'.xlsx';
			eLink.style.display = 'none';
			eLink.href = URL.createObjectURL(blob);
			document.body.appendChild(eLink);
			eLink.click();
			URL.revokeObjectURL(eLink.href);
			document.body.removeChild(eLink);
		},
		async importUpload(params) {
			this.httpDisabled = true;
			const res = await this.$API.finance.cost.import.post(params);
			if(res.code == 200){
				this.$message.success('上传成功，开始导入数据');
			}
		},
		importSuccess(refresh){
			this.httpDisabled = false;
			if(!refresh){
				this.$refs.table.refresh()
			}
		},

		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row);
			})
		},
		//删除
		async all_del(){
			this.$confirm(`确定删除所选项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: this.selection.map(em=>em.id)};
				const res = await this.$API.finance.cost.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}
			}).catch(()=>{})
		},
		async table_del(row){
			this.$confirm(`确定删除 ${row.identifier} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: [row.id]};
				const res = await this.$API.finance.cost.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}
			}).catch(()=>{})
		},

		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},

		// 下载导出
		exportChangeShow(params){
			if(params.type == 19){
				this.exportShow = params.status==0?true:false
			}
		},
		async exportData() {
			if(this.exportShow) return
			const res = await this.$API.finance.cost.export.post(this.params);
			if(res.code == 200){
				this.$message.success('开始导出');
			}
		},

		//本地更新数据
		handleSuccess(){
			this.$refs.table.refresh();
		},
		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>
.logoCell{
	display: flex;
	align-items: center;
	height: 20px;
	margin: 0 auto;
}
</style>
