<template>
  <div>
    <Title title="基本情况">
      <template v-slot:title-slot>
        <div class="slot-right">
          <div class="btn yse-events" @click="toGybg">果园报告</div>
        </div>
      </template>
    </Title>
    <div class="content-box">
      <div class="box flex">
        <div class="box-title flex-center">{{ obj1.name }}</div>
        <div class="box-bar">
          <div class="progress-bar">
            <div
              class="progress-bar-fill"
              :style="{ width: obj1.persents + '%' }"
            >
              <span class="num">{{ obj1.persents + "%" }}</span>
            </div>
          </div>
          <div class="progress-bar-text flex-space-around">
            <div
              class="line"
              v-for="(item, index) in obj1.periodList"
              :key="index"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>
      <div class="box flex">
        <div class="box-title flex-center">{{ obj2.name }}</div>
        <div class="box-bar">
          <div class="progress-bar">
            <div
              class="progress-bar-fill"
              :style="{
                width:
                  ((obj2.anomalyCount / obj2.totalCount) * 100).toFixed(0) +
                  '%',
              }"
            ></div>
            <div
              class="progress-bar-fill-two"
              :style="{
                width:
                  ((obj2.warningCount / obj2.totalCount) * 100).toFixed(0) +
                  '%',
              }"
            ></div>
          </div>
          <div class="progress-bar-text">
            <span>总数木:{{ obj2.totalCount }}棵</span>
            <span>实际异常:{{ obj2.anomalyCount }}棵</span>
            <span>风险预警:{{ obj2.warningCount }}棵</span>
          </div>
        </div>
      </div>
      <div class="box flex">
        <div class="box-title flex-center">{{ obj3.name }}</div>
        <div class="box-bar">
          <div class="progress-bar">
            <div
              class="progress-bar-fill"
              :style="{
                width:
                  ((obj3.anomalyCount / obj3.totalCount) * 100).toFixed(0) +
                  '%',
              }"
            ></div>
            <div
              class="progress-bar-fill-two"
              :style="{
                width:
                  ((obj3.warningCount / obj3.totalCount) * 100).toFixed(0) +
                  '%',
              }"
            ></div>
          </div>
          <div class="progress-bar-text">
            <span>总数木:{{ obj3.totalCount }}棵</span>
            <span>实际异常:{{ obj3.anomalyCount }}棵</span>
            <span>风险预警:{{ obj3.warningCount }}棵</span>
          </div>
        </div>
      </div>
      <div class="box flex">
        <div class="box-title flex-center">{{ obj4.name }}</div>
        <div class="box-bar flex-space-between">
          <div class="progress-bar">
            <div
              class="progress-bar-fill"
              :style="{
                width: (obj4.normalCount / obj4.totalCount) * 100 + '%',
              }"
            ></div>
          </div>
          <span class="progress-bar-text"
            >{{ obj4.normalCount }}/{{ obj4.totalCount }}(树木)</span
          >
        </div>
      </div>
    </div>
  </div>

</template>
<script setup>
import { onMounted, reactive, toRefs, ref,onBeforeUnmount } from "vue";
import {base_img_url3} from "@/api/config.js";
import { useStore } from "vuex";
import Title from "../../../components/title";

const emits = defineEmits(["gybg"])
const getters = useStore().getters;
const curGardenId = ref(getters.userinfo.curGardenId);

onMounted(() => {
  initData();
});
const infoObj = reactive({
  obj1: {},
  obj2: {},
  obj3: {},
  obj4: {},
});
const { obj1, obj2, obj3, obj4 } = toRefs(infoObj);
// 在组件或全局监听器中监听自定义事件
function handleStorageUpdate(e) {
    curGardenId.value = e.detail
    initData()
}
// 在组件或全局监听器中监听自定义事件
window.addEventListener('storageChanged', handleStorageUpdate);

onBeforeUnmount(()=>{
  window.removeEventListener('storageChanged', handleStorageUpdate);
})

const initData = async () => {
  const { code, data } = await VE_API.znxy.baseInfo({
    gardenId: curGardenId.value,
  });
  infoObj.obj1 = data.periodProgress;
  infoObj.obj2 = data.pestRisk;
  infoObj.obj3 = data.growRisk;
  infoObj.obj4 = data.normalState;
};

