<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="month"
              :label="$t('dfunction.xzsj')"
            >
              <el-date-picker
                v-model="queryForm.month"
                size="small"
                type="month"
                :placeholder="$t('dfunction.xzsj')"
                @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="$t('dfunction.y')"
        prop="month"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.xmmc')"
        prop="projectName"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.zdzs')"
        prop="deviceCount"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.gzcs')"
        prop="deviceFaultNum"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.zyxsc')"
        prop="totalDuration"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.byyxsc')"
        prop="monthDuration"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.yxl')"
        prop="runRatio"
        align="center"
      ></el-table-column>
      <el-table-column
        :label="$t('dfunction.gzl')"
        prop="faultRatio"
        align="center"
      ></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;
    },
  },
  created() {
    this.queryForm.projectId = sessionStorage.getItem('projectId');
    this.setDefaultTime();
    this.fetchData();
  },
  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>
