<script setup lang="ts">
import zkItem from "@/components/zk-item";
import LeftTop from "./left-top.vue";
import LeftCenter from "./left-center.vue";
import LeftBottom from "./left-bottom.vue";
import CenterMap from "./center-map.vue";
import CenterTop from "./center-top.vue";
import RightTop from "./right-top.vue";
import RightCenter from "./right-center.vue";
import RightBottom from "./right-bottom.vue";
import RightTopTwo from "./right-top-two.vue";
import RightBottomTwo from "./right-bottom-two.vue";
import { useTimeStore } from "@/stores/index";
import { storeToRefs } from "pinia";
const timeStore = useTimeStore();

const { nowTitle, years, months, days } = storeToRefs(timeStore);

import { AdminPCIndex, WeekPay, StatisticsReportedRanking, GetBatch } from "@/api";
import { useDataStore } from "@/stores/index";
const dataStore = useDataStore();
const { allData, rankingDto, collageId, professionRank, batchData } = storeToRefs(dataStore)

const rightCenterRef: any = ref(null)

const mapRef: any = ref(null)
const rbRef: any = ref(null)

const tableData = ref()
const showRight = ref(true)
const changeRight = () => {
  showRight.value = !showRight.value
  if (!showRight.value && rbtwoRef) {
    setTimeout(() => {
      rbtwoRef.value.getData(professionRank.value)

    })
  }
};
/**
 * @description: 处理map中的数据
 * @param {*} data
 * @return {*}
 */
const dealMapData = (data: any) => {
  tableData.value = data.provinceDto?.map((item: any) => {
    return {
      name: item.provinceName,
      // zk: item.degreesDto[0].paymentCompleted + item.degreesDto[0].nonPayment,
      // zsb:item.degreesDto[1].paymentCompleted+ '/'+(item.degreesDto[1].paymentCompleted+item.degreesDto[1].nonPayment),
      // bk: item.degreesDto[2].paymentCompleted + item.degreesDto[2].nonPayment,
      zk: findDegree(item.degreesDto, '统招专科').paymentCompleted + findDegree(item.degreesDto, '统招专科').nonPayment,
      zsb: findDegree(item.degreesDto, '统招专升本').paymentCompleted + findDegree(item.degreesDto, '统招专升本').nonPayment,
      bk: findDegree(item.degreesDto, '统招本科').paymentCompleted + findDegree(item.degreesDto, '统招本科').nonPayment,
      zkrate: divideAndFormat(findDegree(item.degreesDto, '统招专科').paymentCompleted, findDegree(item.degreesDto, '统招专科').paymentCompleted + findDegree(item.degreesDto, '统招专科').nonPayment),
      bkrate: divideAndFormat(findDegree(item.degreesDto, '统招本科').paymentCompleted, findDegree(item.degreesDto, '统招本科').paymentCompleted + findDegree(item.degreesDto, '统招本科').nonPayment),
      zbdrate: divideAndFormat(findDegree(item.degreesDto, '统招专科').paymentCompleted + findDegree(item.degreesDto, '统招本科').paymentCompleted, findDegree(item.degreesDto, '统招专科').paymentCompleted + findDegree(item.degreesDto, '统招专科').nonPayment + findDegree(item.degreesDto, '统招本科').paymentCompleted + findDegree(item.degreesDto, '统招本科').nonPayment),
      num1: findDegree(item.degreesDto, '统招专科')?.paymentCompleted,
      num2: findDegree(item.degreesDto, '统招专升本')?.paymentCompleted,
      num3: findDegree(item.degreesDto, '统招本科')?.paymentCompleted,
      reported: findDegree(item.degreesDto, '统招专科')?.paymentCompleted + findDegree(item.degreesDto, '统招专升本')?.paymentCompleted + findDegree(item.degreesDto, '统招本科')?.paymentCompleted,
      AdmissionNum: findDegree(item.degreesDto, '统招专科').paymentCompleted + findDegree(item.degreesDto, '统招专科').nonPayment + findDegree(item.degreesDto, '统招本科').paymentCompleted + findDegree(item.degreesDto, '统招本科').nonPayment
    }
  })
  tableData.value?.sort((a: any, b: any) => {
    return b.reported - a.reported
  })
  tableData.value?.forEach((item: any, index: any) => {
    item.sort = index + 1
    item.sort = item.sort < 10 ? "0" + item.sort : item.sort;
    if (item.zbdrate == '0.0%') {
      item.zbdrate = '0%'
    }
  })
}


