<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-view">
        <el-select
          v-model="params.pageVo.roleName"
          class="filter-view-item"
          @change="toSearch"
          clearable>
          <el-option
            v-for="(item, index) in dialogAction.roleOptions"
            :key="index"
            :label="item.roleDesc"
            :value="item.roleName"/>
        </el-select>
        <div class="flex1" style="max-width: 350px;">
          <el-input
            v-model="params.pageVo.criteria"
            class="filter-view-item"
            prefix-icon="el-icon-search"
            placeholder="Search by Name"
            @keyup.enter.native="toSearch"
            @change="toSearch"
            clearable/>
        </div>
        <div>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="toSearch">
            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"
        min-width="100">
          <template slot-scope="{row}">
            <span>{{ row.userName }}</span>
          </template>
      </el-table-column>
      <el-table-column
        label="Email Address"
        align="center"
        prop="email"
        min-width="120"/>
      <el-table-column
        label="Role"
        align="center"
        prop="roleName"
        min-width="100"/>
      <el-table-column
        label="Description"
        align="center"
        prop="desc"
        min-width="150">
        <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"
        min-width="120"/>
      <el-table-column
        label="Action"
        align="center"
        min-width="120">
        <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: "",
          roleName: ""
        }
      },
      table: {
        loading: false,
        list: [],
        total: 0
      },
      dialogAction: {
        isEdit: false,
        visible: false,
        roleOptions: []
      }
    }
  },
  components: { Pagination,TableAction,DialogDetail },
  created() {
    this.getRoleOptions();
    this.getTableData()
  },
  methods: {
    toSearch() {
      this.params.pageNo = 1;
      this.getTableData();
    },
    getRoleOptions() {
      api.getRoleOptions().then(res => {
        if (res.data) {
          this.dialogAction.roleOptions = res.data
        }
      });
    },
    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>