// @ts-ignore: 忽略
import type { FormSchemaGetter } from '#/adapter/form';
// @ts-ignore: 忽略

import dayjs from 'dayjs';
import { ElDatePicker, ElInputNumber } from 'element-plus';

// 抽屉表单配置
export const drawerFormSchema: any | FormSchemaGetter = () => [
  {
    fieldName: 'description',
    label: '填写说明',
    component: 'Input',
    slot: 'description',
  },
  {
    fieldName: 'additiveInventoryTable',
    label: '添加剂库存表格',
    hideLabel: true,
    renderComponentContent: () => {
      return {
        default: () => [],
      };
    },
  },
];

// 默认表格列配置
export const defautTableColumns: any = (formValues: any) => [
  {
    field: 'additiveType',
    title: '添加剂类型',
    width: 120,
    fixed: 'left',
    formatter: ({ row }) => {
      return row.additiveType || '-';
    },
  },
  {
    field: 'additiveModel',
    title: '添加剂型号',
    width: 120,
    editRender: { name: 'input' },
  },
  {
    field: 'initialInventory',
    title: '期初库存',
    width: 120,
    // editRender: { name: 'input' },
    formatter: ({ row }) => {
      return row.initialInventory || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.initialInventory || '-';
      },
      edit: (data: any) => {
        return (
          <ElInputNumber
            controls-position="right"
            max={1_000_000}
            min={0}
            step={1}
            v-model={data.row.initialInventory}
          />
        );
      },
    },
  },
  {
    field: 'purchase',
    title: '进货',
    width: 100,
    editRender: { name: 'input', immediate: true },
    formatter: ({ row }) => {
      return row.purchase || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.purchase || '-';
      },
      edit: (data: any) => {
        const change = async () => {
          const { row } = data;
          row.expectedInventory =
            Number(row.initialInventory || 0) +
            Number(row.purchase || 0) -
            Number(row.useAmount || 0);
        };
        return (
          <ElInputNumber
            controls-position="right"
            max={1_000_000}
            min={0}
            onChange={change}
            step={1}
            v-model={data.row.purchase}
          />
        );
      },
    },
  },
  {
    field: 'useAmount',
    title: '使用',
    width: 100,
    editRender: { name: 'input', immediate: true },
    formatter: ({ row }) => {
      return row.useAmount || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.useAmount || '-';
      },
      edit: (data: any) => {
        const change = async () => {
          const { row } = data;
          row.expectedInventory =
            Number(row.initialInventory || 0) +
            Number(row.purchase || 0) -
            Number(row.useAmount || 0);
        };
        return (
          <ElInputNumber
            controls-position="right"
            max={1_000_000}
            min={0}
            onChange={change}
            step={1}
            v-model={data.row.useAmount}
          />
        );
      },
    },
  },
  {
    field: 'expectedInventory',
    title: '期末应存',
    width: 120,
    // editRender: { name: 'input' },
    formatter: ({ row }) => {
      return row.expectedInventory || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.expectedInventory || '-';
      },
      edit: (data: any) => {
        return (
          <ElInputNumber
            controls-position="right"
            max={1_000_000}
            min={0}
            step={1}
            v-model={data.row.expectedInventory}
          />
        );
      },
    },
  },
  {
    field: 'actualInventory',
    title: '期末实存',
    width: 120,
    editRender: { name: 'input' },
    formatter: ({ row }) => {
      return row.actualInventory || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.actualInventory || '-';
      },
      edit: (data: any) => {
        return (
          <ElInputNumber
            controls-position="right"
            max={1_000_000}
            min={0}
            step={1}
            v-model={data.row.actualInventory}
          />
        );
      },
    },
  },
  {
    field: 'registerDate',
    title: '登记日期',
    width: 180,
    editRender: {
      name: 'input',
    },
    formatter: ({ row }) => {
      return row.registerDate || '-';
    },
    slots: {
      default: (data: any) => {
        return data.row.registerDate || '-';
      },
      edit: (data: any) => {
        const dateRange = (data: Date) => {
          // 判断事件是一号或者 16 号
          const day = dayjs(data).format('D');
          return day !== '1' && day !== '16';
        };
        return (
          <ElDatePicker
            disabledDate={dateRange}
            format="YYYY-MM-DD"
            v-model={data.row.registerDate}
            valueFormat="YYYY-MM-DD"
          />
        );
      },
    },
  },
];
