<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-view-item"
          v-model="filters.pageVo.groupType"
          placeholder="Group Type"
          @change="onClickSearch"
          clearable>
          <el-option
            v-for="item in groupTypes"
            :key="item.value"
            :label="item.name"
            :value="item.value" />
        </el-select>
        <el-select
          class="filter-view-item"
          v-model="filters.pageVo.countryCode"
          placeholder="Country"
          @change="onClickSearch"
          clearable>
          <el-option
            v-for="item in countryList"
            :key="item.value"
            :label="item.name"
            :value="item.value" />
        </el-select>
        <div style="flex: 1; max-width: 300px;">
          <el-input
            v-model="filters.pageVo.criteria"
            class="filter-view-item"
            prefix-icon="el-icon-search"
            placeholder="Search by Group Name"
            @keyup.enter.native="onClickSearch"
            @change="onClickSearch"
            clearable/>
        </div>
        <div>
          <el-button 
            @click="onClickSearch"
            icon="el-icon-search"
            type="primary">
            Search
          </el-button>
        </div>
        <div class="filter-flex-button" v-if="!$route.meta.onlyView">
          <el-button
            icon="el-icon-plus"
            type="primary"
            @click="onClickAdd">
            Add Group
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      :data="table.list"
      class="no-border"
      v-loading="table.loading">
      <!-- <el-table-column
        align="center"
        label="Group ID"
        prop="groupPk"
      ></el-table-column> -->
      <el-table-column
        align="center"
        label="Name"
        prop="groupName"
        min-width="200"
        fixed="left">
        <template v-slot="{ row }">
          <span
            class="link-type"
            v-if="!$route.meta.onlyView"
            @click="onClickEditButton(row)">
            {{row.groupName}}
          </span>
          <div v-else>{{row.groupName}}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Type"
        prop="groupType"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Country"
        prop="country"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Approved"
        prop="approvedCount"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Pending"
        prop="pendingCount"
        min-width="120"/>
      <!-- <el-table-column
        align="center"
        label="No. Of Drivers"
        prop="noOfDrivers"/> -->
      <el-table-column
        align="center"
        label="Country"
        prop="country"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Discount"
        prop="discount"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Eligible Sites (Discount)"
        prop="eligibleSiteCount"
        min-width="120">
        <template slot="header">
          Eligible Sites<br/><span style="white-space: nowrap;">(Discount)</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Action"
        min-width="100"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ 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="assignSites">
                Assign Sites (Discount)
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickEditButton">
                Edit Info
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickDeleteButton">
                Delete
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <TableAction
            @edit="onClickEditButton(row)"
            @delete="onClickDeleteButton(row)"/> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="table.pageNo"
        :limit.sync="table.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogAssignment
      :visible="assign.visible"
      :title="'ASSIGN SITES (DISCOUNT: ' + assign.item.discount + ')'"
      :group="assign.item"
      @hide="assignSites"/>
  </div>
</template>

<script>
import TableAction from '@/components/TableAction.vue'
import Pagination from '@/components/Pagination'
import DialogAssignment from '@/components/DialogAssignment'
import api from '@/http/api/group'
import site from '@/http/api/site'

export default {
  components: { Pagination, TableAction, DialogAssignment },
  data() {
    return {
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: '',
          groupType: '',
          countryCode: '',
        }
      },
      table: {
        list: [],
        total: 0,
        loading: false
      },
      assign: {
        item: {},
        visible: false
      },
      groupTypes: [],
      countryList: []
    }
  },
  created() {
    this.getGroupTypeOptions();
    this.getCountryList();
    this.getTableData()
  },
  methods: {
    onClickSearch() {
      this.filters.pageNo = 1;
      this.getTableData();
    },
    getGroupTypeOptions() {
      api.getUserGroupType().then(res => {
        if (res.data) {
          this.groupTypes = res.data
        }
      }).catch(err => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getCountryList() {
      site.getCountryList().then(res => {
        if (res.data) {
          this.countryList = res.data
        }
      }).catch(err => {
        this.$message({
          message: error,
          type: 'error'
        })
      })
    },
    getTableData() {
      this.table.loading = true;
      api.getGroupPages(this.filters).then(res => {
        if (res.data && res.total) {
          this.table.total = res.total;
          this.table.list = res.data;
        } else {
          this.table.total = 0;
          this.table.list = [];
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.total = 0;
        this.table.list = [];
      }).finally(() => {
        this.table.loading = false;
      })
    },
    onClickAdd() {
      this.$router.push({
        path: "/partnership-management/group-management/add"
      })
    },
    onClickDeleteButton(row) {
      this.$confirm('Are you sure you want to delete this group?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.onDeleteCompany(row);
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    assignSites(row) {
      if (row) {
        this.assign.item = row;
        this.assign.visible = true;
      } else {
        this.assign.item = {};
        this.assign.visible = false;
        this.getGroupPages();
      }
    },
    onClickEditButton(row) {
      this.$router.push({
        path: "/partnership-management/group-management/edit/" + row.groupPk
      })
    },
    onDeleteCompany(row) {
      api.deleteUserGroup({
        groupPk: row.groupPk
      }).then(res => {
        if (res.success) {
          this.$message({
            type: 'success',
            message: 'Delete success!'
          })
          this.getGroupPages()
        }
      }).catch(error => {
        this.$message({
          type: 'error',
          message: error
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>