<script setup lang="ts">
defineOptions({
  name: "healthDrank"
});
import { ref, reactive, nextTick } from "vue";
import { useRouter } from "vue-router";
import radar from "@/components/echarts/radar.vue";
import barEcharts from "@/components/echarts/bar.vue";
import barDimEcharts from "@/components/echarts/barDim.vue";
import rankTable from "@/components/rankTable/draw.vue";
import personList from "@/components/personList/index.vue";
import qvanping from "@/assets/rank/qvanping@2x.png";
import seachData from "./componements/seachOld.vue";
import { getPersonDimensionChartsList, getChartsList } from "@/api/draw";
import type { TabsPaneContext } from "element-plus";
const router = useRouter();
const radarRef = ref();
const rankTableRef = ref();
const activeName = ref("1");
const barEchartsRef = ref();
const barDimEchartsRef = ref();
const seachParams = ref();
// 排行榜
const showLength = ref(0);
const getChartsListApi = async () => {
  const { code, data } = await getChartsList({
    ...seachParams.value,
    type: 2
  });
  if (code == 200) {
    // showLength.value = data.length;
    nextTick(() => {
      if (data && data.length > 0) {
        showLength.value = data.length;
        setTimeout(() => {
          console.log("1243tethdgfsa", data);
          rankTableRef.value.init(data, "医疗组");
        }, 500);
      }
    });
  }
};
// 雷达图
const dataList = reactive({
  dimName: []
});
const getPersonDimensionChartsListApiDimList = async () => {
  const { code, data } = await getPersonDimensionChartsList({
    ...seachParams.value,
    type: 2
  });
  if (code == 200) {
    radarRefList.value = data;
    dataList.dimName = [];
    if (data && data.length > 0) {
      data.map(it => {
        it.dimensionList.map(item => {
          dataList.dimName.push({
            name: item.dimName,
            id: item.dimId
          });
        });
      });
    }
  }
};
const radarRefList = ref([]);
const barDimEchartsList = reactive({
  data: null,
  type: null
});
const init = (item, type, val) => {
  seachParams.value = item;
  if (Number(type)) {
    nextTick(() => {
      barDimEchartsList.data = item;
      barDimEchartsList.type = 2;
      if (val) {
        getPersonDimensionChartsListApiDimList();
        seachParams.value = {
          ...item,
          assessmentObjectName: "",
          assessmentObjectId: ""
        };
        barDimEchartsList.data = {
          ...item,
          assessmentObjectName: "",
          assessmentObjectId: ""
        };
      } else {
        radarRefList.value = [];
      }
      getChartsListApi();
      activeName.value = "1";
    });
  } else {
    showLength.value = 0;
    return;
  }
  setTimeout(() => {
    nextTick(() => {
      if (barDimEchartsRef.value) {
        // barDimEchartsRef.value[0].init(seachParams.value, 0);
        if (Array.isArray(barDimEchartsRef.value)) {
          dataList.dimName.map((it, id) => {
            barDimEchartsRef?.value[id - 1]?.init(seachParams.value, 0);
          });
        } else {
          barDimEchartsRef.value.init(seachParams.value, 0);
        }
      }
    });
  }, 500);
};
// 维度表格
const paramsIndex = ref({});
const personListRef = ref();
const radarRefList2 = ref([]);
const handleClick = (tab: TabsPaneContext, event: Event) => {
  loading.value = true;

  if (tab.props.label == "总览") {
    getChartsListApi();
  } else {
    nextTick(() => {
      Object.assign(paramsIndex.value, {
        ...seachParams.value,
        dimId: tab.props.name,
        dimensionName: tab.props.label,
        type: 2
      });
      radarRefList2.value = [];
      setTimeout(() => {
        const data = [];
        const lists = [...radarRefList.value];
        lists.forEach(el => {
          data.push({ ...el });
        });
        data[0].dimensionList = data[0].dimensionList.filter(item => {
          if (item.dimId === tab.props.name) {
            return true;
          }
        });
        radarRefList2.value = data;
      }, 1000);
    });
  }
  setTimeout(() => {
    loading.value = false;
  }, 1000);
  // barEchartsRef.value.init(tab.props);
};
const fullBig = (item: any) => {
  router.push({
    path: "/fullBig",
    query: {
      ...item,
      ...seachParams.value,
      type: 2
    }
  });
};
const loading = ref(false);
</script>

<template>
  <div class="w-[100%]">
    <div class="w-[100%]">
      <seachData @handClick="init" />
      <div v-if="showLength > 0">
        <div class="flex mr-2">
          <radar ref="radarRef" :radarRefList="radarRefList" />
        </div>
        <el-tabs
          v-model="activeName"
          v-loading="loading"
          class="demo-tabs"
          :lazy="true"
          @tab-click="handleClick"
        >
          <el-tab-pane label="总览" name="1">
            <div class="flex mr-8">
              <rankTable ref="rankTableRef" />
            </div>
            <!-- 总得分 -->
            <div class="mt-5 pr-8 w-full h-60">
              <barEcharts
                ref="barEchartsRef"
                :title="'总得分'"
                :barDimEchartsList="barDimEchartsList"
              />
            </div>
            <!-- 维度得分 -->
            <!-- <div
              v-for="(item, index) in dataList.dimName"
              :key="index"
              class="flex justify-between flex-wrap items-center"
            >
              <div class="mt-5 pr-8 w-1/2 h-60">
                <barDimEcharts ref="barDimEchartsRef" :title="item.name" />
              </div>
            </div> -->
            <div class="flex justify-between flex-wrap items-center">
              <div v-for="(item, index) in dataList.dimName" :key="index">
                <div class="mt-5 pr-8 h-60 w-[600px]">
                  <barDimEcharts
                    ref="barDimEchartsRef"
                    :title="item.name"
                    :barDimEchartsList="barDimEchartsList"
                    :itemDim="item"
                  />
                </div>
              </div>
            </div>
          </el-tab-pane>
          <el-tab-pane
            v-for="(item, index) in dataList.dimName"
            :key="index"
            :label="item.name"
            :name="item.id"
            :lazy="true"
          >
            <div class="w-full">
              <div class="flex justify-between">
                <personList
                  ref="personListRef"
                  :key="item.id + index"
                  class="w-11/12"
                  :paramsIndex="paramsIndex"
                  :detailData="radarRefList2"
                />
                <div
                  class="w-[30px] h-[30px] mr-10 cursor-pointer"
                  @click="fullBig(item)"
                >
                  <img class="w-full h-full" :src="qvanping" alt="" />
                </div>
              </div>
              <div class="w-full h-60 mt-5 pr-8">
                <barEcharts
                  ref="barEchartsRef"
                  :title="item.name"
                  :barDimEchartsList="barDimEchartsList"
                  :itemDim="item"
                />
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
  </div>
</template>
