<template>
  <div>
    <div class="query mg-b-8">
      <div class="query-items">
        <div
          class="newspan"
          v-for="(item, index) of optionData"
          :key="index"
          :class="{ active: cur == index }"
          @click="handleOpen(item, index)"
        >
          {{ item }}
        </div>
      </div>
    </div>
    <div class="query mg-b-8">
      <div class="query-items">
        <div class="query-item">
          <div class="lable">场站：</div>
          <div class="search-input">
            <el-select
              v-model="value1"
              clearable
              placeholder="请选择"
              popper-class="select"
            >
              <el-option
                v-for="item in ChangZhan"
                :key="item.id"
                :value="item.id"
                :label="item.name"
              >
              </el-option>
            </el-select>
          </div>
        </div>
        <div class="query-item" v-if="shows == true">
          <div class="lable">年月：</div>
          <div class="search-input">
            <el-date-picker
              v-model="date"
              type="month"
              placeholder="选择年月"
              popper-class="date-select"
              value-format="YYYY-MM"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="query-item" v-else>
          <div class="lable">日期：</div>
          <div class="search-input">
            <el-date-picker
              v-model="date2"
              type="date"
              placeholder="选择日期"
              popper-class="date-select"
              value-format="YYYY-MM-DD"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="query-actions">
          <button class="btn green" @click="handleSubmit">查询</button>
        </div>
      </div>
    </div>
    <div class="table-box">
      <div class="title">{{ showTitle }}</div>
      <ComTable
        :data="tableData"
        :pageSize="20"
        height="73vh"
        v-loading="tableLoading"
        element-loading-text="拼命加载中.."
        element-loading-background="rgba(0, 0, 0, 0.8)"
      ></ComTable>
    </div>
  </div>
</template>
<script>
import ComTable from "@/components/coms/table/table-qc.vue";
import thermometerVue from "../../components/chart/bar/thermometer.vue";
export default {
  name: "qxpclfx",
  components: { ComTable },
  data() {
    return {
      cur: 0,
      optionData: ["月曲线偏差率排行榜", "日曲线偏差率排行榜"],
      ChangZhan: [],
      value1: "MHS_FDC",
      date: "",
      date2: "",
      shows: true,
      showTitle: "月曲线偏差率排行榜",
      tableLoading: true,
      tableData: {
        column: [
          {
            name: "风机",
            field: "fj",
            is_num: false,
            is_light: false,
            sortable: thermometerVue
          },
          {
            name: "实际 / 保证（%）",
            field: "sjbz",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          },
          {
            name: "实际 / 最优（%）",
            field: "sjzy",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          },
          {
            name: "最优 / 保证（%）",
            field: "zybz",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          },
          {
            name: "环比",
            field: "hb",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          },
          {
            name: "同比",
            field: "tb",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          },
          {
            name: "比标杆风机",
            field: "bbgfj",
            is_num: false,
            is_light: false,
            sortable: true,
            id: "id"
          }
        ],
        data: []
      },
      tableId: ""
    };
  },
  created() {
    this.ChangZhanVal();
    this.date = this.getDate(1);
    this.date2 = this.getDate(2);
    this.getTable(this.date);
  },
  methods: {
    // 获取表格数据 
    getTable(date) {
      let that = this;
      that.tableLoading = true;
      that.API.requestData({
        timeout:60000,
        method: "POST",
        subUrl: "/leaderboard/curvefittingmainList",
        data: {
          isAsc: "asc",
          wpId: that.value1,
          recorddate: date
        },
        success(res) {
          that.tableLoading = false;
          if (res.code === 200) {
            // that.tableData.data = res.data
            var data = [];
            res.data.forEach((item, index) => {
              data[index] = {
                fj: item.windturbineid,
                sjbz: item.deviationrate2,
                sjzy: item.deviationrate1,
                zybz: item.deviationrate3,
                hb: item.monthdeviationrate,
                tb: item.yeardeviationrate,
                bbgfj: item.standarddeviationrate
              };
            });
            that.tableData.data = data;
          }
        },
        Error(res) {
          console.log("tag", res);
        }
      });
    },
    //   tab
    handleOpen(vl, index) {
      this.$nextTick(() => {
        this.cur = index;
        this.showTitle = vl;
        if (index == 0) {
          this.getDate(1);
          this.shows = true;
        } else {
          this.getDate(2);
          this.shows = false;
        }
      });
    },
    // 场站
    ChangZhanVal() {
      var that = this;
      that.API.requestData({
        method: "GET",
        baseURL: "http://10.155.32.4:9001/",
        subUrl: "benchmarking/wplist",
        success(res) {
          that.ChangZhan = res.data;
          that.value1 = res.data[0].id;
        }
      });
    },
    // 查询
    handleSubmit() {
      if (this.shows == true) {
        // alert(this.date);
        this.getTable(this.date);
      } else {
        // alert(this.date2);
        this.getTable(this.date2);
      }
    },
    // 获取日期
    getDate(vl) {
      //时间戳处理，val=1是默认开始时间（当前月第一天），val=2是默认结束时间（今天）
      var date = new Date();
      var year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate() - 1;
      month >= 1 && month <= 9 ? (month = "0" + month) : "";
      day >= 0 && day <= 9 ? (day = "0" + day) : "";
      var begin = year + "-" + month;
      var end = year + "-" + month + "-" + day;
      if (vl == 1) {
        return begin;
      } else if (vl == 2) {
        return end;
      }
    },
  }
};
</script>
<style scoped>
.newsDiv {
  color: rgba(255, 255, 255, 0.75);
  background-color: rgba(255, 255, 255, 0.1);
  margin-bottom: 8px;
  line-height: 3.4259vh;
  padding: 0 15px;
}
.newspan {
  line-height: 30px;
  cursor: pointer;
  padding: 0 1vw;
  margin: 0 2px;
  color: #9ca5a8;
  transition: color 0.2s ease-in-out;
  position: relative;
}
.newspan:hover {
  background: linear-gradient(
    to top,
    rgba(5, 187, 76, 0.5),
    rgba(5, 187, 76, 0)
  );
  color: white;
  position: relative;
}
.newspan:hover::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 0.463vh;
  border: 0.093vh solid #05bb4c;
  border-top: 0;
  left: 0;
  bottom: 0;
  box-sizing: border-box;
}
.active {
  background: linear-gradient(
    to top,
    rgba(5, 187, 76, 0.5),
    rgba(5, 187, 76, 0)
  );
  color: white;
  position: relative;
}
.active::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 0.463vh;
  border: 0.093vh solid #05bb4c;
  border-top: 0;
  left: 0;
  bottom: 0;
  box-sizing: border-box;
}
.title {
  background: rgba(255, 255, 255, 0.1);
  margin-bottom: 8px;
  padding: 1vh;
}
</style>