<template>
  <div>
    <Search :search="search" :reset="reset">
      <template v-slot:body>
        <el-form-item label="菜单名称" prop="permission_name">
          <el-input v-model="query.permission_name" name="permission_name" clearable />
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <Operate :show="open" />
      <el-table :data="tableData" class="mt-3" v-loading="loading" row-key="id" default-expand-all
        :tree-props="{ children: 'children' }">
        <el-table-column prop="permission_name" label="菜单名称" />
        <el-table-column prop="route" label="菜单路由" />
        <el-table-column prop="permission_mark" label="权限标识" width="360">
          <template #default="scope">
            <div v-if="scope.row.actions.length">
              <el-tag v-for="action in scope.row.actions" class="mb-3 mr-3 cursor-pointer w-fit min-w-fit"
                @click="open(action.id)" closable @close="destroy(api, action.id)">{{ action.permission_name }}</el-tag>
            </div>
            <div v-else>
              <el-popconfirm confirm-button-text="确认" title="添加基础actions" @confirm="actionGenerate(scope.row.id)"
                placement="top">
                <template #reference>
                  <el-tag class="w-8 cursor-pointer" v-if="scope.row.type === MenuType.PAGE_TYPE">
                    <Icon name="cog-6-tooth" class="w-5 h-5 animate-spin" v-if="actionLoading" />
                    <Icon name="plus" className="w-4 h-4" v-else />
                  </el-tag>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="hidden" label="状态" width="100">
          <template #default="scope">
            <Status v-model="scope.row.hidden" :id="scope.row.id" :api="api" @refresh="search" />
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <Update @click="open(scope.row.id)" />
            <Destroy @click="destroy(api, scope.row.id)" />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue';
import Create from './form/create.vue';
import { useGetList } from '@/hook/curd/useGetList';
import { useDestroy } from '@/hook/curd/useDestroy';
import { useOpen } from '@/hook/curd/useOpen';
import { MenuType } from '@/enum/app';
import http from '@/support/http';

const api = 'permissions/permissions';

const { data, query, search, reset, loading } = useGetList(api, false);
const { destroy, deleted } = useDestroy();
const { open, close, title, visible, id } = useOpen();

const tableData = computed(() => data.value?.data);

onMounted(() => {
  search();
  deleted(reset);
});

const actionLoading = ref<boolean>(false);
const actionGenerate = async (id: number) => {
  actionLoading.value = true;
  http.post(api, { parent_id: id, actions: true }).then(r => {
    search();
    actionLoading.value = false;
  });
};
</script>
