import type { FormSchema } from '/@/components/Form/index';
import { getAreaList } from '/@/api/oem/index';
import { emailRule, phoneRule } from '/@/utils/rules';

export enum Level {
  PROVINCE = 'PROVINCE',
  CITY = 'CITY',
  COUNTY = 'COUNTY',
  TOWN = 'TOWN',
}

export const schemas: FormSchema[] = [
  {
    field: 'name',
    component: 'Input',
    label: '公司名称',
    colProps: {
      span: 24,
    },
    required: true,
    componentProps: {
      maxLength: 100,
      placeholder: '请输入公司名称',
    },
  },
  {
    field: 'abbreviation',
    component: 'Input',
    label: '公司简称',
    colProps: {
      span: 24,
    },
    componentProps: {
      maxLength: 100,
      placeholder: '请输入公司简称',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 100) {
              return Promise.reject('字数不超过100个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'officialWebsite',
    component: 'Input',
    label: '公司官网',
    colProps: {
      span: 24,
    },
    componentProps: {
      maxLength: 255,
      placeholder: '请输入公司官网',
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 255) {
              return Promise.reject('字数不超过255个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'email',
    component: 'Input',
    label: '公司邮箱',
    colProps: {
      span: 24,
    },
    componentProps: {
      placeholder: '请输入公司邮箱',
    },
    rules: emailRule,
  },
  {
    field: 'synopsis',
    component: 'InputTextArea',
    label: '公司简介',
    colProps: {
      span: 24,
    },
    componentProps: {
      maxLength: 500,
      placeholder: '请输入公司简介',
      autoSize: { minRows: 8, maxRows: 12 },
    },
    dynamicRules: () => {
      return [
        {
          required: false,
          validator: (_, value) => {
            if (String(value).length > 500) {
              return Promise.reject('字数不超过500个字');
            }
            return Promise.resolve();
          },
        },
      ];
    },
  },
  {
    field: 'nameCountry',
    component: 'Select',
    label: '国家/地区',
    colProps: {
      span: 24,
    },
    // componentProps: {
    //   api: getAreaList,
    //   params: { parentId: 0 },
    //   labelField: 'name',
    //   valueField: 'code',
    //   placeholder: '国家/地区',
    // },
    componentProps: {
      options: [{ label: '中国', value: 1 }],
    },
  },
  {
    field: 'prov',
    label: '所在城市',
    required: true,
    component: 'Select',
    colProps: {
      span: 24,
    },
    slot: 'customProv',
  },
  {
    field: 'address',
    component: 'Input',
    label: '详细地址',
    colProps: {
      span: 24,
    },
    required: true,
    componentProps: {
      maxLength: 100,
      placeholder: '请输入详细地址',
    },
  },

  {
    field: 'contacts',
    component: 'Input',
    label: '联系人',
    colProps: {
      span: 24,
    },
    required: true,
    componentProps: {
      maxLength: 25,
      placeholder: '请输入联系人',
    },
  },
  {
    field: 'tel',
    component: 'Input',
    label: '联系电话',
    colProps: {
      span: 24,
    },
    componentProps: {
      placeholder: '请输入联系电话',
    },
    rules: phoneRule,
  },
  {
    field: 'qrcode',
    label: '联系我们',
    required: true,
    component: 'Select',
    colProps: {
      span: 24,
    },
    slot: 'qrcode',
  },
  {
    field: 'id',
    label: '唯一id',
    component: 'Input',
    show: false,
    componentProps: {
      maxLength: 36,
      placeholder: '请输入唯一id',
    },
  },
];

export const provSchemas: FormSchema[] = [
  {
    field: 'nameProv',
    component: 'ApiSelect',
    label: '',
    colProps: {
      span: 6,
    },
    componentProps({ formActionType }) {
      const { setFieldsValue } = formActionType;
      return {
        api: getAreaList,
        labelField: 'name',
        valueField: 'code',
        placeholder: '省份',
        params: { parentId: 1, level: Level.PROVINCE },
        onChange: () => {
          setFieldsValue({ nameCity: null, nameCoun: null, nameTown: null });
        },
      };
    },
  },
  {
    field: 'nameCity',
    component: 'ApiSelect',
    label: '',
    colProps: {
      span: 6,
    },
    componentProps: ({ formActionType, formModel }) => {
      const nameProv = Reflect.get(formModel, 'nameProv');
      const { setFieldsValue } = formActionType;
      return {
        api: async (params: Recordable) => {
          try {
            if (!nameProv) return;
            const result = await getAreaList(params);
            return result;
          } catch (error) {
            return [];
          }
        },
        labelField: 'name',
        valueField: 'code',
        placeholder: '城市',
        params: { parentId: nameProv, level: Level.CITY },
        onChange: () => {
          setFieldsValue({ nameCoun: null, nameTown: null });
        },
      };
    },
  },
  {
    field: 'nameCoun',
    component: 'ApiSelect',
    label: '',
    colProps: {
      span: 6,
    },
    componentProps: ({ formActionType, formModel }) => {
      const nameCity = Reflect.get(formModel, 'nameCity');
      const { setFieldsValue } = formActionType;
      return {
        api: async (params: Recordable) => {
          try {
            if (!nameCity) return;
            const result = await getAreaList(params);
            return result;
          } catch (error) {
            return [];
          }
        },
        labelField: 'name',
        valueField: 'code',
        placeholder: '区/县',
        params: { parentId: nameCity, level: Level.COUNTY },
        onChange: () => {
          setFieldsValue({ nameTown: null });
        },
      };
    },
  },
  {
    field: 'nameTown',
    component: 'ApiSelect',
    label: '',
    colProps: {
      span: 6,
    },
    componentProps: ({ formModel }) => {
      const nameCoun = Reflect.get(formModel, 'nameCoun');
      return {
        api: async (params: Recordable) => {
          try {
            if (!nameCoun) return;
            const result = await getAreaList(params);
            return result;
          } catch (error) {
            return [];
          }
        },
        labelField: 'name',
        valueField: 'code',
        placeholder: '城镇/街道',
        params: { parentId: nameCoun, level: Level.TOWN },
      };
    },
  },
];
