<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'sparePartsInfoAdd'" :size="size" icon="el-icon-plus" @click="add">新增备件信息</el-button>
				<el-button type="danger" v-auth="'sparePartsInfoDelete'" :size="size" plain @click="batch_del" :disabled="selection.length==0?true:false">删除</el-button>
				<scImport ref="scImport" :size="size" title="批量导入" @parentParams="importUpload" @importSuccess="importSuccess">
					<template #header>
						<el-button v-auth="'sparePartsInfoImport'" type="primary" :size="size" plain @click="importFile">批量导入</el-button>
					</template>
					<template #download>
						<div v-auth="'sparePartsInfoImportTemplate'" @click="importTemplate">下载导入模版</div>
					</template>
				</scImport>
			</div>
			<div class="right-panel">
				<scExport :size="size" @exportData="exportData" @updateShow="exportChangeShow" :show="exportShow" type="30">
					<el-button :size="size" v-auth="'sparePartsInfoExport'" icon="sc-icon-Download" :disabled="exportShow" @click="exportData">下载</el-button>
				</scExport>
			</div>
		</el-header>
		<el-main class="nopadding">
			<div class="searchMain searchMainNoTop">
				<scSearch ref="scSearch" :searchList="searchList" @fetchSelectData="getSelectData"></scSearch>

				<div class="searchItem searchBtn">
					<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" row-key="id" @selection-change="selectionChange" stripe border :size="size">
				<el-table-column type="selection" align="center" width="40"></el-table-column>
				<sc-table-column label="序号" align="center" type="index"></sc-table-column>
				<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 @click="table_show(scope.row, 'see')" icon="sc-icon-See">查看详情</el-dropdown-item>
									<span v-auth="'sparePartsInfoAdd'">
										<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑</el-dropdown-item>
									</span>
									<span v-auth="'sparePartsInfoDelete'">
										<el-dropdown-item divided @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete">删除</el-dropdown-item>
									</span>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

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

<script>
import infoSave from "../infoSave"
export default {
	name: "info",
	components:{
		infoSave
	},
	data(){
		return{
			size:'small',
			setMap:{
				status:[]
			},
			dialog: {
				save: false,
			},
			list:{
				apiObj: this.$API.system.spareInfo.list,
				column:[]
			},
			selection: [],
			searchList:[
				{name:'创建日期',type:'date',code:'created_at',show:true},
				{name:'ERP编号',type:'select',code:['erp_pn'],data:[],placeholder:"请输入ERP编号",show:true},
				{name:'供应商',type:'select',code:['vendor'],data:[],placeholder:"请输入供应商",isOpen:true,show:false},
				{name:'名称',type:'text',code:['name'],data:[],placeholder:"请输入名称",show:true},
				{name:'制造商型号',type:'select',code:['mpn'],data:[],placeholder:"请输入制造商型号",isOpen:true,show:false},
				{name:'规格',type:'text',code:['specification'],placeholder:"请输入规格",isOpen:true,show:false},
				{name:'创建人',type:'select',code:['creator_name'],data:[], placeholder:"请输入创建人",isOpen:true,show:false},
				{name:'关键字',type:'text',code:['remark','name'],keyword:true,show:true},
			],
			searchShow:false,
			exportShow:false,
			params: {}
		}
	},
	mounted() {

	},
	methods:{
		searchShowClick(){
			this.searchShow = !this.searchShow;
			this.searchList.forEach(item=>{
				if(item.isOpen){
					item.show = this.searchShow
				}
			})
		},
		async getSelectData(item){
			let {data, params} = item;
			this.params = params;
			let searchParams = this.$TOOL.objCopy(params);
			searchParams.field = ""
			if (typeof data.code === 'string') {
				searchParams.field = data.code;
			} else {
				searchParams.field = data.code[0];
			}
			if (data.type == 'select') {
				const res = await this.$API.system.spareInfo.field.post(searchParams);
				if (res.code == 200) {
					if (res.data && res.data.length > 0) {
						res.data.forEach(item => {
							item.label = item[data.code];
						})
					}
					this.searchList.forEach(item => {
						if (item.code == data.code) {
							item.data = res.data;
						}
					})
				}
			}
		},

		//添加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add');
			})
		},
		// 批量导入
		importFile(){
			this.$nextTick(()=>{
				this.$refs.scImport.importFile();
			})
		},
		async importTemplate() {
			const res = await this.$API.system.spareInfo.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) {
			const res = await this.$API.system.spareInfo.import.post(params);
			if(res.code == 200){
				this.$message.success('上传成功，开始导入数据');
			}
		},
		importSuccess(){
			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);
			})
		},
		//权限设置
		permission(){
			this.dialog.permission = true
			this.$nextTick(() => {
				this.$refs.permissionDialog.open();
			})
		},
		//删除
		async table_del(row){
			const reqData = {ids: [row.id]};
			const res = await this.$API.system.spareInfo.delete.post(reqData);
			if(res.code == 200){
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			}
		},
		//批量删除
		async batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				const params = {
					ids:this.selection.map(em=>em.id)
				}
				const res = await this.$API.system.spareInfo.delete.post(params);
				loading.close();
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success("操作成功")
				}
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		// 下载导出
		exportChangeShow(params){
			if(params.type == 30){
				this.exportShow = params.status==0?true:false
			}
		},
		async exportData() {
			if(this.exportShow) return
			const res = await this.$API.system.spareInfo.export.post(this.params);
			if(res.code == 200){
				this.$message.success('开始导出');
			}
		},
		//搜索
		upSearch(){
			this.$refs.table.upData(this.params);
		},
		reset(){
			this.params = {};
			this.$refs.scSearch.reload();
			this.$refs.table.reload();
		},

		//本地更新数据
		handleSaveSuccess(data, mode){
			if(mode=='add'){
				this.$refs.table.refresh()
			}else if(mode=='edit'){
				this.$refs.table.refresh()
			}
		},
	}
}
</script>

<style scoped lang="scss">
	.mainBox{padding: 0;}
</style>
