<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:category: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="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="160" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleAdd(row.id)" v-perm="['sys:category:add']">
              新增
            </el-button>
            <el-button type="primary" link @click="handleEdit(row)" v-perm="['sys:category:update']">
              编辑
            </el-button>
            <el-button  type="danger" link @click="handleDelete(row.id)" v-perm="['sys:category:delete']">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <editDialog
        ref="editRef"
        v-if="editVisible"
        :categoryId="categoryId"
        :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 {getCategoryList,categoryDelete} from "@/api/content/category";
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 categoryId=ref(0);
const pid=ref(0)
const lists = ref([]);

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

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

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

const handleDelete = async (categoryId: number) => {
  await confirm("确定要删除？");
  try {
    loading.value = true;
    await categoryDelete(categoryId);
    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>