const divideAndFormat = (divisor: any, dividend: any) => {
  if (dividend === 0) {
    return "0.0%";
  }

  let result = (divisor / dividend) * 100;
  return result.toFixed(1) + "%";
}
const findDegree = (degreesDto: any, degree: string) => {
  return degreesDto.find(d => d.degreesName === degree)

}


const rbtwoRef: any = ref(null)
const getProfession = () => {
  let params = {
    "Id": collageId.value,
    "Year": years.value,
    "Month": months.value || null,
    "Day": days.value || null
  }
  StatisticsReportedRanking(params).then(res => {
    dataStore.setProfessionRank(res.data)
    setTimeout(() => {
      if (!showRight.value && rbtwoRef) {
        rbtwoRef.value.getData(professionRank.value)
      }
    })
  })

}
watch(collageId, () => {
  getProfession()

})

/**
 * @description: 获取页面数据
 * @return {*}
 */
const getAdminPCIndex = () => {
  let params = {
    "CandidateCategory": 0,
    "Year": years.value,
    "Month": months.value || null,
    "Day": days.value || null
  }
  AdminPCIndex(params).then((res: any) => {
    dataStore.setData(res.data);
    dataStore.setCollageId(res.data.rankingDto[0].collegeId)
    dealMapData(res.data)
    setTimeout(() => {
      if (mapRef) {
        mapRef.value.getMapChart(tableData.value)
      }
      if (rbRef) {
        rbRef.value.rankProvince(tableData.value)
      }
    });
  })
};

watch(years,()=>{
  console.log('years.value',years.value);
  payData()
})
watch(months,()=>{
  console.log('months.value',months.value);
  payData()
})
watch(days,()=>{
  console.log('days.value',days.value);
  payData()
})

const batchs = ref()
// 获取学历层次
/* const getBatch = () => {
  GetBatch().then((res: any) => {
    if (res.succeed) {
      batchs.value = res.data.item1
      dataStore.setBatchData(res.data.item1)
    }

  })
} */

// 缴费人数-- 本科 专科 专升本
/* const payData = () => {
  let params: any = [{
    "EducationalLevel": 3,
    "Year": years.value,
    "Month": months.value || null,
    "Day": days.value || null
  }, {
    "EducationalLevel": 4,
    "Year": years.value,
    "Month": months.value || null,
    "Day": days.value || null
  }, {
    "EducationalLevel": 6,
    "Year": years.value,
    "Month": months.value || null,
    "Day": days.value || null
  }]
  for (let item of params) {
    WeekPay(item).then((res: any) => {
      switch (item.EducationalLevel) {
        case 3:
          dataStore.setBkPay(res.data);
        case 4:
          dataStore.setZkPay(res.data);
        case 6:
          dataStore.setZsbPay(res.data);
      }
    })
  }
  setTimeout(() => {
    if (rightCenterRef) {
      rightCenterRef.value.getData()
    }
  });

} */
/* 
 缴费人数---根据批次去区分
*/
const payData = () => {
  let payParams: any = {
  "CandidateCategory": 0,
  "Id": null,
  "Year": years.value,
  "Month": months.value || null,
  "Day": days.value || null,
}
  WeekPay(payParams).then((res: any) => {
    const bk = res.data.batchTodayPays?.find((item: any) => item.name === "统招本科");
    const zk = res.data.batchTodayPays?.find((item: any) => item.name === "统招专科");
    const zsb = res.data.batchTodayPays?.find((item: any) => item.name === "统招专升本");
    dataStore.setBkPay(bk)
    dataStore.setZkPay(zk)
    dataStore.setZsbPay(zsb)
    dataStore.setTotalPay(res.data?.payNum)
    setTimeout(() => {
      if (rightCenterRef) {
        rightCenterRef.value.getData()
      }
    });
  })
}
const timer: any = ref(null)
const startTimer = () => {
  timer.value = setInterval(() => {
    getAdminPCIndex();
    payData();
  }, 60000)
}
const clearTimer = () => {
  clearInterval(timer.value)
}

