import moment from "moment";
import { capitalize } from "../../../common/util/Util";
import { AdminUserDTO } from "../../../common/dto/AdminUserDTO";
import log from "loglevel";

export async function viewUsers(authToken: string) {
  // const users = await getOtherUsers(authToken);

  const usersTable = $("#usersTable").DataTable({
    ajax: {
      url: "/api/admin/get/users",
      headers: {
        "X-AUTH-TOKEN": authToken,
      },
      dataSrc: "",
    },
    columns: [
      { data: "id" },
      { data: "userName" },
      {
        data: "role",
        render: (data: string) => {
          return capitalize(data.replace("_", " ").toLowerCase());
        },
      },
      {
        data: "joinDate",
        render: (data: string, type) => {
          return type === "sort"
            ? data
            : moment.utc(data).format("DD/MM/YY").toString();
        },
      },
    ],
    buttons: [
      {
        extend: "selectedSingle",
        text: "User Profile",
        action: (e, dt, button, config) => {
          const username = (dt.row({ selected: true }).data() as AdminUserDTO)
            .userName;
          window.location.assign(`/admin/users/${username}`);
        },
      },
      {
        extend: "selectedSingle",
        text: "Delete User",
        action: (e, dt, button, config) => {
          log.error("Not implemented yet");
        },
      },
      {
        text: "New User",
        action: () => {
          log.error("Not implemented yet");
        },
      },
    ],
    dom: "Blfrtip",
    lengthMenu: [2, 10, 25, 50, 75, 100],
    select: true,
  });
}
