<template>
  <div class="parcel-box">
    <div class="search">
      <el-select size="mini" v-model="company" placeholder="请选择">
        <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="tabClick(val.id)"
          :class="tabIndex === val.id ? 'active' : ''"
          v-for="val in tabOptions"
          :key="val.id"
        >
          <span>{{ val.name }}</span>
        </div>
      </div>
      <div class="search-input">
        <el-date-picker
          v-model="searchYear"
          type="year"
          value-format="YYYY"
          placeholder="选择年"
          popper-class="date-select"
        >
        </el-date-picker>
      </div>
      <el-button round size="mini" class="searchColor" @click="search"
        >搜索</el-button
      >
      <!-- <el-button round size="mini">导出</el-button> -->
    </div>
    <div class="data-bodys">
      <div class="line clearfix">
        <div class="leftContent left"><span>性能损失率</span></div>
      </div>
      <div class="economicTable">
        <el-table
          :data="tableData"
          style="width: 100%"
          size="mini"
          stripe
          height="50vh"
          @row-click="rowClick"
        >
          <el-table-column
            align="center"
            prop=""
            label="风场"
            width="165"
            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="142"
            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" style="position: relative">
                    <div
                      style="
                        padding: 0 10px;
                        width: 100%;
                        height: 100%;
                        display: flex;
                        flex-direction: row;
                        align-items: center;
                        justify-content: center;
                      "
                    >
                      <span style="width: 40px">{{
                        scope.row.targetList &&
                        (scope.row.targetList[index]?.current ||
                        scope.row.targetList[index]?.current === 0
                          ? scope.row.targetList[index]?.current
                          : "--")
                      }}</span>
                      <i
                        style="position: absolute; right: 0"
                        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
                        style="position: absolute; right: 0"
                        v-if="
                          scope.row.targetList &&
                          scope.row.targetList[index]?.compare < 0
                        "
                        class="active_icon icon-arrow-dpwn 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" style="height: 30vh">
      <div class="chart-name">
        <div class="point point-left bottom"></div>
        <div class="point point-right bottom"></div>
        性能损失率
      </div>
      <div>
        <BarCharts
          :list="barList"
          width="100%"
          height="32vh"
          :top="60"
          :pillarName="pillarName"
          :xdate="false"
          :showLegend="true"
          :units="['', '']"
        ></BarCharts>
      </div>
    </div>
  </div>
</template>

<script>
import Panel from "@/components/curveAnalyse/panel.vue";
import ChartColumnar from "@/components/curveAnalyse/multiple-bar-chart.vue";

import BarCharts from "@/views/economicsOperation/thematicAnalysis/components/barCharts.vue";
import { companys, xnsslList } from "@/api/curveAnalyse";
export default {
  name: "curveAnalyse",
  components: {
    ChartColumnar,
    Panel,
    BarCharts,
  },
  data() {
    return {
      pillarName: "",
      echartsData: [],
      tableName: "",
      list: [],
      DayPower: {
        // 图表所用单位
        units: [""],
        value: [
          {
            title: "",
            yAxisIndex: 0, // 使用单位
            value: [],
          },
        ],
      },
      BarChartsList: [],
      tabIndex: -1,
      tabOptions: [
        { id: -1, name: "风电" },
        { id: -2, name: "光伏" },
      ],
      barHeight: "260px",
      barList: [],
      tableData: [],
      tableHead: [
        "一月",
        "二月",
        "三月",
        "四月",
        "五月",
        "六月",
        "七月",
        "八月",
        "九月",
        "十月",
        "十一月",
        "十二月",
      ],
      company: "",
      companyOptions: [],
      searchYear: this.getmonthValue(),
    };
  },
  watch: {},
  filters: {},
  computed: {},
  created() {
    this.initialization();
  },
  methods: {
    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);
        });
      }
    },
    isNumber(val) {
      return typeof val === "number" && !isNaN(val);
    },
    search() {
      xnsslList({
        companys: this.company,
        type: this.tabIndex,
        year: this.monthChange(this.searchYear),
      }).then(({ data: res }) => {
        if (res.data) {
          let arr = [
            {
              wpname: "",
              data: {
                wpname: "期次",
                current: "本期",
                sameperiod: "同期",
              },
            },
          ];
          this.tableData = [...arr, ...res.data];
          this.rowClick(res.data[res.data.length - 1]);
        }
      });
    },
    monthChange(data) {
      let year = new Date(data).getFullYear().toString();
      return year;
    },
    getmonthValue() {
      let year = new Date().getFullYear().toString();
      return year;
    },

    tabClick(data) {
      this.tabIndex = data;
      this.search();
    },
    initialization() {
      companys().then(({ data: res }) => {
        if (res.data) {
          this.companyOptions = res.data;
          this.company = res.data[0].id;
          this.search();
        }
      });
    },
  },
  mounted() {},
  beforeUnmount() {},
};
</script>

<style lang="less" scoped>
.data-bodys {
  display: flex;
  flex-direction: column;
  background-color: rgba(0, 0, 0, 0.45);
  border-radius: 5px;
  padding: 5px;
}

.parcel-box {
  padding: 0 20px;
  box-sizing: border-box;

  .tableDataBox {
  }

  .line {
    padding-bottom: 5px;
    .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: #ffffff;
        margin-left: 25px;
      }
    }

    .rightContent {
      width: 212px;
      height: 28px;
      margin-top: 13px;
      background: url("../../../../assets/imgs/title_right_bg.png");
    }
  }

  .search {
    display: flex;
    padding-top: 10px;
    align-items: center;

    .tabCut {
      display: inline-block;
      margin: 0 20px;

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

    button {
      margin-left: 10px;
      background: rgba(67, 81, 107, 0.3);
      border: 1px solid #274934;
      color: #b3b3b3;
    }

    .searchColor {
      background-color: rgba(5, 187, 76, 0.2);
      border: 1px solid #3b6c53;
      color: #b3b3b3;
      font-size: 14px;

      &:hover {
        background-color: rgba(5, 187, 76, 0.5);
        color: #ffffff;
      }
    }
  }

  .echarts {
    margin-top: 20px;
    background-color: rgba(0, 0, 0, 0.45);
    border-radius: 5px;
    width: 100%;
  }
}

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

.clearfix {
  zoom: 1;
}

.left {
  float: left;
}

.right {
  float: right;
}

/deep/ .el-input__prefix {
  right: -15px;
  left: unset;
}

/deep/ .el-input--prefix .el-input__inner {
  padding-left: 20px;
}

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

.compare-lift-down {
  margin-left: 7px;
  font-size: 14px;
  font-family: Arial;
  font-weight: 400;
  color: #ff8300;
}

.compare-lift-up {
  margin-left: 7px;
  font-size: 14px;
  font-family: Arial;
  font-weight: 400;
  color: #1c99ff;
}

.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 {
    top: 0;
    right: 0;
  }

  &.top {
    top: -1px;
  }

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