<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form
        ref="filter-form"
        v-bind:inline="true"
        label-position="left"
        label-width="70px"
        style="width: 100%;">
        <div class="filter-view">
          <div>
            <el-input
              v-model="criteria"
              placeholder="Search by User ID, Email, Phone or Licence Plate"
              style="width: 400px;" />
          </div>
          <div>
            <el-button
              v-waves
              type="primary"
              icon="el-icon-search"
              @click="handleFilter">
                Search
            </el-button>
          </div>
          <div class="filter-flex-button" v-if="!$route.meta.onlyView">
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="handleClickAddUserButton">
                Add User
            </el-button>
          </div>
        </div>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      style="width: 100%;">
      <el-table-column
        label="Name"
        prop="userId"
        align="center"
        min-width="150">
        <template slot-scope="{row}">
          <div
            class="link-type"
            v-if="!$route.meta.onlyView"
            @click="handleUpdateUser(row)">
            {{row.nickName}}
          </div>
          <div v-else>{{row.nickName}}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="Name"
        prop="nickName"
        align="center"/> -->
      <el-table-column
        label="Credit"
        prop="credit"
        align="center"
        min-width="120"/>
      <el-table-column
        label="Email"
        prop="email"
        align="center"
        min-width="150"/>
      <el-table-column
        min-width="150"
        label="Phone"
        prop="phone"
        align="center"/>
      <el-table-column
        min-width="120"
        label="Status"
        prop="status"
        align="center"/>
      <el-table-column
        label="Action"
        align="center"
        min-width="140"
        v-if="!$route.meta.onlyView">
        <template slot-scope="{row, $index}">
          <TableAction
            @edit="handleUpdateUser(row, $index)"
            @delete="handleDeleteUser(row, $index)"/>
          <!-- <el-button
            type="primary"
            size="mini"
            @click="handleUpdateUser(row, $index)">
              Edit
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDeleteUser(row, $index)">
              Delete
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="handlePageChange" />
    </div>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import TableAction from '@/components/TableAction'
import {
  fetchList,
  deleteUser,
  addUser,
} from '../../http/api/userManagement'

export default {
  name: 'UserManagement',
  components: { Pagination, TableAction },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      criteria: '',
      listQuery: {
        page: 1,
        limit: 10,
      },
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList({
        limit: this.listQuery.limit,
        page: this.listQuery.page,
        criteria: this.criteria,
      })
        .then(response => {
            this.list = response.data
            this.total = response.total
          })
        .finally(() => {
          this.listLoading = false
        })
    },
    handlePageChange(value) {
      const {limit, page} = value;
      this.listQuery.limit = limit;
      this.listQuery.page = page;
      this.getList();
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClickAddUserButton() {
      this.$router.push({ path: '/user-management/add-user' })
    },
    handleUpdateUser(user, index) {
      //this.$store.commit('userManagement/SET_SELECTED_USER', user)
      this.$router.push({ path: '/user-management/update-user/' + user.userId })
    },
    handleDeleteUser(row, index) {
      this.$confirm('Are you sure you want to delete this user?', 'Delete', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        deleteUser(row.userId).then(() => {
          this.$notify({
            title: 'Success',
            message: `Delete user succese ${row.nickName}`,
            type: 'success',
            duration: 2000
          })
          this.list.splice(index, 1)
        }).catch(error => {
          this.$notify({
            title: 'Failed',
            message: `Delete User Error: ${error}`,
            type: 'error',
            duration: 5000
          });
        });
      });
    },
  }
}
</script>
