<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <div class="flex1" style="min-width: 300px; max-width: 350px;">
          <el-input
            v-model="filter.search"
            placeholder="Search by Site ID, Station ID"
            @keyup.enter.native="handleFilter" />
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleFilter">
            Search
          </el-button>
        </div>
        <div class="filter-flex-button">
          <el-button
            type="primary"
            icon="el-icon-download"
            :loading="logLoading"
            @click="handleDownloadLog">
            LOG
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      style="width: 100%;">
      <el-table-column
        label="Site"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.siteName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Station ID"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.chargeBoxId }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Connector ID"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.connectorId }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Connector Status"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.status }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Error Code"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.errorCode }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Vendor Error Code"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.vendorErrorCode }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Timestamp"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.statusTimestamp }}</span>
          </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="handlePageChange" />
    </div>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import TableAction from '@/components/TableAction.vue'
import api from '@/http/api/incident'

export default {
  components: { Pagination, TableAction },
  data() {
    return {
      filter: {
        search: "",
      },
      listLoading: true,
      logLoading: false,
      tableList: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    handleFilter() {
      this.listLoading = true;
      this.listQuery.page = 1;
      this.getList();
    },
    handlePageChange() {
      this.listLoading = true;
      this.getList();
    },
    handleDownloadLog() {
      this.logLoading = true;
      api.downloadLog().then(res => {
        if (res && res.size > 0) {
          const name = 'Log-' + new Date().getTime() + '.log'
          this.downloadFile(res, name);
        } else {
          this.$message.error('Empty files');
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.logLoading = false
      })
    },
    getList() {
      this.listLoading = true
      const params = {
        pageSize: this.listQuery.limit,
        pageNo: this.listQuery.page,
        pageVo: {
          criteria: this.filter.search,
        },
      }
      api.fetchErrorPages(params).then((response) => {
        const {success, data, total } = response
        if (success) {
          this.tableList = data
          this.total = total
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.listLoading = false
      })
    },
    downloadFile(res, fileName) {
      const blob = new Blob([res], {
        type: 'application/pdf;charset=utf-8'
      })
      // let href = window.URL.createObjectURL(blob)
      if ('download' in document.createElement('a')) {
        // 非IE下载
        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL 对象
        document.body.removeChild(elink)
      } else {
        // IE10+下载
        navigator.msSaveBlob(blob, fileName)
      }
    }
  }
}
</script>
