<template>
	<a-row :gutter="10">
		<a-col :xs="24" :sm="24" :md="24" :lg="5" :xl="5">
			<a-card :bordered="false" :loading="cardLoading">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				/>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
			</a-card>
		</a-col>
		<a-col :xs="24" :sm="24" :md="24" :lg="19" :xl="19">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item name="searchKey" label="名称关键词">
								<a-input v-model:value="searchFormState.searchKey" placeholder="请输入栋舍名称或关键词" />
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-button type="primary" @click="table.refresh(true)">
								<template #icon><SearchOutlined /></template>
								查询
							</a-button>
							<a-button class="snowy-buttom-left" @click="reset">
								<template #icon><redo-outlined /></template>
								重置
							</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="table"
					:columns="columns"
					:data="loadData"
					:expand-row-by-click="true"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" v-if="hasPerm('bizHouseAdd')"  @click="formRef.onOpen(undefined, searchFormState.parentId)">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete v-if="hasPerm('bizBatchHouseDelete')" :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchDepartment" />
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'departmentImgUrl'">
							<div v-viewer>
								<img v-if="record.departmentImgUrl" class="record-img" :src="record.departmentImgUrl" alt="/">
								<span v-else>暂无图片</span>
							</div>
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a v-if="hasPerm('bizHouseEdit')" @click="formRef.onOpen(record)">编辑</a>
							<a-divider type="vertical" v-if="hasPerm(['bizHouseEdit', 'bizHouseDelete'], 'and')" />
							<a-popconfirm title="删除此部门吗？" @confirm="deleteDepartment(record)">
								<a-button type="link" v-if="hasPerm('bizHouseDelete')" danger size="small">删除</a-button>
							</a-popconfirm>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="department">
	import { Empty } from 'ant-design-vue'
	import { isEmpty } from "lodash-es";
	import bizDepartmentApi from "@/api/biz/bizDepartmentApi";
	import Form from './form.vue'
	const table = ref()
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '部门名称',
			dataIndex: 'departmentName'
		},
		{
			title: '上级部门',
			dataIndex: 'parentName'
		},
		{
			title: '部门电话',
			dataIndex: 'departmentPhone'
		},
		{
			title: '部门图片',
			dataIndex: 'departmentImgUrl'
		},
		{
			title: '部门负责人',
			dataIndex: 'managerName'
		},
		{
			title: '所属区域',
			dataIndex: 'departmentRegion'
		},
		{
			title: '详细地址',
			dataIndex: 'detailLocation'
		},
		{
			title: '操作',
			dataIndex: 'action'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['basePigpenEdit', 'basePigpenDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}

	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}

	// 默认展开的节点
	const defaultExpandedKeys = ref([])
	const treeData = ref([])
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const cardLoading = ref(true)

	const loadData = (parameter) => {
		loadTreeData()
		return bizDepartmentApi.getDepartmentPage(Object.assign(parameter, searchFormState.value)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}

	const deleteDepartment = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizDepartmentApi.delDepartment(params).then(() => {
			table.value.refresh(true)
		})
	}

	const deleteBatchDepartment = (params) => {
		bizDepartmentApi.delDepartment(params).then(() => {
			table.value.refresh(true)
		})
	}

	// 加载左侧的树
	const loadTreeData = () => {
		bizDepartmentApi.getDepartmentTree().then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
				if (isEmpty(defaultExpandedKeys.value)) {
					// 默认展开2级
					treeData.value.forEach((item) => {
						// 因为0的顶级
						if (item.parentId === '0') {
							defaultExpandedKeys.value.push(item.id)
							// 取到下级ID
							if (item.children) {
								item.children.forEach((items) => {
									defaultExpandedKeys.value.push(items.id)
								})
							}
						}
					})
				}
			}
		})
	}

	// 点击树查询
	const treeSelect = (selectedKeys) => {
		console.log(selectedKeys)
		if (selectedKeys.length > 0) {
			searchFormState.value.parentId = selectedKeys.toString()
		} else {
			delete searchFormState.value.parentId
		}
		table.value.refresh(true)
	}
</script>

<style scoped>
.record-img {
	width: 40px;
	height: 40px;
}
</style>
