"use client";

import HeaderManagement from "@/components/landing-page/header-management";
import SidebarManagement from "@/components/landing-page/sidebar-management";
import { close, error, loading } from "@/config/swal";
import { getCookiesDecrypt } from "@/lib/utils";
import { getInfoProfile, getUsersTeams, saveUserReports } from "@/service/landing/landing";
import React, { useEffect, useState } from "react";
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import withReactContent from "sweetalert2-react-content";
import Swal from "sweetalert2";
import { Button } from "@/components/ui/button";
import toast from "react-hot-toast";
import { useToast } from "@/components/ui/use-toast";
import { ToastAction } from "@/components/ui/toast";

const page = () => {
  const [user, setUser] = useState<any>();
  const [profile, setProfile] = useState<any>();
  const instituteId = getCookiesDecrypt("uinse");
  const [userSelected, setUserSelected] = useState();
  const [reportMessage, setReportMessage] = useState<string>();
  const MySwal = withReactContent(Swal);
  const { toast } = useToast();
  const [reportMessageOpen, setReportMessageOpen] = useState(false);

  // const launchModal = (user: any) => {
  //   setUserSelected(user);
  //   $("#modalDetailProfile").modal("show");
  // };
  async function onSubmit() {
    MySwal.fire({
      title: "Simpan Data",
      text: "",
      icon: "warning",
      showCancelButton: true,
      cancelButtonColor: "#d33",
      confirmButtonColor: "#3085d6",
      confirmButtonText: "Simpan",
    }).then((result) => {
      if (result.isConfirmed) {
        save();
      }
    });
  }

  async function save() {
    setReportMessageOpen(false);

    loading();
    const data = {
      userId: user?.id,
      message: reportMessage,
    };

    const response = await saveUserReports(data);

    if (response?.error) {
      toast({
        variant: "destructive",
        title: "Uh oh! Something went wrong.",
      });
      // error(response?.message);
      return false;
    }

    close();

    toast({
      title: "Success !!",
    });

    // toast.success("SUKSES !!", {
    //   position: "top-right",
    // });
    // successSubmit();
  }

  function successSubmit() {
    MySwal.fire({
      title: "Sukses",
      icon: "success",
      confirmButtonColor: "#3085d6",
      confirmButtonText: "OK",
    }).then((result) => {
      if (result.isConfirmed) {
        ("hide");
        // $("#modalReportProfile").modal("hide");
      }
    });
  }

  useEffect(() => {
    async function getTeams() {
      if (instituteId != undefined) {
        loading();
        const response = await getUsersTeams(instituteId);
        setUser(response?.data?.data);
        close();
      }
    }

    async function getProfile() {
      loading();
      const response = await getInfoProfile();
      setProfile(response?.data?.data);
      close();
    }

    getTeams();
    getProfile();
  }, []);

  return (
    <>
      <HeaderManagement />
      <div className="flex flex-row">
        <SidebarManagement />
        <div className="w-2/3 p-12">
          <div className="flex flex-col">
            <p className="text-lg font-semibold">Tim {profile?.institute?.name}</p>
            <p className="text-base mb-3">{user?.length} Anggota</p>
          </div>
          <div className="flex flex-row gap-5">
            {user?.map((row: any) => (
              <div key={row?.id}>
                <Dialog>
                  <DialogTrigger>
                    <div className="flex flex-col items-center">
                      <svg xmlns="http://www.w3.org/2000/svg" width="5em" height="5em" viewBox="0 0 16 16">
                        <path fill="currentColor" d="M11 7c0 1.66-1.34 3-3 3S5 8.66 5 7s1.34-3 3-3s3 1.34 3 3" />
                        <path
                          fill="black"
                          fill-rule="evenodd"
                          d="M16 8c0 4.42-3.58 8-8 8s-8-3.58-8-8s3.58-8 8-8s8 3.58 8 8M4 13.75C4.16 13.484 5.71 11 7.99 11c2.27 0 3.83 2.49 3.99 2.75A6.98 6.98 0 0 0 14.99 8c0-3.87-3.13-7-7-7s-7 3.13-7 7c0 2.38 1.19 4.49 3.01 5.75"
                          clip-rule="evenodd"
                        />
                      </svg>
                      <p className="font-semibold text-md">{row?.fullname}</p>
                      <p className="text-sm font-light">{row?.username || "username"}</p>
                    </div>
                  </DialogTrigger>
                  <DialogContent className="flex flex-row justify-center" size="sm">
                    <div className="flex flex-col w-full gap-2">
                      <div>
                        <h1 className="font-semibold">{row?.fullname}</h1>
                      </div>
                      <div className="border-b border-black w-full"></div>
                      <div className="gap-1">
                        <div className="font-light">{row?.email}</div>
                        <div className="font-light">{row?.phoneNumber}</div>
                        <div className="font-light">{row?.address}</div>
                      </div>
                      <div className="border-b border-black w-full"></div>
                      <div className="place-items-end">
                        <Dialog open={reportMessageOpen} onOpenChange={setReportMessageOpen}>
                          <DialogTrigger asChild>
                            <Button variant="outline" className="bg-red-500 rounded-md text-white hover:bg-white hover:text-red-500">
                              Report
                            </Button>
                          </DialogTrigger>
                          <DialogContent className="sm:max-w-[425px]">
                            <div className="flex flex-col w-full gap-2">
                              <div>
                                <h1 className="font-semibold">{row?.fullname}</h1>
                              </div>
                              <div className="border-b border-black w-full"></div>
                              <div className="flex flex-col">
                                <h1 className="text-red-600 mb-2">Alasan Report Akun</h1>
                                <textarea id="formControlTextarea1" rows={4} className="border border-black font-light" onChange={(e: any) => setReportMessage(e.target.value)} />
                              </div>
                            </div>
                            <DialogFooter>
                              <Dialog>
                                <DialogTrigger asChild>
                                  <Button className="bg-red-500 text-white" type="submit">
                                    Kirim
                                  </Button>
                                </DialogTrigger>
                                <DialogContent>
                                  <h1>Simpan Data?</h1>
                                  <DialogFooter>
                                    <DialogClose>
                                      <Button onClick={save}>Simpan</Button>
                                    </DialogClose>
                                    <DialogClose>
                                      <Button>Cancel</Button>
                                    </DialogClose>
                                  </DialogFooter>
                                </DialogContent>
                              </Dialog>
                            </DialogFooter>
                          </DialogContent>
                        </Dialog>
                      </div>
                    </div>
                  </DialogContent>
                </Dialog>
              </div>
            ))}
          </div>
        </div>
      </div>
    </>
  );
};

export default page;
