"use client";

import {
  ChangeEvent,
  FormEvent,
  useEffect,
  useState,
} from "react";

import Link from "next/link";
import { useRouter } from "next/navigation";

type PreviewImage = {
  file: File;
  preview: string;
};

type UploadedImage = {
  url: string;
  fileName: string;
};

export default function CreateGalleryPage() {
  const router = useRouter();

  const [title, setTitle] = useState("");
  const [description, setDescription] = useState("");
  const [category, setCategory] = useState("");
  const [images, setImages] = useState<PreviewImage[]>([]);
  const [loading, setLoading] = useState(false);
  const [uploading, setUploading] = useState(false);
  const [sidebarOpen, setSidebarOpen] = useState(false);

  /*
   * Membersihkan object URL
   * ketika component dihancurkan.
   */

  useEffect(() => {
    return () => {
      images.forEach((item) => {
        URL.revokeObjectURL(item.preview);
      });
    };
  }, [images]);

  /*
   * Pilih gambar
   */

  const handleImageChange = (
    event: ChangeEvent<HTMLInputElement>
  ) => {
    const files = Array.from(event.target.files || []);

    if (files.length === 0) {
      return;
    }

    const allowedTypes = [
      "image/jpeg",
      "image/jpg",
      "image/png",
      "image/webp",
      "image/gif",
    ];

    const maxSize = 5 * 1024 * 1024;

    const validFiles: PreviewImage[] = [];

    for (const file of files) {
      if (!allowedTypes.includes(file.type)) {
        alert(
          `${file.name} bukan file gambar yang didukung.`
        );

        continue;
      }

      if (file.size > maxSize) {
        alert(
          `${file.name} terlalu besar. Maksimal 5 MB.`
        );

        continue;
      }

      validFiles.push({
        file,
        preview: URL.createObjectURL(file),
      });
    }

    setImages((previous) => [
      ...previous,
      ...validFiles,
    ]);

    event.target.value = "";
  };

  /*
   * Hapus satu gambar
   */

  const removeImage = (index: number) => {
    setImages((previous) => {
      const item = previous[index];

      if (item) {
        URL.revokeObjectURL(item.preview);
      }

      return previous.filter(
        (_, itemIndex) => itemIndex !== index
      );
    });
  };

  /*
   * Submit Gallery
   */

  const handleSubmit = async (
    event: FormEvent
  ) => {
    event.preventDefault();

    if (!title.trim()) {
      alert("Title wajib diisi.");
      return;
    }

    if (images.length === 0) {
      alert("Minimal satu gambar harus dipilih.");
      return;
    }

    setLoading(true);

    try {
      /*
       * STEP 1
       * Upload semua file
       */

      setUploading(true);

      const formData = new FormData();

      images.forEach((item) => {
        formData.append("files", item.file);
      });

      const uploadResponse = await fetch(
        "/api/gallery/upload",
        {
          method: "POST",
          body: formData,
        }
      );

      const uploadResult =
        await uploadResponse.json();

      if (
        !uploadResponse.ok ||
        !uploadResult.success
      ) {
        throw new Error(
          uploadResult.message ||
            "Gagal mengupload gambar."
        );
      }

      const uploadedImages: UploadedImage[] =
        uploadResult.data;

      setUploading(false);

      /*
       * STEP 2
       * Simpan Gallery ke database
       */

      const galleryResponse = await fetch(
        "/api/gallery",
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify({
            title: title.trim(),

            description:
              description.trim() || null,

            category:
              category.trim() || null,

            images: uploadedImages.map(
              (item) => item.url
            ),
          }),
        }
      );

      const galleryResult =
        await galleryResponse.json();

      if (
        !galleryResponse.ok ||
        !galleryResult.success
      ) {
        throw new Error(
          galleryResult.message ||
            "Gagal menyimpan Gallery."
        );
      }

      alert(
        "Gallery berhasil ditambahkan."
      );

      router.push("/secure-panel/gallery");

      router.refresh();
    } catch (error) {
      console.error(
        "CREATE GALLERY:",
        error
      );

      alert(
        error instanceof Error
          ? error.message
          : "Terjadi kesalahan saat menyimpan Gallery."
      );
    } finally {
      setUploading(false);
      setLoading(false);
    }
  };

  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: #ffffff;
          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;
        }

        /* =========================
           FORM CARD
        ========================= */

        .form-card {
          background: #ffffff;
          border: 1px solid #eaecf0;
          border-radius: 16px;
          overflow: hidden;
          box-shadow: 0 3px 12px rgba(16, 24, 40, 0.035);
        }

        .form-card-header {
          padding: 21px 24px;
          border-bottom: 1px solid #eaecf0;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 20px;
        }

        .form-card-eyebrow {
          font-size: 10px;
          font-weight: 700;
          color: #198754;
          letter-spacing: 1px;
          margin-bottom: 4px;
        }

        .form-card-title {
          font-size: 17px;
          font-weight: 750;
          color: #101828;
          margin: 0;
        }

        .form-card-description {
          margin: 5px 0 0;
          color: #667085;
          font-size: 12px;
          line-height: 1.65;
          text-align: justify;
          max-width: 700px;
        }

        .form-card-icon {
          width: 40px;
          height: 40px;
          border-radius: 10px;
          background: #e9f7ef;
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 17px;
          flex-shrink: 0;
        }

        .form-card-body {
          padding: 26px 24px;
        }

        /* =========================
           FORM
        ========================= */

        .form-section {
          margin-bottom: 23px;
        }

        .form-section:last-child {
          margin-bottom: 0;
        }

        .form-label-custom {
          display: block;
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 8px;
        }

        .required-mark {
          color: #dc3545;
          margin-left: 3px;
        }

        .form-control-custom {
          width: 100%;
          min-height: 45px;
          padding: 10px 13px;
          border: 1px solid #d0d5dd;
          border-radius: 9px;
          background: #ffffff;
          color: #344054;
          font-size: 12px;
          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;
          box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
        }

        .form-control-custom:disabled {
          background: #f8fafc;
          cursor: not-allowed;
        }

        textarea.form-control-custom {
          min-height: 125px;
          resize: vertical;
          text-align: justify;
        }

        .file-input {
          padding: 8px 10px;
          cursor: pointer;
        }

        .form-help {
          margin-top: 7px;
          color: #98a2b3;
          font-size: 10px;
          line-height: 1.6;
          text-align: justify;
        }

        /* =========================
           SELECTED IMAGES
        ========================= */

        .selected-images-section {
          padding-top: 4px;
        }

        .selected-images-header {
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 15px;
          margin-bottom: 14px;
        }

        .selected-images-title {
          margin: 0;
          color: #344054;
          font-size: 13px;
          font-weight: 750;
        }

        .image-count-badge {
          display: inline-flex;
          align-items: center;
          gap: 5px;
          padding: 6px 9px;
          border-radius: 7px;
          background: #e9f7ef;
          color: #198754;
          font-size: 10px;
          font-weight: 700;
          white-space: nowrap;
        }

        .image-grid {
          display: grid;
          grid-template-columns: repeat(4, minmax(0, 1fr));
          gap: 14px;
        }

        .image-item {
          min-width: 0;
          border: 1px solid #eaecf0;
          border-radius: 12px;
          overflow: hidden;
          background: #ffffff;
          box-shadow: 0 2px 6px rgba(16, 24, 40, 0.025);
          transition:
            transform 0.2s ease,
            box-shadow 0.2s ease;
        }

        .image-item:hover {
          transform: translateY(-2px);
          box-shadow: 0 8px 20px rgba(16, 24, 40, 0.08);
        }

        .image-preview-wrapper {
          position: relative;
          width: 100%;
          height: 175px;
          background: #f5f7f9;
        }

        .image-preview {
          width: 100%;
          height: 100%;
          display: block;
          object-fit: cover;
        }

        .image-number {
          position: absolute;
          top: 9px;
          left: 9px;
          min-width: 25px;
          height: 25px;
          padding: 0 7px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          border-radius: 7px;
          background: rgba(16, 24, 40, 0.76);
          color: #ffffff;
          font-size: 9px;
          font-weight: 700;
        }

        .remove-image-button {
          position: absolute;
          top: 8px;
          right: 8px;
          width: 30px;
          height: 30px;
          padding: 0;
          border: 0;
          border-radius: 8px;
          background: #dc3545;
          color: #ffffff;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 11px;
          cursor: pointer;
          box-shadow: 0 4px 10px rgba(220, 53, 69, 0.2);
          transition: all 0.2s ease;
        }

        .remove-image-button:hover {
          background: #bb2d3b;
          transform: scale(1.04);
        }

        .remove-image-button:disabled {
          opacity: 0.6;
          cursor: not-allowed;
          transform: none;
        }

        .image-file-name {
          padding: 10px 11px;
          color: #475467;
          font-size: 10px;
          line-height: 1.5;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        /* =========================
           INFO BOX
        ========================= */

        .gallery-info-box {
          display: flex;
          align-items: flex-start;
          gap: 13px;
          padding: 16px 17px;
          margin-top: 4px;
          margin-bottom: 23px;
          border: 1px solid #b7dfc9;
          border-radius: 12px;
          background: #f0f9f4;
          color: #344054;
        }

        .gallery-info-icon {
          width: 38px;
          height: 38px;
          border-radius: 9px;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;
          background: #e9f7ef;
          color: #198754;
          font-size: 17px;
        }

        .gallery-info-title {
          color: #198754;
          font-size: 12px;
          font-weight: 750;
          margin-bottom: 4px;
        }

        .gallery-info-text {
          color: #667085;
          font-size: 10px;
          line-height: 1.7;
          text-align: justify;
        }

        /* =========================
           ACTION
        ========================= */

        .form-actions {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          gap: 9px;
          padding-top: 20px;
          border-top: 1px solid #eaecf0;
        }

        .action-button {
          min-height: 42px;
          padding: 9px 16px;
          border-radius: 9px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          border: 1px solid transparent;
          font-size: 12px;
          font-weight: 650;
          cursor: pointer;
          text-decoration: none;
          transition: all 0.2s ease;
        }

        .save-button {
          background: #198754;
          border-color: #198754;
          color: #ffffff;
          box-shadow: 0 6px 15px rgba(25, 135, 84, 0.14);
        }

        .save-button:hover {
          background: #157347;
          border-color: #157347;
          color: #ffffff;
          transform: translateY(-1px);
          box-shadow: 0 9px 20px rgba(25, 135, 84, 0.19);
        }

        .save-button:disabled {
          opacity: 0.65;
          cursor: not-allowed;
          transform: none;
        }

        .cancel-button {
          background: #ffffff;
          border-color: #d0d5dd;
          color: #475467;
        }

        .cancel-button:hover {
          background: #f8fafc;
          border-color: #98a2b3;
          color: #344054;
        }

        .cancel-button:disabled {
          opacity: 0.6;
          cursor: not-allowed;
        }

        /* =========================
           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) {
          .admin-content {
            padding: 28px;
          }

          .image-grid {
            grid-template-columns: repeat(3, 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;
          }

          .form-card {
            border-radius: 14px;
          }

          .image-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
          }
        }

        /* =========================
           MOBILE
        ========================= */

        @media (max-width: 767.98px) {
          .form-card-header {
            padding: 18px;
          }

          .form-card-body {
            padding: 20px 18px;
          }

          .form-card-description {
            max-width: 100%;
          }

          .image-grid {
            gap: 10px;
          }

          .image-preview-wrapper {
            height: 155px;
          }

          .form-actions {
            justify-content: stretch;
          }

          .form-actions .action-button {
            flex: 1 1 0;
          }
        }

        /* =========================
           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;
          }

          .form-card-header {
            padding: 17px 15px;
          }

          .form-card-title {
            font-size: 15px;
          }

          .form-card-description {
            font-size: 11px;
          }

          .form-card-icon {
            width: 36px;
            height: 36px;
            font-size: 15px;
          }

          .form-card-body {
            padding: 18px 15px;
          }

          .form-label-custom {
            font-size: 11px;
          }

          .form-control-custom {
            min-height: 43px;
            font-size: 11px;
          }

          textarea.form-control-custom {
            min-height: 115px;
          }

          .form-help {
            font-size: 9px;
          }

          .selected-images-title {
            font-size: 12px;
          }

          .image-count-badge {
            font-size: 9px;
          }

          .image-grid {
            grid-template-columns: 1fr 1fr;
            gap: 8px;
          }

          .image-preview-wrapper {
            height: 135px;
          }

          .image-number {
            top: 7px;
            left: 7px;
            min-width: 22px;
            height: 22px;
            font-size: 8px;
          }

          .remove-image-button {
            top: 6px;
            right: 6px;
            width: 27px;
            height: 27px;
            font-size: 9px;
          }

          .image-file-name {
            padding: 8px 9px;
            font-size: 9px;
          }

          .gallery-info-box {
            padding: 13px;
            gap: 10px;
          }

          .gallery-info-icon {
            width: 34px;
            height: 34px;
            font-size: 14px;
          }

          .gallery-info-title {
            font-size: 11px;
          }

          .gallery-info-text {
            font-size: 9px;
          }

          .form-actions {
            flex-direction: column-reverse;
            gap: 8px;
          }

          .form-actions .action-button {
            width: 100%;
            flex: none;
          }

          .admin-page-footer {
            flex-direction: column;
            align-items: flex-start;
            padding-top: 20px;
          }
        }
      `}</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">

            {/* BRAND */}

            <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>

            {/* MAIN MENU */}

            <div className="sidebar-section-label">
              MAIN MENU
            </div>

            <nav className="sidebar-nav">
              <Link
                href="/secure-panel"
                className="sidebar-link"
                onClick={() =>
                  setSidebarOpen(false)
                }
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-speedometer2"></i>
                </span>

                <span>Dashboard</span>
              </Link>
            </nav>

            {/* CONTENT */}

            <div className="sidebar-section-label">
              CONTENT
            </div>

            <nav className="sidebar-nav">

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                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>
              </Link>

              <Link
                href="/secure-panel/gallery"
                className="sidebar-link active"
                onClick={() =>
                  setSidebarOpen(false)
                }
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-images"></i>
                </span>

                <span>Gallery</span>
              </Link>

              <Link
                href="/secure-panel/contact-info"
                className="sidebar-link"
                onClick={() =>
                  setSidebarOpen(false)
                }
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-person-lines-fill"></i>
                </span>

                <span>Contact</span>
              </Link>

              <Link
                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 Social Media
                </span>
              </Link>

              <Link
                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>
              </Link>

              <Link
  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>
