<template>
	<el-container class="mainBox mainBoxHeaderNoBorder">
		<el-main class="nopadding">
			<el-header>
				<div class="left-panel">
					<el-button type="primary" :size="size" icon="el-icon-plus" @click="add">新增</el-button>
					<el-button plain type="danger" :disabled="selection.length>0?false:true" :size="size" icon="el-icon-Delete" @click="all_delete"></el-button>
					<scImport ref="scImport" :size="size" :httpDisabled="httpDisabled" type="17" title="批量导入物料成本" @parentParams="importUpload" @importSuccess="importSuccess">
						<template #header>
							<el-button type="primary" :size="size" plain @click="importFile">批量导入</el-button>
						</template>
						<template #download>
							<div @click="importTemplate">下载导入模版</div>
						</template>
					</scImport>
				</div>
				<div class="right-panel">

				</div>
			</el-header>
			<div class="searchMain">
				<scSearch ref="scSearch" :searchList="searchList" @fetchSelectData="getSelectData"></scSearch>

				<div class="searchItem searchBtn">
					<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" stripe :size="size" border highlightCurrentRow @selection-change="selectionChange">
				<sc-table-column type="selection" align="center" width="40"></sc-table-column>
				<el-table-column label="操作" fixed="right" align="center" width="160">
					<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_del(scope.row, 'delete')" icon="sc-icon-Delete">删除配置</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

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

<script>
import addMaterial from "../components/addMaterial";
export default {
	name: "material",
	components:{
		addMaterial
	},
	data(){
		return{
			size:'small',
			httpDisabled:false,
			dialog:{
				show:false,
			},
			selection:[],
			list:{
				apiObj: this.$API.setup.material.list,
				column:[]
			},
			params: {},
			searchList:[
				{name:'操作时间',type:'date',code:'created_at',show:true},
				{name:'物料MPN',type:'text',code:['m_mpn'], placeholder:"请输入物料MPN",show:true},
				{name:'物料名称',type:'text',code:['m_name'], placeholder:"请输入物料名称",show:true},
			],
		}
	},
	provide(){
		return{
			columnHeaderParams:false,
		}
	},
	mounted() {

	},
	methods:{
		getSelectData(item){
			let {params} = item;
			this.params = params;
		},

		// 批量导入
		importFile(){
			this.$nextTick(()=>{
				this.$refs.scImport.importFile();
			})
		},
		async importTemplate() {
			const res = await this.$API.setup.material.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.setup.material.import.post(params);
			if(res.code == 200){
				this.$refs.scImport.setAnalysisShow(true);
				this.$message.success('上传成功，开始导入数据');
			}
		},
		importSuccess(refresh){
			this.httpDisabled = false;
			if(!refresh){
				this.$refs.table.refresh()
			}
		},

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

		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		// 新增成功
		handleSuccess(){
			this.$refs.table.refresh();
		},

		all_delete(){
			this.$confirm(`确定删除所选配置吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: this.selection.map(em=> em.id)};
				const res = await this.$API.setup.material.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}
			}).catch(()=>{})
		},
		table_del(row){
			this.$confirm(`确定删除 ${row.m_mpn} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: [row.id]};
				const res = await this.$API.setup.material.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}
			}).catch(()=>{})
		},

		upSearch(){
			this.$refs.table.upData(this.params);
		},
		reset(){
			this.params = {};
			this.$refs.scSearch.reload();
			this.$refs.table.reload();
		},
	}
}
</script>

<style scoped lang="scss">

</style>
