"use client";

import { Suspense, useEffect, useState } from "react";
import { useRouter, useSearchParams } from "next/navigation";

type ContactInfo = {
  id: number;
  description: string | null;
  email: string | null;
  phone: string | null;
  location: string | null;
  website: string | null;
  github: string | null;
  linkedin: string | null;
  facebook: string | null;
  instagram: string | null;
  twitter: string | null;
  youtube: string | null;
  tiktok: string | null;
  active: boolean;
};

function ContactInfoAdminPage() {
  const router = useRouter();
  const searchParams = useSearchParams();

  const editId = searchParams.get("id");

  const [data, setData] = useState<ContactInfo | null>(null);

  const [description, setDescription] = useState("");
  const [email, setEmail] = useState("");
  const [phone, setPhone] = useState("");
  const [location, setLocation] = useState("");
  const [website, setWebsite] = useState("");

  const [github, setGithub] = useState("");
  const [linkedin, setLinkedin] = useState("");
  const [facebook, setFacebook] = useState("");
  const [instagram, setInstagram] = useState("");
  const [twitter, setTwitter] = useState("");
  const [youtube, setYoutube] = useState("");
  const [tiktok, setTiktok] = useState("");

  const [active, setActive] = useState(true);

  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [error, setError] = useState("");

  const [sidebarOpen, setSidebarOpen] = useState(false);

  useEffect(() => {
    const loadData = async () => {
      try {
        setLoading(true);
        setError("");

        const url = editId
          ? `/api/contact-info?id=${editId}`
          : "/api/contact-info";

        const response = await fetch(url, {
          cache: "no-store",
        });

        const result = await response.json();

        if (!response.ok) {
          throw new Error(
            result.message ||
              "Gagal mengambil Contact Information."
          );
        }

        const contact = result.data as ContactInfo | null;

        setData(contact);

        if (contact) {
          setDescription(contact.description || "");
          setEmail(contact.email || "");
          setPhone(contact.phone || "");
          setLocation(contact.location || "");
          setWebsite(contact.website || "");

          setGithub(contact.github || "");
          setLinkedin(contact.linkedin || "");
          setFacebook(contact.facebook || "");
          setInstagram(contact.instagram || "");
          setTwitter(contact.twitter || "");
          setYoutube(contact.youtube || "");
          setTiktok(contact.tiktok || "");

          setActive(contact.active);
        }
      } catch (err) {
        setError(
          err instanceof Error
            ? err.message
            : "Gagal mengambil data."
        );
      } finally {
        setLoading(false);
      }
    };

    loadData();
  }, [editId]);

  const handleSubmit = async (
    e: React.FormEvent<HTMLFormElement>
  ) => {
    e.preventDefault();

    setSaving(true);
    setError("");

    try {
      const payload = {
        description,
        email,
        phone,
        location,
        website,
        github,
        linkedin,
        facebook,
        instagram,
        twitter,
        youtube,
        tiktok,
        active,
      };

      const response = await fetch("/api/contact-info", {
        method: data ? "PUT" : "POST",
        headers: {
          "Content-Type": "application/json",
        },
        body: JSON.stringify(
          data
            ? {
                id: data.id,
                ...payload,
              }
            : payload
        ),
      });

      const result = await response.json();

      if (!response.ok) {
        throw new Error(
          result.message ||
            "Gagal menyimpan Contact Information."
        );
      }

      router.push("/secure-panel/contact-info/list");
      router.refresh();
    } catch (err) {
      setError(
        err instanceof Error
          ? err.message
          : "Terjadi kesalahan."
      );

      setSaving(false);
    }
  };

  const handleDelete = async () => {
    if (!data) return;

    const confirmed = window.confirm(
      "Yakin ingin menghapus Contact Information?"
    );

    if (!confirmed) return;

    try {
      setSaving(true);
      setError("");

      const response = await fetch(
        `/api/contact-info?id=${data.id}`,
        {
          method: "DELETE",
        }
      );

      const result = await response.json();

      if (!response.ok) {
        throw new Error(
          result.message ||
            "Gagal menghapus Contact Information."
        );
      }

      router.push("/secure-panel/contact-info/list");
      router.refresh();
    } catch (err) {
      setError(
        err instanceof Error
          ? err.message
          : "Gagal menghapus data."
      );

      setSaving(false);
    }
  };

  if (loading) {
    return (
      <>
        <style>{`
          * {
            box-sizing: border-box;
          }

          .contact-loading-layout {
            min-height: 100vh;
            background: #f5f7f9;
            color: #1f2937;
          }

          .contact-loading-main {
            margin-left: 260px;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
          }

          .contact-loading-content {
            text-align: center;
            padding: 40px 20px;
          }

          .contact-loading-spinner {
            width: 42px;
            height: 42px;
            border: 3px solid #dcefe4;
            border-top-color: #198754;
            border-radius: 50%;
            animation: contactSpin 0.8s linear infinite;
            margin: 0 auto 15px;
          }

          .contact-loading-content h5 {
            margin: 0 0 5px;
            font-size: 15px;
            font-weight: 700;
            color: #344054;
          }

          .contact-loading-content p {
            margin: 0;
            color: #98a2b3;
            font-size: 12px;
          }

          @keyframes contactSpin {
            to {
              transform: rotate(360deg);
            }
          }

          @media (max-width: 991.98px) {
            .contact-loading-main {
              margin-left: 0;
            }
          }
        `}</style>

        <main className="contact-loading-layout">
          <div className="contact-loading-main">
            <div className="contact-loading-content">
              <div className="contact-loading-spinner"></div>

              <h5>Memuat Contact Information</h5>

              <p>
                Sedang mengambil data dari database...
              </p>
            </div>
          </div>
        </main>
      </>
    );
  }

  return (
    <>
      <style>{`
        * {
          box-sizing: border-box;
        }

        .admin-layout {
          min-height: 100vh;
          background: #f5f7f9;
          color: #1f2937;
        }

        /* =========================
           SIDEBAR
        ========================= */

        .admin-sidebar {
          position: fixed;
          top: 0;
          left: 0;
          width: 260px;
          height: 100vh;
          background: #ffffff;
          border-right: 1px solid #e9ecef;
          z-index: 1050;
          transition: transform 0.3s ease;
          overflow-y: auto;
        }

        .sidebar-inner {
          min-height: 100%;
          display: flex;
          flex-direction: column;
          padding: 22px 16px;
        }

        .sidebar-brand {
          display: flex;
          align-items: center;
          gap: 12px;
          padding: 4px 8px 24px;
          border-bottom: 1px solid #eef0f2;
        }

        .brand-icon {
          width: 42px;
          height: 42px;
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          background: #198754;
          color: white;
          font-size: 19px;
          box-shadow: 0 8px 20px rgba(25, 135, 84, 0.18);
          flex-shrink: 0;
        }

        .brand-text {
          min-width: 0;
          line-height: 1.15;
        }

        .brand-text strong {
          display: block;
          font-size: 15px;
          font-weight: 700;
          color: #17202a;
        }

        .brand-text span {
          display: block;
          margin-top: 4px;
          font-size: 11px;
          color: #8a94a6;
          letter-spacing: 0.4px;
        }

        .sidebar-close {
          margin-left: auto;
          border: 0;
          background: transparent;
          color: #6c757d;
          font-size: 18px;
        }

        .sidebar-section-label {
          padding: 20px 10px 8px;
          font-size: 10px;
          font-weight: 700;
          color: #a0a8b3;
          letter-spacing: 1.1px;
        }

        .sidebar-nav {
          display: flex;
          flex-direction: column;
          gap: 3px;
        }

        .sidebar-link {
          display: flex;
          align-items: center;
          gap: 12px;
          min-height: 44px;
          padding: 9px 12px;
          border-radius: 10px;
          color: #667085;
          text-decoration: none;
          font-size: 13px;
          font-weight: 500;
          transition:
            background 0.2s ease,
            color 0.2s ease,
            transform 0.2s ease;
        }

        .sidebar-link:hover {
          background: #f0f9f4;
          color: #198754;
          transform: translateX(2px);
        }

        .sidebar-link.active {
          background: #e9f7ef;
          color: #198754;
          font-weight: 600;
        }

        .sidebar-link-icon {
          width: 20px;
          display: flex;
          justify-content: center;
          font-size: 16px;
          flex-shrink: 0;
        }

        .sidebar-bottom {
          margin-top: auto;
          padding-top: 20px;
          border-top: 1px solid #eef0f2;
        }

        .sidebar-website {
          display: flex;
          align-items: center;
          gap: 10px;
          padding: 11px 12px;
          border-radius: 10px;
          color: #667085;
          text-decoration: none;
          font-size: 13px;
          transition: all 0.2s ease;
        }

        .sidebar-website:hover {
          background: #f5f7f9;
          color: #198754;
        }

        /* =========================
           MAIN
        ========================= */

        .admin-main {
          margin-left: 260px;
          min-height: 100vh;
          width: calc(100% - 260px);
        }

        /* =========================
           TOPBAR
        ========================= */

        .admin-topbar {
          height: 76px;
          background: rgba(255, 255, 255, 0.96);
          border-bottom: 1px solid #e9ecef;
          display: flex;
          align-items: center;
          justify-content: space-between;
          padding: 0 32px;
          position: sticky;
          top: 0;
          z-index: 1000;
          backdrop-filter: blur(12px);
        }

        .topbar-left,
        .topbar-right {
          display: flex;
          align-items: center;
        }

        .topbar-left {
          gap: 14px;
        }

        .topbar-right {
          gap: 10px;
        }

        .mobile-menu-button {
          width: 40px;
          height: 40px;
          border: 1px solid #e5e7eb;
          background: #ffffff;
          color: #475467;
          border-radius: 9px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 20px;
        }

        .topbar-label {
          font-size: 10px;
          font-weight: 700;
          color: #98a2b3;
          letter-spacing: 1px;
          margin-bottom: 2px;
        }

        .topbar-title {
          font-size: 15px;
          font-weight: 700;
          color: #1d2939;
        }

        .topbar-website,
        .topbar-logout {
          min-height: 40px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          padding: 8px 13px;
          border-radius: 9px;
          text-decoration: none;
          font-size: 13px;
          font-weight: 600;
          transition: all 0.2s ease;
          white-space: nowrap;
        }

        .topbar-website {
          color: #475467;
          border: 1px solid #e4e7ec;
          background: #ffffff;
        }

        .topbar-website:hover {
          color: #198754;
          border-color: #b7dfc9;
          background: #f4fbf7;
        }

        .topbar-logout {
          color: #dc3545;
          background: #fff5f5;
          border: 1px solid #ffd6d9;
        }

        .topbar-logout:hover {
          background: #dc3545;
          border-color: #dc3545;
          color: #ffffff;
        }

        /* =========================
           CONTENT
        ========================= */

        .admin-content {
          padding: 32px;
          max-width: 1600px;
          margin: 0 auto;
        }

        .page-heading {
          display: flex;
          align-items: flex-end;
          justify-content: space-between;
          gap: 25px;
          margin-bottom: 28px;
        }

        .page-heading-left {
          min-width: 0;
        }

        .back-dashboard {
          display: inline-flex;
          align-items: center;
          gap: 7px;
          color: #667085;
          text-decoration: none;
          font-size: 12px;
          font-weight: 600;
          margin-bottom: 17px;
          transition: color 0.2s ease;
        }

        .back-dashboard:hover {
          color: #198754;
        }

        .page-title-row {
          display: flex;
          align-items: center;
          gap: 14px;
        }

        .page-title-icon {
          width: 48px;
          height: 48px;
          border-radius: 13px;
          background: #e9f7ef;
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 21px;
          flex-shrink: 0;
        }

        .page-eyebrow {
          color: #198754;
          font-size: 10px;
          font-weight: 700;
          letter-spacing: 1.2px;
          margin-bottom: 4px;
        }

        .page-title {
          margin: 0;
          font-size: clamp(24px, 3vw, 32px);
          line-height: 1.15;
          font-weight: 750;
          color: #101828;
        }

        .page-description {
          margin: 7px 0 0;
          color: #667085;
          font-size: 13px;
          line-height: 1.7;
          max-width: 680px;
          text-align: justify;
        }

        .page-heading-action {
          flex-shrink: 0;
        }

        .back-button {
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          min-height: 44px;
          padding: 10px 17px;
          border-radius: 10px;
          background: #ffffff;
          color: #475467;
          border: 1px solid #e4e7ec;
          text-decoration: none;
          font-size: 13px;
          font-weight: 600;
          transition: all 0.2s ease;
        }

        .back-button:hover {
          color: #198754;
          border-color: #b7dfc9;
          background: #f4fbf7;
          transform: translateY(-1px);
        }

        /* =========================
           INFORMATION CARDS
        ========================= */

        .contact-grid {
          display: grid;
          grid-template-columns: minmax(0, 1.3fr) minmax(320px, 0.7fr);
          gap: 18px;
          margin-bottom: 18px;
        }

        .content-card {
          background: #ffffff;
          border: 1px solid #eaecf0;
          border-radius: 16px;
          overflow: hidden;
          box-shadow: 0 3px 12px rgba(16, 24, 40, 0.035);
        }

        .content-card-header {
          padding: 21px 24px;
          border-bottom: 1px solid #eaecf0;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 20px;
        }

        .content-card-eyebrow {
          font-size: 10px;
          font-weight: 700;
          color: #198754;
          letter-spacing: 1px;
          margin-bottom: 4px;
        }

        .content-card-title {
          font-size: 17px;
          font-weight: 750;
          color: #101828;
          margin: 0;
        }

        .content-card-description {
          margin: 5px 0 0;
          color: #667085;
          font-size: 12px;
          line-height: 1.65;
          text-align: justify;
          max-width: 650px;
        }

        .content-card-count {
          min-width: 38px;
          height: 38px;
          padding: 0 10px;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          border-radius: 10px;
          background: #f5f7f9;
          color: #667085;
          font-size: 12px;
          font-weight: 700;
          flex-shrink: 0;
        }

        .content-card-body {
          padding: 24px;
        }

        /* =========================
           FORM
        ========================= */

        .form-group {
          margin-bottom: 22px;
        }

        .form-group:last-child {
          margin-bottom: 0;
        }

        .form-label-custom {
          display: block;
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 8px;
        }

        .form-control-custom {
          display: block;
          width: 100%;
          min-height: 43px;
          padding: 10px 13px;
          border: 1px solid #d0d5dd;
          border-radius: 9px;
          background: #ffffff;
          color: #344054;
          font-size: 13px;
          line-height: 1.5;
          outline: none;
          transition:
            border-color 0.2s ease,
            box-shadow 0.2s ease,
            background 0.2s ease;
        }

        .form-control-custom::placeholder {
          color: #98a2b3;
        }

        .form-control-custom:focus {
          border-color: #198754;
          background: #ffffff;
          box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
        }

        textarea.form-control-custom {
          min-height: 125px;
          resize: vertical;
          line-height: 1.7;
          text-align: justify;
        }

        .form-row {
          display: grid;
          grid-template-columns: repeat(2, minmax(0, 1fr));
          gap: 20px;
        }

        /* =========================
           SOCIAL
        ========================= */

        .social-grid {
          display: grid;
          grid-template-columns: repeat(2, minmax(0, 1fr));
          gap: 20px;
        }

        .social-field {
          min-width: 0;
        }

        .social-label {
          display: flex;
          align-items: center;
          gap: 8px;
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 8px;
        }

        .social-icon {
          width: 28px;
          height: 28px;
          border-radius: 8px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          background: #f0f9f4;
          color: #198754;
          font-size: 14px;
          flex-shrink: 0;
        }

        /* =========================
           STATUS CARD
        ========================= */

        .status-card {
          background: #ffffff;
          border: 1px solid #eaecf0;
          border-radius: 16px;
          overflow: hidden;
          box-shadow: 0 3px 12px rgba(16, 24, 40, 0.035);
          margin-bottom: 18px;
        }

        .status-body {
          padding: 21px 24px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 20px;
        }

        .status-info {
          min-width: 0;
        }

        .status-title {
          margin: 0 0 5px;
          color: #101828;
          font-size: 14px;
          font-weight: 750;
        }

        .status-description {
          margin: 0;
          color: #667085;
          font-size: 12px;
          line-height: 1.65;
          text-align: justify;
        }

        .switch-wrapper {
          display: flex;
          align-items: center;
          gap: 11px;
          flex-shrink: 0;
        }

        .switch-label {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
        }

        .custom-switch {
          position: relative;
          width: 46px;
          height: 25px;
          flex-shrink: 0;
        }

        .custom-switch input {
          opacity: 0;
          width: 0;
          height: 0;
        }

        .switch-slider {
          position: absolute;
          cursor: pointer;
          inset: 0;
          background: #d0d5dd;
          border-radius: 999px;
          transition: 0.25s ease;
        }

        .switch-slider::before {
          content: "";
          position: absolute;
          width: 19px;
          height: 19px;
          left: 3px;
          top: 3px;
          background: #ffffff;
          border-radius: 50%;
          box-shadow: 0 2px 5px rgba(16, 24, 40, 0.18);
          transition: 0.25s ease;
        }

        .custom-switch input:checked + .switch-slider {
          background: #198754;
        }

        .custom-switch input:checked + .switch-slider::before {
          transform: translateX(21px);
        }

        /* =========================
           ERROR
        ========================= */

        .error-alert {
          display: flex;
          align-items: flex-start;
          gap: 10px;
          padding: 13px 15px;
          margin-bottom: 22px;
          border: 1px solid #f5c2c7;
          border-radius: 10px;
          background: #fff5f5;
          color: #842029;
          font-size: 12px;
          line-height: 1.6;
        }

        .error-alert i {
          margin-top: 1px;
          flex-shrink: 0;
        }

        /* =========================
           ACTIONS
        ========================= */

        .form-actions-card {
          background: #ffffff;
          border: 1px solid #eaecf0;
          border-radius: 16px;
          box-shadow: 0 3px 12px rgba(16, 24, 40, 0.035);
          overflow: hidden;
        }

        .form-actions {
          padding: 21px 24px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 18px;
        }

        .action-help {
          min-width: 0;
        }

        .action-help-title {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 4px;
        }

        .action-help-text {
          margin: 0;
          color: #98a2b3;
          font-size: 11px;
          line-height: 1.6;
          text-align: justify;
        }

        .action-buttons {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          gap: 8px;
          flex-shrink: 0;
        }

        .action-button {
          min-height: 40px;
          padding: 8px 14px;
          border-radius: 9px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          font-size: 12px;
          font-weight: 600;
          cursor: pointer;
          text-decoration: none;
          transition: all 0.2s ease;
          white-space: nowrap;
        }

        .save-button {
          color: #ffffff;
          background: #198754;
          border: 1px solid #198754;
          box-shadow: 0 6px 15px rgba(25, 135, 84, 0.14);
        }

        .save-button:hover {
          color: #ffffff;
          background: #157347;
          border-color: #157347;
          transform: translateY(-1px);
        }

        .delete-button {
          color: #dc3545;
          background: #fff5f5;
          border: 1px solid #ffd9dc;
        }

        .delete-button:hover {
          color: #ffffff;
          background: #dc3545;
          border-color: #dc3545;
          transform: translateY(-1px);
        }

        .action-button:disabled {
          opacity: 0.6;
          cursor: not-allowed;
          transform: none !important;
        }

        /* =========================
           FOOTER
        ========================= */

        .admin-page-footer {
          padding: 22px 2px 8px;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 15px;
          color: #98a2b3;
          font-size: 11px;
        }

        .admin-page-footer-left {
          display: flex;
          align-items: center;
          gap: 7px;
        }

        .admin-page-footer a {
          color: #667085;
          text-decoration: none;
          font-weight: 600;
        }

        .admin-page-footer a:hover {
          color: #198754;
        }

        /* =========================
           OVERLAY
        ========================= */

        .sidebar-overlay {
          display: none;
          position: fixed;
          inset: 0;
          background: rgba(15, 23, 42, 0.38);
          z-index: 1040;
          backdrop-filter: blur(2px);
        }

        /* =========================
           TABLET
        ========================= */

        @media (max-width: 1199.98px) {
          .contact-grid {
            grid-template-columns: 1fr;
          }

          .social-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
          }
        }

        /* =========================
           MOBILE / TABLET
        ========================= */

        @media (max-width: 991.98px) {
          .admin-sidebar {
            transform: translateX(-100%);
            box-shadow: 12px 0 30px rgba(15, 23, 42, 0.1);
          }

          .admin-sidebar.sidebar-open {
            transform: translateX(0);
          }

          .sidebar-overlay.sidebar-overlay-show {
            display: block;
          }

          .admin-main {
            width: 100%;
            margin-left: 0;
          }

          .admin-topbar {
            padding: 0 20px;
          }

          .admin-content {
            padding: 25px 20px;
          }

          .page-heading {
            align-items: flex-start;
            flex-direction: column;
          }

          .page-heading-action {
            width: 100%;
          }

          .back-button {
            width: 100%;
          }

          .form-row {
            grid-template-columns: 1fr;
          }

          .status-body {
            align-items: flex-start;
            flex-direction: column;
          }

          .switch-wrapper {
            width: 100%;
            justify-content: space-between;
          }

          .form-actions {
            align-items: stretch;
            flex-direction: column;
          }

          .action-buttons {
            width: 100%;
            justify-content: stretch;
          }

          .action-button {
            flex: 1;
          }
        }

        /* =========================
           SMALL MOBILE
        ========================= */

        @media (max-width: 575.98px) {
          .admin-topbar {
            height: 68px;
            padding: 0 14px;
          }

          .topbar-right {
            gap: 6px;
          }

          .topbar-website,
          .topbar-logout {
            width: 38px;
            height: 38px;
            min-height: 38px;
            padding: 0;
          }

          .topbar-website span,
          .topbar-logout span {
            display: none !important;
          }

          .admin-content {
            padding: 20px 14px;
          }

          .page-title-row {
            align-items: flex-start;
          }

          .page-title-icon {
            width: 43px;
            height: 43px;
            border-radius: 11px;
            font-size: 18px;
          }

          .page-title {
            font-size: 25px;
          }

          .page-description {
            font-size: 12px;
          }

          .content-card-header {
            padding: 17px;
          }

          .content-card-title {
            font-size: 15px;
          }

          .content-card-description {
            font-size: 11px;
          }

          .content-card-body {
            padding: 17px;
          }

          .form-group {
            margin-bottom: 18px;
          }

          .form-row,
          .social-grid {
            grid-template-columns: 1fr;
            gap: 17px;
          }

          .form-label-custom,
          .social-label {
            font-size: 11px;
          }

          .form-control-custom {
            min-height: 41px;
            font-size: 12px;
          }

          textarea.form-control-custom {
            min-height: 115px;
          }

          .status-body {
            padding: 17px;
          }

          .status-title {
            font-size: 13px;
          }

          .status-description {
            font-size: 11px;
          }

          .form-actions {
            padding: 17px;
          }

          .action-buttons {
            flex-direction: column;
          }

          .action-button {
            width: 100%;
            flex: none;
          }

          .admin-page-footer {
            flex-direction: column;
            align-items: flex-start;
          }

          .error-alert {
            font-size: 11px;
          }
        }
      `}</style>

      <div className="admin-layout">
        {/* MOBILE OVERLAY */}
        <div
          className={`sidebar-overlay ${
            sidebarOpen ? "sidebar-overlay-show" : ""
          }`}
          onClick={() => setSidebarOpen(false)}
        />

        {/* SIDEBAR */}
        <aside
          className={`admin-sidebar ${
            sidebarOpen ? "sidebar-open" : ""
          }`}
        >
          <div className="sidebar-inner">
            <div className="sidebar-brand">
              <div className="brand-icon">
                <i className="bi bi-grid-1x2-fill"></i>
              </div>

              <div className="brand-text">
                <strong>GloVaTech</strong>
                <span>Admin Panel</span>
              </div>

              <button
                type="button"
                className="sidebar-close d-lg-none"
                onClick={() => setSidebarOpen(false)}
                aria-label="Tutup sidebar"
              >
                <i className="bi bi-x-lg"></i>
              </button>
            </div>

            <div className="sidebar-section-label">
              MAIN MENU
            </div>

            <nav className="sidebar-nav">
              <a
                href="/secure-panel"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-speedometer2"></i>
                </span>
                <span>Dashboard</span>
              </a>
            </nav>

            <div className="sidebar-section-label">
              CONTENT
            </div>

            <nav className="sidebar-nav">
              <a
                href="/secure-panel/hero"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-stars"></i>
                </span>
                <span>Hero</span>
              </a>

              <a
                href="/secure-panel/about"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-person-vcard"></i>
                </span>
                <span>About</span>
              </a>

              <a
                href="/secure-panel/about/stats"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-bar-chart"></i>
                </span>
                <span>About Stats</span>
              </a>

              <a
                href="/secure-panel/education"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-mortarboard"></i>
                </span>
                <span>Education</span>
              </a>

              <a
                href="/secure-panel/skills"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-tools"></i>
                </span>
                <span>Skills</span>
              </a>

              <a
                href="/secure-panel/experience"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-briefcase"></i>
                </span>
                <span>Experience</span>
              </a>

              <a
                href="/secure-panel/projects"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-code-square"></i>
                </span>
                <span>Projects</span>
              </a>

              <a
                href="/secure-panel/certifications"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-patch-check"></i>
                </span>
                <span>Certification</span>
              </a>

              <a
                href="/secure-panel/achievements"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-trophy"></i>
                </span>
                <span>Achievement</span>
              </a>

              <a
                href="/secure-panel/services"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-gear-wide-connected"></i>
                </span>
                <span>Services</span>
              </a>

              <a
                href="/secure-panel/gallery"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-images"></i>
                </span>
                <span>Gallery</span>
              </a>

              <a
                href="/secure-panel/contact-info"
                className="sidebar-link active"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-person-lines-fill"></i>
                </span>
                <span>Contact</span>
              </a>

              <a
                href="/secure-panel/contact-info/list"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-share-fill"></i>
                </span>
                <span>Contact Sosial Media</span>
              </a>

              <a
                href="/secure-panel/articles"
                className="sidebar-link"
                onClick={() => setSidebarOpen(false)}
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-journal-text"></i>
                </span>
                <span>Articles</span>
              </a>

              <a
  href="/secure-panel/contact-messages"
  className="sidebar-link"
  onClick={() => setSidebarOpen(false)}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-chat-left-text"></i>
  </span>
  <span>Contact Messages</span>
</a>

<a
  href="/secure-panel/contact-users"
  className="sidebar-link"
  onClick={() => setSidebarOpen(false)}
>
  <span className="sidebar-link-icon">
    <i className="bi bi-people-fill"></i>
  </span>
  <span>Contact User</span>
</a>
            </nav>

            <div className="sidebar-bottom">
              <a href="/" className="sidebar-website">
                <i className="bi bi-globe2"></i>
                <span>View Website</span>
                <i className="bi bi-arrow-up-right ms-auto"></i>
              </a>
            </div>
          </div>
        </aside>

        {/* MAIN */}
        <div className="admin-main">
          {/* TOPBAR */}
          <header className="admin-topbar">
            <div className="topbar-left">
              <button
                type="button"
                className="mobile-menu-button d-lg-none"
                onClick={() => setSidebarOpen(true)}
                aria-label="Buka menu"
              >
                <i className="bi bi-list"></i>
              </button>

              <div>
                <div className="topbar-label">
                  ADMIN PANEL
                </div>

                <h6 className="topbar-title mb-0">
                  Contact Management
                </h6>
              </div>
            </div>

            <div className="topbar-right">
              <a
                href="/"
                className="topbar-website"
                title="Lihat Website"
              >
                <i className="bi bi-globe2"></i>
                <span>Website</span>
              </a>

              <a
                href="/secure-panel/login"
                className="topbar-logout"
                title="Logout"
              >
                <i className="bi bi-box-arrow-right"></i>
                <span>Logout</span>
              </a>
            </div>
          </header>

          {/* CONTENT */}
          <main className="admin-content">
            {/* PAGE HEADING */}
            <section className="page-heading">
              <div className="page-heading-left">
                <a
                  href="/secure-panel/contact-info/list"
                  className="back-dashboard"
                >
                  <i className="bi bi-arrow-left"></i>
                  <span>Kembali ke Contact List</span>
                </a>

                <div className="page-title-row">
                  <div className="page-title-icon">
                    <i className="bi bi-person-lines-fill"></i>
                  </div>

                  <div>
                    <div className="page-eyebrow">
                      WEBSITE CONTENT
                    </div>

                    <h1 className="page-title">
                      {editId
                        ? "Edit Contact"
                        : "Contact Information"}
                    </h1>

                    <p className="page-description">
                      {editId
                        ? "Perbarui informasi kontak, alamat, website, dan sosial media yang digunakan pada halaman contact website GloVaTech."
                        : "Kelola informasi kontak, alamat, website, dan sosial media yang digunakan pada halaman contact website GloVaTech."}
                    </p>
                  </div>
                </div>
              </div>

              <div className="page-heading-action">
                <button
                  type="button"
                  className="back-button"
                  onClick={() =>
                    router.push(
                      "/secure-panel/contact-info/list"
                    )
                  }
                >
                  <i className="bi bi-arrow-left"></i>
                  <span>Kembali</span>
                </button>
              </div>
            </section>

            {/* ERROR */}
            {error && (
              <div className="error-alert">
                <i className="bi bi-exclamation-triangle-fill"></i>
                <span>{error}</span>
              </div>
            )}

            <form onSubmit={handleSubmit}>
              {/* CONTACT + PREVIEW INFO */}
              <div className="contact-grid">
                {/* CONTACT INFORMATION */}
                <section className="content-card">
                  <div className="content-card-header">
                    <div>
                      <div className="content-card-eyebrow">
                        CONTACT INFORMATION
                      </div>

                      <h2 className="content-card-title">
                        Informasi Kontak
                      </h2>

                      <p className="content-card-description">
                        Informasi utama yang digunakan pengunjung
                        untuk menghubungi atau menemukan lokasi
                        website owner.
                      </p>
                    </div>

                    <div className="content-card-count">
                      <i className="bi bi-person-lines-fill"></i>
                    </div>
                  </div>

                  <div className="content-card-body">
                    <div className="form-group">
                      <label className="form-label-custom">
                        Description
                      </label>

                      <textarea
                        className="form-control-custom"
                        rows={5}
                        value={description}
                        onChange={(e) =>
                          setDescription(e.target.value)
                        }
                        placeholder="Masukkan deskripsi contact..."
                      />
                    </div>

                    <div className="form-row">
                      {/* EMAIL */}
                      <div className="form-group">
                        <label className="form-label-custom">
                          Email
                        </label>

                        <input
                          type="email"
                          className="form-control-custom"
                          value={email}
                          onChange={(e) =>
                            setEmail(e.target.value)
                          }
                          placeholder="email@example.com"
                        />
                      </div>

                      {/* PHONE */}
                      <div className="form-group">
                        <label className="form-label-custom">
                          Phone
                        </label>

                        <input
                          type="text"
                          className="form-control-custom"
                          value={phone}
                          onChange={(e) =>
                            setPhone(e.target.value)
                          }
                          placeholder="+62..."
                        />
                      </div>

                      {/* LOCATION */}
                      <div className="form-group">
                        <label className="form-label-custom">
                          Location
                        </label>

                        <input
                          type="text"
                          className="form-control-custom"
                          value={location}
                          onChange={(e) =>
                            setLocation(e.target.value)
                          }
                          placeholder="Padang, Sumatera Barat, Indonesia"
                        />
                      </div>

                      {/* WEBSITE */}
                      <div className="form-group">
                        <label className="form-label-custom">
                          Website
                        </label>

                        <input
                          type="url"
                          className="form-control-custom"
                          value={website}
                          onChange={(e) =>
                            setWebsite(e.target.value)
                          }
                          placeholder="https://example.com"
                        />
                      </div>
                    </div>
                  </div>
                </section>

                {/* INFORMATION SIDE CARD */}
                <section className="content-card">
                  <div className="content-card-header">
                    <div>
                      <div className="content-card-eyebrow">
                        CONTACT SETTINGS
                      </div>

                      <h2 className="content-card-title">
                        Pengaturan Contact
                      </h2>

                      <p className="content-card-description">
                        Pastikan informasi kontak yang disimpan
                        sudah sesuai dengan informasi yang ingin
                        ditampilkan pada website.
                      </p>
                    </div>

                    <div className="content-card-count">
                      <i className="bi bi-gear"></i>
                    </div>
                  </div>

                  <div className="content-card-body">
                    <div className="form-group">
                      <label className="form-label-custom">
                        Status Data
                      </label>

                      <div
                        style={{
                          display: "flex",
                          alignItems: "center",
                          gap: "10px",
                          padding: "12px",
                          borderRadius: "10px",
                          background: active
                            ? "#e9f7ef"
                            : "#f2f4f7",
                          color: active
                            ? "#198754"
                            : "#667085",
                          fontSize: "12px",
                          fontWeight: 700,
                        }}
                      >
                        <i
                          className={
                            active
                              ? "bi bi-check-circle-fill"
                              : "bi bi-pause-circle-fill"
                          }
                        ></i>

                        <span>
                          {active
                            ? "Contact Information Aktif"
                            : "Contact Information Tidak Aktif"}
                        </span>
                      </div>
                    </div>

                    <div className="form-group">
                      <label className="form-label-custom">
                        Informasi
                      </label>

                      <p
                        style={{
                          margin: 0,
                          color: "#667085",
                          fontSize: "12px",
                          lineHeight: 1.7,
                          textAlign: "justify",
                        }}
                      >
                        Contact Information digunakan sebagai
                        sumber informasi kontak pada website.
                        Pastikan email, nomor telepon, lokasi,
                        website, dan tautan sosial media telah
                        diisi dengan benar agar dapat digunakan
                        oleh pengunjung.
                      </p>
                    </div>
                  </div>
                </section>
              </div>

              {/* SOCIAL MEDIA */}
              <section className="content-card" style={{ marginBottom: "18px" }}>
                <div className="content-card-header">
                  <div>
                    <div className="content-card-eyebrow">
                      SOCIAL MEDIA
                    </div>

                    <h2 className="content-card-title">
                      Social Media Links
                    </h2>

                    <p className="content-card-description">
                      Kelola tautan sosial media yang akan
                      ditampilkan pada bagian contact atau
                      footer website.
                    </p>
                  </div>

                  <div className="content-card-count">
                    <i className="bi bi-share-fill"></i>
                    <span>7</span>
                  </div>
                </div>

                <div className="content-card-body">
                  <div className="social-grid">
                    {/* GITHUB */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-github"></i>
                        </span>
                        GitHub
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={github}
                        onChange={(e) =>
                          setGithub(e.target.value)
                        }
                        placeholder="https://github.com/username"
                      />
                    </div>

                    {/* LINKEDIN */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-linkedin"></i>
                        </span>
                        LinkedIn
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={linkedin}
                        onChange={(e) =>
                          setLinkedin(e.target.value)
                        }
                        placeholder="https://linkedin.com/in/username"
                      />
                    </div>

                    {/* FACEBOOK */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-facebook"></i>
                        </span>
                        Facebook
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={facebook}
                        onChange={(e) =>
                          setFacebook(e.target.value)
                        }
                        placeholder="https://facebook.com/username"
                      />
                    </div>

                    {/* INSTAGRAM */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-instagram"></i>
                        </span>
                        Instagram
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={instagram}
                        onChange={(e) =>
                          setInstagram(e.target.value)
                        }
                        placeholder="https://instagram.com/username"
                      />
                    </div>

                    {/* TWITTER */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-twitter-x"></i>
                        </span>
                        Twitter / X
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={twitter}
                        onChange={(e) =>
                          setTwitter(e.target.value)
                        }
                        placeholder="https://x.com/username"
                      />
                    </div>

                    {/* YOUTUBE */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-youtube"></i>
                        </span>
                        YouTube
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={youtube}
                        onChange={(e) =>
                          setYoutube(e.target.value)
                        }
                        placeholder="https://youtube.com/@channel"
                      />
                    </div>

                    {/* TIKTOK */}
                    <div className="social-field">
                      <label className="social-label">
                        <span className="social-icon">
                          <i className="bi bi-tiktok"></i>
                        </span>
                        TikTok
                      </label>

                      <input
                        type="url"
                        className="form-control-custom"
                        value={tiktok}
                        onChange={(e) =>
                          setTiktok(e.target.value)
                        }
                        placeholder="https://tiktok.com/@username"
                      />
                    </div>
                  </div>
                </div>
              </section>

              {/* ACTIVE STATUS */}
              <section className="status-card">
                <div className="status-body">
                  <div className="status-info">
                    <h3 className="status-title">
                      Status Contact Information
                    </h3>

                    <p className="status-description">
                      Aktifkan data ini agar Contact Information
                      dapat digunakan dan ditampilkan pada
                      website utama GloVaTech.
                    </p>
                  </div>

                  <div className="switch-wrapper">
                    <span className="switch-label">
                      {active ? "Aktif" : "Tidak Aktif"}
                    </span>

                    <label className="custom-switch">
                      <input
                        type="checkbox"
                        checked={active}
                        onChange={(e) =>
                          setActive(e.target.checked)
                        }
                      />

                      <span className="switch-slider"></span>
                    </label>
                  </div>
                </div>
              </section>

              {/* ACTIONS */}
              <section className="form-actions-card">
                <div className="form-actions">
                  <div className="action-help">
                    <div className="action-help-title">
                      {editId
                        ? "Perbarui Contact Information"
                        : "Simpan Contact Information"}
                    </div>

                    <p className="action-help-text">
                      Periksa kembali seluruh informasi sebelum
                      menyimpan perubahan agar data yang
                      ditampilkan pada website tetap akurat.
                    </p>
                  </div>

                  <div className="action-buttons">
                    <button
                      type="button"
                      className="action-button delete-button"
                      onClick={handleDelete}
                      disabled={!data || saving}
                    >
                      <i className="bi bi-trash"></i>
                      <span>Hapus</span>
                    </button>

                    <button
                      type="submit"
                      className="action-button save-button"
                      disabled={saving}
                    >
                      {saving ? (
                        <>
                          <span
                            className="spinner-border spinner-border-sm"
                            role="status"
                            aria-hidden="true"
                          ></span>
                          <span>Menyimpan...</span>
                        </>
                      ) : (
                        <>
                          <i className="bi bi-save"></i>
                          <span>
                            {editId
                              ? "Update Contact"
                              : "Simpan Contact"}
                          </span>
                        </>
                      )}
                    </button>
                  </div>
                </div>
              </section>
            </form>

            {/* FOOTER */}
            <footer className="admin-page-footer">
              <div className="admin-page-footer-left">
                <i className="bi bi-shield-check"></i>
                <span>
                  Admin Content Management System
                </span>
              </div>

              <a href="/secure-panel">
                <i className="bi bi-arrow-left me-1"></i>
                Dashboard
              </a>
            </footer>
          </main>
        </div>
      </div>
    </>
  );
}

export default function ContactInfoAdminPageWithSuspense() {
  return (
    <Suspense
      fallback={
        <div
          style={{
            minHeight: "100vh",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          Memuat Contact Information...
        </div>
      }
    >
      <ContactInfoAdminPage />
    </Suspense>
  );
}