<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="radio-group">
        <el-radio-group
          v-model="params.pageVo.notification"
          @change="changeTab">
          <el-radio-button
            v-for="(item,index) in typeTabs"
            :key="index"
            :label="item.value">
            {{item.name}}
          </el-radio-button>
        </el-radio-group>
      </div>
      <div class="filter-view">
        <div class="flex1" style="min-width: 200px; max-width: 350px;">
          <el-input
            v-model="params.pageVo.criteria"
            placeholder="Search by Site ID, Station ID"
            @keyup.enter.native="getTableData" />
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getTableData">
            Search
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list"
      style="width: 100%;">
      <el-table-column
        label="Site ID"
        align="center"
        width="100">
          <template slot-scope="{row}">
            <span>{{ row.sitePk }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Site Name"
        align="center"
        prop="siteName"/>
      <el-table-column
        label="Station ID"
        align="center"
        prop="chargeBoxId"/>
      <el-table-column
        label="Timestamp"
        align="center"
        prop="timestamp"/>
      <el-table-column
        label="Latest Notification"
        align="center"
        prop="notification"/>
      <el-table-column
        label="Action"
        align="center"
        width="100">
          <template slot-scope="{row}">
            <el-dropdown
              class="action-dropdown"
              @command="(v) => handleCommand(v, row)">
              <i class="el-icon-more icon-action"></i>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  command="viewDetail">
                  View Info
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="params.pageNo"
        :limit.sync="params.pageSize"
        @pagination="getTableData" />
    </div>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import api from '@/http/api/incident'
import { Base64 } from 'js-base64';
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          notification: ""
        }
      },
      table: {
        loading: false,
        list: [],
        total: 0
      },
      typeTabs: [{
        name: "Loading...",
        value: "-"
      }]
    }
  },
  components: { Pagination },
  created() {
    this.getTabList();
  },
  methods: {
    getTabList() {
      this.table.loading = true;
      api.getConnectivityTabs().then(res => {
        if (res.data) {
          this.typeTabs = res.data;
          const type = window.sessionStorage.getItem("notification")
          if (type) this.params.pageVo.notification = type;
          this.getTableData();
        } else {
          this.table.loading = false;
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.loading = false;
      })
    },
    getTableData() {
      this.table.loading = true;
      api.getConnectivityPages(this.params).then(res => {
        if (res.data) {
          this.table.list = res.data
          this.table.total = res.total
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.table.loading = false
      })
    },
    changeTab() {
      window.sessionStorage.setItem("notification", this.params.pageVo.notification)
      this.getTableData();
    },
    handleCommand(func, item) {
      this[func](item)
    },
    viewDetail(row) {
      const params = {
        sitePk: row.sitePk,
        //siteName: row.siteName,
        chargeBoxId: row.chargeBoxId
      }
      this.$router.push({
        path: "/incident-management/charger-connectivity/info/" + Base64.encode(JSON.stringify(params))
      })
    }
  }
}
</script>

<style lang="scss" scoped>
@import '../../styles/element-ui.scss';
.radio-group {
  padding-top: 5px;
  user-select: none;
}
.radio-group ::v-deep .el-radio-button {
  padding: 5px;
  box-shadow: none !important;
  .el-radio-button__inner {
    border-radius: 4px;
    border: 1px solid $--color-primary;
  }
}
</style>