<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.remarks"
          placeholder="Remarks"
          prefix-icon="el-icon-search"
          @change="onSearch"
          clearable/>
        <div
          class="filter-flex-button"
          v-if="!$route.meta.onlyView">
          <el-button
            type="primary"
            @click="onClickSend(true)">
            SEND CREDENTIALS
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list">
      <el-table-column
        align="center"
        label="Remarks"
        prop="remarks"
        min-width="180"/>
      <el-table-column
        align="center"
        label="Address"
        prop="address"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Token"
        prop="token"
        min-width="150"
        v-if="showToken"/>
      <el-table-column
        align="center"
        label="Date Created"
        prop="createTime"
        min-width="150"/>
      <el-table-column
        align="center"
        label="Status"
        prop="status"
        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>
    <DialogConfigs
      v-bind="action"
      @hide="a => onClickSend(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 DialogConfigs from './dialog/DialogConfigs'
export default {
  data() {
    return {
      table: {
        list: [],
        total: 0,
        loading: false
      },
      filters: {
        pageNum: 1,
        pageSize: 10,
        pageCriteria: {
          countryCode: setting.defaultCountry,
          remarks: ""
        }
      },
      options: {
        country: []
      },
      action: {
        visible: false,
        info: {},
        isEdit: false
      },
      showToken: setting.enableOCPIToken
    };
  },
  components: { Pagination, DialogConfigs },
  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.pagesSendConfig(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;
      })
    },
    onClickSend(state, info) {
      this.action.visible = state;
      this.action.isEdit = false;
      this.action.info = {};
      if (state && info) {
        this.action.info = info;
        this.action.isEdit = true;
      } else if (info) {
        this.onSearch();
      }
    },
    handleCommand(cb, item) {
      this[cb](item)
    },
    onClickEdit(row) {
      this.onClickSend(true, row)
    },
    onClickDelete(row) {
      this.$confirm('Are you sure you want to delete this config?', 'Delete', {
        confirmButtonText: 'OK',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.deleteOperator(row.credentialTokenId);
      })
    },
    deleteOperator(id) {
      this.table.loading = true;
      ocpi.deleteSendConfig(id).then(res => {
        this.$message({
          type: 'success',
          message: "Delete success."
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
        this.table.loading = false;
      })
    }
  }
}
</script>

<style scoped>

</style>