<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <div class="flex1" style="min-width: 200px; max-width: 350px;">
          <el-input
            v-model="params.pageVo.criteria"
            placeholder="Search by Name, Email, Contact"
            @keyup.enter.native="getTableData" />
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getTableData">
            Search
          </el-button>
        </div>
        <div class="filter-flex-button">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="addUser">
            Add
          </el-button>
        </div>
      </div>
    </div>
    <el-table
      v-loading="table.loading"
      :data="table.list"
      class="no-border"
      fit>
      <el-table-column
        label="Name"
        align="center">
          <template slot-scope="{row}">
            <span>{{ row.userName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Email Address"
        align="center"
        prop="email"/>
      <el-table-column
        label="Role"
        align="center"
        prop="roleName"/>
      <el-table-column
        label="Description"
        align="center"
        prop="desc">
        <template slot-scope="{row}">
          <div
            v-for="item in row.desc"
            v-if="row.desc"
            :key="item">
            {{item}}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="Last Login"
        align="center"
        prop="lastLogin"/>
      <el-table-column
        label="Action"
        align="center"
        min-width="140">
        <template slot-scope="{row}">
          <TableAction
            @edit="updateUser(row)"
            @delete="deleteUser(row)"/>
        </template>
      </el-table-column>
    </el-table>
    <div class="right">
      <pagination
        v-show="table.total > 0"
        :total="table.total"
        :page.sync="params.pageNo"
        :limit.sync="params.pageSize"
        @pagination="getTableData" />
    </div>
    <DialogDetail
      v-bind="dialogAction"
      @hide="hideDialog"/>
  </div>
</template>

<script>
import api from '@/http/api/access'
import Pagination from '@/components/Pagination'
import TableAction from '@/components/TableAction'
import DialogDetail from './DialogDetail'
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        pageVo: {
          criteria: ""
        }
      },
      table: {
        loading: false,
        list: [],
        total: 0
      },
      dialogAction: {
        isEdit: false,
        visible: false
      }
    }
  },
  components: { Pagination,TableAction,DialogDetail },
  created() {
    this.getTableData()
  },
  methods: {
    getTableData() {
      this.table.loading = true;
      api.getAccessPages(this.params).then(res => {
        if (res.data) {
          this.table.list = res.data
          this.table.total = res.total
        }
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.table.loading = false
      })
    },
    hideDialog(e) {
      this.dialogAction.id = "";
      this.dialogAction.visible = false;
      if (e)
        this.getTableData();
    },
    addUser() {
      this.dialogAction.id = "";
      this.dialogAction.isEdit = false;
      this.dialogAction.visible = true;
    },
    updateUser(row) {
      this.dialogAction.id = row.userPk;
      this.dialogAction.isEdit = true;
      this.dialogAction.visible = true;
    },
    deleteUser(row) {
      this.$confirm('Are you sure you want to delete this user?', 'Delete', {
        confirmButtonText: 'Confirm',
        cancelButtonText: 'Cancel',
        type: 'warning'
      }).then(res => {
        this.onDeleteUser(row.userPk);
      })
    },
    onDeleteUser(id) {
      this.table.loading = true;
      api.deleteAccessUser(id).then(res => {
        this.$message({
          type: 'success',
          message: "Successfully deleted!"
        })
        this.getTableData()
      }).catch(err => {
        this.$message({
          type: 'error',
          message: err
        })
      }).finally(() => {
        this.table.loading = false
      })
    }
  }
}
</script>

<style>
</style>