/**
 * 项目：-
 * 模型分组：系统管理
 * 模型名称：经销商表
 * @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 {
  PENDING_REVIEW = '待审核',
  PASSED = '已通过',
  REJECT = '拒绝',
  FORBIDDEN = '禁用',
}
export const StatusEnumOptions: any[] = [];
for (const key in StatusEnum) {
  StatusEnumOptions.push({
    value: key,
    label: StatusEnum[key],
  });
}

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

export const schema = {
  model: 'Distributor',
  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: 'name',
      label: '名称',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '名称',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入名称！' }],
      },
      table: {},
    },
    {
      field: 'price',
      label: '授信额度',
      defaultValue: 0,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '授信额度',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {},
    },
    {
      field: 'usedPrice',
      label: '使用额度',
      defaultValue: 0,
      form: {
        componentProps: {
          allowClear: true,
          placeholder: '使用额度',
        },
        colProps,
        component: 'InputNumber',
      },
      table: {},
    },
    {
      field: 'liablePeople',
      label: '负责人',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '负责人',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入负责人！' }],
      },
      table: {},
    },
    {
      field: 'mobile',
      label: '联系方式',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '联系方式',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入联系方式！' }],
      },
      table: {},
    },
    {
      field: 'address',
      label: '地址',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '地址',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入地址！' }],
      },
      table: {},
    },
    {
      field: 'licenseNumber',
      label: '工商营业执照代码',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '工商营业执照代码',
        },
        colProps,
        component: 'Input',
        rules: [{ required: true, message: '请输入工商营业执照代码！' }],
      },
      table: {},
    },
    {
      field: 'licensePic',
      label: '工商营业执照图片',
      defaultValue: undefined,
      form: {
        componentProps: {
          allowClear: false,
          placeholder: '工商营业执照图片',
        },
        colProps,
        component: 'Upload',
        rules: [{ required: true, message: '请输入工商营业执照图片！' }],
      },
      table: {},
    },
    {
      field: 'licenseExpireTime',
      label: '有效期',
      defaultValue: undefined,
      form: {
        colProps,
        component: 'DatePicker',
        componentProps: {
          allowClear: false,
          placeholder: '有效期',
          format: 'YYYY-MM-DD HH:mm:ss',
          showTime: true,
        },
        rules: [{ required: true, message: '请输入有效期！' }],
      },
      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: '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 = [
  'id',
  'name',
  'price',
  'usedPrice',
  'liablePeople',
  'mobile',
  'address',
  'licenseNumber',
  'licenseExpireTime',
  'status',
  'remark',
  'createTime',
  'updateTime',
];
const editFields = [
  'name',
  'price',
  'usedPrice',
  'liablePeople',
  'mobile',
  'address',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
  'remark',
];
const tableFields = [
  'id',
  'name',
  'price',
  'usedPrice',
  'liablePeople',
  'mobile',
  'address',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
  'remark',
  'createTime',
  'updateTime',
];
const descriptionFields = [
  'id',
  'name',
  'price',
  'usedPrice',
  'liablePeople',
  'mobile',
  'address',
  'licenseNumber',
  'licensePic',
  'licenseExpireTime',
  'status',
  'remark',
  '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),
  );
