<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-view-item"
          v-model="filter.pageVo.providerPk"
          placeholder="Service Provider"
          @change="toSearch"
          clearable>
          <el-option
            v-for="(item, index) in providerOptions"
            :key="index"
            :label="item.providerName"
            :value="item.providerPk"/>
        </el-select>
        <div class="flex1" style="max-width: 350px;">
          <el-input
            clearable
            class="filter-view-item"
            v-model="filter.pageVo.criteria"
            prefix-icon="el-icon-search"
            placeholder="Search by Station ID, Site Name, Carpark Code"
            @keyup.enter.native="toSearch"
            @change="toSearch"/>
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="toSearch">
            Search
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      :data="table.list"
      v-loading="loading">
      <el-table-column
        label="Station ID"
        align="center"
        prop="chargeBoxId"
        min-width="100"/>
      <el-table-column
        label="Connector ID"
        align="center"
        prop="connectorId"
        min-width="120"/>
      <el-table-column
        label="Site Name"
        align="center"
        prop="siteName"
        min-width="140"/>
      <el-table-column
        label="Carpark Code"
        prop="carParkCode"
        align="center"
        min-width="125"/>
      <el-table-column
        label="Service Provider"
        prop="providerName"
        align="center"
        min-width="140"/>
      <el-table-column
        label="Description"
        align="center"
        prop="description"
        min-width="120">
        <template slot-scope="{row}">
          <span
            class="link-type"
            v-if="row.description"
            @click="viewProfiles(row)">
            {{row.description}}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="Validity Period"
        align="center"
        prop="validityPeriod"
        min-width="120"/>
      <el-table-column
        label="Action"
        align="center"
        prop="description"
        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="editChargingProfile"
                v-if="row.description">
                Edit Charging Profile
              </el-dropdown-item>
              <el-dropdown-item
                command="setChargingProfile"
                v-else>
                Set Charging Profile
              </el-dropdown-item>
              <el-dropdown-item
                command="clearChargingProfile"
                v-if="row.description">
                Clear Charging Profile
              </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.pageNo"
        :limit.sync="filter.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogSetProfiles
      :visible="actionDialog.visible"
      :item="actionDialog.item"
      :isEdit="actionDialog.isEdit"
      @hide="hideDialog"/>
    <DialogClearProfiles
      :item="actionDialog.item"
      :visible="actionDialog.isClear"
      @hide="hideDialog"/>
  </div>
</template>

<script>
import api from '../../http/api/chargingProfile.js'
import ocpp from '../../http/api/ocpp'
import provider from '../../http/api/provider'
import Pagination from '@/components/Pagination'
import DialogSetProfiles from './components/DialogSetProfiles.vue'
import DialogClearProfiles from './components/DialogClearProfiles.vue'
export default {
  data() {
    return {
      loading: false,
      table: {
        list: [],
        total: 0
      },
      filter: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: "",
          providerPk: ""
        }
      },
      actionDialog: {
        item: {},
        isEdit: false,
        isClear: false,
        visible: false
      },
      providerOptions: []
    }
  },
  components: { Pagination, DialogSetProfiles, DialogClearProfiles },
  created() {
    this.getAllProvider();
    this.getTableData();
  },
  methods: {
    toSearch() {
      this.filter.pageNo = 1;
      this.getTableData();
    },
    getAllProvider() {
      this.listLoading = true;
      provider.getAllServiceProvider().then(res => {
        if (res.data && res.data.length > 0) {
          this.providerOptions = res.data
        }
      });
    },
    getTableData() {
      this.loading = true;
      api.getConfigureChargingProfilePages(this.filter).then(res => {
        this.loading = false;
        this.table.total = res.total;
        this.table.list = res.data;
      }).catch(err => {
        this.loading = false;
        this.table.total = 0;
        this.table.list = [];
      })
    },
    viewProfiles(row) {
      this.$router.push(({
        path: "/smart-energy-management/charging-profiles/view/" + row.chargingProfilePk
      }))
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    hideDialog(resbound) {
      console.log("hideDialog", resbound);
      this.actionDialog.isClear = false;
      this.actionDialog.visible = false;
      if (resbound) {
        setTimeout(() => {
          this.getTableData();
        }, 2000);
      }
    },
    setChargingProfile(row) {
      this.actionDialog.isEdit = false;
      this.actionDialog.visible = true;
      this.actionDialog.item = row;
    },
    editChargingProfile(row) {
      this.actionDialog.isEdit = true;
      this.actionDialog.visible = true;
      this.actionDialog.item = row;
    },
    clearChargingProfile(row) {
      this.actionDialog.item = row;
      this.actionDialog.isClear = true;
    }
  }
}
</script>

<style>
</style>