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

import { ref } from 'vue';

import { getAllDeptTree, queryRoleList } from '#/api';
import { $t } from '#/locales';

const treeData = ref([]);
const optionsRoleData = ref([]);

queryRoleList('').then((res) => {
  optionsRoleData.value = res.records.map((item) => ({
    label: item.name,
    value: item.id,
  }));
});

function transformToTreeData(data: any) {
  return data.map((item) => ({
    value: item.id,
    label: item.name,
    ...(item.children && { children: transformToTreeData(item.children) }),
  }));
}

// 使用示例
getAllDeptTree(1).then((res) => {
  treeData.value = transformToTreeData(res);
});
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: '用户名称',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'name',
      label: '昵称',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'email',
      label: 'Email',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'phone',
      label: '电话',
      rules: 'required',
    },
    {
      component: 'InputPassword',
      fieldName: 'password',
      label: '密码',
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'address',
      label: '地址',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: true },
          { label: $t('common.disabled'), value: false },
        ],
        optionType: 'button',
      },
      defaultValue: true,
      fieldName: 'enable',
      label: '状态',
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: '备注',
    },
    {
      component: 'Select',
      rules: 'required',
      componentProps: {
        allowClear: true,
        mode: 'multiple',
        // api: queryRoleList(''),
        options: optionsRoleData,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
      },
      fieldName: 'roleId',
      label: '所属角色',
    },
    {
      component: 'TreeSelect',
      componentProps: {
        allowClear: true,
        // api: getAllDeptTree(1),
        treeData,
        class: 'w-full',
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
      },
      fieldName: 'deptId',
      label: '所属部门',
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: '用户名称',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          { label: $t('common.enabled'), value: true },
          { label: $t('common.disabled'), value: false },
        ],
      },
      fieldName: 'enableState',
      label: '状态',
    },
    // {
    //   component: 'Input',
    //   fieldName: 'remark',
    //   label: '备注',
    // },
    // {
    //   component: 'RangePicker',
    //   fieldName: 'createTime',
    //   label: '创建时间',
    // },
  ];
}

export function useColumns<T = UserApi.User>(
  onActionClick: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'username',
      title: '用户名称',
      width: 200,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'enableState',
      title: '用户状态',
      width: 100,
    },
    {
      field: 'remark',
      minWidth: 100,
      title: '备注',
    },
    {
      field: 'createTime',
      slots: { default: 'createTime' },
      title: '创建时间',
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: '用户',
          onClick: onActionClick,
        },
        options: [
          {
            code: 'resetPassword',
            text: '重置密码',
            permission: 'system:user:update',
          },
          {
            code: 'edit',
            text: '修改',
            permission: 'system:user:update',
          },
          {
            code: 'delete',
            text: '删除',
            permission: 'system:user:delete',
          },
        ],
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 200,
    },
  ];
}
