<template>
  <div class="app-container">
    <div class="filter-container right">
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="addDevice">
          Add Device
      </el-button>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.data"
      fit
      style="width: 100%;">
      <el-table-column
        label="Device Serial No."
        align="center"
        min-width="150px">
        <template slot-scope="{row}">
          <a class="link-type" href="javascript:void(0);" @click="viewDevice(row)">{{ row.deviceSerialNo }}</a>
        </template>
      </el-table-column>
      <el-table-column
        label="Site Name"
        align="center"
        prop="siteName"
        min-width="150px"/>
      <el-table-column
        label="Service Provider"
        align="center"
        prop="providerName"
        min-width="150px"/>
      <el-table-column
        label="Tied Chargers"
        align="center"
        min-width="120px">
        <template slot-scope="{row}">
          <div class="flexl">
            <span v-for="(item, index) in row.chargeBoxIds">{{ item }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="Status"
        align="center"
        prop="deviceStatus"
        min-width="80px"/>
      <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/posDevices.js'
export default {
  data() {
    return {
      table: {
        data: [],
        total: 0,
        loading: false
      },
      params: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: ""
        }
      }
    }
  },
  components: { Pagination, TableAction },
  created() {
    this.table.loading = true;
    this.getList();
  },
  methods: {
    getList() {
      api.getDevicePages(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)
      });
    },
    addDevice() {
      this.$router.push({
        path: '/pos-device-management/add'
      });
    },
    viewDevice({deviceId}) {
      this.$router.push({
        path: "/pos-device-management/edit/" + deviceId
      });
    },
    handleDelete(row) {
      this.$confirm('Are you sure you want to delete this device?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteDevice(row.deviceId)
      })
    },
    deleteDevice(id) {
      this.table.loading = true;
      api.deletePosDevice({deviceId: 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>