import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { SystemUserApi } from '#/api/system/user';

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

import { z } from '#/adapter/form';
import { getRoleList } from '#/api';
import { $t } from '#/locales';

const userStore = useUserStore();

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export const useColumns = (
  onActionClick: OnActionClickFn<SystemUserApi.SystemUser>,
): VxeTableGridOptions<SystemUserApi.SystemUser>['columns'] => {
  return [
    {
      field: 'name',
      title: $t('system.user.userName'),
    },
    {
      field: 'nick_name',
      title: $t('system.user.nickName'),
      width: 150,
    },
    {
      field: 'email',
      title: $t('system.user.email'),
      width: 150,
    },
    {
      field: 'enabled',
      title: $t('system.user.enabled'),
      // component: {
      //   name: 'Switch',
      //   props: {
      //     value: 'true',
      //   },
      // },
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('system.dept.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          // {
          //   code: 'append',
          //   text: '新增下级',
          // },
          {
            code: 'edit',
            show: () => {
              return userStore.buttonPermissions.includes('editUser');
            },
          }, // 默认的编辑按钮
          {
            code: 'delete', // 默认的删除按钮
            disabled: (_row: SystemUserApi.SystemUser) => {
              return false;
              // return !!(row.children && row.children.length > 0);
            },
            show: () => {
              return userStore.buttonPermissions.includes('deleteUser');
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.dept.operation'),
      width: 200,
    },
  ];
};

export function useEditSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'nick_name',
      label: $t('system.user.nickName'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.nickName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.nickName'), 20]),
        ),
    },
    // {
    //   component: 'Input',
    //   fieldName: 'Password',
    //   label: $t('system.user.password'),
    //   rules: z
    //     .string()
    //     .min(6, $t('ui.formRules.minLength', [$t('system.user.password'), 6]))
    //     .max(
    //       20,
    //       $t('ui.formRules.maxLength', [$t('system.user.password'), 20]),
    //     ),
    // },
    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
    },
    {
      label: $t('system.user.role'),
      fieldName: 'role_ids',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const { list } = await getRoleList(userStore?.userInfo?.org_id || '');
          return list;
        },
        multiple: true,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
      },
    },
  ];
}

export function useCreateSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('system.user.userName'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.password'), 6]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.password'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'nick_name',
      label: $t('system.user.nickName'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('system.user.nickName'), 2]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.nickName'), 20]),
        ),
    },
    {
      component: 'Input',
      fieldName: 'password',
      label: $t('system.user.password'),
      rules: z
        .string()
        .min(6, $t('ui.formRules.minLength', [$t('system.user.password'), 6]))
        .max(
          20,
          $t('ui.formRules.maxLength', [$t('system.user.password'), 20]),
        ),
    },

    {
      component: 'Input',
      fieldName: 'email',
      label: $t('system.user.email'),
    },
    {
      label: $t('system.user.role'),
      fieldName: 'role_ids',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const { list } = await getRoleList(userStore?.userInfo?.org_id || '');
          return list;
        },
        multiple: true,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
      },
    },
  ];
}
