<template>
	<el-container class="mainBox mainBoxHeaderNoBorder">
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'addDept'" :size="size" icon="el-icon-plus" @click="add">新增部门</el-button>
				<el-button type="danger" v-auth="'delDept'" :size="size" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">

			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" stripe :size="size" :showPagination="false" @selection-change="selectionChange">
				<el-table-column type="selection" width="40"></el-table-column>
				<el-table-column label="状态" fixed="right" align="center" width="80" prop="active_status">
					<template #default="scope">
						<el-switch :size="size" v-auth="'updateDeptStatus'" 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>
				<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="'updateDept'">
										<el-dropdown-item @click="table_edit(scope.row, 'edit')" icon="sc-icon-Edit">修改</el-dropdown-item>
									</div>
									<div v-auth="'delDept'">
										<el-dropdown-item @click="table_del(scope.row, 'delete')" icon="sc-icon-Delete" divided>删除</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="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './save'

	export default {
		name: 'dept',
		components: {
			saveDialog
		},
		data() {
			return {
				size:'small',
				dialog: {
					save: false
				},
				list: {
					apiObj: this.$API.system.dept.list,
					column: [],
				},
				selection: [],
				search: {
					keyword: null
				}
			}
		},
		methods: {
			//添加
			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.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			// 状态
			changeSwitch(val, row) {
				row.$switch_yx = true;
				setTimeout(async () => {
					let params = {
						id: row.id,
						status: row.active_status,
					};
					const res = await this.$API.system.dept.status.post(params);
					if(res.code !=200){
						row.active_status = !row.active_status;
					}
					delete row.$switch_status;
					delete row.$switch_yx;
				}, 500);
			},
			//删除
			async table_del(row){
				this.$confirm(`确定删除 ${row.name} 吗？`, '提示', {
					type: 'warning'
				}).then(async () => {
					const reqData = {id: row.id};
					const res = await this.$API.system.dept.delete.post(reqData);
					if(res.code == 200){
						this.$refs.table.refresh()
						this.$message.success("删除成功")
					}else{
						await this.$alert(res.message, "提示", {type: 'error'})
					}
				}).catch(()=>{})

			},
			//批量删除
			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.dept.delete.post(params);
					if (res.code == 200) {
						this.$refs.table.refresh()
						this.$message.success("删除成功")
					}
					loading.close();
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//根据ID获取树结构
			filterTree(id){
				let target = null;
				function filter(tree){
					tree.forEach(item => {
						if(item.id == id){
							target = item
						}
						if(item.children){
							filter(item.children)
						}
					})
				}
				filter(this.$refs.table.tableData)
				return target
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
