<template>
  <div class="manage-container">
    <div class="manage-wrap">
      <div class="manage-input">
        <el-form ref="queryForm" :model="queryForm" label-width="80px">
          <vab-query-form>
            <el-form-item
              width="100"
              prop="deviceName"
              :label="$t('dataEnquiry.zdmc')"
            >
              <el-input
                v-model="queryForm.deviceName"
                size="small"
                :placeholder="$t('dataEnquiry.qsrzdmc')"
              ></el-input>
            </el-form-item>
            <el-form-item width="100" prop="year" :label="$t('dfunction.xzsj')">
              <el-date-picker
                v-model="queryForm.year"
                size="small"
                type="year"
                :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
        v-for="(key, index) in headerData"
        :key="index"
        :label="key"
        :prop="key"
        :fixed="key == '站点名称' ? true : false"
        align="center"
        width="150"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="graph(row)">
            {{ $t('dfunction.qxt') }}
          </el-button>
        </template>
      </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>
    <water-graph ref="water"></water-graph>
  </div>
</template>
<script>
import { elecMonth, elecMonthExport } from '@/api/analysis';
import ajax from '@/api/download';
import { getDate2 } from '@/common/times';
import waterGraph from '@/views/analysis/dfunction/components/watergraph';
export default {
  name: 'Index',
  components: {
    waterGraph,
  },
  data() {
    return {
      reportData: [],
      headerData: {},
      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,
        year: '',
        projectId: '',
        deviceName: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {},
  methods: {
    // 曲线图
    graph(row) {
      this.$refs['water'].showData(row);
    },
    //标准时间转yyyy-mm-dd
    standardToTime() {
      this.queryForm.year = getDate2(this.queryForm.year);
    },
    setDefaultTime() {
      this.queryForm.year = getDate2();
    },
    //导出
    async exportData() {
      const url = await elecMonthExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '电量月报.xls' });
    },
    //数据
    async fetchData() {
      this.listLoading = true;
      const { data } = await elecMonth(this.queryForm);
      const reportData1 = data.items;
      var list = [];
      for (var i = 0; i < reportData1.length; i++) {
        list.push(reportData1[i]);
      }
      this.reportData = data.items;
      this.headerData = Object.keys(list[0]);
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    showData() {
      this.setDefaultTime();
      this.queryForm.projectId = sessionStorage.getItem('projectId');
      setTimeout(() => {
        this.fetchData();
      }, 1000);
    },
    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>
