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

import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import get from 'lodash.get';

export enum StatusEnum {
  TO_BE_USE = '待使用',
  PENDING_REVIEW = '待审核',
  PASSED = '已推送',
  DELIVERED = '已发货',
  BACK = '已回库',
  CONFIRM = '已确认',
  CANCEL = '取消',
  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],
  });
}

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

export const schema = {
  model: 'BatchOrder',
  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: 'orderSn',
      label: '订单编号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '订单编号',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入订单编号！' }],
      },
      table: {},
    },
    {
      field: 'exWarehouse',
      label: '出库仓',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '出库仓',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'distributorName',
      label: '供应商名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '供应商名称',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'ownOrderSn',
      label: '自有订单编号',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '自有订单编号',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入自有订单编号！' }],
      },
      table: {},
    },
    {
      field: 'saleUserId',
      label: '业务员id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '业务员id',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {},
    },
    {
      field: 'saleUserName',
      label: '业务员名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '业务员名称',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'invoicingCompany',
      label: '开票公司',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '开票公司',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      field: 'logisticsMode',
      label: '物流方式',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '物流方式',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      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: 'remark',
      label: '备注',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '备注',
        },
        colProps,
        component: 'Input',
      },
      table: {},
    },
    {
      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: '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 = [
  'orderSn',
  'distributorName',
  'saleUserName',
  'receiveName',
  'receivePhone',
  'status',
  'editorName',
  'createTime',
];
const editFields = [
  'distributorId',
  'orderSn',
  'exWarehouse',
  'distributorName',
  'ownOrderSn',
  'saleUserId',
  'saleUserName',
  'invoicingCompany',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  'type',
  'deleteStatus',
];
const tableFields = [
  'orderSn',
  'exWarehouse',
  'distributorName',
  'ownOrderSn',
  'saleUserName',
  'invoicingCompany',
  'logisticsMode',
  'status',
  'editorName',
  'createTime',
];
const descriptionFields = [
  'id',
  'orderSn',
  'exWarehouse',
  'distributorName',
  'ownOrderSn',
  'saleUserName',
  'invoicingCompany',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  'editorName',
  'createTime',
];

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),
  );

export const detailColumns = [
  {
    title: '产品编号',
    dataIndex: 'thirdProductId',
    key: 'thirdProductId',
  },
  {
    title: '标识名称',
    dataIndex: 'thirdProductId10',
    key: 'thirdProductId10',
  },
  {
    title: '产品名称',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '规格型号',
    dataIndex: 'thirdProductId8',
    key: 'thirdProductId8',
  },
  {
    title: '单位',
    dataIndex: 'thirdProductId7',
    key: 'thirdProductId7',
  },
  {
    title: '类型',
    dataIndex: 'thirdProductId6',
    key: 'thirdProductId6',
  },
  {
    title: '订购数量',
    dataIndex: 'quantity',
    key: 'thirdProductId5',
  },
  {
    title: '取消数量',
    dataIndex: 'thirdProductId4',
    key: 'thirdProductId4',
  },
  {
    title: '发货数量',
    dataIndex: 'thirdProductId3',
    key: 'thirdProductId3',
  },
  {
    title: '单价',
    dataIndex: 'thirdProductId2',
    key: 'thirdProductId2',
  },
  {
    title: '行总价',
    dataIndex: 'thirdProductId1',
    key: 'thirdProductId1',
  },
];
