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

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

/**
 * 油枪自检升数型抽屉表单配置
 */
export const drawerFormSchema: any | FormSchemaGetter = () => [
  {
    fieldName: 'gunCheckDetailList',
    label: '测量信息',
    hideLabel: true,
    renderComponentContent: () => {
      return {
        default: () => [],
      };
    },
  },
];

/**
 * 默认表格列配置
 */
export const defautTableColumns: VxeGridProps['columns'] = [
  {
    field: 'gunNo',
    title: '油枪编号',
    width: 120,
    fixed: 'left',
    // editRender: { name: 'input' },
  },
  {
    field: 'oilType',
    title: '油品种类',
    width: 120,
    // editRender: {
    //   name: 'select',
    //   options: getDictOptions(DictEnum.OIL_PRODUCT),
    //   optionProps: {
    //     label: 'label',
    //     value: 'label',
    //   },
    // },
  },
  {
    field: 'checkType',
    title: '检定形式',
    width: 120,
    editRender: {
      name: 'select',
      options: [
        {
          label: '请选择',
          value: '',
        },
        {
          label: '自检',
          value: '自检',
        },
        {
          label: '强检',
          value: '强检',
        },
      ],
      optionProps: {
        label: 'label',
        value: 'value',
      },
    },
  },
  {
    field: 'machineValueFirst_t',
    title: '油机示值第一次 (L)',
    width: 150,
    children: [
      {
        field: 'machineValueFirst',
        title: 'A',
        width: 150,
        editRender: {
          name: 'input',
          immediate: true,
          defaultValue: 20,
        },
        slots: {
          default: (data: any) => {
            return data.row.machineValueFirst || '-';
          },
          edit: (data: any) => {
            const change = () => {
              const { row } = data;
              // 计算误差率第一次
              row.errorRateFirst =
                row.actualValueFirst && row.actualValueFirst !== 0
                  ? ((row.actualValueFirst - row.machineValueFirst) /
                      row.actualValueFirst) *
                    1000
                  : 0;
              // 计算平均误差
              row.avgErrorRate =
                (row.errorRateFirst + (row.errorRateSecond || 0)) / 2;
            };
            return (
              <ElInputNumber
                controls-position="right"
                max={100}
                min={0}
                onChange={change}
                precision={3}
                step={0.1}
                v-model={data.row.machineValueFirst}
              />
            );
          },
        },
      },
    ],
  },
  {
    field: 'machineValueSecond_t',
    title: '油机示值第二次 (L)',
    width: 150,
    children: [
      {
        field: 'machineValueSecond',
        title: 'B',
        width: 150,
        editRender: {
          name: 'input',
          immediate: true,
          defaultValue: 20,
        },
        slots: {
          default: (data: any) => {
            return data.row.machineValueSecond || '-';
          },
          edit: (data: any) => {
            const change = () => {
              const { row } = data;
              // 计算误差率第二次
              row.errorRateSecond =
                row.actualValueSecond && row.actualValueSecond !== 0
                  ? ((row.actualValueSecond - row.machineValueSecond) /
                      row.actualValueSecond) *
                    1000
                  : 0;
              // 计算平均误差
              row.avgErrorRate =
                ((row.errorRateFirst || 0) + row.errorRateSecond) / 2;
            };
            return (
              <ElInputNumber
                controls-position="right"
                max={100}
                min={0}
                onChange={change}
                precision={3}
                step={0.1}
                v-model={data.row.machineValueSecond}
              />
            );
          },
        },
      },
    ],
  },
  {
    field: 'actualValueFirst_t',
    title: '实测值第一次 (L)',
    width: 150,
    children: [
      {
        field: 'actualValueFirst',
        title: 'C',
        width: 150,
        editRender: {
          name: 'input',
          immediate: true,
        },
        slots: {
          default: (data: any) => {
            return data.row.actualValueFirst || '-';
          },
          edit: (data: any) => {
            const change = () => {
              const { row } = data;
              // 计算误差率第一次
              row.errorRateFirst =
                row.actualValueFirst && row.actualValueFirst !== 0
                  ? ((row.actualValueFirst - row.machineValueFirst) /
                      row.actualValueFirst) *
                    1000
                  : 0;
              // 计算平均误差
              row.avgErrorRate =
                (row.errorRateFirst + (row.errorRateSecond || 0)) / 2;
            };
            return (
              <ElInputNumber
                controls-position="right"
                max={100}
                min={0}
                onChange={change}
                precision={3}
                step={0.1}
                v-model={data.row.actualValueFirst}
              />
            );
          },
        },
      },
    ],
  },
  {
    field: 'actualValueSecond_t',
    title: '实测值第二次 (L)',
    width: 150,
    children: [
      {
        field: 'actualValueSecond',
        title: 'D',
        width: 150,
        editRender: {
          name: 'input',
          immediate: true,
        },
        slots: {
          default: (data: any) => {
            return data.row.actualValueSecond || '-';
          },
          edit: (data: any) => {
            const change = () => {
              const { row } = data;
              // 计算误差率第二次
              row.errorRateSecond =
                row.actualValueSecond && row.actualValueSecond !== 0
                  ? ((row.actualValueSecond - row.machineValueSecond) /
                      row.actualValueSecond) *
                    1000
                  : 0;
              // 计算平均误差
              row.avgErrorRate =
                ((row.errorRateFirst || 0) + row.errorRateSecond) / 2;
            };
            return (
              <ElInputNumber
                controls-position="right"
                max={100}
                min={0}
                onChange={change}
                precision={3}
                step={0.1}
                v-model={data.row.actualValueSecond}
              />
            );
          },
        },
      },
    ],
  },
  {
    field: 'errorRateFirst_t',
    title: '实测误差率第一次 (‰)',
    width: 150,
    children: [
      {
        field: 'errorRateFirst',
        title: 'E=(C-A)/C*1000',
        width: 150,
        formatter: ({ cellValue }: any) => {
          return cellValue ? cellValue.toFixed(3) : '-';
        },
      },
    ],
  },
  {
    field: 'errorRateSecond_t',
    title: '实测误差率第二次 (‰)',
    width: 150,
    children: [
      {
        field: 'errorRateSecond',
        title: 'F=(D-B)/D*1000',
        width: 150,
        formatter: ({ cellValue }: any) => {
          return cellValue ? cellValue.toFixed(3) : '-';
        },
      },
    ],
  },
  {
    field: 'avgErrorRate_t',
    title: '平均误差 (‰)',
    width: 120,
    children: [
      {
        field: 'avgErrorRate',
        title: 'G=(E+F)/2',
        width: 120,
        formatter: ({ cellValue }: any) => {
          return cellValue ? cellValue.toFixed(3) : '-';
        },
      },
    ],
  },
  {
    field: 'remark',
    title: '备注',
    width: 150,
    editRender: { name: 'input' },
  },
];
