import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SystemDeptApi } from '#/api/system/dept';

import { useUserStore } from '@vben/stores';

import { z } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
import { $t } from '#/locales';

const userStore = useUserStore();
/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function useSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('system.dept.deptName'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.dept.deptName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.dept.deptName'), 20]),
        ),
    },
    // {
    //   component: 'ApiTreeSelect',
    //   componentProps: {
    //     allowClear: true,
    //     api: getDeptList,
    //     class: 'w-full',
    //     labelField: 'name',
    //     valueField: 'id',
    //     childrenField: 'children',
    //   },
    //   fieldName: 'pid',
    //   label: $t('system.dept.parentDept'),
    // },
    {
      component: 'Input',
      componentProps: {
        maxLength: 20,
      },
      fieldName: 'phone',
      label: $t('system.dept.phone'),
      rules: z
        .string()
        .max(20, $t('ui.formRules.maxLength', [$t('system.dept.phone'), 20]))
        .optional(),
    },
    {
      component: 'Input',
      componentProps: {
        maxLength: 50,
      },
      fieldName: 'email',
      label: $t('system.dept.email'),
      rules: z
        .string()
        .max(50, $t('ui.formRules.maxLength', [$t('system.dept.email'), 50]))
        .optional(),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.dept.status'),
    },
    {
      component: 'ApiTreeSelect',
      fieldName: 'pid',
      label: $t('system.dept.pid'),
      componentProps: {
        api: async () => {
          const { list } = await getDeptList();
          return list;
        },
        class: 'w-full',
        labelField: 'name',
        treeDefaultExpandAll: true,
        valueField: 'id',
        childrenField: 'children',
      },
    },
    // {
    //   component: 'Textarea',
    //   componentProps: {
    //     maxLength: 50,
    //     rows: 3,
    //     showCount: true,
    //   },
    //   fieldName: 'remark',
    //   label: $t('system.dept.remark'),
    //   rules: z
    //     .string()
    //     .max(50, $t('ui.formRules.maxLength', [$t('system.dept.remark'), 50]))
    //     .optional(),
    // },
  ];
}

/**
生成部门表格的列配置。
@param {OnActionClickFn<SystemDeptApi.SystemDept>} [onActionClick] - 操作按钮点击事件回调函数。
@param {(newStatus: any, row: SystemDeptApi.SystemDept) => PromiseLike<boolean | undefined>} [onStatusChange] - 状态切换事件回调函数。
@returns {VxeTableGridOptions<SystemDeptApi.SystemDept>['columns']} 返回表格列的配置数组。
@description
此函数用于生成部门管理表格的列配置，包括部门名称、状态、电话、邮箱、负责人名称和操作列。
支持树形结构展示，并提供状态切换和操作按钮的功能。 */
export function useColumns(
  onActionClick?: OnActionClickFn<SystemDeptApi.SystemDept>,
  onStatusChange?: (
    newStatus: any,
    row: SystemDeptApi.SystemDept,
  ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions<SystemDeptApi.SystemDept>['columns'] {
  return [
    {
      align: 'left',
      field: 'name',
      fixed: 'left',
      title: $t('system.dept.deptName'),
      treeNode: true,
      width: 150,
    },
    {
      field: 'id',
      title: $t('system.dept.id'),
      width: 150,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'enabled',
      title: $t('system.dept.status'),
      width: 100,
    },
    // {
    //   field: 'phone',
    //   title: $t('system.dept.phone'),
    //   width: 150,
    // },
    // {
    //   field: 'email',
    //   title: $t('system.dept.email'),
    //   width: 200,
    // },
    // {
    //   field: 'createTime',
    //   title: $t('system.dept.createTime'),
    //   width: 180,
    // },
    {
      field: 'manager_name',
      title: $t('system.dept.managerName'),
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.dept.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          //   show: () => {
          //     return userStore.buttonPermissions.includes('addDept');
          //   },
          // },
          // { code: 'edit', show: () => {

          // } }, // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
            disabled: (row: SystemDeptApi.SystemDept) => {
              return (
                !!(row.children && row.children.length > 0) &&
                !userStore.buttonPermissions.includes('deleteUser')
              );
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.dept.operation'),
      width: 200,
    },
  ];
}
