<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:selectedKeys="selectedKeys"
					:checkStrictly="true"
					: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">
				<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)">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete v-if="hasPerm('bizBatchHouseDelete')" :selectedRowKeys="selectedRowKeys" @batchDelete="deleteBatchConfig" />
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
						<template v-if="column.dataIndex === 'status'">
							<a-tag v-if="record.status === 0" color="#f50">未启用</a-tag>
							<a-tag v-else-if="record.status === 1" color="#87d068">已启用</a-tag>
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a v-if="hasPerm('bizHouseEdit')" @click="formRef.onOpen(record, searchFormState)">编辑</a>
							<a-divider type="vertical" v-if="hasPerm(['bizHouseEdit', 'bizHouseDelete'], 'and')" />
							<a-popconfirm title="确定删除吗？" @confirm="deleteConfig(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="config">
	import bizConfigApi from "@/api/biz/bizConfigApi"
	import { Empty } from 'ant-design-vue'
	import { isEmpty, cloneDeep } from "lodash-es"
	import Form from './form.vue'

	const table = ref()
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({
		name: '',
		parentId: ''
	})
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const show = ref(true)
	const columns = [
		{
			title: '名称',
			dataIndex: 'configName'
		},
		{
			title: '备注',
			dataIndex: 'remarks'
		},
		{
			title: '是否启用',
			dataIndex: 'status'
		},
		{
			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 loadData = async (parameter) => {
		await loadTreeData()
		return bizConfigApi.baseConfigDetail(Object.assign(parameter, searchFormState.value)).then((data) => {
			return data
		})
	}

	const deleteConfig = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizConfigApi.delBaseConfig(params).then(() => {
			table.value.refresh(true)
		})
	}

	const deleteBatchConfig = (params) => {
		bizConfigApi.delBaseConfig(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}

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

	// 加载左侧的树
	const loadTreeData = () => {
		bizConfigApi.baseConfigTree().then((res) => {
			cardLoading.value = false
			if (res !== null) {
				res.forEach(items => {
					if (items.children) {
						items.children.forEach(item => {
							item.disabled = true
						})
					}
				})
				treeData.value = res
				if (show.value) {
					selectedKeys.value = [res[0].id]
					searchFormState.value.parentId = res[0].id
					searchFormState.value.name = res[0].name
				}
				show.value = false
				// 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, e) => {
		if (selectedKeys.length > 0) {
			defaultedKeys = cloneDeep([selectedKeys.toString()])
			searchFormState.value.parentId = selectedKeys.toString()
			searchFormState.value.name = e.node.name
			table.value.refresh(true)
		} else {
			selectedKeys.value = defaultedKeys.length > 0 ? defaultedKeys : [treeData.value[0].id]
			return
		}
	}
</script>

<style scoped>

</style>
