<template>
  <div>
    <div class="query mg-b-8">
      <div class="query-items">
        <div class="query-item">
          <div class="lable">开始日期：</div>
          <div class="search-input">
            <el-date-picker
              v-model="value1"
              @change="BeginChange(value1)"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择日期"
              popper-class="date-select"
            >
            </el-date-picker>
          </div>
        </div>
        <div class="query-item">
          <div class="lable">结束日期：</div>
          <div class="search-input">
            <el-date-picker
              v-model="value2"
              @change="EndChange(value2)"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="选择日期"
              popper-class="date-select"
            >
            </el-date-picker>
            <div class="unit svg-icon svg-icon-gray">
              <svg-icon :svgid="''" />
            </div>
          </div>
        </div>
      </div>
      <div class="query-actions">
        <el-button class="btn green" @click="handleSubmit">计算</el-button>
      </div>
    </div>
    <div class="table-box">
      <ComTable
        :data="tableData"
        :pageSize="20"
        height="85vh"
        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.vue";
import api from "@api/economic/index.js";
export default {
  name: "fwjsl",
  components: { ComTable },
  data() {
    return {
      value1: "",
      value2: "",
      tableLoading: true,
      tableData: {
        column: [
          {
            name: "风电场",
            field: "wpid",
            is_num: false,
            is_light: false,
            sortable: true,
          },
          {
            name: "故障次数(次)",
            field: "gzCount",
            is_num: false,
            is_light: false,
            sortable: true,
          },
          {
            name: "消缺及时次数(次)",
            field: "xqCount",
            is_num: false,
            is_light: false,
            sortable: true,
          },
          {
            name: "消缺及时次数(次)",
            field: "xqjsl",
            is_num: false,
            is_light: false,
            sortable: true,
          },
        ],
        date: [],
      },
    };
  },
  mounted() {
    this.getTable();
  },
  created() {
    this.value1 = this.getTime(1);
    this.value2 = this.getTime(2);
  },
  methods: {
    // 获取表格数据
    getTable() {
      let that = this;
      that.tableLoading = true;

      api
        .threerateXqjsl({
          beginDate: this.value1,
          endDate: this.value2,
        })
        .then((res) => {
          this.tableLoading = false;
          console.log(res);
          if (res.code === 200) {
            this.tableData.data = res.data;
          }
        });

      // that.API.requestData({
      //   method: "GET",
      //   baseURL: "http://10.155.32.4:8034/",
      //   subUrl: "/threerate/xqjsl",
      //   data: {
      //     beginDate: this.value1,
      //     endDate: this.value2
      //   },
      //   success(res) {
      //     that.tableLoading = false;
      //     console.log(res);
      //     if (res.code === 200) {
      //       that.tableData.data = res.data;
      //     }
      //   }
      // });
    },
    // 开始日期：
    BeginChange(vl) {
      this.value1 = vl;
    },
    // 结束日期
    EndChange(vl) {
      this.value1 = vl;
    },
    //   计算
    handleSubmit() {
      if (this.value1 == "" || this.value1 == null) {
        this.$message.error("请选择开始时间");
      } else if (this.value2 == "" || this.value2 == null) {
        this.$message.error("请选择结束时间");
      } else {
        this.getTable();
      }
    },
    // 默认开始时间
    getTime(val) {
      //时间戳处理，val=1是默认开始时间（当前月第一天），val=2是默认结束时间（今天）
      var date = new Date();
      var year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate();
      month >= 1 && month <= 9 ? (month = "0" + month) : "";
      day >= 0 && day <= 9 ? (day = "0" + day) : "";
      var begin = year + "-" + month + "-01";
      var end = year + "-" + month + "-" + day;
      if (val == 1) {
        return begin;
      } else if (val == 2) {
        return end;
      }
    },
  },
};
</script>
