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

// @ts-ignore: 忽略
import { ElInputNumber } from 'element-plus';

/**
 * 油枪自检升数型抽屉表单配置
 */
export const drawerFormSchema: any | FormSchemaGetter = () => [
  {
    component: 'InputNumber',
    fieldName: 'neckHeight',
    label: '计量器颈部标准高度（mm）A',
    componentProps: {
      placeholder: '请输入计量器颈部标准高度',
      controlsPosition: 'right',
      // max: 1000,
      min: 0,
      precision: 1,
      step: 0.1,
    },
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'neckUnitVolume',
    label: '颈部单位刻度的油品数量（ml/mm）B',
    componentProps: {
      placeholder: '请输入颈部单位刻度的油品数量',
      controlsPosition: 'right',
      max: 100,
      min: -10,
      precision: 1,
      step: 0.1,
    },
    rules: 'required',
  },
  {
    fieldName: 'gunCheckDetailList',
    label: '测量信息',
    hideLabel: true,
    renderComponentContent: () => {
      return {
        default: () => [],
      };
    },
  },
];

/**
 * 默认表格列配置
 */
export const defautTableColumns: any = (formValues: any) => {
  return [
    {
      field: 'gunNo',
      title: '油枪编号',
      width: 120,
      fixed: 'left',
    },
    {
      field: 'oilType',
      title: '油品种类',
      width: 120,
    },
    {
      field: 'checkType',
      title: '检定形式',
      width: 120,
      editRender: {
        name: 'select',
        options: [
          {
            label: '请选择',
            value: '',
          },
          {
            label: '自检',
            value: '自检',
          },
          {
            label: '强检',
            value: '强检',
          },
        ],
        optionProps: {
          label: 'label',
          value: 'value',
        },
      },
    },
    {
      field: 'machineValue1_t',
      title: '油机示值第一次',
      width: 150,
      children: [
        {
          field: 'machineValue1',
          title: 'C',
          width: 150,
          editRender: {
            name: 'input',
            immediate: true,
          },
          slots: {
            default: (data: any) => {
              return data.row.machineValue1 || '-';
            },
            edit: (data: any) => {
              const change = async () => {
                const { row } = data;
                // G=(E-A)*B/1000 + C
                const { neckHeight, neckUnitVolume } = await formValues();
                // 计算实际值1
                row.actualValue1 =
                  ((row.tankScale1 || 0 - neckHeight || 0) * neckUnitVolume ||
                    0) /
                    1000 +
                  (row.machineValue1 || 0);

                // 计算误差率1: I=(G-C)/G*1000
                row.errorRate1 = Number(
                  (row.actualValue1 && row.actualValue1 !== 0
                    ? ((row.actualValue1 - (row.machineValue1 || 0)) /
                        row.actualValue1) *
                      1000
                    : 0
                  ).toFixed(3),
                );

                // 计算平均误差: K=(I+J)/2
                row.avgErrorRate = Number(
                  (
                    (Number(row.errorRate1 || 0) +
                      Number(row.errorRate2 || 0)) /
                    2
                  ).toFixed(3),
                );

                // 计算合计检测升数: L=C+D
                row.totalCheckVolume = Number(
                  ((row.machineValue1 || 0) + (row.machineValue2 || 0)).toFixed(
                    3,
                  ),
                );
              };
              return (
                <ElInputNumber
                  controls-position="right"
                  max={100}
                  min={0}
                  onChange={change}
                  precision={3}
                  step={0.1}
                  v-model={data.row.machineValue1}
                />
              );
            },
          },
        },
      ],
    },
    {
      field: 'machineValue2_t',
      title: '油机示值第二次 (L)',
      width: 150,
      children: [
        {
          field: 'machineValue2',
          title: 'D',
          width: 150,
          editRender: {
            name: 'input',
            immediate: true,
          },
          slots: {
            default: (data: any) => {
              return data.row.machineValue2 || '-';
            },
            edit: (data: any) => {
              const change = async () => {
                const { row } = data;
                // H=(F-A)*B/1000 + D
                const { neckHeight, neckUnitVolume } = await formValues();
                // 计算实际值2
                row.actualValue2 = Number(
                  ((row.tankScale2 || 0 - neckHeight || 0) * neckUnitVolume ||
                    0) /
                    1000 +
                    (row.machineValue2 || 0),
                ).toFixed(3);

                // 计算误差率2: J=(H-D)/H*1000
                // 计算误差率2: J=(H-D)/H*1000
                // 计算误差率2: J=(H-D)/H*1000
                row.errorRate2 = Number(
                  (row.actualValue2 && row.actualValue2 !== 0
                    ? ((row.actualValue2 - (row.machineValue2 || 0)) /
                        row.actualValue2) *
                      1000
                    : 0
                  ).toFixed(3),
                );

                // 计算平均误差: K=(I+J)/2
                row.avgErrorRate = Number(
                  (
                    (Number(row.errorRate1 || 0) +
                      Number(row.errorRate2 || 0)) /
                    2
                  ).toFixed(3),
                );

                // 计算合计检测升数: L=C+D
                row.totalCheckVolume = Number(
                  ((row.machineValue1 || 0) + (row.machineValue2 || 0)).toFixed(
                    3,
                  ),
                );
              };
              return (
                <ElInputNumber
                  controls-position="right"
                  max={100}
                  min={0}
                  onChange={change}
                  precision={3}
                  step={0.1}
                  v-model={data.row.machineValue2}
                />
              );
            },
          },
        },
      ],
    },
    {
      field: 'tankScale1_t',
      title: '公平罐刻度第一次 (L)',
      width: 160,
      children: [
        {
          field: 'tankScale1',
          title: 'E',
          width: 160,
          editRender: {
            name: 'input',
            immediate: true,
          },
          slots: {
            default: (data: any) => {
              return data.row.tankScale1 || '-';
            },
            edit: (data: any) => {
              const change = async () => {
                const { row } = data;
                // G=(E-A)*B/1000 + C
                const { neckHeight, neckUnitVolume } = await formValues();
                // 计算实际值1
                row.actualValue1 = Number(
                  ((row.tankScale1 || 0 - neckHeight || 0) * neckUnitVolume ||
                    0) /
                    1000 +
                    (row.machineValue1 || 0),
                ).toFixed(3);

                // 计算误差率1: I=(G-C)/G*1000
                row.errorRate1 =
                  row.actualValue1 && row.actualValue1 !== 0
                    ? Number(
                        ((row.actualValue1 - (row.machineValue1 || 0)) /
                          row.actualValue1) *
                          1000,
                      ).toFixed(3)
                    : 0;

                // 计算平均误差: K=(I+J)/2
                row.avgErrorRate = Number(
                  (
                    (Number(row.errorRate1 || 0) +
                      Number(row.errorRate2 || 0)) /
                    2
                  ).toFixed(3),
                );
              };
              return (
                <ElInputNumber
                  controls-position="right"
                  max={1000}
                  min={0}
                  onChange={change}
                  precision={3}
                  step={0.1}
                  v-model={data.row.tankScale1}
                />
              );
            },
          },
        },
      ],
    },
    {
      field: 'tankScale2_t',
      title: '公平罐刻度第二次 (L)',
      width: 160,
      children: [
        {
          field: 'tankScale2',
          title: 'F',
          width: 160,
          editRender: {
            name: 'input',
            immediate: true,
          },
          slots: {
            default: (data: any) => {
              return data.row.tankScale2 || '-';
            },
            edit: (data: any) => {
              const change = async () => {
                const { row } = data;
                // H=(F-A)*B/1000 + D
                const { neckHeight, neckUnitVolume } = await formValues();
                // 计算实际值2
                row.actualValue2 = Number(
                  ((row.tankScale2 || 0 - neckHeight || 0) * neckUnitVolume ||
                    0) /
                    1000 +
                    (row.machineValue2 || 0),
                ).toFixed(3);

                // 计算误差率2: J=(H-D)/H*1000
                // 计算误差率2: J=(H-D)/H*1000
                row.errorRate2 = Number(
                  (row.actualValue2 && row.actualValue2 !== 0
                    ? ((row.actualValue2 - (row.machineValue2 || 0)) /
                        row.actualValue2) *
                      1000
                    : 0
                  ).toFixed(3),
                );

                // 计算平均误差: K=(I+J)/2
                row.avgErrorRate = Number(
                  (
                    (Number(row.errorRate1 || 0) +
                      Number(row.errorRate2 || 0)) /
                    2
                  ).toFixed(3),
                );
              };
              return (
                <ElInputNumber
                  controls-position="right"
                  max={1000}
                  min={0}
                  onChange={change}
                  precision={3}
                  step={0.1}
                  v-model={data.row.tankScale2}
                />
              );
            },
          },
        },
      ],
    },
    {
      field: 'actualValue1_t',
      title: '实测值第一次 (L)',
      width: 150,
      children: [
        {
          field: 'actualValue1',
          title: 'G=(E-A)*B/1000+C',
          width: 150,
        },
      ],
    },
    {
      field: 'actualValue2_t',
      title: '实测值第二次 (L)',
      width: 150,
      children: [
        {
          field: 'actualValue2',
          title: 'H=(F-A)*B/1000+D',
          width: 150,
        },
      ],
    },
    {
      field: 'errorRate1_t',
      title: '实测误差率第一次 (‰)',
      width: 150,
      children: [
        {
          field: 'errorRate1',
          title: 'I=(G-C)/G*1000',
          width: 150,
        },
      ],
      formatter: ({ cellValue }: any) => {
        return cellValue ? cellValue.toFixed(3) : '-';
      },
    },
    {
      field: 'errorRate2_t',
      title: '实测误差率第二次 (‰)',
      width: 150,
      children: [
        {
          field: 'errorRate2',
          title: 'J=(H-D)/H*1000',
          width: 150,
        },
      ],
      formatter: ({ cellValue }: any) => {
        return cellValue ? cellValue.toFixed(3) : '-';
      },
    },
    {
      field: 'avgErrorRate_t',
      title: '平均误差 (‰)',
      width: 120,
      children: [
        {
          field: 'avgErrorRate',
          title: 'K=(I+J)/2',
          width: 120,
        },
      ],
      formatter: ({ cellValue }: any) => {
        return cellValue ? cellValue.toFixed(3) : '-';
      },
    },
    {
      field: 'totalCheckVolume_t',
      title: '合计检测升数 (L)',
      width: 140,
      children: [
        {
          field: 'totalCheckVolume',
          title: 'L=C+D',
          width: 140,
        },
      ],
      formatter: ({ cellValue }: any) => {
        return cellValue ? cellValue.toFixed(3) : '-';
      },
    },
    {
      field: 'remark',
      title: '备注',
      width: 150,
      editRender: { name: 'input' },
    },
  ];
};