onMounted(() => {
  // getBatch();
  getAdminPCIndex();
  setTimeout(() => {
    payData();
    startTimer()
  }, 100)

});

</script>

<template>
  <div class="index-box">
    <div class="contetn_left">
      <zkItem class="contetn_left-top contetn_lt-item" title="迎新进度">
        <LeftTop />
      </zkItem>
      <zkItem class="contetn_left-center contetn_lr-item" title="">
        <LeftCenter />
      </zkItem>
      <zkItem class="contetn_left-bottom contetn_lb-item" title="">
        <LeftBottom />
      </zkItem>
    </div>
    <div class="contetn_center">
      <CenterTop class="contetn_center_top" />
      <CenterMap class="contetn_center_map" ref="mapRef" />
    </div>
    <div class="contetn_right" v-show="showRight">
      <div class="change-btn" @click="changeRight"></div>
      <zkItem class="contetn_left-bottom contetn_rt-one" title="缴费人数">
        <RightTop />
      </zkItem>
      <zkItem class="contetn_left-bottom contetn_rc-item" :title="nowTitle + '缴费人数'" style="padding: 0 10px 16px 10px">
        <RightCenter ref="rightCenterRef" />
      </zkItem>
      <zkItem class="contetn_left-bottom contetn_rb-item" title="报到人数区域排名 ">
        <RightBottom ref="rbRef" />
      </zkItem>
    </div>
    <div class="contetn_right" v-if="!showRight">
      <div class="change-btn" @click="changeRight"></div>
      <zkItem class="contetn_left-bottom contetn_rt_two" title="学院报到人数">
        <RightTopTwo />
      </zkItem>
      <zkItem class="contetn_left-bottom contetn_rb-item" title="专业报到人数 ">
        <RightBottomTwo ref="rbtwoRef" />
      </zkItem>
    </div>
  </div>
</template>

<style scoped lang="scss">
.index-box {
  width: 100%;
  display: flex;
  min-height: calc(100% - 64px);
  justify-content: space-between;
}

//左边 右边 结构一样
.contetn_left,
.contetn_right {
  display: flex;
  flex-direction: column;
  // justify-content: space-around;
  position: relative;
  width: 470px;
  box-sizing: border-box;
  flex-shrink: 0;
  border: 2px solid rgba(107, 163, 247, 0.2);
  border-radius: 16px;
  background-clip: padding-box, border-box;
  background-origin: padding-box, border-box;
  background-image: linear-gradient(to bottom, rgba(0, 0, 51, 0.1), rgba(0, 0, 0, 0.1)), linear-gradient(0deg, rgba(43, 74, 158, 0.1), rgba(89, 153, 252, 0.1));

  .change-btn {
    position: absolute;
    left: -60px;
    top: calc(50% - 200px);
    width: 55px;
    height: 400px;
    background: url('@/assets/img/zheke/right_change.png');
    cursor: pointer;
  }
}

.contetn_center {
  flex: 1;
  // margin: 0 54px;
  display: flex;
  flex-direction: column;
  padding-top: 50px;
  // justify-content: space-around;
  align-items: center;

  .contetn_center_top {
    width: 100%;
    padding: 0 36px;
    margin-bottom: 30px;
  }

  .contetn_center_map {
    width: 100%;
    height: 700px;
    display: flex;
    justify-content: center;

  }

}

.contetn_lt-item {
  height: 300px;
}

.contetn_lr-item {
  height: 310px;
}

.contetn_lb-item {
  height: 310px;
  margin-top: 130px;
}

.contetn_rt-one {
  height: 240px;
}

.contetn_rt_two {
  height: 640px;
}

.contetn_rc-item {
  height: 380px;
}

.contetn_rb-item {
  height: 310px;
}
</style>
