<template>
  <div>
    <el-table :data="tableData" style="min-height:500px" >
      <el-table-column prop="userName" label="User Name" width="200"></el-table-column>
      <el-table-column prop="email" label="Email" width="300" show-overflow-tooltip></el-table-column>
      <el-table-column prop="roles" label="Roles" show-overflow-tooltip></el-table-column>
      <el-table-column prop="lastModifiedDate" label="Last Visit Time" width="240"></el-table-column>
      <el-table-column prop="status" label="Status" width="100"></el-table-column>
      <el-table-column prop="action" label="Action" width="100">
        <template v-slot="scope">
          <el-popconfirm
            v-if="scope.row.roles !== 'CLOUDIDP_EHR_ADMIN'"
            confirm-button-text="Yes"
            cancel-button-text="No"
            icon="el-icon-info"
            icon-color="red"
            :title='"Are you sure to " + (scope.row.status === "ENABLE" ? "disable" : "enable") + " this?"'
            @confirm="changeStatus(scope.row.id, scope.row.status)"
          >
            <el-button type="danger" slot="reference" size="mini" v-if="scope.row.status === 'ENABLE'">Disable</el-button>
            <el-button type="primary" slot="reference" size="mini" v-else>Enable</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
      <template slot="empty" v-if="tableData.length == 0">
        <span>No data available</span>
      </template>
    </el-table>
    <el-pagination
      v-if="total > 0"
      :page-size="limit"
      :current-page.sync="page"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      background
      :page-sizes="[10, 20, 50, 100]"
      layout="total, sizes, prev, pager, next"
    ></el-pagination>
  </div>
</template>
<script>
import { GetUserList, ChangeUserStatus } from "@/api/permission";
export default {
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      total: 0,
    };
  },
  created() {
    this.getTableData();
  },
  methods: {
    getTableData() {
      const { page, limit } = this;
      GetUserList({ pageNo: page, pageSize: limit }).then((res) => {
        const { content, totalElements } = res;
        this.tableData = content;
        this.total = totalElements;
      });
    },
    changeStatus(id, status){
      ChangeUserStatus({
        id,
        status: status === 'ENABLE' ? 'DISABLE' : 'ENABLE'
      }).then(() => {
        this.getTableData();
      })
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getTableData();
    },
    handleSizeChange(val) {
      this.limit = val;
      this.handleCurrentChange(1);
    },
  },
};
</script>
