<template>
  <div class="app-container">
    <div class="filter-container right">
      <div class="filter-view">
        <div style="flex: 1; min-width: 180px; max-width: 350px;">
          <el-input
            v-model="params.pageVo.criteria"
            placeholder="Search by Name, Email, Contact or Login ID"
            @keyup.enter.native="getList" />
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getList">
            Search
          </el-button>
        </div>
        <div class="filter-flex-button">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addRfid">
              Add
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.data"
      fit
      style="width: 100%;">
      <!--el-table-column
        label="RFID No."
        align="center"
        class-name="fixed-width">
        <template slot-scope="{row}">
          <a class="link-detail" href="javascript:void(0);" @click="viewDevice(row)">{{ row.rfidNumber }}</a>
        </template>
      </el-table-column-->
      <el-table-column
        label="RFID No."
        align="center"
        prop="rfidNumber">
      </el-table-column>
      <el-table-column
        label="Email Address"
        align="center"
        prop="email"
        class-name="fixed-width">
      </el-table-column>
      <el-table-column
        label="User ID"
        align="center"
        prop="userPk">
      </el-table-column>
      <el-table-column
        label="User Type"
        align="center"
        prop="userType">
      </el-table-column>
      <el-table-column
        label="Group"
        align="center"
        prop="groupName">
      </el-table-column>
      <el-table-column
        label="Status"
        align="center"
        prop="status"
        width="100px">
      </el-table-column>
      <el-table-column
        label="Action"
        align="center"
        class-name="fixed-width">
        <template slot-scope="{row}">
          <TableAction
            @edit="viewDevice(row)"
            @delete="handleDelete(row)"/>
        </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="handlePageChange" />
    </div>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import TableAction from '@/components/TableAction.vue'
import api from '../../http/api/rfid.js'
export default {
  data() {
    return {
      table: {
        data: [],
        total: 0,
        loading: false
      },
      params: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: ""
        }
      }
    }
  },
  components: { Pagination, TableAction },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.table.loading = true;
      api.getRfidPages(this.params).then(res => {
        this.table.loading = false;
        this.table.total = res.total;
        this.table.data = res.data
      }).catch(err => {
        this.table.loading = false;
        this.$message.error(err)
      });
    },
    addRfid() {
      this.$router.push({
        path: '/rfid-card-management/add'
      });
    },
    viewDevice({ocppTagPk}) {
      this.$router.push({
        path: '/rfid-card-management/edit/' + ocppTagPk
      });
    },
    handleDelete(row) {
      this.$confirm('Are you sure you want to delete this rfid?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteRfid(row.ocppTagPk)
      })
    },
    deleteRfid(id) {
      this.table.loading = true;
      api.deleteRfidCard({ocppTagPk: id}).then(res => {
        this.$message({
          message: 'Delete successfully!',
          type: 'success'
        });
        this.getList();
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        });
        this.table.loading = false;
      })
    },
    handlePageChange() {
      this.table.loading = true;
      this.getList();
    }
  }
}
</script>

<style>
  .link-detail {
    text-decoration: underline;
  }
</style>