<template>
  <div class="monthMachine">
    <div class="monthMachine_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="changeBtnsd(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="请选择"
          clearable
          @change="changeStation"
        >
          <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="month"
            value-format="YYYY-MM"
            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
      style="
        background: rgba(0, 0, 0, 0.4);
        height: calc(100% - 76px);
        padding-bottom: 15px;
      "
    >
      <div class="monthMachine_title clearfix">
        <div class="leftContent floatLeft"><span>单机月度分析</span></div>
      </div>
      <div class="economicTable1">
        <el-table
          :data="monthMachineData"
          stripe
          size="mini"
          height="calc(100% - 40px)"
          ref="month_table"
          style="width: 100%"
        >
          <el-table-column align="center" label="机组名称" width="170">
            <template #default="scope">
              <span
                @click="reportDialogFn(scope.row)"
                style="cursor: pointer; color: #05bb4c"
                >{{ scope.row.windturbineName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            v-for="(item, index) in tableHeader"
            :key="index"
            sortable
            :prop="item.code"
            :label="item.title"
            align="center"
          >
            <template #header="scope">
              <div>
                {{
                  scope.column.label.slice(0, scope.column.label.indexOf("("))
                }}
              </div>
              <div>
                {{ scope.column.label.slice(scope.column.label.indexOf("(")) }}
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-size="page.pagesize"
          layout="total, prev, pager, next, jumper"
          :total="page.total"
        >
        </el-pagination>
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      width="70%"
      custom-class="modal"
      :close-on-click-modal="false"
    >
      <!-- <template #title>
                <div class="dialogHeader">
                    <span>{{ dialogTitle }}号风机{{ pickerTimerYear }}年{{ pickerTimerMonth }}月运行指标性能分析</span>
                </div>
            </template> -->
      <div class="body" :style="pageHeight">
        <report-dialog ref="reportDia"></report-dialog>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getApicompanyslist,
  getApiwpByCplistlist,
  getApisingleanalysisMainlist,
} from "@/api/monthlyPerformanceAnalysis";
import ReportDialog from "./reportDialog.vue";
import utils from "@/utills/downXlsx";
export default {
  name: "monthlyAnalysis", //单机月度分析
  components: {
    ReportDialog,
  },
  data() {
    return {
      companyVal: "",
      companyOptions: [],
      stationVal: "",
      stationOptions: [],
      pickerTimer: "",
      pickerTimerYear: "",
      pickerTimerMonth: "",
      monthMachineData: [],
      showBtn: true,
      tabIndex: -1,
      //   tabOptions: [
      //     { id: -1, name: "风电" },
      //     { id: -2, name: "光伏" },
      //   ],
      tableHeader: [
        { title: "实际发电量(万kWh)", code: "fdl" },
        { title: "理论发电量(万kWh)", code: "llfdl" },
        { title: "设备利用小时数(小时)", code: "lyxs" },
        { title: "等效可用系数(%)", code: "dxklyxs" },
        { title: "平均风速(m/s)", code: "fs" },
        { title: "小风切入风速(m/s)", code: "xfqr" },
        { title: "有效风时数(小时)", code: "yxfss" },
        { title: "故障损失电量(万kWh)", code: "gzss" },
        { title: "调度限电损失(万kWh)", code: "xdss" },
        { title: "计划检修损失(万kWh)", code: "jxss" },
        { title: "性能未达标损失(万kWh)", code: "xnss" },
        { title: "受累损失电量(万kWh)", code: "slss" },
        { title: "故障停用时间(小时)", code: "gzxs" },
        { title: "检修停用时间(小时)", code: "jxxs" },
        { title: "机组功率一致性(%)", code: "glyzxxs" },
      ],
      tableHeaderGf: [
        { title: "实际发电量(万kWh)", code: "fdl" },
        { title: "理论发电量(万kWh)", code: "llfdl" },
        { title: "设备利用小时数(小时)", code: "lyxs" },
        { title: "等效可用系数(%)", code: "dxklyxs" },
        { title: "平均光照度(m/s)", code: "fs" },
        { title: "有效光时数(小时)", code: "yxfss" },
        { title: "故障损失电量(万kWh)", code: "gzss" },
        { title: "调度限电损失(万kWh)", code: "xdss" },
        { title: "计划检修损失(万kWh)", code: "jxss" },
        { title: "性能未达标损失(万kWh)", code: "xnss" },
        { title: "受累损失电量(万kWh)", code: "slss" },
        { title: "故障停用时间(小时)", code: "gzxs" },
        { title: "检修停用时间(小时)", code: "jxxs" },
        { title: "机组功率一致性(%)", code: "glyzxxs" },
      ],
      page: {
        pagesize: 21,
        currentPage: 1,
        total: 0,
      },
      dialogTitle: "",
      dialogVisible: false,
      isFullScreen: false,
    };
  },
  created() {
    const clientHeight =
      document.documentElement.clientHeight || document.body.clientHeight;
    if (clientHeight === 1080) {
      this.isFullScreen = true;
    } else {
      this.isFullScreen = false;
    }
    this.getCompanyData();
  },
  mounted() {
    window.onresize = () => {
      const clientHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      this.isFullScreen = window.screen.height == clientHeight;
      this.getTableData();
    };
  },
  methods: {
    // 获取公司列表
    async getCompanyData() {
      this.companyOptions = [];
      this.pickerTimer = this.getchangeTime(new Date());
      const { data: datas } = await getApicompanyslist();
      this.companyOptions = datas.data;
      this.companyVal = datas.data[0]?.id;
      this.getStationData(this.companyVal);
    },
    // changeBtnsd(id) {
    //   this.tabIndex = id;
    //   this.stationVal = "";
    //   this.stationOptions = [];
    //   this.getStationData();
    // },
    changeCompan(val) {
      this.companyVal = val;
      this.getStationData();
    },
    async getStationData() {
      let params = {
        type: this.tabIndex,
        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();
    },
    seachData() {
      this.getTableData();
    },
    async getTableData() {
      let params = {
        cmId: this.companyVal,
        type: this.tabIndex,
        pageNum: this.page.currentPage,
        pageSize: this.page.pagesize,
        year: this.pickerTimer.slice(0, this.pickerTimer.indexOf("-")),
        month: this.pickerTimer.slice(this.pickerTimer.indexOf("-") + 1),
        wpId: this.stationVal,
      };
      const { data: datas } = await getApisingleanalysisMainlist(params);
      this.monthMachineData = datas.data.values;
      this.page.total = datas.data.total;
    },
    handleSizeChange(val) {
      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;
      return y + "-" + m;
    },
    reportDialogFn(row) {
      this.dialogVisible = true;
      this.dialogTitle = row.wtcode;
      this.pickerTimerYear = this.pickerTimer.substring(
        0,
        this.pickerTimer.indexOf("-")
      );
      this.pickerTimerMonth = this.pickerTimer.substring(
        this.pickerTimer.indexOf("-") + 1
      );
      this.$nextTick(() => {
        this.$refs.reportDia.pickerTimerYear = this.pickerTimerYear;
        this.$refs.reportDia.pickerTimerMonth = this.pickerTimerMonth;
        this.$refs.reportDia.dialogTitle = this.dialogTitle;
        this.$refs.reportDia.init(row);
      });
    },
    downXlsxFn() {
      let header = [];
      this.tableHeader.forEach((it) => {
        if (it.title !== "操作") {
          header.push(it.title);
        }
      });
      if (this.monthMachineData.length > 0) {
        utils.exportExcel(
          this.$refs["month_table"].$el,
          header,
          "单机月度分析"
        );
      }
    },
  },
};
</script>

<style lang="less">
.monthMachine {
  padding: 0 30px;
  height: 100%;
  .monthMachine_title {
    padding-left: 10px;
    .leftContent {
      width: 242px;
      height: 41px;
      line-height: 41px;
      background: url("~@/assets/imgs/title_left_bg1.png") no-repeat;
      span {
        font-size: 16px;
        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;
  }
  .monthMachine_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;
    }
    .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;
      }

      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:nth-child(1) {
        background: rgba(5, 187, 76, 0.6);
        border: 1px solid #3b6c53;
        border-radius: 13px;
        color: #fff;
        &:hover {
          background: rgba(5, 187, 76, 0.9);
          border-radius: 13px;
          color: #fff;
        }
      }
      .buttons:nth-child(2) {
        background: rgba(67, 81, 107, 0.3);
        border: 1px solid #3b6c53;
        border-radius: 13px;
        font-size: 14px;
        color: #b3b3b3;
      }
    }
  }
  .economicTable1 {
    height: calc(100% - 32px);
    .el-pagination {
      padding-top: 5px;
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
