<script setup lang="ts">
defineOptions({
  name: "workerDrak"
});
import { ref, reactive, nextTick, onMounted, watch } 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 value = ref("");
const radarRef = ref();
const rankTableRef = ref();
const activeName = ref("1");
const barEchartsRef = ref();
const barDimEchartsRef = ref();
const seachParams = ref<any>();
// 排行榜
const showLength = ref(0);
const rankTableRefList = reactive({
  data: [],
  dept: "",
  name: ""
});
const getChartsListApi = async () => {
  const { code, data } = await getChartsList({
    ...seachParams.value,
    type: 0
  });
  if (code == 200) {
    nextTick(() => {
      if (data && data.length > 0) {
        showLength.value = data.length;
        setTimeout(() => {
          rankTableRef.value.init(data, "科室", "姓名");
        }, 500);
      }
    });
  }
};
// 雷达图
const dataList = reactive({
  dimName: []
});
const getPersonDimensionChartsListApiDimList = async () => {
  const { code, data } = await getPersonDimensionChartsList({
    ...seachParams.value,
    type: 0
  });
  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: any, type: any, val: any) => {
  seachParams.value = item;
  barDimEchartsList.data = null;
  barDimEchartsList.type = null;
  if (Number(type)) {
    nextTick(() => {
      barDimEchartsList.data = item;
      barDimEchartsList.type = 0;
      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) {
        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: 0
      });
      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);
};
const fullBig = (item: any) => {
  router.push({
    path: "/fullBig",
    query: {
      ...item,
      ...seachParams.value,
      type: 0
    }
  });
};
// 加载loading
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"
                :rankTableRefList="rankTableRefList"
              />
            </div>
            <!-- 总得分 -->
            <div class="mt-5 pr-8 w-full h-60">
              <barEcharts
                ref="barEchartsRef"
                :title="'总得分'"
                :barDimEchartsList="barDimEchartsList"
              />
            </div>
            <!-- 维度得分 -->
            <div class="flex justify-between flex-wrap items-center">
              <div v-for="item in dataList.dimName" :key="item.id">
                <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="item.id + index"
            :label="item.name"
            :name="item.id"
            :lazy="true"
          >
            <div class="w-full">
              <div class="flex justify-between">
                <personList
                  :key="item.id + index"
                  ref="personListRef"
                  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>
