<script lang="ts" setup>
import type { VbenFormProps } from '@vben/common-ui';

import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { ref } from 'vue';

import { Page } from '@vben/common-ui';

import { Button, Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { employeeDeptTreeSelect } from '#/api/hr/employeeInfo';
import { apiExport, apiPage } from '#/api/hr/phoneBook';
import { commonDownloadExcel } from '#/utils/file/download';
import DeptTree from '#/views/auth/user/deptTree.vue';

import { querySchema, useColumns } from './data';
// import Form from './form.vue';

// 左边部门用
const selectDeptId = ref<number[]>([]);
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      allowClear: true,
    },
  },
  schema: querySchema,
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
  handleReset: async () => {
    selectDeptId.value = [];

    const { formApi, reload } = gridApi;
    await formApi.resetForm();
    const formValues = formApi.form.values;
    formApi.setLatestSubmissionValues(formValues);
    await reload(formValues);
  },
};

// const [FormDrawer, formDrawerApi] = useVbenDrawer({
//   connectedComponent: Form,
// });

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions: {
    columns: useColumns(),
    height: 'auto',
    keepSource: true,
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues = {}) => {
          // 部门树选择处理
          if (selectDeptId.value.length === 1) {
            formValues.belongDeptId = selectDeptId.value[0];
          } else {
            Reflect.deleteProperty(formValues, 'belongDeptId');
          }
          return await apiPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      // 高亮当前行
      isCurrent: true,
    },
  } as VxeTableGridOptions,
});

// function onActionClick({ code, row }: OnActionClickParams<PhoneBookApi.PhoneBook>) {
//   switch (code) {
//     case 'delete': {
//       onDelete(row);
//       break;
//     }
//     case 'edit': {
//       onEdit(row);
//       break;
//     }
//     default: {
//       break;
//     }
//   }
// }

// function onRefresh() {
//   gridApi.query();
// }
// function onEdit(row: PhoneBookApi.PhoneBook) {
//   formDrawerApi.setData(row).open();
// }
// function onCreate() {
//   formDrawerApi.setData({}).open();
// }
// function onDelete(row: PhoneBookApi.PhoneBook) {
//   const hideLoading = message.loading({
//     content: `正在删除${row.name}...`,
//     duration: 0,
//     key: 'action_process_msg',
//   });
//   apiDelete(row.id || 0)
//     .then(() => {
//       message.success({
//         content: `${row.name}删除成功`,
//         key: 'action_process_msg',
//       });
//       onRefresh();
//     })
//     .catch(() => {
//       hideLoading();
//     });
// }
function handleDownloadExcel() {
  commonDownloadExcel(apiExport, '电话本', gridApi.formApi.form.values);
}
</script>
<template>
  <Page auto-content-height content-class="flex gap-[8px] w-full">
    <!-- <FormDrawer @success="onRefresh" /> -->
    <DeptTree
      v-model:select-dept-id="selectDeptId"
      :api="employeeDeptTreeSelect"
      class="w-[260px]"
      @reload="() => gridApi.reload()"
      @select="() => gridApi.reload()"
    />
    <Grid class="flex-1 overflow-hidden" table-title="电话本">
      <template #toolbar-tools>
        <Space>
          <Button
            v-access:code="['employee:phoneBook:export']"
            @click="handleDownloadExcel"
          >
            导出
          </Button>
          <!-- <Button
            v-access:code="['hr:phoneBook:add']"
            type="primary"
            @click="onCreate"
          >
            <Plus class="size-5" />
            新增
          </Button> -->
        </Space>
      </template>
    </Grid>
  </Page>
</template>
