<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="form" :model="queryForm" label-width="100px">
          <vab-query-form>
            <el-form-item
              width="100"
              prop="deviceName"
              :label="$t('project.xmmc')"
            >
              <el-input
                v-model="queryForm.projectName"
                size="small"
                placeholder="请输入项目名称"
              ></el-input>
            </el-form-item>
            <el-form-item width="100" prop="month" :label="$t('defalult.sj')">
              <el-date-picker
                v-model="queryForm.month"
                size="small"
                type="month"
                :placeholder="$t('defalult.xzrq')"
                @change="standardToTime"
              ></el-date-picker>
            </el-form-item>
          </vab-query-form>
        </el-form>
      </div>
      <div class="manage-button">
        <el-button size="small" @click="resetForm()">
          {{ $t('defalult.chongz') }}
        </el-button>
        <el-button type="primary" size="small" @click="search">
          {{ $t('defalult.cx') }}
        </el-button>
        <el-button type="primary" size="small" @click="exportData">
          {{ $t('defalult.dc') }}
        </el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="reportData"
      border
      tooltip-effect="light"
      style="width: 100%"
    >
      <el-table-column
        label="月"
        prop="month"
        align="center"
        width="120"
      ></el-table-column>
      <el-table-column
        :label="$t('project.xmmc')"
        prop="projectName"
        align="center"
      ></el-table-column>
      <el-table-column
        label="站点总数"
        prop="deviceCount"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        label="故障次数"
        prop="deviceFaultNum"
        align="center"
        width="100"
      ></el-table-column>
      <el-table-column
        label="总运行时长"
        prop="totalDuration"
        align="center"
        width="120"
      ></el-table-column>
      <el-table-column
        label="本月运行时长"
        prop="monthDuration"
        align="center"
        width="120"
      ></el-table-column>
      <el-table-column
        label="运行率"
        prop="runRatio"
        align="center"
        width="120"
      ></el-table-column>
      <el-table-column
        label="故障率"
        prop="faultRatio"
        align="center"
        width="120"
      ></el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.page"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>
<script>
import { deviceMonth, deviceMonthExport } from '@/api/analysis';
import ajax from '@/api/download';
export default {
  name: 'Index',
  components: {},
  data() {
    return {
      reportData: [],
      lazy: true,
      activeName: 'first',
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      input: '',
      textarea: '',
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
        month: '',
        projectId: '',
        projectName: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  methods: {
    //获取当前时间
    getDate(time = '') {
      var now = new Date();
      if (time != '') {
        now = new Date(time);
      }
      const year = now.getFullYear(); //得到年份
      let month = now.getMonth(); //得到月份
      month = month + 1;
      month = month.toString().padStart(2, '0');
      return `${year}-${month}`;
    },
    //标准时间转yyyy-mm-dd
    standardToTime() {
      this.queryForm.month = this.getDate(this.queryForm.month);
    },
    setDefaultTime() {
      this.queryForm.month = this.getDate();
    },
    //导出
    async exportData() {
      const url = await deviceMonthExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '电量月报.xls' });
    },
    //数据
    async fetchData() {
      this.listLoading = true;
      const { data } = await deviceMonth(this.queryForm);
      this.reportData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    showData() {
      this.setDefaultTime();
      this.fetchData();
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    handleAdd() {
      this.$refs['edit'].showEdit();
    },
    handleEdit(row) {
      this.$refs['edit'].showEdit(row);
    },
    handleQuery() {
      this.queryForm.page = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.page = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-wrap {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  .el-input {
    width: 200px !important;
  }
  .el-select {
    width: 200px !important;
  }
  .manage-button {
    padding-left: 12px;
    margin-bottom: 30px;
  }
}
</style>
