import type {
  OnActionClickFn,
  VbenFormSchema,
  VxeTableGridOptions,
} from '#/adapter';

import { EnumApi, TenantApi } from '#/api';
import { dbTypeOptions } from '#/api/model';

export const useSearchSchema = (): VbenFormSchema[] => {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: '租户名称',
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: '联系人',
    },
  ];
};

export function useColumns(
  onActionClick?: OnActionClickFn<TenantApi.RecordItem>,
): VxeTableGridOptions<TenantApi.RecordItem>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      align: 'left',
      field: 'name',
      title: '租户名称',
      // type: 'checkbox',
      width: 200,
    },
    { align: 'left', field: 'adminAccount', title: '联系人', width: 120 },
    { align: 'left', field: 'phone', title: '联系电话', width: 120 },
    { field: 'tenantTypeName', title: '租户类型', width: 80 },
    {
      field: 'status',
      title: '状态',
      width: 82,
      cellRender: { name: 'CellTag' },
    },
    { field: 'sort', title: '排序', width: 50 },
    { align: 'left', field: 'remark', title: '备注' },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '租户名称',
          onClick: onActionClick,
        },
        name: 'CellAction',
        options: [
          {
            code: 'createDb',
            label: '创建库',
            auth: ['tenant:createDb'],
            confirm: {
              title: '提示',
              content: `确定创建/更新租户数据库?`,
            },
          },
          {
            code: 'edit',
            auth: ['role:edit'],
          },
          {
            code: 'resetPwd',
            label: '重置密码',
            auth: ['tenant:resetPwd'],
          },
          {
            code: 'delete',
            auth: ['tenant:delete'],
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: '操作',
      width: 100,
    },
  ];
}

export const useSchema = (): VbenFormSchema[] => {
  return [
    {
      component: 'ApiRadio',
      componentProps: {
        api: {
          type: 'enum',
          params: EnumApi.EnumType.TenantType,
        },
      },
      fieldName: 'tenantType',
      label: '租户类型',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'name',
      label: '租户名称',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'adminAccount',
      label: '管理员',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'phone',
      label: '联系电话',
      rules: 'required',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'email',
      label: '电子邮箱',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'sort',
      label: '排序',
      rules: 'required',
    },
    {
      component: 'Select',
      componentProps: {
        placeholder: '请输入',
        options: dbTypeOptions,
      },
      dependencies: {
        show(values) {
          return values.tenantType === 1;
        },
        triggerFields: ['tenantType'],
      },
      fieldName: 'dbType',
      label: '数据库类型',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入',
      },
      dependencies: {
        show(values) {
          return values.tenantType === 1;
        },
        triggerFields: ['tenantType'],
      },
      fieldName: 'connection',
      label: '连接字符串',
      rules: 'required',
      formItemClass: 'col-span-2 items-baseline',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入',
      },
      fieldName: 'remark',
      label: '备注',
      formItemClass: 'col-span-2 items-baseline',
    },
  ];
};
