<template>
  <div class="cutInAndOutAnalysis">
    <div class="cutInAndOutAnalysis_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 :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 class="">
          <el-date-picker
            size="mini"
            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 sousuo" @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)">
      <div class="cutInAndOut_title clearfix">
        <div class="leftContent floatLeft"><span>切入切出分析</span></div>
        <div class="rightContent floatRight"></div>
      </div>
      <div class="economicTable1">
        <el-table
          :data="cutInAndOutAnalysisData"
          stripe
          size="mini"
          height="75vh"
          ref="cutInAndOut_table"
          style="width: 100%"
        >
          <el-table-column
            align="center"
            prop="wtcode"
            label="风机"
            width="150"
            fixed="left"
          >
          </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
                v-for="(item, index) in headerArr(scope.column.label)"
                :key="index"
              >
                {{ item }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="windturbineId"
            label="操作"
            width="70"
            fixed="right"
          >
            <template #default="scope">
              <!-- <el-button class="historyBtn" @click="seachHistoryData(scope.row)">历史</el-button> -->
              <span class="historyBtn1" @click="seachHistoryData(scope.row)"
                >历史</span
              >
            </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="100%"
      custom-class="modal"
      :close-on-click-modal="false"
    >
      <template #title>
        <div class="dialog-title">
          <img class="dialog-title-img" src="@assets/imgs/dialog-title.png" />
          <div class="title">历史数据查询</div>
        </div>
      </template>
      <div class="body">
        <history-detail
          ref="historyDetail"
          :historyCompanyOptions="companyOptions"
          :historyStationOptions="stationOptions"
          :tableHeader="tableHeader"
        >
        </history-detail>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getApicompanyslist,
  getApiwpByCplistlist,
  getApioutputSpeedlist,
} from "@/api/monthlyPerformanceAnalysis";
import HistoryDetail from "./historyDetail.vue";
import utils from "@/utills/downXlsx";
import dayjs from "dayjs";
export default {
  name: "cutInAndOutAnalysis", //切入切出分析
  components: {
    HistoryDetail,
  },
  data() {
    return {
      companyVal: "",
      companyOptions: [],
      stationVal: "",
      stationOptions: [],
      pickerTimer: "",
      cutInAndOutAnalysisData: [],
      showBtn: true,
      tabIndex: -1,
      tabOptions: [
        { id: -1, name: "风电" },
        { id: -2, name: "光伏" },
      ],
      tableHeader: [
        { title: "总小风切入", code: "inputSmall" },
        { title: "总大风切入", code: "inputBig" },
        { title: "总小风切出", code: "outputSmall" },
        { title: "总大风切出", code: "outputBig" },
        { title: "日小风切入", code: "dayInputSmall" },
        { title: "日小风切入合格率", code: "dayInputSmallRatio" },
        { title: "日小风切出", code: "dayOutputSmall" },
        { title: "日小风切出合格率", code: "dayOutputSmallRatio" },
        { title: "日大风切入", code: "dayInputBig" },
        { title: "日大风切入合格率", code: "dayInputBigRatio" },
        { title: "日大风切出", code: "dayOutputBig" },
        { title: "日大风切出合格率", code: "dayOutputBigRatio" },
        { title: "月小风切入", code: "monthInputSmall" },
        { title: "月小风切入合格率", code: "monthInputSmallRatio" },
        { title: "月大风切入", code: "monthInputBig" },
        { title: "月大风切入合格率", code: "monthInputBigRatio" },
        { title: "月小风切出", code: "monthOutputSmall" },
        { title: "月小风切出合格率", code: "monthOutputSmallRatio" },
        { title: "月大风切出", code: "monthOutputBig" },
        { title: "月大风切出合格率", code: "monthOutputBigRatio" },
        { title: "年小风切入", code: "yearInputSmall" },
        { title: "年小风切入合格率", code: "yearInputSmallRatio" },
        { title: "年大风切入", code: "yearInputBig" },
        { title: "年大风切入合格率", code: "yearInputBigRatio" },
        { title: "年小风切出", code: "yearOutputSmall" },
        { title: "年小风切出合格率", code: "yearOutputSmallRatio" },
        { title: "年大风切出", code: "yearOutputBig" },
        { title: "年大风切出合格率", code: "yearOutputBigRatio" },
      ],
      page: {
        pagesize: 17,
        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();
    };
  },
  computed: {
    pageHeight() {
      return {
        height: document.documentElement.clientHeight - 130 + "px",
      };
    },
  },
  methods: {
    headerArr(label) {
      let arr = [];
      let num = 0;
      if (label.length > 2) {
        if (label.indexOf("风") > 0) {
          arr.push(label.substring(0, label.indexOf("风") + 1));
          num = label.indexOf("风");
        }
        if (label.indexOf("入") > 0 || label.indexOf("出") > 0) {
          if (label.indexOf("入") > 0) {
            arr.push(label.substring(num + 1, label.indexOf("入") + 1));
            num = label.indexOf("入");
          } else {
            arr.push(label.substring(num + 1, label.indexOf("出") + 1));
            num = label.indexOf("出");
          }
        }
        arr.push(label.substring(num + 1, label.length));
      } else {
        arr.push(label);
      }

      return arr;
    },
    // 获取公司列表
    async getCompanyData() {
      this.companyOptions = [];

      this.pickerTimer = dayjs(new Date().getTime() - 86400000).format(
        "YYYY-MM-DD"
      );
      console.log(999, this.pickerTimer);
      const { data: datas } = await getApicompanyslist();
      this.companyOptions = datas.data;
      this.companyVal = datas.data[0]?.id;
      this.getStationData(this.companyVal);
    },
    changeCompan(val) {
      this.companyVal = val;
      this.stationVal = "";
      this.getStationData();
    },
    // changeBtn(id) {
    //   this.tabIndex = id;
    //   this.stationVal = "";
    //   this.getStationData();
    // },
    async getStationData() {
      this.stationOptions = [];
      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() {
      if (this.isFullScreen) {
        this.page.pagesize = 18;
      } else {
        this.page.pagesize = 17;
      }
      let params = {
        cmId: this.companyVal,
        type: this.tabIndex,
        pageNum: this.page.currentPage,
        pageSize: this.page.pagesize,
        recorddate: this.pickerTimer,
        wpId: this.stationVal,
      };
      const { data: datas } = await getApioutputSpeedlist(params);
      this.cutInAndOutAnalysisData = datas.data.records;
      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;
      var d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + m + "-" + d;
    },
    seachHistoryData(row) {
      this.dialogVisible = true;
      this.$nextTick(() => {
        let startT =
          new Date(this.pickerTimer).getTime() - 10 * 24 * 60 * 60 * 1000;
        this.$refs.historyDetail.pickerTimer = [
          this.getchangeTime(new Date(startT)),
          this.pickerTimer,
        ];
        this.$refs.historyDetail.init(row);
      });
    },
    downXlsxFn() {
      let header = [];
      this.tableHeader.forEach((it) => {
        if (it.title !== "操作") {
          header.push(it.title);
        }
      });
      if (this.cutInAndOutAnalysisData.length > 0) {
        utils.exportExcel(
          this.$refs["cutInAndOut_table"].$el,
          header,
          "切入切出分析"
        );
      }
    },
  },
};
</script>

<style lang="less">
.cutInAndOutAnalysis {
  padding: 0 23px;

  .cutInAndOut_title {
    padding-left: 10px;

    .leftContent {
      width: 242px;
      height: 41px;
      line-height: 41px;
      background: url("../../../../assets/imgs/title_left_bg.png");

      span {
        font-size: 16px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #ffffff;
        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;
  }

  .cutInAndOutAnalysis_top {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 10px;
    margin-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;
        cursor: not-allowed;
      }

      .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 {
    margin: 0 5px 5px 5px;
    padding-bottom: 10px;

    .el-pagination {
      display: flex;
      justify-content: flex-end;
      margin-right: 40px;
      margin-top: 20px;

      //   .btn-prev,
      //   .btn-next,
      //   .btn-quickprev,
      //   .btn-quicknext,
      //   .el-pager,
      //   .number {
      //     background: rgba(58, 63, 75, 0.4);
      //     color: #fff;
      //     border-radius: 2px;
      //   }

      //   .el-pager .active {
      //     background: #05bb4c;
      //     color: #fff;
      //     border-radius: 2px;
      //   }

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

        //     .el-input__inner {
        //       background: rgba(58, 63, 75, 0.2);
        //       border: 1px solid #3e4349;
        //       color: #fff;
        //       border-radius: 2px;
        //     }
      }
    }

    .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-overlay {
    .el-overlay-dialog {
      overflow-y: hidden !important;

      .el-dialog {
        margin-top: 0 !important;
      }
    }
  }

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

.historyBtn1 {
  cursor: pointer;
  color: #05bb4c;
}
</style>
