<template>
	<pi-table ref="tableRef" :api="api.system.dept.list" row-key="dept_id" primary-key="parent_id"
			  @selection-change="selectionChange" hidePagination>
		<template #do>
			<el-button v-auth="'dept:add'" type="primary" icon="el-icon-plus" @click="add"></el-button>
			<el-button v-auth="'dept:del'" type="danger" plain icon="el-icon-delete" :disabled="selection.length===0"
					   @click="batch_del"></el-button>
		</template>
		<template #search>
			<el-input v-model="search.dept_name" placeholder="部门名称" clearable style="width: 200px;"></el-input>
			<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
		</template>
		<el-table-column type="selection" width="50"></el-table-column>
		<el-table-column label="部门名称" prop="dept_name"></el-table-column>
		<el-table-column label="排序" prop="rank"></el-table-column>
		<el-table-column label="状态" prop="status">
			<template #default="scope">
				<el-tag v-if="scope.row.status===1" type="success">启用</el-tag>
				<el-tag v-if="scope.row.status===0" type="danger">停用</el-tag>
			</template>
		</el-table-column>
		<el-table-column label="创建时间" prop="create_time"></el-table-column>
		<el-table-column label="备注" prop="remark"></el-table-column>
		<el-table-column label="操作" fixed="right" align="right" width="120">
			<template #default="scope">
				<el-button-group>
					<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看
					</el-button>
					<el-button text type="primary" size="small" @click="add(scope.row, scope.$index)">支部
					</el-button>
					<el-button v-auth="'dept:edit'" text type="success" size="small"
							   @click="table_edit(scope.row, scope.$index)">编辑
					</el-button>
					<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
						<template #reference>
							<el-button v-auth="'dept:del'" text type="danger" size="small">删除</el-button>
						</template>
					</el-popconfirm>
				</el-button-group>
			</template>
		</el-table-column>
	</pi-table>
	<save-dialog v-if="dialogShow" ref="saveRef" @success="tableRef.refresh()"
				 @closed="dialogShow=false"></save-dialog>
</template>

<script setup>
import saveDialog from './save'
import {getCurrentInstance, nextTick, ref} from "vue";
import api from "@/api/index";

defineOptions({
	name: "systemDept"
})

const {proxy} = getCurrentInstance()
const saveRef = ref(null)
const tableRef = ref(null)

let dialogShow = ref(false)
let selection = ref([])
let search = ref({
	dept_name: null
})


//添加
function add(row) {
	dialogShow.value = true
	nextTick(() => {
		saveRef.value.open().setParent(row.dept_id ?? 0)
	})
}

//编辑
function table_edit(row) {
	dialogShow.value = true
	nextTick(() => {
		saveRef.value.open('edit', row)
	})
}

//查看
function table_show(row) {
	dialogShow.value = true
	nextTick(() => {
		saveRef.value.open('show', row)
	})
}

//删除
async function table_del(row) {
	const res = await api.system.dept.del({ids: [row.dept_id]});
	tableRef.value.refresh()
	proxy.$message.success(res.msg)
}

//批量删除
async function batch_del() {
	proxy.$confirm(`确定删除选中的 ${selection.value.length} 项吗？如果删除项中含有子集将会被一并删除`, '提示', {
		type: 'warning'
	}).then(async () => {
		const loading = proxy.$loading();
		const res = await api.system.dept.del({ids: selection.value.map(item => item.dept_id)});
		tableRef.value.refresh()
		loading.close();
		proxy.$message.success(res.msg)
	})
}

//表格选择后回调事件
function selectionChange(e) {
	selection.value = e;
}

//搜索
function upsearch() {
	tableRef.value.upData(search)
}

</script>

<script>

export default {
	methods: {}
}
</script>

<style>
</style>
