<script lang="ts" setup>
// @ts-ignore
import { ElCard } from 'element-plus';

// @ts-ignore
import { useVbenVxeGrid } from '#/adapter/vxe-table';

// 定义组件的Props接口
interface GunCheckProps {
  taskDetail: any;
}

// 接收父组件传递的任务详情数据
const props = defineProps<GunCheckProps>();

// 表格列配置
const tableColumns: any = [
  {
    field: 'gunNo',
    title: '油枪编号',
    width: 120,
    fixed: 'left',
  },
  {
    field: 'oilType',
    title: '油品种类',
    width: 120,
    fixed: 'left',
  },
  {
    field: 'checkType',
    title: '检定形式',
    width: 120,
  },
  {
    field: 'machineValue1_t',
    title: '油机示值第一次',
    width: 150,
    children: [
      {
        field: 'machineValue1',
        title: 'C',
        width: 150,
      },
    ],
  },
  {
    field: 'machineValue2_t',
    title: '油机示值第二次 (L)',
    width: 150,
    children: [
      {
        field: 'machineValue2',
        title: 'D',
        width: 150,
      },
    ],
  },
  {
    field: 'tankScale1_t',
    title: '公平罐刻度第一次 (L)',
    width: 160,
    children: [
      {
        field: 'tankScale1',
        title: 'E',
        width: 160,
      },
    ],
  },
  {
    field: 'tankScale2_t',
    title: '公平罐刻度第二次 (L)',
    width: 160,
    children: [
      {
        field: 'tankScale2',
        title: 'F',
        width: 160,
      },
    ],
  },
  {
    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,
  },
];

// 初始化表格配置
const gridOptions: any = {
  size: 'medium',
  toolbarConfig: {
    custom: false,
    refresh: false,
    zoom: false,
  },
  pagerConfig: {
    enabled: false,
  },
  proxyConfig: {
    ajax: {
      query: async () => {
        // 处理数据，确保计算列正确
        const items = props.taskDetail?.heights || [];
        // 计算误差率和平均误差
        // items.forEach((item: any) => {
        //   // 计算误差率第一次
        //   item.errorRateFirst =
        //     item.actualValueFirst && item.actualValueFirst !== 0
        //       ? ((item.actualValueFirst - item.machineValueFirst) /
        //           item.actualValueFirst) *
        //         1000
        //       : 0;

        //   // 计算误差率第二次
        //   item.errorRateSecond =
        //     item.actualValueSecond && item.actualValueSecond !== 0
        //       ? ((item.actualValueSecond - item.machineValueSecond) /
        //           item.actualValueSecond) *
        //         1000
        //       : 0;

        //   // 计算平均误差
        //   item.avgErrorRate = (item.errorRateFirst + item.errorRateSecond) / 2;
        // });
        return {
          items,
        };
      },
    },
  },
  columns: tableColumns,
};

// 使用BasicTable组件
const [BasicTable] = useVbenVxeGrid({
  gridOptions,
});
</script>

<template>
  <ElCard>
    <template #header>
      <div class="flex items-center gap-4">
        <div style="width: 4px; height: 12px; background-color: #215acd"></div>
        <span
          class="text-lg font-bold text-gray-800"
          style="font-size: 14px; font-weight: 600"
        >
          油枪自检高度型详情
        </span>
      </div>
    </template>

    <div class="task-info">
      <!-- <div class="section-title">
        <div class="section-bar"></div>
        <div class="section-text">测量信息</div>
      </div> -->

      <BasicTable table-title="" class="mt-4 w-full" />
    </div>
  </ElCard>
</template>

<style scoped lang="scss">
.task-info {
  .section-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 20px 0 12px 0;

    .section-bar {
      width: 4px;
      height: 12px;
      background-color: #215acd;
    }

    .section-text {
      font-size: 14px;
      font-weight: 600;
      color: var(--text-color-primary);
    }
  }

  :deep(.el-descriptions) {
    margin-bottom: 0 !important;
  }

  :deep(.el-descriptions__label) {
    font-size: 14px;
    font-weight: 400;
    color: var(--text-color-secondary);
  }

  :deep(.el-descriptions__content) {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-color-primary);
  }

  :deep(.el-descriptions__table) {
    background: transparent !important;
  }

  :deep(.el-table) {
    margin-top: 12px;
  }

  :deep(.el-table__header-wrapper) {
    background-color: #f5f7fa;
  }

  :deep(.el-table th) {
    font-weight: 600;
    color: var(--text-color-primary);
  }
}
</style>
