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

import { BasicColumn } from '/@/components/Table'
import { FormSchema } from '/@/components/Table'

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: '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: {},
    },
    {
      field: 'distributorId',
      label: '经销商id',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '经销商id',
        },
        colProps,
        component: 'InputNumber',
        rules: [{ required: true, message: '请输入经销商id！' }],
      },
      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: '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: '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: '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',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'status',
  'remark',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
]
const editFields = ['distributorId', 'logisticsMode', 'receiveName', 'receivePhone', 'address', 'status', 'remark']
const tableFields = [
  'id',
  'distributorId',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  'editorId',
  'editorName',
  'createTime',
  'updateTime',
]
const descriptionFields = [
  'id',
  'distributorId',
  'logisticsMode',
  'receiveName',
  'receivePhone',
  'address',
  'status',
  'remark',
  'editorId',
  'editorName',
  '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),
  )
