<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form
        :model="filter"
        v-bind:inline="true"
        label-position="left"
        label-width="100px"
        style="width: 100%;">
        <div class="filter-view">
          <div class="flex1" style="min-width: 200px; max-width: 400px;">
            <el-input
              v-model="filter.criteria"
              placeholder="Search by Provider Name/Address/Contact"
              @keyup.enter.native="handleFilter" />
          </div>
          <div>
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleFilter">
              Search
            </el-button>
          </div>
          <div class="filter-flex-button">
            <el-button
              v-if="!$route.meta.onlyView"
              type="primary"
              icon="el-icon-plus"
              @click="addProvider">
                Add Provider
            </el-button>
          </div>
        </div>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      fit
      style="width: 100%;">
      <el-table-column
        label="Provider ID"
        align="center"
        min-width="120">
          <template slot-scope="{row}">
            <span>{{ row.providerPk }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Provider Name"
        align="center"
        min-width="150">
          <template slot-scope="{row}">
            <span>{{ row.providerName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="No.of Stations"
        align="center"
        min-width="120">
          <template slot-scope="{row}">
            <span>{{ row.boxCount }}</span>
          </template>
      </el-table-column>
      <el-table-column
        v-if="!$route.meta.onlyView"
        label="Action"
        align="center"
        min-width="140">
          <template slot-scope="{row}">
            <TableAction
              @edit="editProvider(row)"
              @delete="deleteProvider(row)"/>
          </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="handlePageChange" />
    </div>
  </div>
</template>

<script>
  import Pagination from '@/components/Pagination'
  import TableAction from '@/components/TableAction.vue'
  import provider from '../../http/api/provider'
  export default {
    components: { Pagination, TableAction },
    data() {
      return {
        filter: {
          criteria: ''
        },
        listLoading: true,
        tableList: [],
        total: 0,
        listQuery: {
          page: 1,
          limit: 10,
        },
      }
    },
    created() {
      this.getList();
    },
    methods: {
      handleFilter() {
        this.listLoading = true;
        this.listQuery.page = 1;
        this.getList();
      },
      handlePageChange() {
        this.listLoading = true;
        this.getList();
      },
      getList() {
        const params = {
          pageSize: this.listQuery.limit,
          pageNo: this.listQuery.page,
          pageVo: this.filter
        }
        provider.getServiceProviderPages(params).then(res => {
          this.listLoading = false;
          this.total = res.total;
          this.tableList = res.data;
        }).catch(err => {
          this.listLoading = false;
          this.total = 0;
          this.tableList = [];
        })
      },
      addProvider() {
        this.$router.push({path: '/partnership-management/service-provider-management/add'})
      },
      editProvider(row) {
        //this.$store.commit('provider/SET_ProviderInfo', row)
        this.$router.push({
          path: '/partnership-management/service-provider-management/edit/' + row.providerPk,
        });
      },
      deleteProvider(row) {
        this.$confirm('Are you sure you want to delete this service provider?', 'Delete', {
          confirmButtonText: 'OK',
          cancelButtonText: 'Cancel',
          type: 'warning'
        }).then(res => {
          this.handleDeleteProvider(row.providerPk)
        })
      },
      handleDeleteProvider(id) {
        provider.deleteProviderInfo({providerPk: id}).then(res => {
          this.$message({
            message: 'Delete service provider successfully',
            type: 'success'
          });
          this.listLoading = true;
          this.getList();
        }).catch(err => {
          this.$message({
            message: err,
            type: 'error'
          })
        })
      }
    }
  }
</script>