import { Time } from '/@/components/Time';
import { FormProps, BasicColumn } from '/@/components/Table';
import { h, computed } from 'vue';
import { FormSchema } from '/@/components/Form/index';
import { useI18n } from '/@/hooks/web/useI18n';
import { useLocaleStore } from '/@/store/modules/locale';
const { t } = useI18n();
const localeStore = useLocaleStore();
const isEn = computed(() => localeStore.getLocale === 'en');

import { dincrementList } from '/@/api/equity';
export const columnsDown: BasicColumn[] = [
  {
    title: t('routes.equity.count'),
    dataIndex: 'userName',
  },
  {
    title: t('routes.equity.dowmCount'),
    dataIndex: 'count',
    // slots: { customRender: 'orderStatus' },
  },
  {
    title: t('routes.equity.timeList'),
    dataIndex: 'createTime',
    // slots: { customRender: 'orderStatus' },
  },
];
export const columns: BasicColumn[] = [
  {
    title: t('routes.equity.id'),
    dataIndex: 'id',
    ellipsis: false,
    width: 180,
  },
  {
    title: t('routes.equity.userName'),
    dataIndex: 'userName',
    ellipsis: false,
    width: 180,
  },
  {
    title: t('routes.equity.Type'),
    dataIndex: 'incrementTypeId',
    width: 120,
    customRender: ({ record }) => {
      return t(`routes.equity.equityType.${record.incrementTypeId}`);
    },
  },
  {
    title: t('routes.equity.time'),
    dataIndex: 'incrementStartTime',
    width: 150,
    customRender: ({ record }) => {
      return (
        record.incrementStartTime &&
        h(Time, {
          value: record.incrementStartTime,
          mode: 'datetime',
        })
      );
    },
  },
  {
    title: t('routes.equity.expiryTime'),
    dataIndex: 'incrementEndTime',
    width: 150,
    customRender: ({ record }) => {
      return (
        record.incrementEndTime &&
        h(Time, {
          value: record.incrementEndTime,
          mode: 'datetime',
        })
      );
    },
  },
  {
    title: t('routes.equity.newTime'),
    dataIndex: 'invoiceTime',
    width: 150,
    customRender: ({ record }) => {
      return (
        record.updateTime &&
        h(Time, {
          value: record.updateTime,
          mode: 'datetime',
        })
      );
    },
  },
];
export const searchForm: Partial<FormProps> = {
  labelWidth: 100,
  schemas: [
    {
      field: 'id',
      label: t('routes.equity.id'),
      component: 'Input',
      componentProps: {
        maxLength: 100,
      },
      colProps: {
        xl: 6,
        xxl: 6,
      },
      rules: [
        {
          required: false,
          // @ts-ignore
          validator: async (rule, value) => {
            // var reg = /\S+@\S+\.\S+/;
            const regPos = /^[0-9]*$/;
            if (!value) {
              return Promise.resolve();
            }
            if (!regPos.test(value)) {
              /* eslint-disable-next-line */
              return Promise.reject(t('routes.equity.rules.id'));
            }
            return Promise.resolve();
          },
          trigger: 'change',
        },
      ],
    },
    {
      field: 'userName',
      label: t('routes.equity.userName'),
      component: 'Input',
      componentProps: {
        maxLength: 100,
      },
      colProps: {
        xl: 6,
        xxl: 6,
      },
    },
    {
      field: 'incrementTypeId',
      component: 'ApiSelect',
      label: t('routes.equity.Type'),
      colProps: {
        xl: 6,
        xxl: 6,
      },
      componentProps: {
        maxLength: 50,
        api: async function () {
          const list = await dincrementList();
          return list.map((ele) => {
            return { name: t(`routes.finance.equityType.${ele.validTimeType}`), value: ele.id };
          });
        },
        numberToString: true,
        labelField: 'name',
        valueField: 'value',
        immediate: true,
      },
    },
  ],
};
export const DownSchemas: Partial<FormProps> = {
  labelWidth: isEn.value ? 125 : 100,
  schemas: [
    {
      field: 'userName',
      label: t('routes.equity.userName'),
      component: 'Input',
      componentProps: {
        maxLength: 100,
      },
      colProps: {
        xl: 6,
        xxl: 6,
      },
    },
    {
      field: 'timeList',
      label: t('routes.equity.timeList'),
      component: 'RangePicker',
      componentProps: {
        maxLength: 100,
        valueFormat: 'YYYY-MM-DD',
        format: 'YYYY-MM-DD',
      },
      colProps: {
        xl: 7,
        xxl: 7,
      },
    },
  ],
};
export const InvoiceSchemas: FormSchema[] = [
  {
    field: 'type',
    component: 'Input',
    label: t('routes.product.types'),
    slot: 'text',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'version',
    component: 'Input',
    label: t('routes.product.version'),
    required: true,
    colProps: {
      span: 24,
    },
    rules: [
      {
        required: true,
        // @ts-ignore
        validator: async (rule, value) => {
          if (!value) {
            return Promise.reject(t('common.inputText') + t('routes.product.version'));
          }
          if (/.*[\u4e00-\u9fa5]+.*$/.test(value)) {
            /* eslint-disable-next-line */
            return Promise.reject('不支持中文字符');
          }
          return Promise.resolve();
        },
        trigger: 'change',
      },
    ],
    componentProps: {
      maxLength: 15,
      onChange: (data) => {
        console.log('data', data);
      },
    },
  },
  {
    field: 'description',
    component: 'InputTextArea',
    required: true,
    label: t('routes.product.description'),
    componentProps: {
      rows: 4,
    },
    colProps: {
      span: 24,
    },
  },
];
