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

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import get from 'lodash.get';
import * as OperationOrderApi from '/@/api/order/operationOrderApi';

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 YesNoEnum {
  YES = '是',
  NO = '否',
}
export const YesNoEnumOptions: any[] = [];
for (const key in YesNoEnum) {
  YesNoEnumOptions.push({
    value: key,
    label: YesNoEnum[key],
  });
}
export enum OrderModeEnum {
  SALES_CONFIRM_ORDER = '销售确认单',
  CONSUME_ORDER = '消耗单',
  BACK_ORDER = '返货单',
}
export const OrderModeEnumOptions: any[] = [];
for (const key in OrderModeEnum) {
  OrderModeEnumOptions.push({
    value: key,
    label: OrderModeEnum[key],
  });
}

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

export const schema = {
  model: 'BatchWorkOrder',
  viewInPage: true,
  properties: [
    {
      field: 'id',
      label: 'ID系统自动生成',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: 'ID系统自动生成',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入ID系统自动生成！' }],
      },
      table: {
      },
    },
    {
      field: 'distributorId',
      label: '经销商id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '经销商id',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入经销商id！' }],
      },
      table: {
      },
    },
    {
      field: 'batchOrderId',
      label: '手术订单id',
      defaultValue: undefined,
      form: {
        componentProps: {
          labelInValue: true,
          api: OperationOrderApi.all,
          params: {},
          resultField: 'records',
          labelField: 'id',
          valueField: 'id',
          allowClear: false,
          placeholder: '手术订单id',
        },
        colProps,
        component: 'ApiSelect',
        rules: [{ required: true, type: 'object', message: '请输入手术订单id！' }],
      },
      table: {
        customRender: ({ text, record }) => get(record, 'operationOrder.name') || text,
      },
    },
    {
      field: 'orderSn',
      label: '订单编号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '订单编号',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入订单编号！' }],
      },
      table: {
      },
    },
    {
      field: 'userId',
      label: '执行人ID',
      defaultValue: 0,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '执行人ID',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {
      },
    },
    {
      field: 'realName',
      label: '执行人姓名',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '执行人姓名',
        },
        colProps,
        component: 'Input',
      },
      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: 'remark',
      label: '备注',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '备注',
        },
        colProps,
        component: 'Input',
      },
      table: {
      },
    },
    {
      field: 'endTime',
      label: '返货时间',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: true,
          placeholder: '返货时间',
          format: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
        },
      },
      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: 'orderMode',
      label: '工单类型',
      defaultValue: 'SALES_CONFIRM_ORDER',
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '工单类型',
          options: OrderModeEnumOptions,
        },
        colProps,
        component: 'Select',
      },
      table: {
        customRender: ({ text }) => {
          const option = OrderModeEnumOptions.find((item) => item.value === text)
          return option ? option.label : text;
        },
      },
    },
    {
      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','batchOrderId','orderSn','userId','realName','status','remark','endTime','deleteStatus','orderMode','createTime','updateTime'];
const editFields = ['distributorId','batchOrderId','orderSn','userId','realName','status','remark','endTime','deleteStatus','orderMode'];
const tableFields = ['id','distributorId','batchOrderId','orderSn','userId','realName','status','remark','endTime','deleteStatus','orderMode','createTime','updateTime'];
const descriptionFields = ['id','distributorId','batchOrderId','orderSn','userId','realName','status','remark','endTime','deleteStatus','orderMode','createTime','updateTime'];

export const searchFormSchema: FormSchema[] = schema.properties.filter(item => 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),
  );
