<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <div class="flex1" style="min-width: 200px; max-width: 350px;">
          <el-input
            v-model="filter.pageCriteria.criteria"
            prefix-icon="el-icon-search"
            placeholder="Search by Provider Name"
            @keyup.enter.native="handleFilter"
            @change="handleFilter"
            clearable/>
        </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>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.data">
      <el-table-column
        label="Provider ID"
        align="center"
        min-width="100">
        <template slot-scope="{row}">
          <span v-if="$route.meta.onlyView">{{ row.providerPk }}</span>
          <div v-else class="link-type" @click="editProvider(row)">{{row.providerPk}}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="Provider Name"
        prop="providerName"
        align="center"
        min-width="150"/>
      <el-table-column
        label="Country"
        prop="country"
        align="center"
        min-width="90"/>
      <el-table-column
        label="No.of Sites"
        align="center"
        prop="siteCount"
        min-width="120"/>
      <el-table-column
        label="No.of Stations"
        prop="boxCount"
        align="center"
        min-width="150"/>
      <el-table-column
        v-if="!$route.meta.onlyView"
        label="Action"
        align="center"
        min-width="80">
        <template slot-scope="{row}">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="editProvider">
                Edit
              </el-dropdown-item>
              <el-dropdown-item
                command="deleteProvider">
                Delete
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import TableAction from '@/components/TableAction.vue'
import provider from '../../api/provider'
export default {
  data() {
    return {
      filter: {
        pageNum: 1,
        pageSize: 10,
        pageCriteria: {
          criteria: ""
        }
      },
      table: {
        loading: false,
        data: [],
        total: 0
      }
    };
  },
  created() {
    this.handleFilter()
  },
  methods: {
    handleFilter() {
      this.table.pageNum = 1;
      this.getTableData();
    },
    getTableData() {
      this.table.loading = true;
      provider.getServiceProviderPages(this.filter).then(res => {
        this.table.loading = false;
        if (res.data.records && res.data.totalRow) {
          this.table.total = res.data.totalRow;
          this.table.data = res.data.records;
        } else {
          this.table.total = 0;
          this.table.data = [];
        }
      }).catch(err => {
        this.table.loading = false;
        this.table.total = 0;
        this.table.data = [];
      })
    },
    addProvider() {
      this.$router.push({
        path: '/partnership-management/service-provider-mgmt/add',
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    editProvider(row) {
      this.$router.push({
        path: '/partnership-management/service-provider-mgmt/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(id).then(res => {
        this.$message({
          message: 'Delete service provider successfully',
          type: 'success'
        });
        this.getTableData();
      }).catch(err => {
        this.$message({
          message: err,
          type: 'error'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>