<script setup lang="ts">
defineOptions({
  name: "departmentDrank"
});
import { ref, reactive, nextTick, 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,
  getPersonDimensionChartsRanking,
  getAssessmentList,
  getChartsList
} from "@/api/draw";
import type { TabsPaneContext } from "element-plus";
const router = useRouter();
const activeName = ref("1");
// 排行榜
const rankTableRef = ref();
const chartList = ref([]);
const showLength = ref(0);
const getChartsListApi = async () => {
  const { code, data } = await getChartsList({
    ...seachParams.value,
    type: 1
  });
  if (code == 200) {
    if (data && data.length > 0) {
      showLength.value = data?.length;
      nextTick(() => {
        setTimeout(() => {
          rankTableRef.value.init(data, "科室", "姓名");
        }, 500);
      });
    }
  }
};
// 柱状图
const barEchartsRef = ref();
const radarRef = ref();
const barDimEchartsRef = ref();
const dataList = reactive({
  dimName: []
});
const seachParams = ref();
const getPersonDimensionChartsListApiDimList = async () => {
  const { code, data } = await getPersonDimensionChartsList({
    ...seachParams.value,
    type: 1
  });
  if (code == 200) {
    dataList.dimName = [];
    if (data && data.length > 0) {
      data.map(it => {
        if (it.dimensionList.length > 0) {
          it.dimensionList.map((item, index) => {
            dataList.dimName.push({
              name: item.dimName,
              id: item.dimId
            });
          });
        }
      });
    }
    console.log(dataList.dimName, "====");
  }
};
const radarRefList = ref([]);
const getPersonDimensionChartsListApi = async (
  dimensionName?: string,
  dimId?: number | string,
  index?: any
) => {
  const { code, data } = await getPersonDimensionChartsList({
    ...seachParams.value,
    dimId,
    dimensionName,
    type: 1
  });
  let tabIndex = Number(index);
  if (code == 200) {
    if (data && data.length > 0) {
      console.log(data);
      if (dimensionName && dimId) {
        setTimeout(() => {
          nextTick(() => {
            if (personListRef.value) {
              // personListRef?.value[tabIndex - 1]?.init(data);
            }
          });
        }, 500);
      } else {
        setTimeout(() => {
          nextTick(() => {
            // radarRefList.value = data;
            // radarRef.value.initChart(data);
          });
        }, 500);
      }
    }
  }

  // console.log(res);
};
const getPersonDimensionChartsRankingApi = async (
  dimensionName?: string,
  dimId?: number | string
) => {
  const { code, data } = await getPersonDimensionChartsRanking({
    ...seachParams.value,
    dimId,
    dimensionName,
    type: 1
  });
  if (code == 200) {
    if (data && data.length > 0) {
      // barEchartsRef.value.init(data);
    }
  }
};
const barDimEchartsList = reactive({
  data: null,
  type: null
});
const init = (item, type) => {
  seachParams.value = item;
  barDimEchartsList.data = null;
  barDimEchartsList.type = null;
  if (Number(type)) {
    nextTick(() => {
      console.log(item, type, "=====");
      radarRefList.value = { ...seachParams.value, type: 1 };
      barDimEchartsList.data = item;
      barDimEchartsList.type = 1;
      // getPersonDimensionChartsListApi();
      getPersonDimensionChartsListApiDimList();
      getPersonDimensionChartsRankingApi();
      getChartsListApi();
      activeName.value = "1";
    });
  }
  // setTimeout(() => {
  //   nextTick(() => {
  //     //
  //   });
  // }, 500);
};
// 维度表格
const paramsIndex = ref({});
const personListRef = ref();
const handleClick = (tab: TabsPaneContext, event: Event) => {
  loading.value = true;
  if (tab.props.label == "总览") {
    getPersonDimensionChartsListApi();
    getPersonDimensionChartsRankingApi();
    getChartsListApi();
  } else {
    getPersonDimensionChartsListApi(tab.props.label, tab.props.name, tab.index);
    getPersonDimensionChartsRankingApi(tab.props.label, tab.props.name);
  }
  nextTick(() => {
    Object.assign(paramsIndex.value, {
      ...seachParams.value,
      dimId: tab.props.name,
      dimensionName: tab.props.label,
      type: 1
    });
  });
  setTimeout(() => {
    loading.value = false;
  }, 1000);
  // barEchartsRef.value.init(tab.props);
};
const fullBig = (item: any) => {
  console.log(item);
  router.push({
    path: "/fullBig",
    query: {
      ...item,
      ...seachParams.value,
      type: 1
    }
  });
};
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 w-full pr-8 h-60">
              <barEcharts
                ref="barEchartsRef"
                :title="'总得分'"
                :barDimEchartsList="barDimEchartsList"
              />
            </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"
                  class="w-11/12"
                  :paramsIndex="paramsIndex"
                />
                <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>
