<template>
  <div class="health-6">
    <div class="header-info mg-b-16">
      <div class="selections">
        <div class="item" @click="tabSelect('1')" :class="{ active: type === '1' }">
          <i class="svg-icon svg-icon-sm">
            <svg-icon :svgid="'svg-wind-site'" />
          </i>
          <span>风场</span>
        </div>
        <div class="item" @click="tabSelect('2')" :class="{ active: type === '2' }">
          <i class="svg-icon svg-icon-sm">
            <svg-icon :svgid="'svg-h-project'" />
          </i>
          <span>项目</span>
        </div>
      </div>
      <div class="state" v-if="false">
        <div class="state-item green">
          <div class="dot "></div>
          <div class="text">良好数量：</div>
          <div class="value">999</div>
        </div>
        <div class="state-item purple">
          <div class="dot "></div>
          <div class="text">正常数量：</div>
          <div class="value">999</div>
        </div>
        <div class="state-item yellow">
          <div class="dot "></div>
          <div class="text">注意数量：</div>
          <div class="value">999</div>
        </div>
        <div class="state-item orange">
          <div class="dot "></div>
          <div class="text">严重数量：</div>
          <div class="value">999</div>
        </div>
      </div>
    </div>
    <row :type="'flex'" class="mg-b-16">
      <Col :span="12">
        <panel :title="'报警统计图'" :showLine="false">
          <dual-pie-chart :innerData="healPieData" :outerData="healPieData" :height="'250px'" />
        </panel>
      </Col>
      <Col :span="12">
        <panel :title="'故障统计图'" :showLine="false">
          <dual-pie-chart :innerData="stopPieData" :outerData="stopPieData" :height="'250px'" />
        </panel>
      </Col>
      <!-- <Col :span="16">
        <panel :title="'矩阵'" :showLine="false">
          <div class="matrix">
            <div class="item green">1号</div>
            <div class="item purple">2号</div>
            <div class="item orange">3号</div>
            <div class="item" v-for="index of 150" :key="index">{{ index + 3 }}号</div>
            <div class="blank" v-for="index of 30" :key="index"></div>
          </div>
        </panel>
      </Col> -->
    </row>
    <div class="mg-b-16">
      <panel :title="'健康状态占比'" :showLine="false">
        <bar-line-chart :bardata="barData" :lineData="[]" :height="'250px'" />
      </panel>
    </div>
    <div class="mg-b-16 curStyle">
      <panel :title="'健康趋势'" :showLine="false">
        <MultipleLineChart :list="statusData" :units="['']" :height="'250px'" />
      </panel>
      <div class="selections">
        <div class="item" @click="changeStatus('1')" :class="{ active: status === '1' }">
          <span>良好</span>
        </div>
        <div class="item" @click="changeStatus('2')" :class="{ active: status === '2' }">
          <span>注意</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BarLineChart from "../../components/chart/combination/bar-line-chart.vue";
import MultipleLineChart from "../../components/chart/line/multiple-line-chart.vue";
import DualPieChart from "../../components/chart/pie/dual-pie-chart.vue";
import Col from "../../components/coms/grid/col.vue";
import Row from "../../components/coms/grid/row.vue";
import SvgIcon from "../../components/coms/icon/svg-icon.vue";
import Panel from "../../components/coms/panel/panel.vue";
export default {
  setup() {},
  components: { SvgIcon, Panel, MultipleLineChart, BarLineChart, Row, Col, DualPieChart },
  data() {
    return {
      type: "1",
      status: "1",
      healPieData: [],
      stopPieData:[],
      statusData:[],
      barData:{
        area: [],
        legend: [],
        data: []
      }
    };
  },
  created(){
    this.requestData();
  },
  methods: {
    requestData(){
      this.getWpwarn();
      this.getStop();
      this.getWpOrProStatus();
      this.getStatus();
    },

    // 获取健康走势图
    getWpwarn(){
      let that = this;
      that.API.requestData({
        method: "POST",
        subUrl: "healthoperation/countWpwarn",
        data: {
          type: that.type
        },
        success (res) {
          let healPieData=[];

          res.data.forEach(ele=>{
            healPieData.push({
              value: ele.value,
              unit: "次",
              name: ele.name,
            });
          });

          that.healPieData = healPieData;
        }
      });
    },

    
    // 获取故障统计图
    getStop(){
      let that = this;
      that.API.requestData({
        method: "POST",
        subUrl: "healthoperation/countStop",
        data: {
          type: that.type
        },
        success (res) {
          let stopPieData=[];

          res.data.forEach(ele=>{
            stopPieData.push({
              value: ele.value,
              unit: "次",
              name: ele.name,
            });
          });

          that.stopPieData = stopPieData;
        }
      });
    },

    // 获取健康状态占比
    getWpOrProStatus(){
      let that = this;
      that.API.requestData({
        method: "POST",
        subUrl: "healthoperation/countWpOrProStatus",
        data: {
          type: that.type
        },
        success (res) {
          let barData = {
            area: res.data.name,
            legend: ["良好数量", "正常数量", "注意数量", "严重数量"],
            data: []
          };

          let length=res.data.name.length;

          for(let i=0;i<length;i++){
            barData.data.push([]);
          }

          for(let i=0;i<length;i++){
            barData.data[i].push(res.data.lhList[i]);
            barData.data[i].push(res.data.hgList[i]);
            barData.data[i].push(res.data.zyList[i]);
            barData.data[i].push(res.data.yzList[i]);
          }
          
          that.barData=barData;
        }
      });
    },

    // 获取健康状态
    getStatus(){
      let that = this;
      that.API.requestData({
        method: "POST",
        subUrl: "healthoperation/findWpOrProStatusForHistory",
        data: {
          type: that.type,
          status: that.status
        },
        success (res) {

          let statusData = [];

          const time = res.data.time;

          for(let key in res.data){
            if(key !== "name" && key !== "time"){
              let item=res.data[key];

              let statusItem = {
                title: res.data.name[0][key],
                yAxisIndex: 0,
                value: []
              }

              time.forEach((text, index)=>{
                statusItem.value.push({
                  text:res.data.name[0][key],
                  value:item[index]
                });
              });

              statusData.push(statusItem);
            }
          }

          that.statusData = statusData;
          
        }
      });
    },

    tabSelect(type) {
      this.type = type;
      this.requestData();
    },

    changeStatus(status){
      this.status = status;
      this.getStatus();
    }
  },
};
</script>

