<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-view-item"
          v-model="params.pageVo.dataStatus"
          placeholder="Status"
          @change="toSearch"
          clearable>
          <el-option
            label="Active"
            value="A" />
          <el-option
            label="Inactive"
            value="I" />
        </el-select>
        <el-select
          class="filter-view-item"
          v-model="params.pageVo.userType"
          placeholder="User Type"
          @change="toSearch"
          clearable>
          <el-option
            v-for="item in userTypes"
            :key="item"
            :label="item"
            :value="item" />
        </el-select>
        <div style="flex: 1; max-width: 350px;">
          <el-input
            v-model="params.pageVo.criteria"
            class="filter-view-item"
            prefix-icon="el-icon-search"
            placeholder="Search by E-mail, User ID, RFID number"
            @keyup.enter.native="toSearch"
            @change="toSearch"
            clearable/>
        </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 Card
          </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"
        min-width="140">
      </el-table-column>
      <el-table-column
        label="Email Address"
        align="center"
        prop="email"
        min-width="140">
      </el-table-column>
      <el-table-column
        label="User ID"
        align="center"
        prop="userPk"
        min-width="100">
      </el-table-column>
      <el-table-column
        label="User Type"
        align="center"
        prop="userType"
        min-width="120">
        <template slot-scope="{row}" v-if="row.userTypes">
          <span
            v-for="(item, idx) in row.userTypes"
            :key="idx"
            style="display: block;"
            :title="item">
            {{item}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="Group"
        align="center"
        prop="groupName"
        min-width="160">
        <template slot-scope="{row}" v-if="row.groupNames">
          <span
            v-for="(item, idx) in row.groupNames"
            :key="idx"
            style="display: block;"
            :title="item">
            {{item}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="Valid Date"
        align="center"
        prop="validDate"
        min-width="120"/>
      <el-table-column
        label="Status"
        align="center"
        prop="status"
        min-width="100px">
      </el-table-column>
      <el-table-column
        label="Action"
        align="center"
        min-width="140">
        <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'
import apiUser from '@/http/api/apiUser'
export default {
  data() {
    return {
      table: {
        data: [],
        total: 0,
        loading: false
      },
      params: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          dataStatus: "",
          userType: ""
        }
      },
      userTypes: []
    }
  },
  components: { Pagination, TableAction },
  created() {
    this.getUserTypeOption();
    this.getList();
  },
  methods: {
    toSearch() {
      this.params.pageNo = 1;
      this.getList();
    },
    getUserTypeOption() {
      apiUser.getUserTypeOptions().then(res => {
        if (res.data) {
          this.userTypes = res.data
        }
      }).catch(error => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    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)
        this.table.total = 0;
        this.table.data = [];
      });
    },
    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>