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

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { SexEnum, StatusEnumOptions } from '/@/views/system/doctor/schema';
import { getDistributorId } from '/@/commonSchemaProperty';
import { usePermission } from '/@/hooks/web/usePermission';
import { BatchVerifyParams } from '/@/api/model/receiver';
import { useUserStore } from '/@/store/modules/user';
import { phoneRule } from '/@/commonSchemaProperty/formRules';
const { hasPermission } = usePermission();
const userStore = useUserStore();
export enum YesNoEnum {
  YES = '是',
  NO = '否',
}
export const YesNoEnumOptions: any[] = [];
for (const key in YesNoEnum) {
  YesNoEnumOptions.push({
    value: key,
    label: YesNoEnum[key],
  });
}

export enum StatusEnum {
  PENDING_REVIEW = '待审核',
  PASSED = '已通过',
  REJECT = '已拒绝',
  FORBIDDEN = '禁用',
}
export enum StatusColorEnum {
  PENDING_REVIEW = 'processing',
  PASSED = 'success',
  REJECT = 'error',
  FORBIDDEN = 'default',
}
export enum StatusValEnum {
  PENDING_REVIEW = 'PENDING_REVIEW',
  PASSED = 'PASSED',
  REJECT = 'REJECT',
  FORBIDDEN = 'FORBIDDEN',
}

export const StatusOptions: any[] = [];
for (const key in StatusEnum) {
  StatusOptions.push({
    value: key,
    label: SexEnum[key],
  });
}

export const isShowHandlePassed = (status: string): boolean => {
  return (
    (hasPermission('AUTH_SYSTEM_RECEIVER:PASSED') && status === 'PENDING_REVIEW') || status === StatusValEnum.FORBIDDEN
  );
};
export const isShowHandleReject = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_RECEIVER:REJECT') && status === StatusValEnum.PENDING_REVIEW;
};
export const isShowHandleForbidden = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_RECEIVER:FORBIDDEN') && status === StatusValEnum.PASSED;
};
export const isShowHandleEdit = (status: string): boolean => {
  return hasPermission('AUTH_SYSTEM_RECEIVER:EDIT') && status !== StatusValEnum.FORBIDDEN;
};
export const isShowHandleDelete = (): boolean => {
  return hasPermission('AUTH_SYSTEM_RECEIVER:DELETE');
};
/**
 * @description: 返回审核所需参数
 * @param record
 * @param status
 */
export const getVerifyParams = (record: Recordable, status: string): BatchVerifyParams => {
  const params: BatchVerifyParams = {
    idList: [record.id],
    status,
  };
  return params;
};

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

export const schema = {
  model: 'Receiver',
  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: 'logisticsMode',
      label: '物流方式',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '物流方式',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'receiveName',
      label: '姓名',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '姓名',
        },
        rules: [{ required: true, message: '请输入姓名！' }],
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'receivePhone',
      label: '电话',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '电话',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入电话！' }, phoneRule()],
      },
      table: {},
    },
    {
      field: 'address',
      label: '收货地址',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '收货地址',
        },
        component: 'InputTextArea',
        colProps: { span: 24 },
        required: true,
      },
      table: {},
    },
    {
      field: 'isDefault',
      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: 'status',
      label: '状态',
      defaultValue: 'PENDING_REVIEW',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '状态',
          options: StatusEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入状态！' }],
      },
      table: {
        fixed: 'right',
        customRender: ({ text }) => {
          const option = StatusEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'attachment',
      label: '照片',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '照片',
          maxNumber: 1,
          multiple: false,
        },
        colProps,
        component: 'Upload',
        rules: [{ required: true, message: '请上传' }],
      },
      table: {},
    },
    {
      field: 'remark',
      label: '备注',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '备注',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      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 = [
  // 'id',
  'distributorId',
  'receiveName',
  'receivePhone',
  'status',
  'remark',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const editFields = ['receiveName', 'receivePhone', 'address', 'remark'];
const tableFields = [
  'id',
  'distributorId',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const descriptionFields = [
  'id',
  'distributorId',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];

export const searchFormSchema: FormSchema[] = schema.properties
  .filter((item) => {
    if (item.field === 'distributorId') {
      return queryFields.includes(item.field) && userStore.getDistributorId === 0;
    } else {
      return 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),
  );