<style lang="less">
.health-6 {

  .curStyle{
    position: relative;

    .selections{
      position: absolute;
      display: flex;
      right: 0;
      top: 0;
      width: 50%;
      justify-content: flex-end;

      .item{
        flex: 0 0 80px;
        text-align: center;
        line-height: 33px;
        margin-right: 8px;
        color: @font-color;
        font-size: @fontsize-s;
        background: fade(@gray, 20);
        border: 1px solid fade(@gray, 20);
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;

        &:hover,
        &.active {
          background: fade(@green, 20);
          border: 1px solid @green;
          color: @green;
          cursor: pointer;

          i svg use {
            fill: @green;
          }
        }
      }
    }
  }

  .header-info {
    display: flex;

    .selections {
      display: flex;
      flex: 0 0 200px;

      .item {
        flex: 0 0 80px;
        text-align: center;
        line-height: 33px;
        margin-right: 8px;
        color: @font-color;
        font-size: @fontsize-s;
        background: fade(@gray, 20);
        border: 1px solid fade(@gray, 20);
        display: flex;
        justify-content: center;
        align-items: center;

        i {
          margin-right: 8px;

          svg use {
            fill: @gray;
          }
        }

        &:hover,
        &.active {
          background: fade(@green, 20);
          border: 1px solid @green;
          color: @green;
          cursor: pointer;

          i svg use {
            fill: @green;
          }
        }
      }
    }

    .state {
      display: flex;

      .state-item {
        display: flex;
        height: 32px;
        align-items: center;

        .dot {
          width: 8px;
          height: 8px;
          margin-right: 8px;
        }

        .text {
          margin-left: 8px;
        }

        .value {
          height: 32px;
          line-height: 32px;
          background: fade(@gray, 20);
          padding: 0 16px;
        }

        &.green {
          .dot {
            background: @green;
          }
        }

        &.purple {
          .dot {
            background: @purple;
          }
        }

        &.yellow {
          .dot {
            background: @yellow;
          }
        }

        &.orange {
          .dot {
            background: @orange;
          }
        }

        & + .state-item {
          margin-left: 16px;
        }
      }
    }
  }

  .matrix {
    display: flex;
    flex-wrap: wrap;

    .item {
      flex: 1 0 calc(100% / 20 - 4px);
      line-height: 26px;
      background: fade(@gray, 20);
      color: @gray-l;
      font-size: 12px;
      margin-bottom: 4px;
      text-align: center;
      border: 1px solid transparent;

      & + .item {
        margin-left: 4px;
      }

      &:nth-child(20n + 1) {
        margin-left: 0px;
      }

      &.green {
        color: @green;
        background: fade(@green, 20);
        border-color: @green;
      }

      &.purple {
        color: @purple;
        background: fade(@purple, 20);
        border-color: @purple;
      }

      &.orange {
        color: @orange;
        background: fade(@orange, 20);
        border-color: @orange;
      }

      &.red {
        color: @red;
        background: fade(@red, 20);
        border-color: @red;
      }
    }

    .blank {
      flex: 1 0 calc(100% / 20 - 4px);
      margin-left: 4px;
    }
  }
}
</style>
