<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">
        <button class="btn green" @click="handleSubmit">计算</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";
export default {
  name: "fwjsl",
  components: { ComTable },
  data() {
    return {
      value1: "",
      value2: "",
      tableData: {
        column: [
          {
            name: "单位",
            field: "unit",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "5分钟 / 复位次数",
            field: "five",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "5分钟 / 复位率",
            field: "fives",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "10分钟 / 复位次数",
            field: "ten",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "10分钟 / 复位率",
            field: "tens",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "15分钟 / 复位次数",
            field: "fifteen",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "15分钟 / 复位率",
            field: "fifteens",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "20分钟 / 复位次数",
            field: "twenty",
            is_num: false,
            is_light: false,
            sortable: true
          },
          {
            name: "20分钟 / 复位率",
            field: "twentys",
            is_num: false,
            is_light: false,
            sortable: true
          }
        ],
        data: []
      },
      tableLoading: true
    };
  },
  mounted() {
    //   获取表格数据
    this.getTable();
  },
  created() {
    this.value1 = this.getTime(1);
    this.value2 = this.getTime(2);
  },
  methods: {
    //   获取表格数据
    getTable() {
      let that = this;
      that.tableLoading = true;
      that.API.requestData({
        method: "GET",
        baseURL: "http://10.155.32.4:8034/",
        subUrl: "/threerate/fwjsl",
        data: {
          beginDate: this.value1,
          endDate: this.value2
        },
        success(res) {
          that.tableLoading = false;
          if (res.code === 200) {
            var data = [];
            res.data.forEach((item, index) => {
              data[index] = {
                unit: item.wpid,
                five: item.wfz,
                fives: item.wfzfwl,
                ten: item.sfz,
                tens: item.sfzfwl,
                fifteen: item.swfz,
                fifteens: item.swfzfwl,
                twenty: item.esfz,
                twentys: item.esfzfwl,
                is_light: false
              };
            });
            that.tableData.data = data;
          }
        }
      });
    },
    BeginChange(vl) {
      this.value1 = vl;
    },
    EndChange(vl) {
      this.value2 = 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>
<style lang="less" scope>
@titleGray: #9ca5a8;
@rowGray: #606769;
@darkBack: #536268;
</style>
