<template>
  <div class="performance-page">
    <BaseTable
      v-if="showTableContent"
      ref="performanceTableRef"
      :head="tableColumns"
      :tableName="checkTableName"
      hidePagination
    >
      <template #performanceInfo="{ row }">
        {{ row.performanceInfo }}
      </template>

    </BaseTable>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, nextTick } from 'vue';
import BaseTable from '@/components/common/table/baseTable.vue';
import { objectTypeArrayRemovesDuplicates } from '@/utils/common';
import { useDict } from '@/utils/useDict';
// import { getTaskAchieveStyleClass } from '@/components/common/statusDot/statusMap';
// import StatusDot from '@/components/common/statusDot/index.vue';

const { PERFORMANCE_UNIT, RESULT_ACHIEVE_STATUS } = useDict(
  'PERFORMANCE_UNIT',
  'RESULT_ACHIEVE_STATUS'
);

const props = defineProps({
  checkTaskInfo: {
    type: Array,
    default: () => [],
  },
  checkTableName: {
    type: String,
    default: '',
  },
  cloumnWidth: {
    type: String,
    default: '',
  },
});

const performanceTableRef = ref();
const showTableContent = ref(false);
const tableData = ref<any>([]);
const tableColumns = ref<any>([]);

const modelAttribute = ref<any>([]);

const getTableColumnsFun = (data: any) => {
  const performanceDatas = data || [];

  for (let i = 0; i < performanceDatas.length; i++) {
    performanceDatas[i].completeStatus = getPerformanceSTatus(performanceDatas[i]) || 0;
  }
  const names = performanceDatas?.map((item: any) => {
    return item.taskName;
  });

  const taskNames = Array.from(new Set(names));
  modelAttribute.value = performanceDatas.map((item: any) => {
    const nodeName = item.nodeName || '';
    const method = item.method || '';
    const targetValue = item.targetValue ?? '';
    const unitLabel = PERFORMANCE_UNIT.value?.O?.[item.unit] || item.unit || '';
    const obj: any = {
      name: `${nodeName} (${method} ${targetValue} ${unitLabel})`,
      value: item.uuid,
    };

    obj[item.uuid] = item.highValue;
    return obj;
  });

  const defaultColumns = [
    {
      title: '指标信息',
      key: 'performanceInfo',
      isShow: true,
    },
    {
      title: '达成情况',
      key: 'completeStatus',
      isShow: false,
    },
  ];
  const list: any = data || [];

  showTableContent.value = false;
  tableData.value = [];
  tableColumns.value = [];
  let modelcolumns: any = [];
  for (let j = 0; j < taskNames.length; j++) {
    for (let i = 0; i < list.length; i++) {
      if (list[i].taskName === taskNames[j]) {
        const obj: any = {
          title: `${list[i].taskName}`,
          key: `${list[i].uuid}`,
          isShow: true,
        };
        modelcolumns.push(obj);
      }
    }
  }
  modelcolumns = objectTypeArrayRemovesDuplicates(modelcolumns, 2);
  tableColumns.value = defaultColumns.concat(modelcolumns);
  if (props.cloumnWidth) {
    for (let i = 0; i < tableColumns.value.length; i++) {
      tableColumns.value[i].width = props.cloumnWidth;
    }
  }
  for (let i = 0; i < modelAttribute.value.length; i++) {
    const obj: any = {};
    for (let j = 0; j < tableColumns.value.length; j++) {
      if (tableColumns.value[j].key === 'performanceInfo') {
        obj[tableColumns.value[j].key] = modelAttribute.value[i].name;
      } else {
        obj[tableColumns.value[j].key] = modelAttribute.value[i][tableColumns.value[j].key];
      }
    }

    tableData.value.push(obj);
  }

  showTableContent.value = true;
  nextTick(() => {
    performanceTableRef.value.setDataFun(tableData.value);
  });
};

const getPerformanceSTatus = (row: any) => {
  let status: any = 0;
  // 当指标值，达标方式，目标值有一个不存在时，返回状态未分析
  if (!row?.method || !row?.highValue || !row?.targetValue) {
    return status;
  }

  if (row?.method && row?.highValue && row?.targetValue) {
    const highValue = Number(row?.highValue);
    const targetValue = Number(row?.targetValue);
    const lowValue = Number(row?.lowValue);

    // 小于等于
    if (row.method === '≤') {
      if (targetValue <= highValue) {
        status = 2;
      } else {
        status = 1;
      }
    }

    // 小于
    if (row.method === '<') {
      if (targetValue < highValue) {
        status = 2;
      } else {
        status = 1;
      }
    }
    // 大于
    if (row.method === '>') {
      if (targetValue > highValue) {
        status = 2;
      } else {
        status = 1;
      }
    }
    // 大于等于
    if (row.method === '≥') {
      if (targetValue >= highValue) {
        status = 2;
      } else {
        status = 1;
      }
    }
    // 包含
    if (row.method === '[]') {
      if (targetValue <= highValue && targetValue >= lowValue) {
        status = 2;
      } else {
        status = 1;
      }
    }
  }

  return status;
};

watch(
  () => props.checkTaskInfo,
  (newVal) => {
    getTableColumnsFun(newVal);
  },
  {
    immediate: true,
    deep: true,
  }
);
</script>

<style lang="scss" scoped>
.performance-page {
  width: 100%;
  height: 100%;
}
</style>
