<template>
  <div
    class="w-[51.29vh] h-[40.09vh] bgBottomLeftBox mt-[2.68vh] relative"
    v-loading="isLoading"
  >
    <div
      class="w-[12.4vh] h-[5.07vh] ml-[1.85vh] flex justify-between items-center"
    >
      <img src="@/assets/svg/Monitor/Triangle.svg" />
      <span class="text-[2.03vh] youshe leading-none">手术量统计</span>
    </div>
    <div class="w-[42.96vh] h-[27.12vh] ml-[2.96vh] my-[3.7vh]">
      <Histogram
        v-if="getDataList.length"
        :getDataList="getDataList"
      ></Histogram>
      <div v-else class="flex w-[100%] h-[100%] items-center justify-center">
        <!-- 暂无数据 -->
        <img src="@/assets/svg/noData.svg" />
      </div>
    </div>
    <div
      class="absolute right-0 top-1/4 w-[3.51vh] h-[12.6vh]"
      :class="sliderClass"
    >
      <div
        class="h-[6.3vh] leading-[6.3vh] text-center text-[#C0D3E1] cursor-pointer"
        @click="toWeek"
      >
        周
      </div>
      <div
        class="h-[6.3vh] leading-[6.3vh] text-center text-[#C0D3E1] cursor-pointer"
        @click="toMonth"
      >
        月
      </div>
    </div>
  </div>
</template>

<script>
import { GetMonitorOperationNumber } from "@/api/monitor";
import dayjs from "dayjs";
import Histogram from "@/components/Histogram.vue";

export default {
  data() {
    return {
      startDate: dayjs().subtract(7, "day").format("YYYY-MM-DD"),
      endDate: dayjs().format("YYYY-MM-DD"),
      sliderClass: {
        bgWeekBox: true,
        bgMonthBox: false,
      },
      renderTimer: null,
      isLoading: true,
      getDataList: [],
    };
  },
  components: {
    Histogram,
  },
  methods: {
    onGetPageData() {
      this.isLoading = true;
      GetMonitorOperationNumber({
        startDate: this.startDate,
        endDate: this.endDate,
      }).then((res) => {
        this.getDataList = res["data"] || [];
        this.isLoading = false;
      });
    },
    toWeek() {
      this.startDate = dayjs().subtract(7, "day").format("YYYY-MM-DD");
      this.sliderClass.bgWeekBox = true;
      this.sliderClass.bgMonthBox = false;
      this.onGetPageData();
    },
    toMonth() {
      this.startDate = dayjs().subtract(30, "day").format("YYYY-MM-DD");
      this.sliderClass.bgWeekBox = false;
      this.sliderClass.bgMonthBox = true;
      this.onGetPageData();
    },
  },
  mounted() {
    this.onGetPageData();
    this.renderTimer = setInterval(() => {
      if (this.sliderClass.bgWeekBox) {
        this.toMonth();
      } else {
        this.toWeek();
      }
    }, 20000);
  },
  beforeRouteLeave() {
    clearInterval(this.renderTimer);
  },
};
</script>

<style>
.bgBottomLeftBox {
  background-image: url("@/assets/svg/Monitor/BottomLeft/Box.svg");
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}
.bgWeekBox {
  background-image: url("@/assets/svg/Monitor/BottomLeft/WeekActive.svg");
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;
}
.bgMonthBox {
  background-image: url("@/assets/svg/Monitor/BottomLeft/MonthActive.svg");
}
</style>
