import { BasicColumn, FormSchema } from '@/components/Table';
import { render } from '@/utils/common/renderUtils';
import { rules } from '@/utils/helper/validator';

export const columns: BasicColumn[] = [
  {
    title: '序号',
    width: 60,
    align: 'center',
    dataIndex: 'seqNumber',
  },
  {
    title: '用户名',
    align: 'left',
    dataIndex: 'realname',
  },
  {
    title: '登录账号',
    align: 'left',
    dataIndex: 'username',
  },
  {
    title: '头像',
    align: 'left',
    customRender: render.renderAvatar,
    width: 80,
  },
  {
    title: '类型',
    align: 'left',
    width: 80,
    dataIndex: 'primaryAccount',
    customRender: function ({ text }) {
      return text ? '主账户' : '子账户';
    },
  },
  {
    title: '分配的询盘数',
    align: 'left',
    dataIndex: 'enquiryNum',
  },

  {
    title: '手机号码',
    align: 'left',
    dataIndex: 'phone',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
  {
    title: '邮箱',
    align: 'left',
    dataIndex: 'email',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
  {
    title: '更新时间',
    align: 'left',
    dataIndex: 'updateTime',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
];

export const subColumns: BasicColumn[] = [
  {
    title: '序号',
    width: 60,
    align: 'center',
    dataIndex: 'seqNumber',
  },
  {
    title: '用户名',
    align: 'left',
    dataIndex: 'realname',
  },
  {
    title: '登录账号',
    align: 'left',
    dataIndex: 'username',
  },
  {
    title: '头像',
    align: 'left',
    dataIndex: 'avatar',
    width: 80,
  },
  {
    title: '类型',
    align: 'left',
    width: 80,
    dataIndex: 'primaryAccount',
    customRender: function ({ text }) {
      return text ? '主账户' : '子账户';
    },
  },
  {
    title: '性别',
    align: 'left',
    dataIndex: 'sex',
    customRender: function ({ text }) {
      if (text === null) {
        return '-';
      }
      return text === 1 ? '男' : '女';
    },
  },
  {
    title: '手机号码',
    align: 'left',
    dataIndex: 'phone',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
  {
    title: '邮箱',
    align: 'left',
    dataIndex: 'email',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
  {
    title: '更新时间',
    align: 'left',
    dataIndex: 'updateTime',
    customRender: function ({ text }) {
      return text === null ? '--' : text;
    },
  },
  {
    title: '主账号询盘',
    align: 'center',
    dataIndex: 'isInquiry',
    customRender: function ({ text }) {
      return text === 1 ? '不接收' : '接收中';
    },
  },
  {
    title: '询盘接收',
    dataIndex: 'action',
    fixed: 'right',

    align: 'left',
  },
];

export const formSchema: FormSchema[] = [
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
  {
    label: '用户账号',
    field: 'username',
    component: 'Input',
    dynamicDisabled: ({ values }) => {
      return !!values.id;
    },
    dynamicRules: ({ model, schema }) => rules.duplicateCheckRule('sys_user', 'username', model, schema, true),
  },
  {
    label: '登录密码',
    field: 'password',
    component: 'StrengthMeter',
    componentProps: {
      autocomplete: 'new-password',
    },
    rules: [
      {
        required: true,
        message: '请输入登录密码',
      },
      {
        pattern: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[~!@#$%^&*()_+`\-={}:";'<>?,./]).{8,}$/,
        message: '密码由8位数字、大小写字母和特殊符号组成!',
      },
    ],
    ifShow: ({ values }) => {
      return !values.id;
    },
  },
  {
    label: '确认密码',
    field: 'confirmPassword',
    component: 'InputPassword',
    dynamicRules: ({ values }) => rules.confirmPassword(values, true),
    ifShow: ({ values }) => {
      return !values.id;
    },
  },
  {
    label: '用户名称',
    field: 'realname',
    required: true,
    component: 'Input',
  },
  {
    label: '手机号码',
    field: 'phone',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [
        { ...rules.duplicateCheckRule('sys_user', 'phone', model, schema, true)[0], trigger: 'blur' },
        { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误', trigger: 'blur' },
      ];
    },
  },
  {
    label: '邮箱',
    field: 'email',
    component: 'Input',
    required: true,
    dynamicRules: ({ model, schema }) => {
      return [
        { ...rules.duplicateCheckRule('sys_user', 'email', model, schema, true)[0], trigger: 'blur' },
        { ...rules.rule('email', false)[0], trigger: 'blur' },
      ];
    },
  },

  {
    label: '头像',
    field: 'avatar',
    component: 'JImageUpload',
    componentProps: {
      fileMax: 1,
    },
  },
];
