import type {
  ProColumns,
  ProCoreActionType,
  ProFormColumnsType,
} from '@ant-design/pro-components';
import { Modal, message, Switch } from 'antd';
import dayjs from 'dayjs';
import { updateUserStatus } from '@/services/system/user';
import type { UserVO } from '@/services/system/user/index';

export const baseTenantColumns: ProColumns<UserVO>[] = [
  {
    title: '用户编号',
    dataIndex: 'id',
    width: 80,
    hideInSearch: true, // 在搜索表单中隐藏
  },
  {
    title: '用户名称',
    dataIndex: 'username',
    width: 100,
  },
  {
    title: '用户昵称',
    dataIndex: 'nickname',
    width: 100,
    hideInSearch: true,
  },
  {
    title: '部门',
    dataIndex: 'deptName',
    width: 100,
  },
  {
    title: '手机号码',
    dataIndex: 'mobile',
    width: 100,
  },
  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'select',
    valueEnum: {
      all: { text: '全部', status: 'Default' },
      open: { text: '未解决', status: 'Error' },
      closed: { text: '已解决', status: 'Success' },
    },
    render: (
      _dom: React.ReactNode,
      record: UserVO,
      _index: number,
      action: ProCoreActionType | undefined,
    ) => [
      <Switch
        key={record.id}
        checked={record.status === 0}
        onChange={async (_checked) => {
          const text = record.status ? '启用' : '停用';
          Modal.confirm({
            title: '确认操作',
            content: `确认要"${text}""${record.username}"用户吗?`,
            onOk: async () => {
              const status = record.status === 0 ? 1 : 0;
              await updateUserStatus(record.id, status);
              message.success('修改成功');
              action?.reload();
              // 执行状态变更逻辑
            },
          });
          //   await message.confirm("修改成功");
        }}
      ></Switch>,
    ],
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    valueType: 'dateRange',
    search: {
      transform: (value) => {
        return {
          'createTime[0]': dayjs(value[0])
            .startOf('day')
            .format('YYYY-MM-DD HH:mm:ss'),
          'createTime[1]': dayjs(value[1])
            .endOf('day')
            .format('YYYY-MM-DD HH:mm:ss'),
        };
      },
    },
    render: (_, record: UserVO) =>
      dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss'),
  },
  {
    title: '备注',
    dataIndex: 'remark',
    hideInSearch: true, // 在搜索表单中隐藏
  },
];

export const formColumns = (type: string): ProFormColumnsType[] => [
  {
    title: '用户昵称',
    dataIndex: 'nickname',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入用户名',
        },
      ],
    },
  },
  {
    title: '归属部门',
    dataIndex: 'deptId',
    valueType: 'treeSelect',
    fieldProps: {
      multiple: true,
      placeholder: '请选择归属部门',
      options: [{ lable: '11', value: 5016 }],
    },
  },
  {
    title: '手机号码',
    dataIndex: 'mobile',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入联系手机',
        },
      ],
    },
  },
  //     { prop: "email", label: "邮箱", type: "input", inputType: "email" },
  {
    title: '邮箱',
    dataIndex: 'email',
    valueType: 'text',
    fieldProps: {
      type: 'email',
      placeholder: '请输入邮箱',
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入邮箱',
        },
      ],
    },
  },
  {
    title: '用户名称',
    dataIndex: 'username',
    hideInForm: type === 'update',
  },
  {
    title: '用户密码',
    dataIndex: 'password',
    valueType: 'password',
    hideInForm: type === 'update',
    fieldProps: {
      placeholder: '请输入用户密码',
      autoComplete: 'new-password',
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入用户密码',
        },
      ],
    },
  },
  {
    title: '用户性别',
    dataIndex: 'sex',
    valueType: 'select',
    fieldProps: {
      placeholder: '请选择性别',
      options: [
        {
          label: '男',
          value: 1,
        },
        {
          label: '女',
          value: 2,
        },
      ],
    },
  },
  {
    title: '岗位',
    dataIndex: 'postIds',
    valueType: 'select',
    fieldProps: {
      mode: 'multiple',
      placeholder: '请选择岗位',
      options: [
        {
          label: '管理员',
          value: 1,
        },
        {
          label: '普通用户',
          value: 2,
        },
      ],
    },
    formItemProps: {},
  },
  {
    title: '备注',
    dataIndex: 'remark',
    valueType: 'textarea',
  },
];
