<template>
  <div class="w-[41.75vh] h-[40.09vh] bgBottomRightBox mt-[2.68vh] relative" v-loading="isLoading">
    <div class="w-[20.8vh] 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-[35.46vh] h-[28.05vh] ml-[2.4vh] my-[3.7vh]" ref="echartsContainer"></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 * as echarts from 'echarts';
import { GetMonitorOperationDept } from '@/api/monitor';
import { fnR } from '@/utils/common';
import dayjs from 'dayjs';
let chartLeft = null;

export default {
  data() {
    return {
      chartOptions: {
        grid: {
          left: '0%',
          right: '0%',
          top: '0%',
          bottom: '10%',
        },
        xAxis: {
          type: 'category',
          data: ['骨科', '心胸外科', '普通外科', '神经外科', '整形外科'],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            data: [180, 165, 155, 148, 137],
            type: 'bar',
            barWidth: '10%',
          },
        ],
      },
      startDate: dayjs().subtract(7, 'day').format('YYYY-MM-DD'),
      endDate: dayjs().format('YYYY-MM-DD'),
      sliderClass: {
        bgWeekBox: true,
        bgMonthBox: false,
      },
      renderTimer: null,
      isLoading: true,
    };
  },
  methods: {
    onGetPageData() {
      this.isLoading = true;
      GetMonitorOperationDept({ startDate: this.startDate, endDate: this.endDate }).then(res => {
        if (res['data']['length']) {
          this.chartOptions.xAxis.data = [];
          this.chartOptions.series[0].data = [];
          res['data'].forEach(item => {
            this.chartOptions.xAxis.data.push(item['deptName']);
            this.chartOptions.series[0].data.push(item['deptCount']);
          });
        } else {
          this.chartOptions.xAxis.data = ['骨科', '心胸外科', '普通外科', '神经外科', '整形外科'];
          this.chartOptions.series[0].data = [fnR(), fnR(), fnR(), fnR(), fnR()].sort((a, b) => {
            return b - a;
          });
        }
        this.isLoading = false;
        chartLeft.setOption(this.chartOptions);
      });
    },
    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();
    chartLeft = echarts.init(this.$refs.echartsContainer);
    chartLeft.setOption(this.chartOptions);
    this.renderTimer = setInterval(() => {
      if (this.sliderClass.bgWeekBox) {
        this.toMonth();
      } else {
        this.toWeek();
      }
    }, 20000);
  },
  beforeRouteLeave() {
    clearInterval(this.renderTimer);
  },
};
</script>

<style>
.bgBottomRightBox {
  background-image: url('@/assets/svg/Monitor/BottomRight/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>
