<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-input
          class="filter-input"
          v-model="filters.pageVo.criteria"
          placeholder="Search by Rate Name or Service Provider"
          clearable/>
        <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">
            Create Rate
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Rate ID"
        prop="userPk"
        min-width="120">
        <template slot-scope="{row}" >
          <span
            class="link-type"
            @click="onClickEdit(row)"
            v-if="!$route.meta.onlyView">
            {{ row.dynamicRateId }}
          </span>
          <span v-else>{{ row.dynamicRateId }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Rate Name"
        prop="rateName"
        min-width="120"/>
      <el-table-column
        align="center"
        label="No. of Sites Configured"
        prop="assignedSiteCount"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Status"
        prop="dataStatus"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Update Time"
        prop="updateTime"
        min-width="120"/>
      <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="assignRates">
                Assign Sites
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickEdit">
                Edit
              </el-dropdown-item>
              <el-dropdown-item
                command="onClickDelete">
                Delete
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="filters.pageNo"
        :limit.sync="filters.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogAssignment
      :visible="assign.visible"
      :title="'ASSIGN SITES (RATE NAME: ' + assign.item.rateName + ')'"
      :rate="assign.item"
      @hide="assignRates"/>
  </div>
</template>

<script>
import TableAction from '@/components/TableAction.vue'
import Pagination from '@/components/Pagination'
import DialogAssignment from '@/components/DialogAssignment'
import api from '@/http/api/rates'
export default {
  components: { Pagination, TableAction, DialogAssignment },
  data() {
    return {
      loading: false,
      filters: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: ""
        }
      },
      table: {
        list: [],
        total: 0
      },
      assign: {
        item: {},
        visible: false
      }
    }
  },
  created() {
    this.onClickSearch()
  },
  methods: {
    onClickSearch() {
      this.filters.pageNo = 1
      this.getTableData()
    },
    getTableData() {
      this.loading = true;
      api.getRatePages(this.filters).then(res => {
        if (res.data && res.total) {
          this.table.total = res.total;
          this.table.list = res.data;
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.total = 0;
        this.table.list = [];
      }).finally(() => {
        this.loading = false;
      })
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickAdd() {
      this.$router.push({
        path: "/site-management/dynamic-rate-add"
      })
    },
    onClickEdit(row) {
      this.$router.push({
        path: "/site-management/dynamic-rate-update/" + row.dynamicRateId
      })
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this rate?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteDynamicRate(row.dynamicRateId);
      })
    },
    assignRates(row) {
      if (row) {
        this.assign.item = row;
        this.assign.visible = true;
      } else {
        this.assign.item = {};
        this.assign.visible = false;
        this.getTableData();
      }
    },
    deleteDynamicRate(id) {
      this.loading = true;
      api.deleteDynamicRate({
        dynamicRateId: id
      }).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.loading = false;
      })
    }
  }
}
</script>

<style>
  .filter-input {
    min-width: 100px;
    max-width: 300px;
  }
</style>