<template>
  <div class="parcel-box">
    <div class="search">
      <div class="search-left">
        <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 class="tabCut" v-if="$store.state.showGf">
          <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
            size="mini"
            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
        >
      </div>
      <div class="search-right">
        <img class="select-back" src="@assets/imgs/select-back.png" />
        <div class="title-select">
          <el-select
            v-model="selectValue"
            placeholder="请选择"
            @change="search()"
            clearable
          >
            <el-option
              v-for="item in selectList"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <!-- <el-button round size="mini">导出</el-button> -->
    </div>
    <div class="data-bodys">
      <div class="leftContent">
        <span>{{ selectValue }}</span>
      </div>

      <div class="economicTable">
        <el-table
          :data="tableData"
          style="width: 100%"
          size="mini"
          stripe
          height="100%"
          @row-click="rowClick"
        >
          <el-table-column
            align="center"
            prop=""
            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="140"
            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" class="columnFlex">
                    <div
                      v-if="
                        scope.row.targetList[index]?.current ||
                        scope.row.targetList[index]?.current === 0
                      "
                      class="box-item"
                    >
                      <el-tooltip
                        effect="dark"
                        :content="scope.row.targetList[index]?.current"
                        placement="top-start"
                      >
                        <div>
                          <span style="width: 80%">{{
                            scope.row.targetList &&
                            scope.row.targetList[index]?.current
                          }}</span>
                        </div>
                      </el-tooltip>
                    </div>
                    <div v-else class="box-item">--</div>
                    <div style="flex: 0 0 auto; line-height: 1">
                      <Bottom
                        class="botSvg"
                        v-if="
                          scope.row.targetList &&
                          scope.row.targetList[index]?.current <
                            scope.row.targetList[index]?.sameperiod
                        "
                      />
                      <Top
                        class="topSvg"
                        v-if="
                          scope.row.targetList &&
                          scope.row.targetList[index]?.current >
                            scope.row.targetList[index]?.sameperiod
                        "
                      />
                    </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 style="height: 100%">
        <BarCharts
          :list="barList"
          width="97%"
          height="100%"
          :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, mtbfList, mttrList } from "@/api/curveAnalyse";
import dataJson from "./dataJson.json"
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(),
      selectList: [
        {
          name: "MTBF",
        },
        {
          name: "MTTR",
        },
      ],
      selectValue: "MTBF",
    };
  },
  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);
        });
        this.barList = barList;
        this.BASE.closeLoading();
      }
    },
    // isNumber(val) {
    //     return typeof val === "number" && !isNaN(val);
    // },
    search() {
      this.BASE.showLoading();
      switch (this.selectValue) {
        case "MTBF":
            this.dataChange(dataJson.data.mtbfdata)
          mtbfList({
            companys: this.company,
            type: this.tabIndex,
            year: this.monthChange(this.searchYear),
          }).then(({ data: res }) => {
            this.dataChange(res.data)
          });
          break;

        case "MTTR":
            this.dataChange(dataJson.data.mttrdata)
          mttrList({
            companys: this.company,
            type: this.tabIndex,
            year: this.monthChange(this.searchYear),
          }).then(({ data: res }) => {
            this.dataChange(res.data)
          });
          break;
      }
    },
    dataChange(res) {
        if (res.length) {
            let arr = [
            {
                wpname: "",
                data: {
                wpname: "期次",
                current: "本期",
                sameperiod: "同期",
                },
            },
            ];
            this.tableData = [...arr, ...res];
            this.rowClick(res[res.length - 1]);
        } else {
            this.BASE.closeLoading();
            this.barList = [];
            this.tableData = [];
        }
    },
    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;
      if (data == -2) {
        this.company = "SXJ_KGDL_GFFD_ZGS";
        this.search();
      } else {
        this.company = this.companyOptions[0].id;
        this.search();
      }
    },
    initialization() {
        this.dataChange(dataJson.data.mtbfdata)
      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>
.parcel-box {
  padding: 0 20px;
  box-sizing: border-box;
  height: 100%;
  .search {
    display: flex;
    flex-direction: row;
    padding-top: 10px;
    padding-bottom: 10px;
    align-items: center;
    justify-content: space-between;

    .search-left {
      display: flex;
      flex-direction: row;
      align-items: center;
    }

    .search-right {
      position: relative;

      .select-back {
        position: absolute;
        right: 5px;
        top: 0px;
        z-index: 0;
      }

      .title-select {
        z-index: 2;
      }
    }

    .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;
        border-right-width: 0;
      }

      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;
      }
    }
  }
  .data-bodys {
    height: calc(100% - 58px - 350px - 20px);
    display: flex;
    flex-direction: column;
    background-color: rgba(0, 0, 0, 0.45);
    border-radius: 5px;
  }
  .economicTable {
    height: calc(100% - 32px);
    .el-table__body-wrapper {
      .columnFlex {
        display: flex;
        align-items: center;
      }
      .box-item {
        flex: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
    .topSvg {
      width: 1em;
      height: 1em;
      color: #1c99ff;
    }

    .botSvg {
      width: 1em;
      height: 1em;
      color: #ff8300;
    }
  }

  .leftContent {
    width: 242px;
    height: 45px;
    line-height: 45px;
    background: url("~@/assets/imgs/title_left_bg1.png") no-repeat;

    span {
      font-size: 16px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      color: #05bb4c;
      margin-left: 25px;
    }
  }

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

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