<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="month" :label="$t('defalult.sj')">
              <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
        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('defalult.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 { elecDay, elecDayExport } from '@/api/analysis';
import ajax from '@/api/download';
import waterGraph from '@/views/analysis/dfunction/components/watergraph';

export default {
  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,
        month: '',
        projectId: '',
        deviceName: '',
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {},
  methods: {
    // 曲线图
    graph(row) {
      console.log(row);
      this.$refs['water'].showData(row, 'elecChar');
    },
    //获取当前时间
    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 elecDayExport(this.queryForm);
      ajax.downloadFile(url, { fileName: '电量日报.xls' });
    },
    //数据
    async fetchData() {
      this.listLoading = true;
      const { data } = await elecDay(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>
