/**
 * 项目：-
 * 模型分组：订单管理
 * 模型名称：手术订单管理
 * @Author: xiongwei
 * @Date: 2022-06-28 11:50:00
 */

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { useUserStore } from '/@/store/modules/user';
import {
  getDistributorId,
  getHospitalIdAndDoctorId,
  getSaleUserId,
  getProductLineName,
  getReceiverId,
} from '/@/commonSchemaProperty';
import { logisticsEnumOptions } from '/@/views/order/enum';
import { usePermission } from '/@/hooks/web/usePermission';
import { BatchVerifyParams } from '/@/views/system/user/schema';

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 StatusValEnum {
  PENDING_REVIEW = 'PENDING_REVIEW',
  PASSED = 'PASSED',
  REJECT = 'REJECT',
  FORBIDDEN = 'FORBIDDEN'
}
const { hasPermission } = usePermission();
export const isShowHandlePassed = (status: string): boolean => {
  return (
    (hasPermission('AUTH_ORDER_OPERATION_ORDER:PASSED') && status === 'PENDING_REVIEW') || status === StatusValEnum.FORBIDDEN
  );
};
export const isShowHandleReject = (status: string): boolean => {
  return hasPermission('AUTH_ORDER_OPERATION_ORDER:REJECT') && status === StatusValEnum.PENDING_REVIEW;
};
export const StatusEnumOptions: any[] = [];
for (const key in StatusEnum) {
  StatusEnumOptions.push({
    value: key,
    label: StatusEnum[key],
  });
}
/**
 * @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: 'OperationOrder',
  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: 'orderSn',
      label: '订单编号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '订单编号',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入订单编号！' }],
      },
      table: {},
    },
    getProductLineName({ colProps }),
    {
      field: 'productSelectField',
      label: '产品',
      defaultValue: undefined,
      form: {
        componentProps: () => {
          return {
            allowClear: true,
            placeholder: '选择产品',
            onChange: (e) => {
              console.log('e', e);
            },
          };
        },
        colProps,
        component: 'Test',
      },
      table: {},
    },
    ...getHospitalIdAndDoctorId({ colProps }),
    getSaleUserId({ colProps }),
    // {
    //   field: 'saleUserName',
    //   label: '业务员名称',
    //   defaultValue: undefined,
    //   form: {
    //     componentProps: {
    //       allowClear: true,
    //       placeholder: '业务员名称',
    //     },
    //     colProps,
    //     component: 'Input',
    //   },
    //   table: {},
    // },
    // {
    //   field: 'doctorName',
    //   label: '医生名称',
    //   defaultValue: undefined,
    //   form: {
    //     componentProps: {
    //       allowClear: true,
    //       placeholder: '医生名称',
    //     },
    //     colProps,
    //     component: 'Input',
    //   },
    //   table: {},
    // },
    // {
    //   field: 'HospitalName',
    //   label: '医院名称',
    //   defaultValue: undefined,
    //   form: {
    //     componentProps: {
    //       allowClear: true,
    //       placeholder: '医院名称',
    //     },
    //     colProps,
    //     component: 'Input',
    //   },
    //   table: {},
    // },
    {
      field: 'surgicalName',
      label: '手术名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '手术名称',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'surgicalTime',
      label: '手术时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: true,
          placeholder: '手术时间',
          format: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
          rules: [{ required: true, message: '请选择手术时间' }],
        },
      },
      table: {},
    },
    {
      field: 'deliveryTime',
      label: '期望送达时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: true,
          placeholder: '送达时间',
          format: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
          rules: [{ required: true, message: '请选择送达时间' }],
        },
      },
      table: {},
    },
    {
      field: 'needTool',
      label: '配套工具',
      defaultValue: 'NO',
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '配套工具',
          options: YesNoEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请选择' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = YesNoEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'logisticsMode',
      label: '物流方式',
      defaultValue: undefined,
      form: {
        componentProps: {
          options: logisticsEnumOptions,
          allowClear: true,
          placeholder: '物流方式',
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请选择物流方式' }],
      },
      table: {},
    },
    getReceiverId({ colProps }),
    {
      field: 'receiveName',
      label: '接收人姓名',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          // placeholder: '接收人姓名',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'receivePhone',
      label: '接收人电话',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          // placeholder: '接收人电话',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'address',
      label: '收货地址',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          // placeholder: '收货地址',
        },
        component: 'InputTextArea',
        colProps: { span: 24 },
      },
      table: {},
    },
    {
      field: 'status',
      label: '状态',
      defaultValue: 'PENDING_REVIEW',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '状态',
          options: StatusEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入状态！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = StatusEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'type',
      label: '是否是主订单',
      defaultValue: 'YES',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '是否是主订单',
          options: YesNoEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入是否是主订单！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = YesNoEnumOptions.find((item) => item.value === text);
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'remark',
      label: '备注',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '备注',
        },
        colProps,
        component: 'Input',
      },
      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: '请输入创建时间！' }],
      },
      table: {},
    },
    {
      field: 'updateTime',
      label: '更新时间',
      defaultValue: undefined,
      form: {
        colProps,
        componentProps: {
          allowClear: false,
          placeholder: ['开始时间', '结束时间'],
          format: 'YYYY-MM-DD',
          showTime: false,
        },
        component: 'RangePicker',
        rules: [{ required: true, message: '请输入更新时间！' }],
      },
      table: {},
    },
  ],
};

const queryFields = [
  // 'id',
  'distributorId',
  'orderSn',
  'productLineName',
  'hospitalId',
  'saleUserId',
  'saleUserName',
  'doctorId',
  'doctorName',
  // 'surgicalName',
  'surgicalTime',
  'deliveryTime',
  'needTool',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'status',
  // 'type',
  'remark',
  // 'deleteStatus',
  // 'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const editFields = [
  // 'distributorId',
  // 'orderSn',
  'productLineName',
  'productSelectField',
  'hospitalId',
  'doctorId',
  'saleUserId',
  // 'saleUserName',
  // 'doctorName',
  // 'surgicalName',
  'surgicalTime',
  'deliveryTime',
  'needTool',
  'logisticsMode',
  'receiveId',
  'receiveName',
  'receivePhone',
  'address',
  // 'status',
  // 'type',
  'remark',
  // 'deleteStatus',
];
const tableFields = [
  'id',
  'distributorId',
  'orderSn',
  'productLineName',
  'hospitalId',
  'saleUserId',
  'saleUserName',
  'doctorId',
  'doctorName',
  // 'surgicalName',
  'surgicalTime',
  'deliveryTime',
  'needTool',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  // 'type',
  'remark',
  // 'deleteStatus',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const descriptionFields = [
  'id',
  'distributorId',
  'orderSn',
  'productLineName',
  'hospitalId',
  'saleUserId',
  'saleUserName',
  'doctorId',
  'doctorName',
  // 'surgicalName',
  'surgicalTime',
  'deliveryTime',
  'needTool',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  // 'type',
  'remark',
  // 'deleteStatus',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
];
const userStore = useUserStore();
export const searchFormSchema: FormSchema[] = schema.properties
  .filter((item) => queryFields.includes(item.field))
  .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,
        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),
  );
