/**
 * 项目：-
 * 模型分组：系统管理
 * 模型名称：用户表
 * @Author: xiongwei
 * @Date: 2022-06-28 11:50:00
 */

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import * as RoleApi from '/@/api/system/roleApi';
import { getDistributorId } from '/@/commonSchemaProperty';
import { usePermission } from '/@/hooks/web/usePermission';
import { phoneRule } from '/@/commonSchemaProperty/formRules';
import { StatusValEnum } from './enum';
const { hasPermission } = usePermission();
export interface BatchVerifyParams {
  /**
   * id数组
   */
  idList: Array<number | string>;
  /**
   * 状态
   */
  status: string;
}

/**
 * @description: 返回审核所需参数
 * @param record
 * @param status
 */
export const getVerifyParams = (record: Recordable, status: string): BatchVerifyParams => {
  const params: BatchVerifyParams = {
    idList: [record.id],
    status,
  };
  return params;
};

export enum StatusEnum {
  PENDING_REVIEW = '待审核',
  PASSED = '已通过',
  REJECT = '拒绝',
  FORBIDDEN = '禁用',
}
export const StatusEnumOptions: any[] = [];
for (const key in StatusEnum) {
  StatusEnumOptions.push({
    value: key,
    label: StatusEnum[key],
  });
}

export enum StatusColorEnum {
  PENDING_REVIEW = 'processing',
  PASSED = 'success',
  REJECT = 'error',
  FORBIDDEN = 'default',
}

export const isShowHandlePassed = (status: string): boolean => {
  return (
    (hasPermission('AUTH_SYSTEM_USER:PASSED') && status === 'PENDING_REVIEW') || status === StatusValEnum.FORBIDDEN
  );
};
export const isShowHandleReject = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_USER:REJECT') && status === StatusValEnum.PENDING_REVIEW;
};
export const isShowHandleForbidden = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_USER:FORBIDDEN') && status === StatusValEnum.PASSED;
};
export const isShowHandleEdit = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_USER:EDIT') && status !== StatusValEnum.FORBIDDEN;
};
export const isShowHandleDelete = (): boolean => {
  return hasPermission('AUTH_SYSTEM_USER:DELETE');
};

export enum YesNoEnum {
  YES = '是',
  NO = '否',
}
export const YesNoEnumOptions: any[] = [];
for (const key in YesNoEnum) {
  YesNoEnumOptions.push({
    value: key,
    label: YesNoEnum[key],
  });
}

const colProps = { xs: { span: 24 }, sm: { span: 24 }, lg: { span: 8 } };
const colPropsInDrawer = { span: 24 };

export const PASSWORD = 'password';

export const schema = {
  model: 'User',
  viewInPage: true,
  properties: [
    {
      field: 'id',
      label: 'ID系统自动生成',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: 'ID系统自动生成',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入ID系统自动生成！' }],
      },
      table: {},
    },
    getDistributorId({ colProps }),
    {
      field: 'username',
      label: '用户名',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '用户名',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入用户名！' }],
      },
      table: {},
    },
    {
      field: PASSWORD,
      label: '密码',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '密码',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入密码！' }],
      },
      table: {},
    },
    {
      field: 'realName',
      label: '姓名',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '姓名',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入姓名！' }],
      },
      table: {},
    },
    {
      field: 'mobile',
      label: '手机号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '手机号',
        },
        colProps,
        component: 'Input',
        rules: [phoneRule()],
      },
      table: {},
    },
    {
      field: 'roleList',
      label: '角色',
      defaultValue: undefined,
      form: {
        itemProps: {
          validateTrigger: 'blur',
        },
        componentProps: {
          labelInValue: true,
          api: RoleApi.all,
          params: {},
          resultField: 'records',
          labelField: 'remark',
          valueField: 'id',
          allowClear: false,
          placeholder: '角色',
        },
        colProps,
        component: 'ApiSelect',
        rules: [{ required: true, type: 'object', message: '请选择角色' }],
      },
      table: {
        customRender: ({ text }) => {
          return text?.map((item: any) => item.remark).join(',');
        },
      },
    },
    {
      field: 'status',
      label: '状态',
      defaultValue: 'PENDING_REVIEW',
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '状态',
          options: StatusEnumOptions,
        },
        colProps,
        component: 'Select',
      },
      table: {
        customRender: ({ text }) => {
          const option = StatusEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'salePeopleId',
      label: '业务员id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '业务员id',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {},
    },
    {
      field: 'deleteStatus',
      label: '删除状态',
      defaultValue: 'NO',
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '删除状态',
          options: YesNoEnumOptions,
        },
        colProps,
        component: 'Select',
      },
      table: {
        customRender: ({ text }) => {
          const option = YesNoEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'editorId',
      label: '操作人ID',
      defaultValue: 1,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '操作人ID',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入操作人ID！' }],
      },
      table: {},
    },
    {
      field: 'editorName',
      label: '操作人',
      defaultValue: 'admin',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '操作人',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入操作人！' }],
      },
      table: {},
    },
    {
      field: 'createTime',
      label: '创建时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入CreateTime！' }],
      },
      table: {},
    },
    {
      field: 'updateTime',
      label: '更新时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入UpdateTime！' }],
      },
      table: {},
    },
  ],
};

const queryFields = ['distributorId', 'username', 'realName', 'mobile', 'status', 'createTime'];
const editFields = ['distributorId', 'username', PASSWORD, 'realName', 'mobile', 'roleList'];
const tableFields = [
  'distributorId',
  'username',
  'realName',
  'mobile',
  'roleList',
  'status',
  'editorName',
  'createTime',
];
const descriptionFields = [
  'id',
  'distributorId',
  'username',
  'realName',
  'mobile',
  'status',
  'salePeopleId',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];

export const searchFormSchema: FormSchema[] = schema.properties
  .filter((item) => queryFields.includes(item.field))
  .map(
    ({ field, label, form: { rules = [], ...formProps } }) =>
      ({
        field,
        label,
        defaultValue: undefined,
        // @ts-ignore
        rules: rules.filter((r) => !r.required),
        ...formProps,
      } as FormSchema),
  );

export const formSchema: FormSchema[] = schema.properties
  .filter((item) => editFields.includes(item.field))
  .map(
    ({ field, label, defaultValue, form }) =>
      ({
        field,
        label,
        defaultValue,
        ...form,
        colProps: colPropsInDrawer,
      } as FormSchema),
  );

export const columns: BasicColumn[] = schema.properties
  .filter((item) => tableFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn),
  );

export const descriptionColumns: BasicColumn[] = schema.properties
  .filter((item) => descriptionFields.includes(item.field))
  .map(
    ({ field, label, table }) =>
      ({
        dataIndex: field,
        title: label,
        ...table,
      } as BasicColumn),
  );
