<template>
  <div class="timeTransition">
    <div class="Evaluation_topall">
      <div class="Evaluation_top">
        <el-select
          size="mini"
          v-model="companyVal"
          placeholder="请选择"
          @change="changeCompan"
        >
          <el-option
            v-for="item in companyOptions"
            :key="item.id"
            :label="item.aname"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <div class="station">
          场站：
          <el-select
            size="mini"
            v-model="stationVal"
            placeholder="请选择"
            clearable
            @change="changeStation"
          >
            <el-option
              v-for="item in stationOptions"
              :key="item.id"
              :label="item.aname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="station">
          时间：
          <el-date-picker
            v-model="pickerTimer"
            type="date"
            size="mini"
            popper-class="date-select"
          />
        </div>
        <div class="but">
          <el-button round size="mini" class="buttons" @click="seachData"
            >搜索</el-button
          >
        </div>
      </div>
    </div>
    <div
      style="
        background: rgba(0, 0, 0, 0.4);
        height: calc(100% - 52px);
        padding-bottom: 15px;
      "
    >
      <div class="Evaluation_title clearfix">
        <div class="leftContent floatLeft"><span>状态转换分析</span></div>
      </div>

      <div class="economicTable1">
        <el-table
          :data="EvaluationData"
          stripe
          size="mini"
          height="calc(100% - 40px)"
          ref="Eval_table"
          style="width: 100%"
        >
          <el-table-column
            prop="stationId"
            label="设备"
            align="center"
            width="100"
          >
            <template #default="{ row }">
              <span>{{ "#" + row.stationId }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态">
            <template #default="{ row }">
              <div style="width: 100%">
                <span
                  v-for="(it, index) in row.data"
                  :key="index"
                  class="comStr"
                  :style="{
                    width: `${
                      Number(it.time / (timeCount / 1000).toFixed(2)) * 100
                    }%`,
                  }"
                  :title="`${it.equipmentId}在${getTimedate(
                    it.startTime
                  )}至${getTimedate(it.endTime)}是${eventData(it.event)}状态`"
                  :class="rowState(it)"
                ></span>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div style="text-align: right">
          <el-pagination
            @size-change="handleSizeChange"
            :page-sizes="[22, 50, 100, 500]" 
             layout="total, sizes, prev, pager, next, jumper"
            @current-change="handlePageChange"
            :current-page="page.currentPage"
            :page-size="page.pagesize"
          
            :total="page.total"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getApiphotovoltaicTimelist,
  getApicompanyslist,
  getApiwpByCplistlist,
} from "@/api/monthlyPerformanceAnalysis";
import dayjs from "dayjs";
export default {
  name: "PerformanceAssess",
  data() {
    return {
      companyVal: "",
      companyOptions: [],
      stationVal: "",
      stationOptions: [],
      pickerTimer: "",
      EvaluationData: [],
      page: {
        currentPage: 1,
        pagesize: 22,
        total: 0,
      },
      timeCount: 0,
    };
  },
  mounted() {
    this.getCompanyData();
    this.pickerTimer = dayjs().add(-1, "day").startOf("date");
  },
  computed: {
    pageHeight() {
      return {
        height: document.documentElement.clientHeight - 130 + "px",
      };
    },
  },
  methods: {
    getTimedate(time) {
      return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
    },
    eventData(event) {
      let showStr = "";
      if (event === 0) {
        showStr = "待机";
      } else if (event === 1) {
        showStr = "并网";
      } else if (event === 2) {
        showStr = "故障";
      } else if (event === 3) {
        showStr = "检修";
      } else if (event === 4) {
        showStr = "限电";
      } else if (event === 5) {
        showStr = "受累";
      } else if (event === 6) {
        showStr = "离线";
      }
      return showStr;
    },
    handleSizeChange(val) {
      this.page.currentPage = 1;
      this.page.pagesize = val;
      this.seachData();
    },

    handlePageChange(val) {
      this.page.currentPage = val;
      this.seachData();
    },
    // 获取公司列表
    async getCompanyData() {
      this.companyOptions = [];
      const { data: datas } = await getApicompanyslist();
      this.companyOptions = datas.data;
      this.companyVal = datas.data[1]?.id;
      this.getStationData();
    },
    changeCompan(val) {
      this.companyVal = val;
      this.stationOptions = [];
      this.stationVal = "";
      this.getStationData();
    },
    // 获取场站列表
    async getStationData() {
      this.stationOptions = [];
      let params = {
        type: -2,
        companyid: this.companyVal,
      };
      const { data: datas } = await getApiwpByCplistlist(params);
      this.stationOptions = datas.data;
      this.stationVal = datas.data[0].id;
      this.getTableData();
    },
    changeStation(val) {
      this.stationVal = val;
      this.getTableData();
    },
    rowState(row) {
      let showStr = "";
      if (row.event === 0) {
        showStr = "daijiStr";
      } else if (row.event === 1) {
        showStr = "bingwangStr";
      } else if (row.event === 2) {
        showStr = "guzhangStr";
      } else if (row.event === 3) {
        showStr = "jianxiuStr";
      } else if (row.event === 4) {
        showStr = "xiandianStr";
      } else if (row.event === 5) {
        showStr = "shouleiStr";
      } else if (row.event === 6) {
        showStr = "lixianStr";
      }
      return showStr;
    },
    seachData() {
      this.getTableData();
    },
    async getTableData() {
      let params = {
        stationId: this.stationVal,
        type: "IN",
        startTime: dayjs(this.pickerTimer).valueOf(),
        endTime: dayjs(this.pickerTimer).add(1, "day").valueOf(),
        pageNum: this.page.currentPage,
        pageSize: this.page.pagesize,
      };
      const { data: datas } = await getApiphotovoltaicTimelist(params);
      if (datas && datas.data) {
        let arr = [];
        for (let i in datas.data) {
          let obj = {
            stationId: i.substring(1),
            data: datas.data[i],
          };
          arr.push(obj);
        }
        this.timeCount =
          dayjs(this.pickerTimer).add(1, "day").valueOf() -
          dayjs(this.pickerTimer).valueOf();
        this.EvaluationData = arr.sort(this.sortBy("stationId"));
      }
      this.page.total = datas.total;
    },
    sortBy(stationId) {
      return (x, y) => {
        return x[stationId] - y[stationId];
      };
    },
  },
};
</script>

<style lang="less" scoped>
.timeTransition {
  padding: 0 20px;
  height: 100%;

  .Evaluation_title {
    .leftContent {
      width: 242px;
      height: 41px;
      line-height: 41px;
      background: url("~@/assets/imgs/title_left_bg1.png") no-repeat;
      span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #05bb4c;
        margin-left: 25px;
      }
    }
    .floatLeft {
      float: left;
    }
    .floatRight {
      float: right;
    }
    .rightContent {
      width: 212px;
      height: 28px;
      margin-top: 13px;
      background: url("../../../../assets/imgs/title_right_bg.png");
    }
  }
  .clearfix::after {
    content: "";
    clear: both;
    height: 0;
    line-height: 0;
    visibility: hidden;
    display: block;
  }
  .clearfix {
    zoom: 1;
  }
  .Evaluation_topall {
    display: flex;
    justify-content: space-between;
    .selections {
      position: relative;
      right: 120px;
      display: flex;
      margin-top: 10px;
      .selections_btn {
        flex: 0 0 55px;
        text-align: center;
        height: 33px;
        line-height: 33px;
        margin-right: 8px;
        color: #b9b9b9;
        font-size: 1.296vh;
        background: fade(#606769, 20);
        border: 1px solid fade(#606769, 20);
        border-radius: 20px;
        &:hover,
        &.active {
          background: fade(#05bb4c, 80);
          border: 1px solid #05bb4c;
          color: #fff;
          cursor: pointer;
        }
      }
    }
    .Evaluation_top {
      display: flex;
      flex-direction: row;
      align-items: center;
      padding-top: 10px;
      padding-bottom: 10px;

      .station {
        display: flex;
        flex-direction: row;
        align-items: center;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #b3b3b3;
        margin-right: 10px;
        margin-left: 10px;
      }

      .search-input {
        margin-left: 10px;
        .el-input__inner {
          width: 175px;
        }
        .el-input__suffix {
          right: -50px;
        }
      }
      .tabCut {
        display: inline-block;
        margin: 0 10px;

        div {
          display: inline-block;
          width: 60px;
          height: 27px;
          border: 1px solid #274934;
          text-align: center;
          line-height: 25px;
          cursor: pointer;
        }

        div:nth-child(1) {
          border-radius: 13px 0px 0px 13px;
          border-right-width: 0;
        }

        div:nth-child(2) {
          border-radius: 0px 13px 13px 0px;
        }

        .active {
          background-color: rgba(5, 187, 76, 0.9);
          color: #fff;
        }
      }

      .buttons {
        background-color: rgba(5, 187, 76, 0.2);
        border: 1px solid #3b6c53;
        color: #b3b3b3;
        font-size: 14px;

        &:hover,
        &.active {
          background-color: rgba(5, 187, 76, 0.5);
          color: #ffffff;
        }
      }
    }
  }
  .economicTable1 {
    height: calc(100% - 40px);
    .el-table--mini {
      .el-table__header-wrapper {
        .el-checkbox {
          display: none;
        }
      }
      .el-table__body-wrapper {
        .el-checkbox {
          .el-checkbox__input {
            display: block;
          }
        }
      }
    }
    .comStr {
      display: inline-block;
      // width: 10px;
      height: 20px;
    }
    .daijiStr {
      background: #1c99ff;
    }
    .bingwangStr {
      background: #05bb4c;
    }
    .guzhangStr {
      background: #ba3237;
    }
    .jianxiuStr {
      background: #e17d24;
    }
    .xiandianStr {
      background: #c530c8;
    }
    .shouleiStr {
      background: #ffffff;
    }
    .lixianStr {
      background: #606769;
    }

    .historyBtn {
      background: #43516b;
      border-radius: 15px;
      margin-top: 5px;
      border: 1px solid #43516b;
      span {
        color: #fff;
      }
    }
  }
  .el-overlay {
    .el-overlay-dialog {
      overflow-y: hidden !important;
      .EvaluationhistoryModel {
        margin-top: 0 !important;
        .el-dialog__body {
          height: calc(100% - 51px - 50px);
        }
      }
      .contrastModal {
        .el-dialog__header {
          border: none;
        }
        .el-dialog__body {
          padding-top: 10px;
        }
      }
    }
  }

  .el-picker__popper .el-date-range-picker__header .el-picker-panel__icon-btn {
    color: #fff;
  }
  .el-picker__popper .el-date-table .in-range div {
    background: #43516b;
  }
}
</style>
