<template>
	<el-container class="mainBox mainHeaderNoBorderPadding">
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'addBom'" :size="size" icon="el-icon-plus" @click="add">新增BOM</el-button>
				<scImport ref="scImport" :size="size" type="42" title="批量导入BOM" @parentParams="importUpload" @importSuccess="importSuccess">
					<template #header>
						<el-button v-auth="'bomImport'" type="primary" :size="size" plain @click="importFile">批量导入</el-button>
					</template>
					<template #download>
						<div v-auth="'bomListImportTemplate'" @click="importTemplate">下载导入模版</div>
					</template>
				</scImport>
			</div>
			<div class="right-panel">
				<scExport :size="size" @exportData="exportData" @updateShow="exportChangeShow" :show="exportShow" type="33">
					<el-button :size="size" v-auth="'bomListExport'" 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" :column="list.column" :params="params" row-key="id" stripe border :size="size" @selection-change="selectionChange">
				<el-table-column type="selection" align="center" width="40"></el-table-column>
				<sc-table-column label="序号" align="center" type="index"></sc-table-column>
<!--				<template #disable_status="scope">-->
<!--					<el-switch :size="size" v-model="scope.row.disable_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="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_show(scope.row, 'show')" icon="sc-icon-See">查看</el-dropdown-item>
									<div v-auth="'updateMe'">
										<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑</el-dropdown-item>
									</div>
									<div v-auth="'deleteBom'">
										<el-dropdown-item @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete">删除</el-dropdown-item>
									</div>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<show-dialog v-if="dialog.show" ref="showDialog" @success="handleSuccess" @closed="dialog.show=false"></show-dialog>
</template>

<script>
import saveDialog from './save'
import showDialog from './show'
export default {
	name:"bom",
	components: {
		saveDialog,
		showDialog
	},
	data() {
		return {
			size:'small',
			dialog: {
				save: false,
				show: false
			},
			list: {
				apiObj: this.$API.setup.bom.list,
				column: [],
			},
			selection: [],
			exportShow:false,
			searchShow:false,
			searchList:[
				{name:'创建时间',type:'date',code:'created_at',show: true},
				{name:'更新时间',type:'date',code:'updated_at',show: true},
				{name:'BOM版本',type:'select',code:['bom_version'],placeholder:"请输入BOM版本",show:true},
				{name:'BOM简称',type:'text',code:'bom_abbreviation',placeholder:"请输入BOM简称",show:false},
				{name:'BOM分类',type:'select',code:['bom_classification'],placeholder:"请输入BOM分类",isOpen:true,show:false},
				{name:'BOM用途',type:'text',code:['bom_purpose'],placeholder:"请输入BOM用途",isOpen:true,show:false},
				{name:'父项物料编码',type:'text',code:['parent_material_code'],placeholder:"请输入父项物料编码",isOpen:true,show:false},
				{name:'父项物料名称',type:'text',code:['parent_material_name'],placeholder:"请输入父项物料名称",isOpen:true,show:false},
				{name:'父项物料单位',type:'text',code:['parent_material_unit'],placeholder:"请输入父项物料单位",isOpen:true,show:false},
				{name:'子项物料编码',type:'text',code:['child_material_code'],placeholder:"请输入子项物料编码",isOpen:true,show:false},
				{name:'子项物料名称',type:'text',code:['child_material_name'],placeholder:"请输入子项物料名称",isOpen:true,show:false},
				{name:'子项物料单位',type:'text',code:['child_material_unit'],placeholder:"请输入子项物料单位",isOpen:true,show:false},
				{name:'次项',type:'text',code:['item_sequence'],placeholder:"请输入次项",isOpen:true,show:false},
				{name:'子项类型',type:'text',code:['child_material_type'],placeholder:"请输入子项类型",isOpen:true,show:false},
				{name:'用量分母',type:'text',code:['usage_numerator'],placeholder:"请输入用量分母",isOpen:true,show:false},
				{name:'用量分子',type:'text',code:['usage_denominator'],placeholder:"请输入用量分子",isOpen:true,show:false},
				{name:'创建组织编码',type:'text',code:['create_org_code'],placeholder:"请输入创建组织编码",isOpen:true,show:false},
				{name:'创建组织名称',type:'text',code:['create_org_name'],placeholder:"请输入创建组织名称",isOpen:true,show:false},
				{name:'使用组织编码',type:'text',code:['use_org_code'],placeholder:"请输入使用组织编码",isOpen:true,show:false},
				{name:'使用组织名称',type:'text',code:['use_org_name'],placeholder:"请输入使用组织名称",isOpen:true,show:false},
				{name:'单据状态',type:'text',code:['document_status'],placeholder:"请输入单据状态",isOpen:true,show:false},
				{name:'禁用状态',type:'text',code:['disable_status'],placeholder:"请输入禁用状态",isOpen:true,show:false},
				{name:'关键字',type:'text',code:['bom_abbreviation'],keyword:true,show:true},
			],
			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' || data.type == 'multiple') {
				const res = await this.$API.setup.bom.field.post(searchParams);
				if (res.code == 200) {
					if (res.data && res.data.length > 0) {
						res.data.forEach(item => {
							item.label = item[data.code];
							item.value = item.id;
							item.id = item[data.code];
						})
					}
					this.searchList.forEach(item => {
						if (item.code == data.code) {
							item.data = res.data;
						}
					})
				}
			}
		},

		// 批量导入
		importFile(){
			this.$nextTick(()=>{
				this.$refs.scImport.importFile();
			})
		},
		async importTemplate() {
			const res = await this.$API.setup.bom.template.post();
			const blob = new Blob([res]);
			const text = new Date().getTime();
			const eLink = document.createElement('a');
			eLink.download = "BOM导入模版_"+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.setup.bom.import.post(params);
			if(res.code == 200){
				this.$message.success('上传成功，开始导入数据');
			}
		},
		importSuccess(){
			this.$refs.table.refresh()
		},

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

		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row);
			})
		},
		//查看
		table_show(row){
			this.dialog.show = true
			this.$nextTick(() => {
				this.$refs.showDialog.open('show').setData(row);
			})
		},
		handleSuccess(){
			this.$refs.table.refresh();
		},
		//删除
		async table_del(row){
			this.$confirm(`确定删除 ${row.name} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {id: row.id};
				const res = await this.$API.system.company.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh()
					this.$message.success("删除成功")
				}else{
					await this.$alert(res.message, "提示", {type: 'error'})
				}
			}).catch(()=>{})
		},
		changeSwitch(){

		},

		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		// 下载导出
		exportChangeShow(params){
			if(params.type == 33){
				this.exportShow = params.status==0?true:false
			}
		},
		async exportData() {
			if(this.exportShow) return
			const res = await this.$API.setup.bom.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(){
			this.$refs.table.refresh();
		},
	}
}
</script>

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