<template>
  <div class="parcel-box">
    <div class="title">
      <div class="form-wrapper">
        <div class="select-wrapper">
          <el-select
            size="mini"
            v-model="tabIndex"
            placeholder="请选择"
            @change="tabClick"
          >
            <el-option
              v-for="item in tabOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            size="mini"
            v-model="company"
            placeholder="请选择"
            @change="search"
          >
            <el-option
              v-for="item in companyOptions"
              :key="item.id"
              :label="item.aname"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="date-wrapper">
          <div class="date-item-wrapper">
            <div class="date-item-date">
              <el-date-picker
                size="mini"
                v-model="searchYear"
                type="year"
                value-format="YYYY"
                placeholder="选择年"
              >
              </el-date-picker>
            </div>
          </div>
        </div>
      </div>
      <div class="btns">
        <el-button round size="mini" class="buttons search" @click="search"
          >搜索</el-button
        >
      </div>
      <!-- <el-button round size="mini">导出</el-button> -->
    </div>
    <div class="data-bodys">
      <div class="line clearfix">
        <div class="leftContent left">
          <span>利用小时分析</span>
        </div>
        <div class="rightContent line-right"></div>
      </div>
      <div class="economicTable">
        <el-table
          :data="tableData"
          style="width: 100%"
          size="mini"
          stripe
          height="100%"
          :header-cell-style="{ 'text-align': 'center' }"
          @row-click="rowClick"
        >
          <el-table-column
            align="left"
            prop=""
            width="80"
            label="场站"
            show-overflow-tooltip
            sortable
          >
            <template v-slot="scope">
              <span style="cursor: pointer">{{ scope.row.wpname }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="val"
            v-for="(val, index) in tableHead"
            :key="index"
            width="auto"
            sortable
          >
            <template v-slot="scope">
              <div v-if="scope.row.wpname == ''">
                <el-row>
                  <el-col :span="12"
                    ><span>{{ scope.row.data?.current }}</span></el-col
                  >
                  <el-col :span="12"
                    ><span>{{ scope.row.data.sameperiod }}</span></el-col
                  >
                </el-row>
              </div>
              <div v-else>
                <el-row>
                  <el-col :span="12">
                    <div style="white-space: nowrap; height: 100%">
                      <span style="white-space: nowrap">{{
                        scope.row.targetList &&
                        (scope.row.targetList[index]?.current ||
                        scope.row.targetList[index]?.current === 0
                          ? scope.row.targetList[index]?.current
                          : "--")
                      }}</span>
                      <i
                        v-if="
                          scope.row.targetList &&
                          (scope.row.targetList[index]?.compare > 0 ||
                            scope.row.targetList[index]?.compare === 0)
                        "
                        class="active_icon icon-arrow-up compare-lift-up"
                      ></i>
                      <i
                        v-if="
                          scope.row.targetList &&
                          scope.row.targetList[index]?.compare < 0
                        "
                        class="active_icon icon-arrow-down compare-lift-down"
                      ></i>
                    </div>
                  </el-col>
                  <el-col :span="12"
                    ><span>{{
                      scope.row.targetList &&
                      (scope.row.targetList[index]?.sameperiod ||
                      scope.row.targetList[index]?.sameperiod === 0
                        ? scope.row.targetList[index]?.sameperiod
                        : "--")
                    }}</span></el-col
                  >
                </el-row>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="echarts">
      <div class="chart-name">
        <div class="point left bottom"></div>
        <div class="point right bottom"></div>
        利用小时分析
      </div>
      <div style="height: calc(100% - 50px)">
        <BarCharts
          :list="barList"
          width="97%"
          height="100%"
          :top="25"
          :pillarName="pillarName"
          :xdate="false"
          :showLegend="true"
          :units="['']"
        ></BarCharts>
      </div>
    </div>
  </div>
</template>

<script>
import { GetOrganization } from "@/api/headerNav.js";
import BarCharts from "../../homePage/components/barCharts.vue";
import { lyxsList } from "@/api/curveAnalyse";
export default {
  name: "utillHoursFd", //利用小时分析
  components: {
    // ChartColumnar,
    // Panel,
    BarCharts,
  },
  data() {
    return {
      pillarName: "",

      list: [],

      tabIndex: -1,
      tabOptions: [
        { id: -1, name: "风电" },
        { id: -2, name: "光伏" },
      ],
      // barHeight: '260px',
      barList: [],
      tableData: [],
      tableHead: [
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
      ],
      company: "",
      companyOptions: [],
      searchYear: this.getmonthValue(),
      selectValue: "利用小时分析",
    };
  },
  watch: {},
  filters: {},
  computed: {},
  created() {
    this.initialization();
  },
  methods: {
    initialization() {
      GetOrganization({ type: this.tabIndex }).then((res) => {
        if (res.data) {
          this.companyOptions = res.data;
          this.company = res.data[0].id;
          this.search();
        }
      });
    },
    compare(property) {
      return (a, b) => {
        let val1 = a[property];
        let val2 = b[property];
        return val1 - val2;
      };
    },
    rowClick(list) {
      if (list?.wpname) {
        let barList = [
          {
            name: "本期",
            children: [],
            date: [],
          },
          {
            name: "同期",
            children: [],
          },
        ];
        this.pillarName = list.wpname;
        list.targetList.forEach((item) => {
          barList[0].date.push(item.month + "月");
          barList[0].children.push(item.current || 0);
          barList[1].children.push(item.compare || 0);
        });
        this.barList = barList;
      }
    },
    isNumber(val) {
      return typeof val === "number" && !isNaN(val);
    },
    search() {
      lyxsList({
        companys: this.company,
        type: this.tabIndex,
        year: this.monthChange(this.searchYear),
      }).then((res) => {
        if (res.data.length) {
          let arr = [
            {
              wpname: "",
              data: {
                wpname: "期次",
                current: "本期",
                sameperiod: "同期",
              },
            },
          ];
          this.tableData = [...arr, ...res.data];
          this.rowClick(res.data[res.data.length - 1]);
        } else {
          this.barList = [];
        }
      });
    },
    monthChange(data) {
      let year = new Date(data).getFullYear().toString();
      return year;
    },
    getmonthValue() {
      let year = new Date().getFullYear().toString();
      return year;
    },

    tabClick(data) {
      this.$router.push({
        path: "/economicsOperation/thematicAnalysis/utillHours",
      });
    },
  },
  mounted() {},
  beforeUnmount() {},
};
</script>

<style lang="less" scoped>
.parcel-box {
  padding: 0 20px;
  box-sizing: border-box;
  height: 100%;
  .title ::v-deep {
    display: flex;
    flex-direction: row;
    align-items: center;
    margin-top: 10px;
    margin-bottom: 10px;
    padding-left: 10px;
    .form-wrapper {
      display: flex;
      align-items: center;
      .select-wrapper {
        display: flex;
        align-items: center;
        .el-select {
          width: 155px;
          margin-right: 10px;
          .el-input .el-input__inner {
            width: 150px;
          }
        }
      }
      .date-wrapper {
        display: flex;
        align-items: center;
        font-size: 14px;
        font-family: Microsoft YaHei;
        font-weight: 400;
        color: #b3b3b3;
        .date-item-wrapper {
          white-space: nowrap;
          display: flex;
          align-items: center;
          margin-right: 15px;
          .date-item-date {
            .el-input {
              width: 155px;
            }
            .el-input .el-input__inner {
              width: 150px;
              font-size: 13px;
              color: #b3b3b3;
            }
          }
        }
      }
    }
    .but {
      display: flex;
      flex-direction: row;
      align-content: center;
    }
    .el-button + .el-button {
      margin-left: 0;
    }
    .buttons {
      background-color: rgba(0, 70, 199, 0.2);
      border: 1px solid #3b4c6c;
      color: #b3b3b3;
      font-size: 14px;
      margin-right: 10px;
      &:hover {
        background-color: rgba(0, 70, 199, 0.5);
        color: #ffffff;
      }
    }
  }

  .line {
    padding-bottom: 5px;
    .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;
      }
    }

    .rightContent {
      width: 212px;
      height: 28px;
      margin-top: 13px;
      background: url("../../../../../assets/imgs/title_right_bg.png");
    }
    .left {
      float: left;
    }
    .line-right {
      float: right;
    }
  }
  .data-bodys {
    height: calc(100% - 60px - 300px - 10px);
    display: flex;
    flex-direction: column;
    background-color: rgba(0, 0, 0, 0.45);
    border-radius: 5px;
    padding: 5px;
    .icon-arrow-down:before {
      color: #ff8300;
    }
    .icon-arrow-up:before {
      color: #1c99ff;
    }
    .economicTable {
      width: 100%;
      height: calc(100% - 46px);
    }
  }
  .echarts {
    margin-top: 10px;
    background-color: rgba(0, 0, 0, 0.45);
    border-radius: 5px;
    width: 100%;
    height: 300px;
  }
}

.clearfix::after {
  content: "";
  clear: both;
  height: 0;
  line-height: 0;
  visibility: hidden;
  display: block;
}

.clearfix {
  zoom: 1;
}

.el-table::before {
  height: 0;
}

.chart-name {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 100%;
  height: 40px;
  border-bottom: 1px solid rgba(106, 106, 106, 0.5);
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #ffffff;
}

.point {
  width: 6px;
  height: 1px;
  background-color: #ffffff;
  position: absolute;

  &.left {
    left: 0;
  }

  &.right {
    right: 0;
  }

  &.top {
    top: -1px;
  }

  &.bottom {
    bottom: -1px;
  }
}
</style>