</Link>

<Link
  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>
</Link>

            </nav>

            {/* BOTTOM */}

            <div className="sidebar-bottom">
              <Link
                href="/"
                className="sidebar-website"
                onClick={() =>
                  setSidebarOpen(false)
                }
              >
                <i className="bi bi-globe2"></i>

                <span>View Website</span>

                <i className="bi bi-arrow-up-right ms-auto"></i>
              </Link>
            </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">
                  Gallery Management
                </h6>
              </div>

            </div>

            <div className="topbar-right">

              <Link
                href="/"
                className="topbar-website"
                title="Lihat Website"
              >
                <i className="bi bi-globe2"></i>
                <span>Website</span>
              </Link>

              <Link
                href="/secure-panel/login"
                className="topbar-logout"
                title="Logout"
              >
                <i className="bi bi-box-arrow-right"></i>
                <span>Logout</span>
              </Link>

            </div>

          </header>

          {/* =========================
              CONTENT
          ========================= */}

          <main className="admin-content">

            {/* PAGE HEADING */}

            <section className="page-heading">

              <div className="page-heading-left">

                <Link
                  href="/secure-panel/gallery"
                  className="back-dashboard"
                >
                  <i className="bi bi-arrow-left"></i>
                  <span>
                    Kembali ke Gallery
                  </span>
                </Link>

                <div className="page-title-row">

                  <div className="page-title-icon">
                    <i className="bi bi-images"></i>
                  </div>

                  <div>

                    <div className="page-eyebrow">
                      WEBSITE CONTENT
                    </div>

                    <h1 className="page-title">
                      Tambah Gallery
                    </h1>

                    <p className="page-description">
                      Tambahkan koleksi foto dan
                      dokumentasi baru ke dalam
                      gallery website portfolio
                      GloVaTech. Anda dapat
                      mengunggah satu atau beberapa
                      gambar sekaligus.
                    </p>

                  </div>

                </div>

              </div>

            </section>

            {/* =========================
                FORM CARD
            ========================= */}

            <section className="form-card">

              {/* HEADER */}

              <div className="form-card-header">

                <div>
                  <div className="form-card-eyebrow">
                    GALLERY CONTENT
                  </div>

                  <h2 className="form-card-title">
                    Informasi Gallery
                  </h2>

                  <p className="form-card-description">
                    Lengkapi informasi gallery,
                    kemudian pilih gambar yang
                    ingin ditampilkan sebagai satu
                    koleksi dokumentasi.
                  </p>
                </div>

                <div className="form-card-icon">
                  <i className="bi bi-folder-plus"></i>
                </div>

              </div>

              {/* BODY */}

              <div className="form-card-body">

                <form onSubmit={handleSubmit}>

                  {/* TITLE */}

                  <div className="form-section">

                    <label
                      htmlFor="gallery-title"
                      className="form-label-custom"
                    >
                      Title
                      <span className="required-mark">
                        *
                      </span>
                    </label>

                    <input
                      id="gallery-title"
                      type="text"
                      className="form-control-custom"
                      value={title}
                      onChange={(event) =>
                        setTitle(
                          event.target.value
                        )
                      }
                      placeholder="Contoh: Engineering Workshop"
                      disabled={loading}
                    />

                  </div>

                  {/* DESCRIPTION */}

                  <div className="form-section">

                    <label
                      htmlFor="gallery-description"
                      className="form-label-custom"
                    >
                      Description
                    </label>

                    <textarea
                      id="gallery-description"
                      className="form-control-custom"
                      rows={5}
                      value={description}
                      onChange={(event) =>
                        setDescription(
                          event.target.value
                        )
                      }
                      placeholder="Deskripsi singkat dokumentasi..."
                      disabled={loading}
                    />

                    <div className="form-help">
                      Gunakan deskripsi singkat
                      dan informatif untuk
                      menjelaskan isi dokumentasi
                      gallery.
                    </div>

                  </div>

                  {/* CATEGORY */}

                  <div className="form-section">

                    <label
                      htmlFor="gallery-category"
                      className="form-label-custom"
                    >
                      Category
                    </label>

                    <input
                      id="gallery-category"
                      type="text"
                      className="form-control-custom"
                      value={category}
                      onChange={(event) =>
                        setCategory(
                          event.target.value
                        )
                      }
                      placeholder="Contoh: Electrical Maintenance"
                      disabled={loading}
                    />

                    <div className="form-help">
                      Masukkan kategori secara
                      manual untuk mengelompokkan
                      dokumentasi gallery.
                    </div>

                  </div>

                  {/* IMAGES */}

                  <div className="form-section">

                    <label
                      htmlFor="gallery-images"
                      className="form-label-custom"
                    >
                      Gallery Images
                      <span className="required-mark">
                        *
                      </span>
                    </label>

                    <input
                      id="gallery-images"
                      type="file"
                      className="form-control-custom file-input"
                      accept="image/jpeg,image/jpg,image/png,image/webp,image/gif"
                      multiple
                      onChange={
                        handleImageChange
                      }
                      disabled={loading}
                    />

                    <div className="form-help">
                      Anda dapat memilih beberapa
                      gambar sekaligus. Format yang
                      didukung: JPG, JPEG, PNG, WEBP,
                      dan GIF. Maksimal 5 MB per
                      gambar.
                    </div>

                  </div>

                  {/* SELECTED IMAGES */}

                  {images.length > 0 && (
                    <div className="form-section selected-images-section">

                      <div className="selected-images-header">

                        <h3 className="selected-images-title">
                          Gambar Dipilih
                        </h3>

                        <span className="image-count-badge">
                          <i className="bi bi-images"></i>
                          {images.length} gambar
                        </span>

                      </div>

                      <div className="image-grid">

                        {images.map(
                          (item, index) => (
                            <div
                              key={`${item.file.name}-${index}`}
                              className="image-item"
                            >

                              <div className="image-preview-wrapper">

                                <img
                                  src={
                                    item.preview
                                  }
                                  alt={
                                    item.file.name
                                  }
                                  className="image-preview"
                                />

                                <span className="image-number">
                                  {index + 1}
                                </span>

                                <button
                                  type="button"
                                  className="remove-image-button"
                                  onClick={() =>
                                    removeImage(
                                      index
                                    )
                                  }
                                  disabled={loading}
                                  aria-label={`Hapus gambar ${index + 1}`}
                                  title="Hapus gambar"
                                >
                                  <i className="bi bi-x-lg"></i>
                                </button>

                              </div>

                              <div
                                className="image-file-name"
                                title={
                                  item.file.name
                                }
                              >
                                {item.file.name}
                              </div>

                            </div>
                          )
                        )}

                      </div>

                    </div>
                  )}

                  {/* INFO */}

                  <div className="gallery-info-box">

                    <div className="gallery-info-icon">
                      <i className="bi bi-images"></i>
                    </div>

                    <div>

                      <div className="gallery-info-title">
                        Multiple Images
                      </div>

                      <div className="gallery-info-text">
                        Semua gambar yang dipilih
                        akan menjadi satu Gallery.
                        Pengunjung dapat menggeser
                        gambar menggunakan carousel
                        pada website utama.
                      </div>

                    </div>

                  </div>

                  {/* ACTION */}

                  <div className="form-actions">

                    <button
                      type="button"
                      className="action-button cancel-button"
                      onClick={() =>
                        router.push(
                          "/secure-panel/gallery"
                        )
                      }
                      disabled={loading}
                    >
                      <i className="bi bi-x-lg"></i>
                      <span>Batal</span>
                    </button>

                    <button
                      type="submit"
                      className="action-button save-button"
                      disabled={
                        loading ||
                        images.length === 0
                      }
                    >
                      {loading ? (
                        <>
                          <span
                            className="spinner-border spinner-border-sm"
                            role="status"
                            aria-hidden="true"
                          ></span>

                          <span>
                            {uploading
                              ? "Mengupload gambar..."
                              : "Menyimpan Gallery..."}
                          </span>
                        </>
                      ) : (
                        <>
                          <i className="bi bi-check-lg"></i>
                          <span>
                            Simpan Gallery
                          </span>
                        </>
                      )}
                    </button>

                  </div>

                </form>

              </div>

            </section>

            {/* =========================
                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>

              <Link href="/secure-panel/gallery">
                <i className="bi bi-arrow-left me-1"></i>
                Gallery
              </Link>

            </footer>

          </main>

        </div>

      </div>
    </>
  );
}