/**
 * 项目：-
 * 模型分组：订单管理
 * 模型名称：批量订工单详情表管理
 * @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 GoodsApi from '/@/api/product/goodsApi';

export enum GoodsTypeEnum {
  PRODUCT = '产品',
  KIT = '套件',
  SURGICAL_BAG = '手术包',
}
export const GoodsTypeEnumOptions: any[] = [];
for (const key in GoodsTypeEnum) {
  GoodsTypeEnumOptions.push({
    value: key,
    label: GoodsTypeEnum[key],
  });
}

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

export const schema = {
  model: 'BatchWorkOrderDetail',
  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: 'batchWorkOrderId',
      label: 'oms_operation_work_order.id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: 'oms_operation_work_order.id',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入oms_operation_work_order.id！' }],
      },
      table: {
      },
    },
    {
      field: 'goodsId',
      label: '商品id',
      defaultValue: undefined,
      form: {
        componentProps: {
          labelInValue: true,
          api: GoodsApi.all,
          params: {},
          resultField: 'records',
          labelField: 'name',
          valueField: 'id',
          allowClear: false,
          placeholder: '商品id',
        },
        colProps,
        component: 'ApiSelect',
        rules: [{ required: true, type: 'object', message: '请输入商品id！' }],
      },
      table: {
        customRender: ({ text, record }) => get(record, 'goods.name') || text,
      },
    },
    {
      field: 'name',
      label: '名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入名称！' }],
      },
      table: {
      },
    },
    {
      field: 'goodsType',
      label: '商品类型',
      defaultValue: 'PRODUCT',
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '商品类型',
          options: GoodsTypeEnumOptions,
        },
        colProps,
        component: 'Select',
        rules: [{ required: true, message: '请输入商品类型！' }],
      },
      table: {
        customRender: ({ text }) => {
          const option = GoodsTypeEnumOptions.find((item) => item.value === text)
          return option ? option.label : text;
        },
      },
    },
    {
      field: 'price',
      label: '单价',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '单价',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入单价！' }],
      },
      table: {
      },
    },
    {
      field: 'discountPrice',
      label: '折扣价',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '折扣价',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入折扣价！' }],
      },
      table: {
      },
    },
    {
      field: 'quantity',
      label: '数量',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '数量',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入数量！' }],
      },
      table: {
      },
    },
    {
      field: 'createTime',
      label: 'CreateTime',
      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: 'UpdateTime',
      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','batchWorkOrderId','goodsId','name','goodsType','price','discountPrice','quantity','createTime','updateTime'];
const editFields = ['distributorId','batchWorkOrderId','goodsId','name','goodsType','price','discountPrice','quantity'];
const tableFields = ['id','distributorId','batchWorkOrderId','goodsId','name','goodsType','price','discountPrice','quantity','createTime','updateTime'];
const descriptionFields = ['id','distributorId','batchWorkOrderId','goodsId','name','goodsType','price','discountPrice','quantity','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),
  );
