<template>
  <div class="singleMachine">
    <div class="singleMachine_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="tabCut">
        <div
          @click="changeBtn(val.id)"
          :class="tabIndex === val.id ? 'active' : ''"
          v-for="val in tabOptions"
          :key="val.id"
        >
          <span>{{ val.name }}</span>
        </div>
      </div>
      <div class="station">
        场站：
        <el-select
          size="mini"
          v-model="stationVal"
          placeholder="请选择"
          @change="changeStation"
          clearable
        >
          <el-option
            v-for="item in stationOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <div class="station">
        时间：
        <div>
          <el-date-picker
            v-model="pickerTimer"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择日期"
            popper-class="date-select"
          >
          </el-date-picker>
        </div>
      </div>
      <div class="but">
        <el-button round size="mini" class="buttons" @click="seachData"
          >搜 索</el-button
        >
        <el-button round size="mini" class="buttons" @click="downXlsxFn"
          >导出</el-button
        >
      </div>
    </div>
    <div class="singleMachine_content">
      <div class="singleMachine_title clearfix">
        <div class="leftContent floatLeft"><span>单机性能分析</span></div>
      </div>
      <div class="economicTable1">
        <el-table
          :data="singleMachineData"
          size="mini"
          stripe
          ref="stand_table"
          height="calc(100% - 40px)"
          style="width: 100%"
        >
          <el-table-column
            align="center"
            prop="wtcode"
            :label="tabIndex == -1 ? '风机' : '逆变器'"
            show-overflow-tooltip
            width="80"
            sortable
          >
            <template #default="scope">
              <span
                @click="seachDetail(scope.row)"
                style="cursor: pointer; color: #05bb4c"
                >{{ scope.row.wtcode }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            v-for="(item, index) in tabIndex === -1
              ? tableHeader
              : tableHeaderGf"
            :key="index"
            sortable
            :prop="item.code"
            :label="item.title"
            show-overflow-tooltip
            align="center"
          >
            <!-- <template #header="scope">
              <div v-if="scope.column.label.indexOf('(') > 0">
                <div>
                  {{
                    scope.column.label.slice(0, scope.column.label.indexOf("("))
                  }}
                </div>
                <div>
                  {{
                    scope.column.label.slice(scope.column.label.indexOf("("))
                  }}
                </div>
              </div>
              <div v-else>{{ scope.column.label }}</div>
            </template> -->

            <template #default="scope">
              <span>{{ scope.row[item.code] }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="80">
            <template #default="scope">
              <span class="historyBtn" @click="seachHistoryData(scope.row)"
                >历史</span
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pagesize"
          @size-change="handleSizeChange"
          :page-sizes="[23, 50, 100, 500]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      width="100%"
      custom-class="windhistoryDetailModel"
      destroy-on-close
      fullscreen
    >
      <template #title>
        <div class="dialog-title">
          <div class="title">{{ dialogTitle }}</div>
        </div>
      </template>
      <div style="height: 100%">
        <history-detail
          ref="windhistoryDetail"
          :tabIndex="tabIndex"
          :historyStationOptions="stationOptions"
        >
        </history-detail>
      </div>
    </el-dialog>
    <el-dialog
      v-model="dialogDetail"
      width="100%"
      custom-class="windDetailmodal"
      fullscreen
      destroy-on-close
    >
      <template #title>
        <div class="dialog-title">
          <div class="title">详情</div>
        </div>
      </template>
      <div style="height: 100%">
        <wind-detail-dialog
          ref="windDetail"
          :wpArray="stationOptions"
        ></wind-detail-dialog>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getApicompanyslist,
  getApiwpByCplistlist,
  getApiwindturbinegoodnesslist,
  getApiwindturbinegoodnesslistGf,
} from "@/api/monthlyPerformanceAnalysis";
import historyDetail from "./historyDetail.vue";
import WindDetailDialog from "./windDetailDialog.vue";
import utils from "@/utills/downXlsx";
import dayjs from "dayjs";
export default {
  name: "SingleWindAnasyle", //单机性能分析
  components: {
    historyDetail,
    WindDetailDialog,
  },
  data() {
    return {
      companyVal: "",
      companyOptions: [],
      stationVal: "",
      stationOptions: [],
      pickerTimer: "",
      singleMachineData: [],
      showBtn: true,
      dialogTitle: "",
      tabIndex: -1,
      tabOptions: [
        { id: -1, name: "风电" },
        { id: -2, name: "光伏" },
      ],
      tableHeader: [
        // { title: "风机", code: "wtcode" },
        { title: "型号", code: "modelId" },
        { title: "日排行榜", code: "dayTop" },
        { title: "日拟合优度(%)", code: "dayGoodness" },
        { title: "日风速(m/s)", code: "daySpeed" },
        { title: "月排行榜", code: "monthTop" },
        { title: "月拟合优度(%)", code: "monthGoodness" },
        { title: "月风速(m/s)", code: "monthSpeed" },
        { title: "年排行榜", code: "yearTop" },
        { title: "年拟合优度(%)", code: "yearGoodness" },
        { title: "年风速(m/s)", code: "yearSpeed" },
        // { title: "操作" },
      ],
      tableHeaderGf: [
        { title: "逆变器", code: "wtname" },
        { title: "型号", code: "model" },
        { title: "日系统效率", code: "rxtxl" },
        { title: "日离散率", code: "rlsl" },
        { title: "日转换效率", code: "rzhxl" },
        { title: "月系统效率", code: "yxtxl" },
        { title: "月离散率", code: "ylsl" },
        { title: "月转换效率", code: "yzhxl" },
        { title: "年系统效率", code: "nxtxl" },
        { title: "年离散率", code: "nlsl" },
        { title: "年转换效率", code: "nzhxl" },
        // { title: "操作" },
      ],
      page: {
        pagesize: 23,
        currentPage: 1,
        total: 0,
      },
      dialogVisible: false,
      dialogDetail: false,
      isFullScreen: false,
    };
  },
  created() {
    this.pickerTimer = dayjs().add(-1, "day").format("YYYY-MM-DD");
    this.getCompanyData();
  },
  mounted() {},
  computed: {
    pageHeight() {
      return {
        height: document.documentElement.clientHeight - 130 + "px",
      };
    },
    pagewindHeight() {
      return {
        height: document.documentElement.clientHeight - 100 + "px",
      };
    },
  },
  methods: {
    // 获取公司列表
    async getCompanyData() {
      this.companyOptions = [];
      const { data: datas } = await getApicompanyslist();
      this.companyOptions = datas.data;
      this.companyVal = datas.data[0]?.id;
      this.getStationData(this.companyVal);
    },
    changeBtn(id) {
      this.tabIndex = id;
      this.stationVal = "";
      this.singleMachineData = [];
      this.getStationData(this.companyVal);
    },
    changeCompan(val) {
      this.companyVal = val;
      this.stationVal = "";
      this.singleMachineData = [];
      this.getStationData(this.companyVal);
    },
    // 获取场站
    async getStationData(companyVal) {
      this.stationOptions = [];
      let params = {
        type: this.tabIndex,
        companyid: companyVal,
      };
      const { data } = await getApiwpByCplistlist(params);
      if (data.data.length) {
        this.stationOptions = data.data;
        this.stationVal = data.data[0].id || "";
      } else {
        this.stationOptions = [];
        this.stationVal = "";
      }
      this.getTableData();
    },
    changeStation(val) {
      this.stationVal = val;
      this.getTableData();
    },
    seachData() {
      this.getTableData();
    },
    async getTableData() {
      let params = {
        cmId: this.companyVal,
        type: this.tabIndex,
        pageNum: this.page.currentPage,
        pageSize: this.page.pagesize,
        recorddate: this.pickerTimer,
        wpId: this.stationVal,
      };
      let datas = {};
      if (this.tabIndex == -2) {
        datas = await getApiwindturbinegoodnesslistGf(params);
        this.singleMachineData = datas.data.data;
        this.page.total = datas.data.data.length;
      } else {
        datas = await getApiwindturbinegoodnesslist(params);
        this.singleMachineData = datas.data.data.records;
        this.page.total = datas.data.data.total;
      }
    },
    handleSizeChange(val) {
      this.page.currentPage = 1;
      this.page.pagesize = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.page.currentPage = val;
      this.getTableData();
    },
    //转换时间
    getchangeTime(date) {
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? "0" + m : m;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + m + "-" + d;
    },
    seachDetail(row) {
      this.dialogDetail = true;
      this.$nextTick(() => {
        this.$refs.windDetail.recorddate = this.pickerTimer;
        this.$refs.windDetail.init(row);
      });
    },
    seachHistoryData(row) {
      this.dialogVisible = true;
      this.dialogTitle = "历史数据查询";
      this.$nextTick(() => {
        let startT = dayjs(this.pickerTimer)
          .add(-10, "day")
          .format("YYYY-MM-DD");
        this.$refs.windhistoryDetail.pickerTimer = [startT, this.pickerTimer];
        if (this.tabIndex == -1) {
          this.$refs.windhistoryDetail.init(row);
        } else {
          row.windtpowerstationId = this.stationVal;
          this.$refs.windhistoryDetail.init(row);
        }
        // this.$refs.windhistoryDetail.getWindPowerStation();
      });
    },
    downXlsxFn() {
      let header = [];
      this.tableHeader.forEach((it) => {
        if (it.title !== "操作") {
          header.push(it.title);
        }
      });
      if (this.singleMachineData.length > 0) {
        utils.exportExcel(
          this.$refs["stand_table"].$el,
          header,
          "单机性能分析"
        );
      }
    },
  },
};
</script>

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

  .singleMachine_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;
    }
  }
  .singleMachine_content {
    background: rgba(0, 0, 0, 0.45);
    height: calc(100% - 48px);
  }
  .clearfix::after {
    content: "";
    clear: both;
    height: 0;
    line-height: 0;
    visibility: hidden;
    display: block;
  }

  .clearfix {
    zoom: 1;
  }

  .singleMachine_top {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding-top: 5px;
    padding-bottom: 5px;

    .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;
    }
    .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;
      }
    }

    .search-input {
      margin-left: 10px;
      .el-input__inner {
        width: 175px;
      }
      .el-input__suffix {
        right: -50px;
      }
    }

    .but {
      display: flex;
      flex-direction: row;
      align-content: center;
      margin-left: 20px;
      .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% - 42px);
    .el-table__fixed,
    .el-table__fixed-right {
      background: rgba(0, 0, 0, 1);
      border-left: 2px solid #000;
      height: calc(100% - 15px) !important;
    }

    .el-table__fixed::before {
      background-color: #2a2a2a;
    }

    .el-table__fixed-right::before {
      background-color: #2a2a2a;
    }

    .el-pagination {
      display: flex;
      justify-content: flex-end;
      padding-top: 5px;

      .el-pagination__total,
      .el-pagination__jump {
        color: #fff;
      }
    }

    .historyBtn {
      cursor: pointer;
      color: #05bb4c;
      margin-right: 15px;
    }
  }

  .el-overlay {
    .el-overlay-dialog {
      overflow-y: hidden !important;

      .windhistoryDetailModel,
      .windDetailmodal {
        margin-top: 0 !important;

        .el-dialog__body {
          padding: 10px 20px 20px 20px;
          height: calc(100% - 80px);
        }
      }
    }
  }
}
</style>
