<template>
  <ReportModule tableOrChart="qita" :showHeader="false" :showTitle="false" :showPrintBtn="false" :showExportBtn="false"
    :showDescribe="false">
    <template #module_qita>
      <ModuleTab :tabList="state.tabList" @TabChange="TabChange"></ModuleTab>
    </template>
  </ReportModule>
  <ReportModule :titleList="['1、平均分分析图']" tableOrChart="chart" :showPrintBtn="false" :showExportBtn="false">
    <template #title_right>
      <EchartType v-if="state.tabActive == 'rateAll'" :chartTypeList="state.fiveRateData.chartTypeList"
        :current="state.fiveRateData.chartType" @ChangeEchartType="(val) => ChangeEchartType(val, 'fiveRateData')" />
      <EchartType v-if="
        state.tabActive == '2' ||
        state.tabActive == 'high' ||
        state.tabActive == 'good' ||
        state.tabActive == 'fine' ||
        state.tabActive == 'pass' ||
        state.tabActive == 'low'
      " :chartTypeList="state.analysisData.chartTypeList" :current="state.analysisData.chartType"
        @ChangeEchartType="(val) => ChangeEchartType(val, 'analysisData')" />
    </template>
    <template #module_table_chart>
      <template v-if="state.analysisData.tableData.length > 0">
        <template v-if="state.tabActive == 'rateAll'">
          <!-- 五率综合分析图 -->
          <template v-if="state.fiveRateData.chartType == 'stacked_chart'">
            <BarStackChartVertical :data="state.fiveRateData.data" :tooltipData="state.fiveRateData.tooltipData"
              :color="state.fiveRateData.color">
            </BarStackChartVertical>
          </template>
          <template v-if="state.fiveRateData.chartType == 'vertical_bar'">
            <BarsCharts :data="state.fiveRateData.data" :fiveTooltipData="state.fiveRateData.tooltipData"
              :color="state.fiveRateData.color" unit="%"></BarsCharts>
          </template>
          <template v-if="state.fiveRateData.chartType == 'horizontal_bar'">
            <BarStackChartHorizontal :data="state.fiveRateData.data" :tooltipData="state.fiveRateData.tooltipData"
              :color="state.fiveRateData.color">
            </BarStackChartHorizontal>
          </template>
        </template>
        <template v-else>
          <!-- 平均分  得分率-->
          <template v-if="state.tabActive == 0">
            <BarChart :datax="state.analysisData.datax" :tooltipData="state.analysisData.tooltipData"
              :typeName="state.groupTitle" :datay="state.analysisData.datay" :unit="state.analysisData.unit"
              :color="state.analysisData.color" :markLineData="state.analysisData.markLineData"></BarChart>
          </template>
          <!-- 标准分 超均分  超均率-->
          <template v-else-if="
            state.tabActive == 1 ||
            state.tabActive == 3 ||
            state.tabActive == 4
          ">
            <DifferenceChart :datax="state.analysisData.datax" :tooltipData="state.analysisData.tooltipData"
              :unit="state.tabActive == 4 ? '%' : ''" :datay="state.analysisData.datay" :rate="0"></DifferenceChart>
          </template>
          <!-- 得分率 -->
          <template v-else-if="state.tabActive == 2">
            <template v-if="state.analysisData.chartType == 'vertical_bar'">
              <BarScoringRateVertical v-if="state.analysisData.datax.length > 0" :datax="state.analysisData.datax"
                :tooltipData="state.analysisData.tooltipData" :typeName="state.groupTitle"
                :datay="state.analysisData.dataStackY" unit="%" :color="['#3BA272', '#EE6666']"
                :average="state.analysisData.average" :markLineData="state.analysisData.markLineData">
              </BarScoringRateVertical>
            </template>
            <template v-if="state.analysisData.chartType == 'horizontal_bar'">
              <BarScoringRateHorzontal v-if="state.analysisData.datax.length > 0" :datax="state.analysisData.datax"
                :datay="state.analysisData.dataStackY" :tooltipData="state.analysisData.tooltipData"
                :typeName="state.groupTitle" :color="['#3BA272', '#EE6666']" :average="state.analysisData.average"
                :markLineData="state.analysisData.markLineData"></BarScoringRateHorzontal>
            </template>
          </template>
          <template v-else>
            <template v-if="state.analysisData.chartType == 'vertical_bar'">
              <BarChart :datax="state.analysisData.datax" :tooltipData="state.analysisData.tooltipData"
                :typeName="state.groupTitle" :datay="state.analysisData.datay" unit="%"
                :color="state.analysisData.color" :markLineData="state.analysisData.markLineData"></BarChart>
            </template>
            <template v-if="state.analysisData.chartType == 'horizontal_bar'">
              <BarChartHorizontal :datax="state.analysisData.datax" :datay="state.analysisData.datay"
                :tooltipData="state.analysisData.tooltipData" :typeName="state.groupTitle"
                :color="state.analysisData.color" :markLineData="state.analysisData.markLineData"></BarChartHorizontal>
            </template>
          </template>
        </template>
      </template>
      <div v-else class="no_content_data" v-loading="state.analysisLoading" :element-loading-text="state.loadingText"
        element-loading-spinner="el-icon-loading" element-loading-background="#ffffff">
        <span>暂无数据</span>
      </div>
    </template>
    <template #module_describe v-html="state.analysisData.expandableText"></template>
  </ReportModule>
  <ReportModule :titleList="['2、平均分分析表']" tableOrChart="table" :showPrintBtn="false" :showDescribe="false" :currentPage="state.analysisData.pageNum" :pageSize="state.analysisData.pageSize"
    :total="state.analysisData.total" @update:pageSize="handleSizeChange" @update:currentPage="handleCurrentChange">
    <template #module_table_chart>
      <el-table :data="analysisTableData" border stripe align="left">
        <template v-for="(header, headerIndex) in state.analysisData.staticHeader">
          <el-table-column align="center" :prop="header.prop" :label="header.label" show-overflow-tooltip
            v-if="header.label == '班级'" width="150" fixed="left" :key="`${header.prop}_${headerIndex}`">
            <template #default="scope">
              {{ scope.row[header.prop] || scope.row.keyName }}
            </template>
          </el-table-column>
          <el-table-column align="center" :prop="header.prop" :label="header.label" show-overflow-tooltip
            v-else-if="header.label == '教师'" fixed="left" :key="`${header.prop}_${headerIndex}`">
            <template #default="scope">
              {{ scope.row[header.prop] || '-' }}
            </template>
          </el-table-column>
          <el-table-column align="center" :prop="header.prop" :label="header.label" show-overflow-tooltip
            v-else-if="header.label == '科目' || header.label == '序号'" fixed="left" :key="`${header.prop}_${headerIndex}`">
          </el-table-column>
          <el-table-column align="center" :prop="header.prop" :label="header.label" show-overflow-tooltip
            v-else-if="header.label == '平均分'" :key="`${header.prop}_${headerIndex}`">
            <template #default="scope">
              {{ scope.row.fiveRateResultVo?.[header.prop] || '-' }}
            </template>
          </el-table-column>
          <el-table-column align="center" :prop="header.prop" :label="header.label" v-else
            :key="`${header.prop}_${headerIndex}`">
            <template #default="scope">
              {{ scope.row.fiveRateResultVo?.[header.prop] || '-' }}<span
                v-if="header.label.includes('率') && scope.row.fiveRateResultVo?.[header.prop] && scope.row.fiveRateResultVo?.[header.prop] != '-'">%</span>
            </template>
          </el-table-column>
        </template>

        <el-table-column align="center" :label="`${header.name}`"
          v-for="(header, headerIndex) in state.analysisData.dynamicsHeader" :key="`${header.prop}_${headerIndex}`">
          <el-table-column align="center" :label="child.label" v-for="(child, childIndex) in header.headerList"
            width="120" :key="childIndex">
            <template #default="scope">
              <template v-if="child.prop.includes('Num')">
                <span class="table_row_blue" @click="OpenStudentDialog(scope.row, header, child)"
                  v-if="scope.row.fiveRateResultVo?.[child.prop] > 0 && scope.row?.isNumClick">
                  {{ scope.row.fiveRateResultVo?.[child.prop] }}
                </span>
                <span v-else>{{ scope.row?.fiveRateResultVo?.[child.prop] ?? 0 }}</span>
              </template>
              <template v-else>
                {{ scope.row.fiveRateResultVo?.[child.prop] || '-' }}<span
                  v-if="child.prop.includes('Rate') && !child.prop.includes('Rank')">%</span>
              </template>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </template>
  </ReportModule>
