<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.criteria"
            prefix-icon="el-icon-search"
            placeholder="Search by Provider Name"
            @keyup.enter.native="handleFilter"
            @change="handleFilter"
            clearable/>
        </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>
    </div>
    <el-table
      v-loading="listLoading"
      :data="tableList"
      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 Sites"
        align="center"
        prop="siteCount"
        min-width="120"/>
      <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="70">
        <template slot-scope="{row}">
          <!-- <TableAction
            @edit="editProvider(row)"
            @delete="deleteProvider(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 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'})
      },
      handleCommand(cb, item) {
        this[cb](item)
      },
      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>