<template>
  <div class="menu-index">
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <div>
        <el-button  type="primary" @click="handleAdd()" v-perm="['sys:dept:add']">
          <template #icon>
            <el-icon>
              <plus />
            </el-icon>
          </template>
          新增
        </el-button>
        <el-button @click="handleExpand"> 展开/折叠</el-button>
      </div>
      </el-card>
      <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <el-table border v-loading="loading" ref="tableRef" :data="lists" row-key="id"
                :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
        <el-table-column label="部门名称" prop="name" min-width="150" show-overflow-tooltip/>
        <el-table-column align="center" label="部门类型" prop="type" min-width="80">
          <!-- <template #default="{ row }">
            <div v-if="row.type ==1">公司</div>
            <div v-else-if="row.type == 2">子公司</div>
            <div v-else-if="row.type == 3">部门</div>
            <div v-else-if="row.type == 4">小组</div>
          </template> -->
          <template #default="{ row }">
            <el-tag v-if="row.type == 1">公司</el-tag>
            <el-tag v-else-if="row.type == 2" type="success">子公司</el-tag>
            <el-tag v-else-if="row.type == 3" type="warning">部门</el-tag>
            <el-tag v-else-if="row.type == 4" type="info">小组</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="部门排序" prop="sort" min-width="100"/>
        <el-table-column align="center" label="部门备注" prop="note" min-width="100"/>
        <el-table-column align="center" label="创建人" prop="createUser" min-width="180"></el-table-column>
        <el-table-column align="center" label="创建时间" prop="createTime" min-width="180"></el-table-column>
        <el-table-column align="center" label="操作" width="300" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" icon="Plus"
                       @click="handleAdd(row.id)"  v-perm="['sys:dept:addz']">
              新增
            </el-button>
            <el-button type="warning" icon="Edit" @click="handleEdit(row)" v-perm="['sys:dept:update']">
              编辑
            </el-button>
            <el-button  type="danger" icon="Delete" @click="handleDelete(row.id)" v-perm="['sys:dept:delete']">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <editDialog
        ref="editRef"
        v-if="editVisible"
        :deptId="deptId"
        :pid="pid"
        v-model:visible="editVisible"
        @success="getLists"
    >
    </editDialog>
  </div>
</template>

<script lang="ts" setup name="menu">
import {defineAsyncComponent, nextTick, onMounted, ref, shallowRef} from "vue";
import {getDeptList,deptDelete} from "@/api/system/dept";
import type {ElTable} from "element-plus";
const tableRef = shallowRef<InstanceType<typeof ElTable>>();
import {confirm, message,buildTree} from "@/utils/auth";

const editDialog = defineAsyncComponent(() =>
    import('./edit.vue')
)

const isExpand = ref(false);
const loading = ref(false);
const editVisible=ref(false);
const deptId=ref(0);
const pid=ref(0)
const lists = ref([]);

const getLists = async () => {
  loading.value = true;
  try {
    const data = await getDeptList();
    lists.value = buildTree(data);
    loading.value = false;
  } catch (error) {
    loading.value = false;
  }
};

const handleAdd = async (parentId:any) => {
  deptId.value=0
  pid.value=parentId?parentId:0
  await nextTick();
  editVisible.value=true
};

const handleEdit = async (data: any) => {
  deptId.value=data.id
  await nextTick();
  editVisible.value=true
};

const handleDelete = async (deptId: number) => {
  await confirm("确定要删除？");
  try {
    loading.value = true;
    await deptDelete(deptId);
    message("删除成功");
    getLists();
  } catch (e) {
    loading.value = false;
  }
};

const handleExpand = () => {
  isExpand.value = !isExpand.value;
  toggleExpand(lists.value, isExpand.value);
};

const toggleExpand = (children: any[], unfold = true) => {
  for (const key in children) {
    tableRef.value?.toggleRowExpansion(children[key], unfold);
    if (children[key].children) {
      toggleExpand(children[key].children!, unfold);
    }
  }
};

const getAll=()=>{
  getLists();
}


onMounted(() => {
  getAll()
});

// onActivated(() => {
//   getAll()
// });

</script>

<style scoped>

</style>