<template>
  <div class="app-container">
    <div class="filter-container filter-view">
      <div class="filter-flex-button">
        <el-button
          type="primary"
          @click="onClickAdd">
          CREATE VOUCHER
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.data">
      <el-table-column
        label="Voucher ID"
        align="center"
        prop="voucherId"
        min-width="140">
        <template slot-scope="{row}">
          <div
            class="link-type"
            @click="onClickEdit(row)"
            v-if="row.dataStatus != 'Inactive'">
            {{row.voucherId}}
          </div>
          <span v-else>{{ row.chargeBoxId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="Voucher Name"
        align="center"
        prop="voucherName"
        min-width="180"/>
      <el-table-column
        label="Validity Period"
        align="left"
        prop="rfidNumber"
        min-width="130">
        <template slot-scope="{row}">
          <div>Start: {{row.periodStart}}</div>
          <div>End: {{row.periodEnd}}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="Quantity"
        align="center"
        prop="voucherQuantity"
        min-width="100"/>
      <el-table-column
        label="Voucher Type"
        align="center"
        prop="voucherType"
        min-width="120"/>
      <el-table-column
        label="Redemption Method"
        align="center"
        prop="redemptionMethod"
        min-width="180"/>
      <el-table-column
        label="Status"
        align="center"
        prop="dataStatus"
        min-width="80"/>
      <el-table-column
        label="Action"
        align="center"
        width="80"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)"
            v-if="row.dataStatus != 'Inactive'">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="onClickEdit">
                Edit
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickDelete">
                Delete
              </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.pageNum"
        :limit.sync="params.pageSize"
        @pagination="getTableData" />
    </div>
  </div>
</template>

<script>
import api from '../../http/api/voucher.js';
import Pagination from '@/components/Pagination'
export default {
  data() {
    return {
      params: {
        pageNum: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
        }
      },
      table: {
        data: [],
        total: 0,
        loading: false
      },
    };
  },
  components: {Pagination},
  created() {
    this.toSearch();
  },
  methods: {
    toSearch() {
      this.params.pageNum = 1;
      this.getTableData();
    },
    getTableData() {
      this.table.loading = true;
      api.getVoucherPages(this.params).then(res => {
        if (res.data && res.data.totalRow) {
          this.table.data = res.data.records
          this.table.total = res.data.totalRow
        } else {
          this.table.data = []
          this.table.total = 0
        }
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
        this.table.data = []
        this.table.total = 0
      }).finally(() => {
        this.table.loading = false
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickAdd() {
      this.$router.push({
        path: "/voucher-management/voucher/add"
      })
    },
    onClickEdit(row) {
      this.$router.push({
        path: "/voucher-management/voucher/update/" + row.voucherId
      })
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this voucher?', 'Delete', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteVoucher(row.voucherId);
      })
    },
    deleteVoucher(id) {
      this.table.loading = true;
      api.deleteVoucher(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.loading = false;
      })
    }
  }
}
</script>

<style scoped>

</style>