</template>
<script lang="ts" setup>
import ReportModule from "@/components/ReportModule.vue";
import ModuleTab from "@/components/ModuleTab.vue";
import EchartType from "@/components/EchartType.vue";
import BarChart from "@/components/echarts/barChart.vue";//单柱状图组件
import BarChartHorizontal from '@/components/echarts/barHorizontal.vue';//横向柱状图
import BarScoringRateVertical from '@/components/echarts/barScoringRate_vertical.vue';//得分率 纵向柱状图
import BarScoringRateHorzontal from '@/components/echarts/barScoringRate_horzontal.vue';//得分率 横向柱状图
import BarStackChartVertical from "@/components/echarts/barStackChart_vertical.vue";//竖向堆叠条形图
import DifferenceChart from "@/components/echarts/differenceChart.vue"; //率差图
import { onMounted, reactive, computed,watch } from "vue";
import { useAnalysisStore } from "@/store/analysis";
import { classContrastSubjectTable } from "@/api/analysis";
const analysisStore = useAnalysisStore();
const state = reactive({
  tabList: [
    {
      title: "平均分",
      value: "0",
    },
    {
      title: "标准分",
      value: "1",
    },
    {
      title: "得分率",
      value: "2",
    },
    {
      title: "超均分",
      value: "3",
    },
    {
      title: "超均率",
      value: "4",
    },
  ], //固定的 0平均分 1标准分 2得分率 3超均分 4超均率
  tabActive: "0", //选择的tab
  fiveRateData: {
    tooltipData: [],
    chartData: [],
    chartTypeList: [
      {
        label: "堆积图",
        value: "stacked_chart",
      },
      {
        label: "柱状图",
        value: "vertical_bar",
      },
      {
        label: "条形图",
        value: "horizontal_bar",
      },
    ],
    chartType: "stacked_chart", //图类型  默认堆积图 stacked_chart
    data: [], //数据集
    color: ["#5470C6", "#3BA272", "#FAC858", "#995FB3", "#EE6666"], //对应颜色值
  }, //班级对比五率数据
  analysisData: {
    chartTypeList: [
      {
        label: "柱状图",
        value: "vertical_bar",
      },
      {
        label: "条形图",
        value: "horizontal_bar",
      },
    ],
    chartType: "vertical_bar", //图表类型 默认柱状图
    datax: [], //x轴数据
    datay: [], //y轴数据
    dataStackY: [], //得分率y轴数据
    color: "#5470C6", //柱状图颜色
    unit: "", //单位
    tooltipData: [],
    chartData: [], //图标的原始数据  年级在前
    chartDatas: [],
    avgScoreChartData: [],
    tableData: [], //班级分析表的原始数据  年级在后
    tableDatas: [], //班级分析表展示的表格数据
    headerData: {
      static: [], //静态表头
      dynamisc: [], //动态表头
      child: [], //子级表头
    }, //班级分析表头总数据
    staticHeader: [], //班级分析静态表头数据
    dynamicsHeader: [], //动态表头数据
    childHeader: [], //子级表头数据
    pageSize: 10, //每页显示数据
    total: 0, //总数
    pageNum: 1, //当前页
    expandableText: "", //说明文案
  }, //班级分析分析表数据
  analysisLoading: false, //班级分析表加载中
  loadingText: "加载中,请稍后……",
  rankType: 2, // 超均分/超均率 联1 校2切换
  groupTitle: "平均分", //默认平均分
});
//班级分析表表格数据
const analysisTableData = computed(() => {
  const { tableDatas, pageSize, pageNum } = state.analysisData;
  const start = (pageNum - 1) * pageSize;
  const end = start + pageSize;
  return tableDatas.slice(start, end);
})
//切换tab
const TabChange = (item: Object) => {
  state.tabActive = item.value;
  state.groupTitle = item.title;
};
const ChangeEchartType = (val: string | number, prop) => {
  state[prop].chartType = val;
};
const GetSingleClassAnalysisData = () => {
  let params = {
    ...analysisStore.filterObject,
    titleType: 0,
  };
  state.analysisLoading = true;
  classContrastSubjectTable(params).then((res) => {
    if (res.code == 200) {
      let chartData = []; //图表原始数据  图标原始数据 需要年级在最前
      let tableData = []; //表格原始数据  表格原始数据 需要年级在最后
      let tableDatas = []; //显示各班的表格数据
      if (res.data.classDataList && res.data.classDataList.length > 0) {
        if (analysisStore.filterObject.schoolLevel == 0) {
          res.data.examDataList &&
            res.data.examDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
                isShow: true,
              });
            });
        }
        if (analysisStore.filterObject.schoolLevel == 1) {
          //联校
          res.data.examDataList &&
            res.data.examDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
              });
            });
          //组合校
          res.data.schoolGroupDataList &&
            res.data.schoolGroupDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
                isShow: true,
              });
            });
        }
        if (analysisStore.filterObject.schoolLevel == 2) {
          //联校
          res.data.examDataList &&
            res.data.examDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
              });
            });
          //组合校
          res.data.schoolGroupDataList &&
            res.data.schoolGroupDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
              });
            });
          res.data.schoolDataList &&
            res.data.schoolDataList.forEach((item) => {
              chartData.push({
                ...item,
                isLine: true,
                isShow: analysisStore.filterObject.classLevel != 1,
              });
            });
        }
        // 组合班级
        res.data.classGroupDataList &&
          res.data.classGroupDataList.forEach((item) => {
            chartData.push({
              ...item,
              isLine: analysisStore.filterObject.classLevel == 1,
              isShow: analysisStore.filterObject.classLevel == 1,
            });
          });
        // 单班
        res.data.classDataList &&
          res.data.classDataList.forEach((item, index) => {
            chartData.push(item);
            item.subjectList &&
              item.subjectList.forEach((i, key) => {
                if (key == 0) {
                  i.index = index + 1;
                }
                tableData.push(i);
                tableDatas.push(i);
              });
          });

        // 组合班级
        res.data.classGroupDataList &&
          res.data.classGroupDataList.forEach((item) => {
            item.subjectList &&
              item.subjectList.forEach((i, key) => {
                if (key == 0) {
                  i.index = "-";
                }
                tableDatas.push(i);
                tableData.push(i);
              });
          });
        // 单校
        res.data.schoolDataList &&
          res.data.schoolDataList.forEach((item) => {
            item.subjectList &&
              item.subjectList.forEach((i, key) => {
                if (key == 0) {
                  i.index = "-";
                }
                tableDatas.push(i);
                tableData.push(i);
              });
          });
        // 组合学校
        res.data.schoolGroupDataList &&
          res.data.schoolGroupDataList.forEach((item) => {
            item.subjectList &&
              item.subjectList.forEach((i, key) => {
                if (key == 0) {
                  i.index = "-";
                }
                tableDatas.push(i);
                tableData.push(i);
              });
          });
        // 联校
        res.data.examDataList &&
          res.data.examDataList.forEach((item) => {
            item.subjectList &&
              item.subjectList.forEach((i, key) => {
                if (key == 0) {
                  i.index = "-";
                }
                tableDatas.push(i);
                tableData.push(i);
              });
          });
      } else {
        tableDatas = [];
        tableData = [];
        chartData = [];
      }

      let list = [...tableDatas];
      console.log(list, 2121221)
      tableDatas = list
        .filter((item) => item.showCharts)
        .map((item, index) => {
          if (item.schoolLevel == 2 && item.classLevel == 2) {
            item.index = index + 1;
          } else {
            item.index = "-";
          }
          return item;
        });

      let childHeaderList = []; //子级表头
      let dynamicsHeader = []; //动态表头
      res.data.fiveRateHeaderList.forEach((item) => {
        let child = [];
        item.headerList.forEach((childItem) => {
          if (childItem.display) {
            child.push(childItem);
          }
          childHeaderList.push(childItem);
        });
        let dynamicsItem = {
          name: item.name,
          value: item.value,
          prop: item.prop || "",
          headerList: child,
        };

        dynamicsHeader.push(dynamicsItem);
      });
      state.analysisData = {
        chartTypeList: [
          {
            label: "柱状图",
            value: "vertical_bar",
          },
          {
            label: "条形图",
            value: "horizontal_bar",
          },
        ],
        chartType: "vertical_bar", //图表类型 默认柱状图
        datax: [], //x轴数据
        datay: [], //y轴数据
        average: 0, //平均分
        markLineData: [],
        headerData: {
          static: res.data.headerList, //静态表头
          dynamisc: res.data.fiveRateHeaderList, //动态表头
          child: childHeaderList, //子级表头
        }, //班级分析表头总数据
        tooltipData: [], //悬浮弹窗的数据
        chartData: chartData, //班级分析图的原始数据
        chartDatas: [],
        tableData: tableData, //班级分析表格原始数据
        tableDatas: tableDatas, //班级分析表格展示数据
        staticHeader: (res.data.headerList || []).filter(
          (header) => header.display === true,
        ), //班级分析静态表头数据
        dynamicsHeader: dynamicsHeader, //动态表头数据
        childHeader: (res.data.childHeaderList || []).filter(
          (child) => child.display === true,
        ), //子级表头数据
        pageSize: 10, //每页显示数据
        total: tableDatas.length, //总数
        pageNum: 1, //当前页
        expandableText: "", //文案内容
      };
      state.analysisLoading = false;
      ChangeChartBarData(); //设置班级分析图数
    }
  });
};
//切换分析图  数据
const ChangeChartBarData = () => {
  let datax = []; //x轴数据
  let datay = []; //y轴数据
  let dataLoseRate = []; // 得分率堆叠图图 显示失分率
  let average = 0; //辅助线值
  let markLineData = []; //辅助线值
  let color = "#5470C6"; //柱状图颜色
  let maxClassName = ""; //平均分最高的班级
  let minClassName = ""; //平均分最低的班级
  let maxScore = 0; // 初始化为一个极小值
  let minScore = 750; //初始化为一个最大值
  let expandableText = ""; //说明文案
  let tooltipData = []; //悬浮弹窗的数据
  let unit = ""; //单位

  let chartData = [];
  state.analysisData.chartData.forEach((item) => {
    if (item.totalSubject) {
      chartData.push({
        ...item.totalSubject,
        isLine: item.isLine,
        isShow: item.isShow ? true : false,
      });
    }
    item.mergeSubjects &&
      item.mergeSubjects.forEach((i) => {
        chartData.push({
          ...i,
          isLine: item.isLine,
          isShow: item.isShow ? true : false,
        });
      });
    item.subjectList &&
      item.subjectList.forEach((i) => {
        chartData.push({
          ...i,
          isLine: item.isLine,
          isShow: item.isShow ? true : false,
        });
      });
  });
  chartData = chartData.filter((item) => item.showCharts);
  let chartDatas = [];
  chartData.forEach((item, index) => {
    if (index == 0) {
      chartDatas.push(item);
    } else {
      chartDatas.push(item);
    }
  });
  const chartDatasList = [];
  state.analysisData.chartData.forEach((item) => {
    chartDatas.forEach((i) => {
      if (item.keyName == i.keyName && !item.isLine) {
        chartDatasList.push(item);
      }
    });
  });
  chartDatas.forEach((item, index) => {
    //平均分 标准分  得分率  超均分 优秀率 良好率 及格率  低分率 都不要年级
    //取辅助线的数据  取第一个即可
    if (item.isLine) {
      // /固定的  0  平均分   1  标准分  2 得分率  3 超均分  4 超均率
      switch (state.tabActive) {
        case "0":
          average = item.fiveRateResultVo?.avgScore; // 平均分
          markLineData.push({
            legendName: `${item.keyName}`,
            value: average,
            isShow: item.isShow, //是否显示
          });
          break;
        case "1":
          average = 0; // 标准分
          break;
        case "2":
          average = item.fiveRateResultVo?.rateScore; // 得分率
          markLineData.push({
            legendName: `${item.keyName}`,
            value: average,
            isShow: item.isShow, //是否显示
          });
          break;
        case "3":
          average = 0; // 超均分
          break;
        case "4":
          average = 0; // 超均率
          break;
        default:
          // 可以在这里添加默认情况的处理逻辑  优秀率 良好率 及格率  低分率
          average = item.fiveRateResultVo?.[`${state.tabActive}Rate`];
          markLineData.push({
            legendName: `${item.keyName}`,
            value: average,
            isShow: item.isShow, //是否显示
          });
          break;
      }
    } else {
      datax.push(item.keyName);
      let list = []; //悬浮弹窗数据
      // 固定的  0  平均分   1  标准分  2 得分率  3 超均分  4 超均率
      switch (state.tabActive) {
        case "0":
          datay.push(item.fiveRateResultVo?.avgScore); // 平均分
          if (item.classLevel == 2) {
            if (!maxClassName && !minClassName) {
              console.log(average);
              maxScore = parseFloat(item.fiveRateResultVo?.avgScore);
              minScore = parseFloat(item.fiveRateResultVo?.avgScore);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (parseFloat(item.fiveRateResultVo?.avgScore) > maxScore) {
                maxScore = parseFloat(item.fiveRateResultVo?.avgScore);
                maxClassName = item.keyName;
              } else if (
                parseFloat(item.fiveRateResultVo?.avgScore) < minScore
              ) {
                minScore = parseFloat(item.fiveRateResultVo?.avgScore);
                minClassName = item.keyName;
              }
            }
          }
          if (analysisStore.filterObject.examLevel == 1) {
            list.push({
              name: "均分联排",
              value: item.fiveRateResultVo?.avgExamRank || "-",
            }); //平均分排名
          }
          list.push({
            name: "均分校排",
            value: item.fiveRateResultVo?.avgRank || "-",
          }); //平均分排名
          list.push({
            name: "最高分",
            value: item.fiveRateResultVo?.maxScore,
          }); //最高分
          list.push({
            name: "最低分",
            value: item.fiveRateResultVo?.minScore,
          }); //最低分
          list.push({
            name: "标准差",
            value: item.fiveRateResultVo?.stdScore,
          }); //标准差
          list.push({
            name: "实考人数",
            value: item.fiveRateResultVo?.effectiveNum,
          }); //实考人数
          list.push({
            name: "教师",
            value: item.teacherName || "-",
          }); //实考人数
          //添加悬浮弹窗的数据
          tooltipData.push({
            list: list,
            name: item.keyName,
          });
          break;
        case "1":
          //标准分
          if (item.classLevel == 2) {
            if (!maxClassName && !minClassName) {
              maxScore = parseFloat(item.fiveRateResultVo?.standardAvgScore);
              minScore = parseFloat(item.fiveRateResultVo?.standardAvgScore);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (
                parseFloat(item.fiveRateResultVo?.standardAvgScore) > maxScore
              ) {
                maxScore = parseFloat(item.fiveRateResultVo?.standardAvgScore);
                maxClassName = item.keyName;
              } else if (
                parseFloat(item.fiveRateResultVo?.standardAvgScore) < minScore
              ) {
                minScore = parseFloat(item.fiveRateResultVo?.standardAvgScore);
                minClassName = item.keyName;
              }
            }
          }
          const zscore = parseFloat(
            item.fiveRateResultVo?.standardAvgScore,
          ).toFixed(2);
          datay.push(isNaN(zscore) ? 0 : zscore); // 标准分
          list.push({
            name: "标准分",
            value: item.fiveRateResultVo?.standardAvgScore,
          }); //标准差
          list.push({
            name: "平均分",
            value: item.fiveRateResultVo?.avgScore,
          });
          list.push({
            name: "实考人数",
            value: item.fiveRateResultVo?.effectiveNum,
          }); //实考人数
          list.push({
            name: "教师",
            value: item.teacherName || "-",
          }); //实考人数
          //添加悬浮弹窗的数据
          tooltipData.push({
            list: list,
            name: item.keyName,
          });
          break;
        case "2":
          //得分率
          if (item.classLevel == 2) {
            if (!maxClassName && !minClassName) {
              maxScore = parseFloat(item.fiveRateResultVo?.rateScore);
              minScore = parseFloat(item.fiveRateResultVo?.rateScore);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (parseFloat(item.fiveRateResultVo?.rateScore) > maxScore) {
                maxScore = parseFloat(item.fiveRateResultVo?.rateScore);
                maxClassName = item.keyName;
              } else if (
                parseFloat(item.fiveRateResultVo?.rateScore) < minScore
              ) {
                minScore = parseFloat(item.fiveRateResultVo?.rateScore);
                minClassName = item.keyName;
              }
            }
          }
          datay.push(item.fiveRateResultVo?.rateScore); // 得分率
          dataLoseRate.push(item.fiveRateResultVo?.lostRate); // 失分率
          list.push({
            name: "失分率",
            value: item.fiveRateResultVo?.lostRate + "%",
          });
          list.push({
            name: "实考人数",
            value: item.fiveRateResultVo?.effectiveNum,
          }); //实考人数
          list.push({
            name: "教师",
            value: item.teacherName || "-",
          }); //实考人数
          //添加悬浮弹窗的数据
          tooltipData.push({
            list: list,
            name: item.keyName,
          });
          break;
        case "3": {
          let exceedAvgScore =
            state.rankType == 2
              ? item.fiveRateResultVo?.exceedAvgScore
              : item.fiveRateResultVo?.examExceedAvgScore;
          let exceedAvgScoreRate =
            state.rankType == 2
              ? item.fiveRateResultVo?.exceedAvgScoreRate
              : item.fiveRateResultVo?.examExceedAvgScoreRate;
          //超均分
          if (analysisStore.filterObject.schoolLevel == 2) {
            if (!maxClassName && !minClassName) {
              maxScore = parseFloat(exceedAvgScore);
              minScore = parseFloat(exceedAvgScore);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (parseFloat(exceedAvgScore) > maxScore) {
                maxScore = parseFloat(exceedAvgScore);
                maxClassName = item.keyName;
              } else if (parseFloat(exceedAvgScore) < minScore) {
                minScore = parseFloat(exceedAvgScore);
                minClassName = item.keyName;
              }
            }
            let superAverage = parseFloat(exceedAvgScore).toFixed(2);
            datay.push(isNaN(superAverage) ? 0 : superAverage); // 超均分
            list.push({
              name: "超均分",
              value: exceedAvgScore,
            }); //超均分
            list.push({
              name: "超均率",
              value: exceedAvgScoreRate + "%",
            }); //超均分
            list.push({
              name: "平均分",
              value: item.fiveRateResultVo?.avgScore,
            });
            list.push({
              name: "实考人数",
              value: item.fiveRateResultVo?.effectiveNum,
            }); //实考人数
            list.push({
              name: "教师",
              value: item.teacherName || "-",
            }); //实考人数
            //添加悬浮弹窗的数据
            tooltipData.push({
              list: list,
              name: item.keyName,
            });
          } else {
            if (item.keyName != "年级") {
              if (!maxClassName && !minClassName) {
                maxScore = parseFloat(exceedAvgScore);
                minScore = parseFloat(exceedAvgScore);
                maxClassName = item.keyName;
                minClassName = item.keyName;
              } else {
                if (parseFloat(exceedAvgScore) > maxScore) {
                  maxScore = parseFloat(exceedAvgScore);
                  maxClassName = item.keyName;
                } else if (parseFloat(exceedAvgScore) < minScore) {
                  minScore = parseFloat(exceedAvgScore);
                  minClassName = item.keyName;
                }
              }
              let superAverage = parseFloat(exceedAvgScore).toFixed(2);
              datay.push(isNaN(superAverage) ? 0 : superAverage); // 超均分
              list.push({
                name: "超均分",
                value: exceedAvgScore,
              }); //超均分
              list.push({
                name: "超均率",
                value: exceedAvgScoreRate + "%",
              }); //超均分
              list.push({
                name: "平均分",
                value: item.fiveRateResultVo?.avgScore,
              });
              list.push({
                name: "实考人数",
                value: item.fiveRateResultVo?.effectiveNum,
              }); //实考人数
              list.push({
                name: "教师",
                value: item.teacherName || "-",
              }); //实考人数
              //添加悬浮弹窗的数据
              tooltipData.push({
                list: list,
                name: item.keyName,
              });
            }
          }

          break;
        }
        case "4": {
          let exceedAvgScore =
            state.rankType == 2
              ? item.fiveRateResultVo?.exceedAvgScore
              : item.fiveRateResultVo?.examExceedAvgScore;
          let exceedAvgScoreRate =
            state.rankType == 2
              ? item.fiveRateResultVo?.exceedAvgScoreRate
              : item.fiveRateResultVo?.examExceedAvgScoreRate;
          // 超均率
          console.log(item);

          //单班的时候
          if (analysisStore.filterObject.schoolLevel == 2) {
            if (!maxClassName && !minClassName) {
              maxScore = parseFloat(exceedAvgScoreRate);
              minScore = parseFloat(exceedAvgScoreRate);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (parseFloat(exceedAvgScoreRate) > maxScore) {
                maxScore = parseFloat(exceedAvgScoreRate);
                maxClassName = item.keyName;
              } else if (parseFloat(exceedAvgScoreRate) < minScore) {
                minScore = parseFloat(exceedAvgScoreRate);
                minClassName = item.keyName;
              }
            }
            let superRate = parseFloat(exceedAvgScoreRate).toFixed(2);
            datay.push(isNaN(superRate) ? 0 : superRate); // 超均率
            list.push({
              name: "超均分",
              value: exceedAvgScore,
            }); //超均分
            list.push({
              name: "超均率",
              value: exceedAvgScoreRate + "%",
            }); //超均分
            list.push({
              name: "平均分",
              value: item.fiveRateResultVo?.avgScore,
            });
            list.push({
              name: "实考人数",
              value: item.fiveRateResultVo?.effectiveNum,
            }); //实考人数
            list.push({
              name: "教师",
              value: item.teacherName || "-",
            }); //实考人数
            //添加悬浮弹窗的数据
            tooltipData.push({
              list: list,
              name: item.keyName,
            });
          } else {
            //年级或者组合班的时候
            if (item.keyName != "年级") {
              if (!maxClassName && !minClassName) {
                maxScore = parseFloat(exceedAvgScoreRate);
                minScore = parseFloat(exceedAvgScoreRate);
                maxClassName = item.keyName;
                minClassName = item.keyName;
              } else {
                if (parseFloat(exceedAvgScoreRate) > maxScore) {
                  maxScore = parseFloat(exceedAvgScoreRate);
                  maxClassName = item.keyName;
                } else if (parseFloat(exceedAvgScoreRate) < minScore) {
                  minScore = parseFloat(exceedAvgScoreRate);
                  minClassName = item.keyName;
                }
              }
              let superRate = parseFloat(exceedAvgScoreRate).toFixed(2);
              datay.push(isNaN(superRate) ? 0 : superRate); // 超均率
              list.push({
                name: "超均分",
                value: exceedAvgScore,
              }); //超均分
              list.push({
                name: "超均率",
                value: exceedAvgScoreRate + "%",
              }); //超均分
              list.push({
                name: "平均分",
                value: item.fiveRateResultVo?.avgScore,
              });
              list.push({
                name: "实考人数",
                value: item.fiveRateResultVo?.effectiveNum,
              }); //实考人数
              list.push({
                name: "教师",
                value: item.teacherName || "-",
              }); //实考人数
              //添加悬浮弹窗的数据
              tooltipData.push({
                list: list,
                name: item.keyName,
              });
            }
          }
          break;
        }
        default:
          let rate = "";
          // 可以在这里添加默认情况的处理逻辑  优秀率 良好率 及格率  低分率
          rate = item.fiveRateResultVo?.[`${state.tabActive}Rate`];
          datay.push(item.fiveRateResultVo?.[`${state.tabActive}Rate`]);
          list.push({
            name: state.groupTitle + "人数",
            value: item.fiveRateResultVo?.[`${state.tabActive}RateNum`],
          }); //平均分排名

          if (item.classLevel == 2) {
            //过滤掉组合班和年级
            if (!maxClassName && !minClassName) {
              maxScore = parseFloat(rate);
              minScore = parseFloat(rate);
              maxClassName = item.keyName;
              minClassName = item.keyName;
            } else {
              if (parseFloat(rate) > maxScore) {
                maxScore = parseFloat(rate);
                maxClassName = item.keyName;
              } else if (parseFloat(rate) < minScore) {
                minScore = parseFloat(rate);
                minClassName = item.keyName;
              }
            }
          }
          list.push({
            name: "实考人数",
            value: item.fiveRateResultVo?.effectiveNum,
          }); //实考人数
          list.push({
            name: "教师",
            value: item.teacherName || "-",
          }); //实考人数
          //添加悬浮弹窗的数据
          tooltipData.push({
            list: list,
            name: item.keyName,
          });
          break;
      }
    }
  });
  //颜色设置
  switch (state.tabActive) {
    case "high":
      color = "#5470C6"; // 拔尖率
      break;
    case "good":
      color = "#3BA272"; // 优秀率
      break;
    case "fine":
      color = "#FAC858"; // 良好率
      break;
    case "pass":
      color = "#995FB3"; // 及格率
      break;
    case "low":
      color = "#EE6666"; // 低分率
      break;
    default:
      // 可以在这里添加默认情况的处理逻辑  优秀率 良好率 及格率  低分率
      color = "#5470C6"; // 默认颜色
      break;
  }
  //说明文案设置
  switch (state.tabActive) {
    case "0":
      //平均分
      unit = "";
      expandableText =
        '说明：平均分指一次考试中一组数据的平均成绩。计算方式为所有人员成绩总和/人员总数。通过平均分可直接体现本次考试各个班级的成绩水平。由图中可以看出，平均分最高的班级为<span class="max_color">' +
        maxClassName +
        '</span>，最低的班级为<span class="min_color">' +
        minClassName +
        "</span>。";
      break;
    case "1":
      //标准分
      unit = "";
      expandableText =
        "说明：标准分，也称为标准分数或Z分数，是一种由原始分数推导出来的相对地位量数，用于说明原始分数在所属分数群体中的相对位置。其计算公式为：Z = (x - μ)/σ（Z是标准分，X是原始数据的值，μ是该组数据的平均数，σ是该组数据的标准差）。标准分的分数区间通常在-3到+3之间，标准分大于0，表示成绩在平均数之上；标准分数小于0，表示成绩在平均分数之下。与原始分相比，标准分最直接的意义是它给出离平均数的距离。柱图的颜色可直接区分出各班级标准分的差异，标准分越高说明该班的表现越优异。";
      break;
    case "2":
      //得分率
      unit = "%";
      expandableText =
        "说明：得分率是指实际得分除以考核分的比值,换算成的百分数。公式：得分率=得分/满分*100%。得分率是评估考试表现的重要指标。较高的得分率通常代表更好的完成度或掌握程度，而较低的得分率则可能提示需要改进或加强。红色柱代表班级得分率低于年级得分率，反之，绿色柱代表班级得分率高于年级得分率。";
      break;
    case "3":
      //超均分
      unit = "";
      expandableText =
        '说明：班级超均分指在本次考试中，班级平均分超出年级平均分的分值，正值代表超过了年级均分，负值为低于年级均分，柱图的颜色可直接区分出超过或者低于年级均分的班级。该数据反映了一个班级的均分与年级整体水平之间的差异，超均分越高说明该班的表现越优异。由图中可以看出，超均分最高的班级为<span class="max_color">' +
        maxClassName +
        '</span>，最低的班级为<span class="min_color">' +
        minClassName +
        "</span>。";
      break;
    case "4":
      //超均率
      unit = "%";
      expandableText =
        '说明：班级超均率表示班级超过或低于年级平均水平的程度。超均率为正，说明班级水平高于年级平均水平；超均率为负，说明班级水平低于年级平均水平，柱图的颜色可直接区分出超过或者低于年级均分的班级。计算方式为（班级平均分-年级平均分）/年级的平均分*100%。该数据反映了一个班级的均分与年级整体水平之间的差异，超均率越高说明该班的表现越优异。由图中可以看出，超均率最高的班级为<span class="max_color">' +
        maxClassName +
        '</span>，最低的班级为<span class="min_color">' +
        minClassName +
        "</span>。";
      break;
    default:
      unit = "%";
      // 可以在这里添加默认情况的处理逻辑  优秀率 良好率 及格率  低分率
      let percent = {
        high: "",
        good: "",
        fine: "",
        pass: "",
        low: "",
      };
      state.analysisData.dynamicsHeader.forEach((item) => {
        percent[item.prop] = `${item.value}%`;
      });
      expandableText = `说明：班级${state.groupTitle}指班级${state.tabActive == "top" ? "在考试中取得高分" : state.tabActive == "low" ? "成绩低于某个特定" : "在考试中达到" + state.groupTitle.replace("率", "")}标准（通常为科目满分的${percent[state.tabActive]}分数${state.tabActive == "low" ? "以下" : "以上"}）人数占比。
          计算方式为${state.groupTitle.replace("率", "")}人数/总人数*100%。通过分析${state.groupTitle}，可以了解班级${state.tabActive == "top" ? "高分" : state.groupTitle.replace("率", "")}人数分布情况。由图中可以看出，${state.groupTitle}最高的班级为${state.tabActive == "low" ? `<span class="min_color">` : `<span class="max_color">`}${maxClassName}</span>，
          最低的班级为${state.tabActive == "low" ? `<span class="max_color">` : `<span class="min_color">`}${minClassName}</span>。`;
      break;
  }
  state.analysisData.chartType = "vertical_bar";
  state.analysisData.color = color;
  state.analysisData.average = parseFloat(average);
  state.analysisData.markLineData = markLineData;
  state.analysisData.datax = datax;
  state.analysisData.datay = datay;
  state.analysisData.dataStackY = [datay, dataLoseRate]; //得分率 失分率
  state.analysisData.tooltipData = tooltipData;
  state.analysisData.avgScoreChartData = datax.map((item, index) => ({
    x: item,
    y: parseFloat(datay[index]), // 转换为数字方便排序
    tip: tooltipData[index],
  }));
  state.analysisData.unit = unit;
  state.analysisData.expandableText = expandableText;
  state.analysisData.chartDatas = chartDatasList;
};
// 分页
const handleCurrentChange = (val: number) => {
  state.analysisData.pageNum = val;
}

const handleSizeChange = (val: number) => {
  state.analysisData.pageSize = val;
  state.analysisData.pageNum = 1;
}
const pageInit = () => {
  GetSingleClassAnalysisData();
};
// 监听筛选条件
watch(
  () => analysisStore.filterObject,
  async () => {
    pageInit();
  },
  { deep: true },
);

onMounted(() => {
  pageInit();
});
</script>

<style lang="scss" scoped>
.el-input__inner {
  height: 36px;
}

.el-input__suffix {
  line-height: 36px;
}

.right_item {
  font-size: 14px;
  color: #999;
  font-weight: 400;
  cursor: pointer;
  height: 30px;
  line-height: 32px;
  margin-right: 5px;
  border-bottom: 2px solid #ffffff;

  &.item_active {
    font-size: 14px;
    color: #2e64fa;
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid #2e64fa;
  }
}

.right_set {
  font-weight: 400;
  font-size: 14px;
  color: #333333;

  .el-input {
    text-align: center;
    padding: 0 5px;
    margin: 0 2px;
  }
}
</style>
