<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%;">
        <el-row :gutter="20">
          <el-col :span="7">
            <el-form-item
              label=""
              class="flex-item">
              <el-input
                v-model="filter.criteria"
                placeholder="Search by Provider Name/Address/Contact"
                @keyup.enter.native="handleFilter" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleFilter">
              Search
            </el-button>
          </el-col>
          <el-col :span="6" :offset="3" style="text-align: right;">
            <el-button
              v-if="!$route.meta.onlyView"
              class="filter-item"
              type="primary"
              icon="el-icon-plus"
              @click="addProvider">
                Add Provider
            </el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      fit
      style="width: 100%;min-height: 65vh;">
      <el-table-column
        label="Provider ID"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.providerPk }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Provider Name"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.providerName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="No.of Stations"
        align="center"
        class-name="fixed-width">
          <template slot-scope="{row}">
            <span>{{ row.boxCount }}</span>
          </template>
      </el-table-column>
      <el-table-column
        v-if="!$route.meta.onlyView"
        label="Action"
        align="center"
        class-name="fixed-width">
          <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: '/service-provider-management/add'})
      },
      editProvider(row) {
        //this.$store.commit('provider/SET_ProviderInfo', row)
        this.$router.push({
          path: '/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>