<template>
  <div class="app-container">
    <div class="filter-container filter-view">
      <el-select
        class="filter-view-item"
        placeholder="Service Provider"
        v-model="filter.pageCriteria.tenantId"
        @change="toSearch"
        v-if="false">
        <el-option
          v-for="(item, index) in options.provider"
          :key="index"
          :label="item.key"
          :value="item.tenantId"/>
      </el-select>
      <el-select
        v-model="filter.pageCriteria.dataStatus"
        placeholder="Status"
        @change="toSearch"
        class="filter-view-item"
        clearable>
        <el-option
          v-for="(item,index) in options.status"
          :key="index"
          :label="item.key"
          :value="item.value"/>
      </el-select>
      <div style="flex: 1; max-width: 300px;">
        <el-input
          class="filter-view-item"
          v-model="filter.pageCriteria.criteria"
          placeholder="Search by Idle Fee Name"
          prefix-icon="el-icon-search"
          @keyup.enter.native="toSearch"
          @change="toSearch"
          clearable/>
      </div>
      <div
        class="filter-flex-button"
        v-if="!$route.meta.onlyView">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="onClickAdd">
          Create
        </el-button>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Idle Fee Name"
        prop="idleFeeName"
        min-width="140"/>
      <el-table-column
        align="center"
        label="Service Provider"
        prop="serviceProvider"
        min-width="140"
        v-if="false"/>
      <el-table-column
        align="center"
        label="No. of Sites Configured"
        prop="assignedSiteCount"
        min-width="180">
        <template v-slot="{ row }">
          <span v-if="$route.meta.onlyView">{{ row.siteCount }}</span>
          <div
            class="link-type"
            @click="assignSites(row)"
            v-else>{{row.siteCount}}</div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Status"
        min-width="100">
        <template slot-scope="{row}">
          <div :class="'status-' + row.dataStatus">
            {{row.dataStatus}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Update Date Time"
        prop="updateTime"
        min-width="140"/>
      <el-table-column
        align="center"
        label="Action"
        min-width="70"
        v-if="!$route.meta.onlyView">
        <template v-slot="{ row }">
          <el-dropdown
            class="action-dropdown"
            @command="(v) => handleCommand(v, row)"
            v-if="row.dataStatus != 'Inactive'">
            <i class="el-icon-more icon-action"></i>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                command="assignSites">
                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="filter.pageNum"
        :limit.sync="filter.pageSize"
        @pagination="getTableList" />
    </div>
    <AssignmentDialog
      :visible="assign.visible"
      :title="'ASSIGN SITES (CONFIG NAME: ' + assign.item.idleFeeName + ')'"
      :item="assign.item"
      @hide="assignSites"/>
  </div>
</template>

<script>
import api from '@/api/apiIdle.js';
//import apiBase from "@/api/apiBase.js";
import Pagination from '@/components/Pagination';
import AssignmentDialog from './AssignmentDialog';
export default {
  data() {
    return {
      filter: {
        pageNum: 1,
        pageSize: 10,
        pageCriteria: {
          tenantId: "",
          criteria: "",
          dataStatus: "A"
        }
      },
      options: {
        status: [],
        provider: []
      },
      table: {
        loading: false,
        total: 0,
        list: []
      },
      assign: {
        item: {},
        visible: false
      }
    };
  },
  components: { Pagination, AssignmentDialog },
  created() {
    this.getStatusOption();
    //this.getProviderOptions();
  },
  methods: {
    /*getProviderOptions() {
      apiBase.getProviderList().then(res => {
        if (res.data) {
          this.options.provider = res.data
          if (this.options.provider.length > 0) {
            this.filter.pageCriteria.tenantId = this.options.provider[0].tenantId;
            this.toSearch();
          }
        }
      }).catch(err => {
        
      })
    },*/
    getStatusOption() {
      apiBase.getDataStatusOptions().then(res => {
        if (res.data && res.data.length > 0) {
          this.options.status = res.data;
        }
      }).catch(err => {
        
      })
    },
    toSearch() {
      this.filter.pageNum = 1;
      this.getTableList();
    },
    getTableList() {
      this.table.loading = true;
      api.getIdleFeePages(this.filter).then(res => {
        if (res.data.totalRow && res.data.records) {
          this.table.list = res.data.records
          this.table.total = res.data.totalRow
        } else {
          this.table.list = []
          this.table.total = 0
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        });
        this.table.list = []
        this.table.total = 0
      }).finally(() => {
        this.table.loading = false;
      });
    },
    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.getTableList();
      }
    },
    onClickAdd() {
      this.$router.push({
        path: "/site-management/idle-fee-configuration-add"
      })
    },
    onClickEdit(row) {
      this.$router.push({
        path: "/site-management/idle-fee-" + row.dynamicIdleFeeId
      })
    },
  }
}
</script>

<style scoped>
.status-Active {
  color: #009e81;
}
.status-Inactive {
  color: #ff2332;
}
</style>