<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>
					<scImport ref="scImport" :size="size" type="44" :httpDisabled="httpDisabled" title="批量导区域地址配置" @parentParams="importUpload" @importSuccess="importSuccess">
						<template #header>
							<el-button type="primary" :size="size" plain @click="importFile" style="margin-right: 12px;">批量导入</el-button>
						</template>
						<template #download>
							<div @click="importTemplate">下载导入模版</div>
						</template>
					</scImport>
					<el-button plain type="danger" :disabled="selection.length==0?true:false" :size="size" icon="el-icon-Delete" @click="all_delete"></el-button>
				</div>
				<div class="right-panel">

				</div>
			</el-header>
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" stripe border :size="size" highlightCurrentRow :hideDo="true" @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 #data_source="scope">
					<span v-for="(item,index) in setMap.sourceList" :key="index">
						<span v-if="item.value === scope.row.data_source">{{item.label}}</span>
					</span>
				</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_see(scope.row, 'see')" icon="sc-icon-See">查看详情</el-dropdown-item>
									<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">编辑配置</el-dropdown-item>
									<el-dropdown-item @click="table_del(scope.row, 'delete')" divided icon="sc-icon-Delete">删除配置</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

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

<script>
import addRegion from "../components/addRegion";
export default {
	name: "region",
	components:{
		addRegion
	},
	data(){
		return{
			size:'small',
			setMap:{
				sourceList:[],
				regionList:[],
			},
			httpDisabled:false,
			list:{
				apiObj: this.$API.setup.regional.list,
				column:[]
			},
			selection:[],
			params: {},

			dialog:{
				show:false,
			},
		}
	},
	provide(){
		return{
			columnHeaderParams:false,
		}
	},
	mounted() {
		this.getRegion();
	},
	methods:{
		async getRegion() {
			const res = await this.$API.setup.regional.sourceList.post();
			if(res.code == 200){
				this.setMap.sourceList = res.data;
			}
		},
		async getRoom() {
			const res = await this.$API.setup.regional.regionalList.post();
			if (res.code == 200) {
				this.setMap.regionList = res.data;
			}
		},

		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		handleSuccess(){
			this.$refs.table.refresh()
		},

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

		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		// 查看
		table_see(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		async table_del(row){
			this.$confirm(`确定删除 ${row.room_code} 吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: [row.id]};
				const res = await this.$API.setup.regional.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success("删除成功");
				}
			}).catch(()=>{})
		},
		async all_delete(){
			this.$confirm(`确定删除所选 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const reqData = {ids: this.selection.map(em=>em.id)};
				const res = await this.$API.setup.regional.delete.post(reqData);
				if(res.code == 200){
					this.$refs.table.refresh();
					this.$message.success("删除成功");
				}
			}).catch(()=>{})
		}
	}
}
</script>

<style scoped lang="scss">
.customerView{
	display: flex;
	align-items: center;
	.iconBtn{
		margin-left: 6px;
	}
}
</style>
