<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          class="filter-view-item"
          v-model="filters.pageCriteria.countryCode"
          placeholder="Country"
          @change="onSearch">
          <el-option 
            v-for="(item, index) in options.country"
            :key="index"
            :value="item.countryCode"
            :label="item.countryName"/>
        </el-select>
        <el-input
          class="filter-view-item"
          v-model="filters.pageCriteria.partyName"
          placeholder="Roaming Operator Name"
          prefix-icon="el-icon-search"
          @change="onSearch"
          clearable/>
        <div
          class="filter-flex-button"
          v-if="!$route.meta.onlyView">
          <el-button
            type="primary"
            @click="onClickReceive(true)">
            RECEIVE CREDENTIALS
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Country Code"
        prop="countryCode"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Party ID"
        prop="partyId"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Name"
        prop="partyName"
        min-width="100"/>
      <el-table-column
        align="center"
        label="Website"
        prop="website"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Logo"
        prop="logo"
        min-width="120">
        <template v-slot="{row}">
          <img
            class="img-logo"
            :src="row.logo"/>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="Sites"
        prop="countOperation.sites"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Charging Stations"
        prop="countOperation.stations"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Connectors"
        prop="countOperation.connectors"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Date Created"
        prop="createTime"
        min-width="120"/>
      <el-table-column
        align="center"
        label="Status"
        prop="dataStatus"
        min-width="120"/>
      <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
              <el-dropdown-item
                command="onClickEdit">
                Edit Info
              </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.pageNum"
        :limit.sync="filters.pageSize"
        @pagination="getTableData" />
    </div>
    <ReceiveCredents
      :visible="showReceive"
      @hide="a => onClickReceive(false, a)"/>
  </div>
</template>

<script>
import api from '../../api/dashboard';
import ocpi from '../../http/api/ocpi.js';
import setting from '../../settings.js';
import Pagination from '@/components/Pagination'
import ReceiveCredents from './dialog/DialogOperators'
export default {
  data() {
    return {
      table: {
        list: [],
        total: 0,
        loading: false
      },
      filters: {
        pageNum: 1,
        pageSize: 10,
        pageCriteria: {
          countryCode: setting.defaultCountry,
          partyName: ""
        }
      },
      options: {
        country: []
      },
      showReceive: false
    };
  },
  components: { Pagination, ReceiveCredents },
  created() {
    this.getContryOptions();
    this.onSearch();
  },
  methods: {
    getContryOptions() {
      api.getCountries().then(res => {
        this.loading = false
        if (res.data) {
          this.options.country = res.data
          if (this.filters.countryCode) {
            this.getProvider();
          }
        }
      }).catch(err => {
        this.loading = false
        this.$message({
          type: 'error',
          message: err
        })
      })
    },
    onSearch() {
      this.filters.pageNum = 1;
      this.getTableData();
    },
    getTableData() {
      this.table.loading = true;
      ocpi.pagesRoamingOperator(this.filters).then(res => {
        if (res.data.records && res.data.totalRow) {
          this.table.total = res.data.totalRow;
          this.table.list = res.data.records;
        } 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;
      })
    },
    onClickReceive(state, a) {
      this.showReceive = state
      if (a) {
        this.onSearch();
      }
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickEdit(row) {
      this.$router.push({
        path: "/ocpi/edit-operators/" + row.credentialId
      })
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this operator?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteOperator(row.credentialId);
      })
    },
    deleteOperator(id) {
      this.table.loading = true;
      ocpi.deleteRoamingOperatorInfo(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.loading = true;
      })
    }
  }
}
</script>

<style scoped>
.img-logo {
  height: 40px;
  object-fit: contain;
}
</style>