<template>
  <div>
    <Title :title="(switchValue == 2?'病害':'虫害') + '态势'">
      <template v-slot:title-slot>
        <div class="switch yse-events" @click="handleSwitch">
            <el-icon color="#00FFF0" size="15"><Switch /></el-icon>
            <span>可切换{{switchValue == 2 ?'虫害':'病害'}}</span>
        </div>
        <div class="slot-right">
          <div class="btn yse-events">异常复盘</div>
          <div class="btn yse-events">态势详情</div>
        </div>
      </template>
    </Title>
    <div class="content-box">
      <div ref="chartDom" class="charts yse-events"></div>
      <div class="info">
        <el-select
          v-model="value"
          class="select yse-events"
          effect="dark"
          popper-class="v-select-popper-ns"
        >
          <el-option
            v-for="item in optionsList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div class="desc" v-html="textArr[value]">
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { onMounted, ref, onBeforeUnmount } from "vue";
import { useStore } from "vuex";
import * as echarts from "echarts";
import Title from "../../../components/title";

// 在组件或全局监听器中监听自定义事件
function handleStorageUpdate(e) {
  curGardenId.value = e.detail;
  initData();
}
// 在组件或全局监听器中监听自定义事件
window.addEventListener("storageChanged", handleStorageUpdate);

// 组件销毁前移除监听
onBeforeUnmount(() => {
  window.removeEventListener("storageChanged", handleStorageUpdate);
});

const getters = useStore().getters;
const curGardenId = ref(getters.userinfo.curGardenId);
let chartDom = ref(null);
let myChart = null;
let textArr = ref([])

onMounted(() => {
  myChart = echarts.init(chartDom.value);
  myChart.setOption(options);
  initData();
});
const sData = [
  {
    name: "虫害",
    value: "40000",
    text: "当天虫害比例",
  },
  {
    name: "正常",
    value: "53000",
    text: "当天虫害比例",
  },
];

//数据模型类
function dataModel(name, radius, mData){
  let data = {
        name: name || "第一层环",
        type: "pie",
        z: 3,
        tooltip: {
          show: true,
        },
        center: ["50%", "45%"],
        radius: radius || ["24%", "30%"],
        hoverAnimation: false,
        clockWise: false,
        itemStyle: {
          normal: {
            color: function (params) {
              return colorList[params.dataIndex];
            },
          },
        },
        label: {
          show: false,
        },
        data: mData || sData,
      }
      return data;
}

const colorList = ["#FFD700", "#02EDE0"];
const options = {
  tooltip: {
    trigger: "item",
    borderColor: "rgba(5,209,199,.5)",
    backgroundColor: "rgba(47, 87, 97.1)",
    borderWidth: 1,
    padding: 0,
    textStyle: {
      color: "#fff",
      fontSize: 11,
    },
    formatter: function (parms) {
      var str = `<div class='v-echarts-tooltip'>
                    <div>${parms.data.text}</div>
                    <span>${parms.data.name}: ${parms.percent}%</span>
                </div>`;
      return str;
    },
  },
  legend: {
    icon: "circle",
    bottom: "0%",
    textStyle: {
      color: "#fff",
    },
    itemWidth: 8,
    data: ["虫害", "正常"],
  },
  series: [
    // new dataModel("第四层环", ["63%", "69%"], sData),
    // new dataModel("第三层环", ["50%", "56%"], sData),
    // new dataModel("第二层环", ["37%", "43%"], sData),
    // new dataModel("第一层环", ["24%", "30%"], sData),
  ],
};


const initData = async () => {
  options.legend.data = [switchValue.value == 2 ?'病害':'虫害', "正常"]
  let radiusList = [["63%", "69%"],["50%", "56%"],["37%", "43%"],["24%", "30%"]]
  let nameList = ["第四层环","第三层环","第二层环","第一层环"]
  let prefixList = ["预计未来7天","当前","过去7天",""]
  options.series = []
  let texts = []
  VE_API.znxy.pestStatistics({gardenId:curGardenId.value,warningType:switchValue.value}).then(({data})=>{
    let j=0
    for(let i = data.length - 1; i >=0; i--){
      let item = data[i]
      let name = item.warningType == 2 ? "病害" : "虫害"
      texts.push(item.text)
      if( j!=2 ){
        options.series.push(new dataModel(nameList[j], radiusList[j],  [{
          name,
          value: item.num7d,
          text: `${prefixList[j]}${name}比例`,
        },
          {
            name: "正常",
            value: item.treeCount - item.num7d+"",
            text: `${prefixList[j]}正常比例`,
          }]))
        j++
      }
      options.series.push(new dataModel(nameList[j], radiusList[j],[{
            name,
            value: item.num,
            text: `${prefixList[j]}${name}比例`,
          },
          {
            name: "正常",
            value: item.treeCount - item.num+"",
            text: `${prefixList[j]}正常比例`,
          }]))
      j++
    }
    textArr.value = texts
    myChart.clear();
    options && myChart.setOption(options);
  })

};

const value = ref(1);

const optionsList = [
  {
    value: 0,
    label: "7天",
  },
  {
    value: 1,
    label: "3天",
  },
];

const switchValue = ref(2)
const handleSwitch = () =>{
  switchValue.value = switchValue.value == 2 ? 1 : 2;
  initData()
}

</script>

<style lang="scss" scoped>
.switch{
    display: flex;
    color: #FF0101;
    font-size: 13px;
    align-items: center;
    width: 39%;
    cursor: pointer;
    span{
        margin-left: 4px;
    }
}
.slot-right {
  width: 70%;
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  justify-content: right;
  align-items: center;
  padding-right: 12px;
  .btn {
    cursor: pointer;
    background: url("@/assets/img/tabs_btn3.png") no-repeat center center / 100%
      100%;
    margin-right: 5px;
    color: rgba(180, 255, 251, 0.8);
    font-weight: bold;
    font-size: 15px;
    width: 92px;
    height: 32px;
    text-align: center;
    line-height: 32px;
  }
}
.content-box {
  width: 100%;
  height: calc(100% - 48px);
  padding: 8px 11px;
  box-sizing: border-box;
  color: #fffdfd;
  display: flex;
  justify-content: space-between;
  .info {
    width: 50%;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    .select {
      width: 40%;
      margin-bottom: 20px;
      ::v-deep {
        .el-select__wrapper {
          background: transparent;
          border: 1px solid rgba(81, 233, 240, 0.6);
          box-shadow: none;
          .el-select__placeholder {
            color: #b4fffb;
          }
          .el-select__caret {
            color: #b4fffb;
          }
        }
      }
    }
  }
  .desc {
    width: 100%;
    height: 100%;
    font-size: 12px;
    font-weight: bold;
    overflow: hidden;
    line-height: 20px;
    // text-overflow: ellipsis;
    // display: -webkit-box;
    // -webkit-box-orient: vertical;
    // -webkit-line-clamp: 5; /* 设置显示的行数 */
    background: url("@/assets/img/znxy/ts_bg.png") no-repeat center center /
      100% 100%;
    box-sizing: border-box;
    padding: 5px;
  }
  .charts {
    width: 50%;
    height: 100%;
  }
}
</style>
