<template>
	<div class="system-organize-container layout-padding">
		<div class="layout-padding-auto layout-padding-view pd20">
			<div class="flex-center-between mb20">
				<p class="table-title">信息列表</p>
				<div>
					<el-button type="primary" @click="expand"
						><SvgIcon
							name="ele-ArrowDownBold"
							style="transition: transform var(--el-transition-duration)"
							:style="state.isExpand ? 'transform: none' : 'transform: rotateZ(180deg)'"
							class="mr5"
						/>
						{{ state.isExpand ? '收起' : '展开' }}</el-button
					>
					<el-button type="primary" @click="onOpenAddOrg" v-auth="'100501'"> <SvgIcon name="ele-Plus" class="mr5" />新增 </el-button>
				</div>
			</div>
			<!-- 表格 -->
			<el-auto-resizer class="table" v-loading="state.loading">
				<template #default="{ height, width }">
					<el-table-v2
						v-model:expanded-row-keys="expandedRowKeys"
						:columns="state.columns"
						:data="state.orgTableData"
						expand-column-key="orgName"
						fixed
						:width="width"
						:height="height"
					>
					</el-table-v2>
				</template>
			</el-auto-resizer>
		</div>
		<AddOrg ref="addOrgRef" @updateList="getOrgListApi" />
		<EditOrg ref="editOrgRef" @updateList="getOrgListApi" />
	</div>
</template>
<script lang="ts" setup name="organize">
import { defineAsyncComponent, ref, h, reactive, onMounted, watch } from 'vue';
import { ElButton,ElTag } from 'element-plus';
import { getOrgList } from '/@/api/system/organize';
import { formatDate } from '/@/utils/formatTime';
import SvgIcon from '/@/views/system/menu/component/SvgIcon.vue';
// 引入组件
const AddOrg = defineAsyncComponent(() => import('/@/views/system/organize/component/addOrg.vue'));
const EditOrg = defineAsyncComponent(() => import('/@/views/system/organize/component/editOrg.vue'));

// 定义变量内容
const addOrgRef = ref();
const editOrgRef = ref();
const state = reactive({
	orgTableData: <any>[], // 获取所有菜单
	loading: false,
	columns: [
		{
			key: 'orgName',
			dataKey: 'orgName', //需要渲染当前列的数据字段，如{id:9527,name:'Mike'}，则填name
			title: '部门名称',
			width: 400,
		},
		{
			key: 'parentName',
			dataKey: 'parentName', //需要渲染当前列的数据字段，如{id:9527,name:'Mike'}，则填name
			title: '上级部门',
			width: 400,
		},
		{
			key: 'orgCode',
			dataKey: 'orgCode', //需要渲染当前列的数据字段，如{id:9527,name:'Mike'}，则填name
			title: '部门编码',
			width: 200,
		},
		{
			key: 'isEnable',
			dataKey: 'isEnable',
			title: '状态',
			width: 100,
			cellRenderer: (data: any) => {
				return h(ElTag, { type: data.rowData.isEnable ? 'success' : 'info' }, { default: () => (data.rowData.isEnable ? '启用' : '禁用') });
			},
		},
		{
			key: 'creationTime',
			dataKey: 'creationTime',
			title: '更新时间',
			width: 200,
			cellRenderer: (data: any) => h('span', {}, { default: () => formatDate(new Date(data.rowData.creationTime), 'YYYY-mm-dd HH:MM:SS') }),
		},
		{
			key: 'handle',
			title: '操作',
			width: 80,
			fixed: 'right',
			align: 'center',
			cellRenderer: (data: any) =>
				h(
					ElButton,
					{
						onClick: () => onOpenEditOrg(data),
						type: 'primary',
						title: '修改',
						icon: h(SvgIcon, { name: 'ele-Edit', size: '16px' }, {}),
						link: true,
					},
					{ default: () => '修改' }
				),
		},
	],
	isExpand: false,
});
const expandedRowKeys = ref<string[]>([]);
// 打开新增菜单弹窗
const onOpenAddOrg = () => {
	addOrgRef.value.openDialog();
};
// 打开编辑菜单弹窗
const onOpenEditOrg = (row: any) => {
	editOrgRef.value.openDialog(row.rowData);
};
const expand = () => {
	state.isExpand = !state.isExpand;
};
watch(
	() => state.isExpand,
	(old: Boolean) => {
		if (old) getAllIds(state.orgTableData);
		else expandedRowKeys.value = [];
	}
);
const getAllIds = (arr: any) => {
	if (!arr) return [];
	arr.forEach((v: any) => {
		if (v.children?.length) {
			getAllIds(v.children);
			expandedRowKeys.value.push(v.id);
		}
	});
};
// 删除当前行
// const onTabelRowDel = (row: any) => {
// 	ElMessageBox.confirm(`此操作将永久删除部门结构：${row.orgName}, 是否继续?`, '提示', {
// 		confirmButtonText: '确定',
// 		cancelButtonText: '取消',
// 		type: 'warning',
// 	}).then(() => {
// 		removeOrg(row.id).then(() => {
// 			ElMessage.success('删除成功');
// 			getOrgListApi();
// 		})
// 	}).catch(() => { });
// };
// 获取所有部门结构
const getOrgListApi = () => {
	state.loading = true;
	getOrgList()
		.then((res: any) => {
			state.orgTableData = res?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 页面加载时
onMounted(() => {
	getOrgListApi();
});
</script>
<style lang="scss" scoped>
.system-organize-container {
	.table {
		flex: 1;
	}
}
</style>