function toGybg(){
  emits("gybg")
}

</script>

<style lang="scss" scoped>
.flex {
  display: flex;
  align-items: center;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-space-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex-space-around {
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.slot-right {
  width: 70%;
  cursor: pointer;
  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: 13px 17px;
  box-sizing: border-box;
  .box {
    width: 100%;
    height: 25%;
    .box-title {
      background-image: url("@/assets/img/znxy/jbqk_bg_1.png");
      background-repeat: no-repeat;
      background-size: 100% 100%;
      width: 92px;
      height: 40px;
      font-weight: 800;
      color: #ed832c;
      margin-right: 8px;
    }
    .box-bar {
      width: 78%;
    }
    .progress-bar {
      width: 100%;
      height: 14px;
      background: rgba(125, 125, 125, 0.5);
      border-radius: 7px;
      box-sizing: border-box;
      padding: 2px 3px;
      overflow: hidden;
      &-fill {
        height: 10px;
        background-color: #ff963f; /* 进度条颜色 */
        border-radius: 5px;
        width: 70%;
        position: relative;
        .num {
          position: absolute;
          right: -28px;
          top: -3px;
          font-weight: 500;
          font-size: 12px;
          color: #ffca9f;
        }
      }
      &-text {
        font-weight: 500;
        font-size: 13px;
        color: #f18832;
        .line {
          position: relative;
          &::before {
            position: absolute;
            content: "";
            width: 1px;
            height: 14px;
            background: rgb(255, 255, 255, 0.2);
            top: -14px;
            left: 60px;
            z-index: 2;
          }
          &:last-child {
            &::before {
              width: 0;
            }
          }
        }
      }
    }
  }
}

.content-box > div.box:nth-of-type(2) {
  .box-title {
    background-image: url("@/assets/img/znxy/jbqk_bg_2.png");
    color: #d48b0e;
  }
  .progress-bar {
    background: rgba(120, 84, 20, 0.5);
    position: relative;
    width: 66%;
    &-fill {
      background: linear-gradient(90deg, rgba(33, 23, 7, 0.8), #ffa861);
      width: 15%;
      z-index: 2;
      opacity: 0.64;
    }
    &-fill-two {
      background: linear-gradient(90deg, #211707, #ffcd07);
      width: 39%;
      position: absolute;
      z-index: 1;
      height: 10px;
      top: 2px;
      border-radius: 5px;
      opacity: 0.64;
    }
    &-text {
      color: #ecbe6f;
      font-weight: 500;
      font-size: 13px;
      span {
        display: inline-block;
        margin-right: 13px;
      }
    }
  }
}

.content-box > div.box:nth-of-type(3) {
  .box-title {
    background-image: url("@/assets/img/znxy/jbqk_bg_3.png");
    color: #15c762;
  }
  .progress-bar {
    background: rgba(35, 60, 32, 0.5);
    position: relative;
    width: 54%;
    &-fill {
      background: linear-gradient(90deg, rgba(18, 39, 28, 0.8), #05e16c);
      width: 18%;
      z-index: 2;
      opacity: 0.64;
    }
    &-fill-two {
      background: linear-gradient(90deg, #101f05, #28bb6e);
      width: 48%;
      position: absolute;
      z-index: 1;
      height: 10px;
      top: 2px;
      border-radius: 5px;
      opacity: 0.64;
    }
    &-text {
      color: #19b75a;
      font-weight: 500;
      font-size: 13px;
      span {
        display: inline-block;
        margin-right: 13px;
      }
    }
  }
}

.content-box > div.box:nth-of-type(4) {
  .box-title {
    background-image: url("@/assets/img/znxy/jbqk_bg_4.png");
    color: #00fff0;
  }
  .box-bar {
    width: 78%;
  }
  .progress-bar {
    background: rgb(52, 107, 157, 0.5);
    width: 67%;
    &-fill {
      background: linear-gradient(90deg, #4facfe, #00e3fe);
      width: 80%;
      opacity: 0.4;
    }
    &-text {
      color: #b4fffb;
    }
  }
}
</style>
