<template>
  <div class="app-container">
    <div class="filter-container right">
      <el-button
        v-if="!$route.meta.onlyView"
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="addCreditLimit">
          Add Credit Limit
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      fit
      :row-class-name="({row})=>'read'+row.readStatus"
      style="width: 100%;">
      <!--el-table-column
        label="ID"
        align="center"
        class-name="fixed-width">
        <template slot-scope="{row}">
          <a class="link-detail" href="javascript:void(0);" @click="viewCreditLimit(row)">{{ row.groupCreditPk }}</a>
        </template>
      </el-table-column-->
      <el-table-column
        label="Group"
        align="center"
        min-width="120">
        <template slot-scope="{row}">{{ row.groupName }}</template>
      </el-table-column>
      <el-table-column
        label="Monthly Credit Limit"
        align="center"
        min-width="160">
        <template slot-scope="{row}">{{ row.creditLimitAmount }}</template>
      </el-table-column>
      <el-table-column
        label="Remaining Credit Limit"
        align="center"
        min-width="180">
        <template slot-scope="{row}">{{ row.creditLimitRemainingAmount }}</template>
      </el-table-column>
      <el-table-column
        label="Effective Date"
        align="center"
        min-width="120">
        <template slot-scope="{row}">{{ row.effectiveStartDate }}</template>
      </el-table-column>
      <el-table-column
        label="Status"
        align="center"
        min-width="100">
        <template slot-scope="{row}">{{ row.dataStatus }}</template>
      </el-table-column>
      <el-table-column
        label="Action"
        align="center"
        min-width="140">
        <template slot-scope="{row}">
          <TableAction
            :showEdit="!$route.meta.onlyView"
            :showView="$route.meta.onlyView"
            @edit="editLimit(row)"
            @delete="deleteLimit(row)"
            @view="viewCreditLimit(row)"/>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <Pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.pageNo"
        :limit.sync="listQuery.pageSize"
        @pagination="handlePageChange" />
    </div>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import TableAction from '@/components/TableAction.vue'
  import limit from '../../http/api/limit.js'
  export default {
    components: { Pagination, TableAction },
    data() {
      return {
        listLoading: false,
        tableList: [],
        total: 0,
        listQuery: {
          pageNo: 1,
          pageSize: 10,
          pageVo: {
            criteria: ""
          }
        },
      }
    },
    created() {
      this.listLoading = true;
      this.getList();
    },
    methods: {
      handleFilter() {
        this.listLoading = true;
        this.listQuery.page = 1;
        this.getList();
      },
      handlePageChange() {
        this.listLoading = true;
        this.getList();
      },
      getList() {
        limit.getCreditLimitPages(this.listQuery).then(res => {
          this.listLoading = false;
          this.total = res.total;
          this.tableList = res.data
        }).catch(err => {
          this.listLoading = false;
        });
      },
      addCreditLimit() {
        this.$router.push({
          path: '/partnership-management/monthly-credit-management/add'
        });
      },
      editLimit(row) {
        //this.$store.commit('provider/SET_CreditLimit', row);
        this.$router.push({
          path: '/partnership-management/monthly-credit-management/edit/' + row.groupCreditPk
        });
      },
      viewCreditLimit(row) {
        this.$store.commit('provider/SET_CreditLimit', row);
        this.$router.push({
          path: '/partnership-management/monthly-credit-management/limit/' + row.groupCreditPk
        });
      },
      deleteLimit(row) {
        this.$confirm('Are you sure you want to delete this credit limit?', 'Delete', {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(res => {
          this.handleDeleteLimit(row.groupCreditPk)
        })
      },
      handleDeleteLimit(id) {
        limit.deleteCreditLimit(id).then(res => {
          this.$message({
            message: 'Delete credit limit successfully',
            type: 'success'
          });
          this.listLoading = true;
          this.getList();
        }).catch(err => {
          this.$message({
            message: err,
            type: 'error'
          })
        })
      }
    }
  }
</script>

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