"use client";

import {
  FormEvent,
  useEffect,
  useState,
} from "react";
import Link from "next/link";
import { useRouter } from "next/navigation";

type PreviewImage = {
  id: string;
  file: File;
  preview: string;
};

export default function CreateAchievementPage() {
  const router = useRouter();

  const [form, setForm] = useState({
    title: "",
    issuer: "",
    date: "",
    description: "",
    url: "",
  });

  const [images, setImages] = useState<PreviewImage[]>([]);
  const [loading, setLoading] = useState(false);
  const [sidebarOpen, setSidebarOpen] = useState(false);

  // =========================
  // HANDLE CHANGE
  // =========================

  const handleChange = (
    e: React.ChangeEvent<
      HTMLInputElement | HTMLTextAreaElement
    >
  ) => {
    const { name, value } = e.target;

    setForm((prev) => ({
      ...prev,
      [name]: value,
    }));
  };

  // =========================
  // SELECT IMAGES
  // =========================

  const handleImageChange = (
    e: React.ChangeEvent<HTMLInputElement>
  ) => {
    const selectedFiles = Array.from(
      e.target.files || []
    );

    if (selectedFiles.length === 0) {
      return;
    }

    const allowedTypes = [
      "image/jpeg",
      "image/jpg",
      "image/png",
      "image/webp",
      "image/gif",
    ];

    const validImages: PreviewImage[] = [];

    for (const file of selectedFiles) {
      if (!allowedTypes.includes(file.type)) {
        alert(
          `${file.name} tidak didukung. Gunakan JPG, JPEG, PNG, WebP, atau GIF.`
        );

        continue;
      }

      if (file.size > 5 * 1024 * 1024) {
        alert(
          `${file.name} terlalu besar. Maksimal 5 MB per gambar.`
        );

        continue;
      }

      const duplicate = images.some(
        (item) =>
          item.file.name === file.name &&
          item.file.size === file.size
      );

      if (duplicate) {
        continue;
      }

      validImages.push({
        id: `${Date.now()}-${Math.random()
          .toString(36)
          .substring(2, 9)}`,
        file,
        preview: URL.createObjectURL(file),
      });
    }

    setImages((prev) => [
      ...prev,
      ...validImages,
    ]);

    e.target.value = "";
  };

  // =========================
  // REMOVE IMAGE
  // =========================

  const removeImage = (id: string) => {
    setImages((prev) => {
      const target = prev.find(
        (item) => item.id === id
      );

      if (target) {
        URL.revokeObjectURL(target.preview);
      }

      return prev.filter(
        (item) => item.id !== id
      );
    });
  };

  // =========================
  // CLEANUP PREVIEW
  // =========================

  useEffect(() => {
    return () => {
      images.forEach((item) => {
        URL.revokeObjectURL(item.preview);
      });
    };
  }, []);

  // =========================
  // UPLOAD IMAGES
  // =========================

  const uploadImages = async () => {
    if (images.length === 0) {
      return [];
    }

    const formData = new FormData();

    images.forEach((item) => {
      formData.append(
        "files",
        item.file
      );
    });

    const response = await fetch(
      "/api/achievements/upload",
      {
        method: "POST",
        body: formData,
      }
    );

    const result =
      await response.json();

    if (
      !response.ok ||
      !result.success
    ) {
      throw new Error(
        result.message ||
          "Gagal mengupload gambar Achievement."
      );
    }

    return result.data as {
      url: string;
      fileName: string;
    }[];
  };

  // =========================
  // SUBMIT
  // =========================

  const handleSubmit = async (
    e: FormEvent
  ) => {
    e.preventDefault();

    if (!form.title.trim()) {
      alert(
        "Judul Achievement wajib diisi."
      );

      return;
    }

    if (images.length === 0) {
      alert(
        "Minimal satu gambar Achievement harus dipilih."
      );

      return;
    }

    setLoading(true);

    try {
      // ======================
      // UPLOAD
      // ======================

      const uploadedImages =
        await uploadImages();

      if (
        uploadedImages.length === 0
      ) {
        throw new Error(
          "Tidak ada gambar yang berhasil diupload."
        );
      }

      // ======================
      // CREATE ACHIEVEMENT
      // ======================

      const response = await fetch(
        "/api/achievements",
        {
          method: "POST",
          headers: {
            "Content-Type":
              "application/json",
          },

          body: JSON.stringify({
            title: form.title,

            issuer:
              form.issuer || null,

            date:
              form.date || null,

            description:
              form.description || null,

            url:
              form.url || null,

            images:
              uploadedImages.map(
                (item) => item.url
              ),

            image:
              uploadedImages[0].url,
          }),
        }
      );

      const result =
        await response.json();

      if (
        !response.ok ||
        !result.success
      ) {
        throw new Error(
          result.message ||
            "Gagal membuat Achievement."
        );
      }

      alert(
        "Achievement berhasil ditambahkan."
      );

      router.push(
        "/secure-panel/achievements"
      );

      router.refresh();
    } catch (error) {
      console.error(
        "CREATE ACHIEVEMENT ERROR:",
        error
      );

      alert(
        error instanceof Error
          ? error.message
          : "Gagal menambahkan Achievement."
      );
    } finally {
      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: 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;
        }

        /* =========================
           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: 650px;
        }

        .form-card-icon {
          width: 40px;
          height: 40px;
          border-radius: 10px;
          background: #e9f7ef;
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          flex-shrink: 0;
        }

        .form-card-body {
          padding: 26px;
        }

        /* =========================
           FORM ELEMENTS
        ========================= */

        .form-section {
          margin-bottom: 28px;
        }

        .form-section:last-child {
          margin-bottom: 0;
        }

        .section-heading {
          display: flex;
          align-items: center;
          gap: 10px;
          margin-bottom: 18px;
        }

        .section-heading-icon {
          width: 34px;
          height: 34px;
          border-radius: 9px;
          display: flex;
          align-items: center;
          justify-content: center;
          background: #f0f9f4;
          color: #198754;
          font-size: 14px;
          flex-shrink: 0;
        }

        .section-heading-text h3 {
          margin: 0;
          color: #101828;
          font-size: 14px;
          font-weight: 700;
        }

        .section-heading-text p {
          margin: 3px 0 0;
          color: #98a2b3;
          font-size: 11px;
          line-height: 1.5;
        }

        .form-label-custom {
          display: block;
          margin-bottom: 7px;
          color: #344054;
          font-size: 12px;
          font-weight: 700;
        }

        .required-mark {
          color: #dc3545;
          margin-left: 3px;
        }

        .form-control-custom {
          width: 100%;
          min-height: 44px;
          padding: 10px 13px;
          border: 1px solid #d0d5dd;
          border-radius: 9px;
          background: #ffffff;
          color: #344054;
          font-size: 13px;
          outline: none;
          transition:
            border-color 0.2s ease,
            box-shadow 0.2s ease;
        }

        .form-control-custom:focus {
          border-color: #198754;
          box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
        }

        .form-control-custom::placeholder {
          color: #98a2b3;
        }

        textarea.form-control-custom {
          min-height: 150px;
          resize: vertical;
          line-height: 1.7;
          text-align: justify;
        }

        .input-group-custom {
          display: flex;
          width: 100%;
        }

        .input-group-icon {
          min-width: 44px;
          height: 44px;
          display: flex;
          align-items: center;
          justify-content: center;
          border: 1px solid #d0d5dd;
          border-right: 0;
          border-radius: 9px 0 0 9px;
          background: #f8fafc;
          color: #667085;
          font-size: 16px;
        }

        .input-group-custom .form-control-custom {
          border-radius: 0 9px 9px 0;
        }

        .field-help {
          display: block;
          margin-top: 6px;
          color: #98a2b3;
          font-size: 11px;
          line-height: 1.6;
          text-align: justify;
        }

        /* =========================
           IMAGE UPLOAD
        ========================= */

        .upload-area {
          border: 1px dashed #b7dfc9;
          border-radius: 12px;
          padding: 18px;
          background: #fbfefc;
        }

        .upload-input {
          width: 100%;
          padding: 11px;
          border: 1px solid #d0d5dd;
          border-radius: 9px;
          background: #ffffff;
          color: #475467;
          font-size: 12px;
        }

        .upload-input:focus {
          border-color: #198754;
          outline: none;
          box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
        }

        .upload-info {
          margin-top: 8px;
          color: #98a2b3;
          font-size: 11px;
          line-height: 1.6;
          text-align: justify;
        }

        /* =========================
           PREVIEW
        ========================= */

        .preview-wrapper {
          margin-top: 20px;
        }

        .preview-header {
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 10px;
          margin-bottom: 12px;
        }

        .preview-title {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
        }

        .preview-count {
          padding: 5px 9px;
          border-radius: 7px;
          background: #e9f7ef;
          color: #198754;
          font-size: 10px;
          font-weight: 700;
        }

        .preview-grid {
          display: grid;
          grid-template-columns: repeat(3, minmax(0, 1fr));
          gap: 14px;
        }

        .preview-card {
          position: relative;
          overflow: hidden;
          border: 1px solid #eaecf0;
          border-radius: 12px;
          background: #ffffff;
          box-shadow: 0 2px 7px rgba(16, 24, 40, 0.035);
        }

        .preview-image-wrapper {
          position: relative;
          height: 180px;
          background: #f2f4f7;
        }

        .preview-image {
          width: 100%;
          height: 100%;
          object-fit: cover;
          display: block;
        }

        .main-image-badge {
          position: absolute;
          top: 9px;
          left: 9px;
          display: inline-flex;
          align-items: center;
          gap: 5px;
          padding: 5px 8px;
          border-radius: 6px;
          background: #198754;
          color: #ffffff;
          font-size: 9px;
          font-weight: 700;
          box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12);
        }

        .remove-image-button {
          position: absolute;
          top: 8px;
          right: 8px;
          width: 31px;
          height: 31px;
          border: 0;
          border-radius: 8px;
          background: rgba(220, 53, 69, 0.94);
          color: #ffffff;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
          transition: all 0.2s ease;
        }

        .remove-image-button:hover {
          background: #dc3545;
          transform: translateY(-1px);
        }

        .remove-image-button:disabled {
          opacity: 0.55;
          cursor: not-allowed;
        }

        .preview-card-body {
          padding: 10px;
        }

        .preview-file-name {
          color: #667085;
          font-size: 10px;
          line-height: 1.5;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }

        /* =========================
           FORM FOOTER
        ========================= */

        .form-footer {
          margin-top: 28px;
          padding-top: 20px;
          border-top: 1px solid #eaecf0;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 20px;
        }

        .form-footer-info {
          display: flex;
          align-items: center;
          gap: 8px;
          color: #98a2b3;
          font-size: 11px;
          line-height: 1.5;
        }

        .form-footer-info i {
          color: #198754;
          font-size: 14px;
        }

        .form-actions {
          display: flex;
          align-items: center;
          justify-content: flex-end;
          gap: 8px;
        }

        .cancel-button,
        .save-button {
          min-height: 42px;
          padding: 9px 15px;
          border-radius: 9px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 7px;
          text-decoration: none;
          font-size: 12px;
          font-weight: 700;
          transition: all 0.2s ease;
          cursor: pointer;
        }

        .cancel-button {
          color: #667085;
          border: 1px solid #d0d5dd;
          background: #ffffff;
        }

        .cancel-button:hover {
          color: #344054;
          background: #f8fafc;
          border-color: #98a2b3;
        }

        .save-button {
          color: #ffffff;
          background: #198754;
          border: 1px solid #198754;
          box-shadow: 0 6px 16px rgba(25, 135, 84, 0.14);
        }

        .save-button:hover {
          color: #ffffff;
          background: #157347;
          border-color: #157347;
          transform: translateY(-1px);
          box-shadow: 0 9px 20px rgba(25, 135, 84, 0.18);
        }

        .save-button:disabled {
          opacity: 0.7;
          cursor: not-allowed;
          transform: none;
        }

        /* =========================
           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) {
          .preview-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;
          }

          .form-card-body {
            padding: 22px;
          }

          .form-footer {
            align-items: flex-start;
            flex-direction: column;
          }

          .form-actions {
            width: 100%;
            justify-content: flex-end;
          }
        }

        /* =========================
           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;
          }

          .form-card-title {
            font-size: 15px;
          }

          .form-card-description {
            font-size: 11px;
          }

          .form-card-body {
            padding: 17px;
          }

          .section-heading {
            margin-bottom: 14px;
          }

          .form-control-custom {
            min-height: 42px;
            font-size: 12px;
          }

          .input-group-icon {
            min-width: 42px;
            height: 42px;
          }

          .upload-area {
            padding: 13px;
          }

          .preview-grid {
            grid-template-columns: 1fr;
          }

          .preview-image-wrapper {
            height: 210px;
          }

          .form-actions {
            flex-direction: column-reverse;
            width: 100%;
          }

          .cancel-button,
          .save-button {
            width: 100%;
          }

          .admin-page-footer {
            flex-direction: column;
            align-items: flex-start;
          }
        }
      `}</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>

              {/* ACTIVE ACHIEVEMENT */}

              <Link
                href="/secure-panel/achievements"
                className="sidebar-link active"
                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"
                onClick={() =>
                  setSidebarOpen(false)
                }
              >
                <span className="sidebar-link-icon">
                  <i className="bi bi-images"></i>
                </span>

                <span>
                  Gallery
                </span>
              </Link>

              <Link
                href="/secure-panel/contact"
                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>

            {/* SIDEBAR BOTTOM */}

            <div className="sidebar-bottom">
              <Link
                href="/"
                className="sidebar-website"
              >
                <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">
                  Achievement 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/achievements"
                  className="back-dashboard"
                >
                  <i className="bi bi-arrow-left"></i>

                  <span>
                    Kembali ke Achievement
                  </span>
                </Link>

                <div className="page-title-row">

                  <div className="page-title-icon">
                    <i className="bi bi-trophy-fill"></i>
                  </div>

                  <div>

                    <div className="page-eyebrow">
                      ACHIEVEMENT MANAGEMENT
                    </div>

                    <h1 className="page-title">
                      Tambah Achievement
                    </h1>

                    <p className="page-description">
                      Tambahkan pencapaian, prestasi,
                      penghargaan, atau achievement
                      baru ke dalam website portfolio
                      GloVaTech.
                    </p>

                  </div>

                </div>

              </div>

            </section>

            {/* FORM CARD */}

            <section className="form-card">

              {/* CARD HEADER */}

              <div className="form-card-header">

                <div>

                  <div className="form-card-eyebrow">
                    NEW ACHIEVEMENT
                  </div>

                  <h2 className="form-card-title">
                    Informasi Achievement
                  </h2>

                  <p className="form-card-description">
                    Isi informasi pencapaian dengan
                    lengkap. Gambar pertama yang
                    dipilih akan digunakan sebagai
                    gambar utama achievement.
                  </p>

                </div>

                <div className="form-card-icon">
                  <i className="bi bi-award"></i>
                </div>

              </div>

              {/* FORM BODY */}

              <div className="form-card-body">

                <form
                  onSubmit={handleSubmit}
                >

                  {/* ======================
                      BASIC INFORMATION
                  ====================== */}

                  <div className="form-section">

                    <div className="section-heading">

                      <div className="section-heading-icon">
                        <i className="bi bi-info-circle"></i>
                      </div>

                      <div className="section-heading-text">

                        <h3>
                          Informasi Dasar
                        </h3>

                        <p>
                          Informasi utama mengenai achievement.
                        </p>

                      </div>

                    </div>

                    <div className="row g-4">

                      {/* TITLE */}

                      <div className="col-12 col-lg-8">

                        <label className="form-label-custom">
                          Judul Achievement
                          <span className="required-mark">
                            *
                          </span>
                        </label>

                        <input
                          type="text"
                          name="title"
                          className="form-control-custom"
                          placeholder="Contoh: Juara 1 Lomba Web Development"
                          value={form.title}
                          onChange={handleChange}
                          required
                        />

                        <small className="field-help">
                          Gunakan judul yang singkat,
                          jelas, dan mudah dipahami.
                        </small>

                      </div>

                      {/* ISSUER */}

                      <div className="col-12 col-lg-4">

                        <label className="form-label-custom">
                          Penerbit / Penyelenggara
                        </label>

                        <input
                          type="text"
                          name="issuer"
                          className="form-control-custom"
                          placeholder="Contoh: Universitas Putra Indonesia"
                          value={form.issuer}
                          onChange={handleChange}
                        />

                        <small className="field-help">
                          Nama organisasi atau pihak
                          yang memberikan penghargaan.
                        </small>

                      </div>

                      {/* DATE */}

                      <div className="col-12 col-md-6">

                        <label className="form-label-custom">
                          Tanggal Achievement
                        </label>

                        <input
                          type="date"
                          name="date"
                          className="form-control-custom"
                          value={form.date}
                          onChange={handleChange}
                        />

                        <small className="field-help">
                          Tanggal ketika achievement
                          diperoleh.
                        </small>

                      </div>

                      {/* URL */}

                      <div className="col-12 col-md-6">

                        <label className="form-label-custom">
                          URL Achievement
                        </label>

                        <div className="input-group-custom">

                          <div className="input-group-icon">
                            <i className="bi bi-link-45deg"></i>
                          </div>

                          <input
                            type="url"
                            name="url"
                            className="form-control-custom"
                            placeholder="https://..."
                            value={form.url}
                            onChange={handleChange}
                          />

                        </div>

                        <small className="field-help">
                          Link sertifikat, dokumentasi,
                          atau halaman terkait achievement.
                        </small>

                      </div>

                    </div>

                  </div>

                  {/* ======================
                      IMAGES
                  ====================== */}

                  <div className="form-section">

                    <div className="section-heading">

                      <div className="section-heading-icon">
                        <i className="bi bi-images"></i>
                      </div>

                      <div className="section-heading-text">

                        <h3>
                          Gambar Achievement
                        </h3>

                        <p>
                          Upload satu atau beberapa
                          gambar sebagai dokumentasi.
                        </p>

                      </div>

                    </div>

                    <div className="upload-area">

                      <label className="form-label-custom">
                        Pilih Gambar
                        <span className="required-mark">
                          *
                        </span>
                      </label>

                      <input
                        type="file"
                        accept="image/jpeg,image/jpg,image/png,image/webp,image/gif"
                        className="upload-input"
                        multiple
                        onChange={
                          handleImageChange
                        }
                      />

                      <div className="upload-info">
                        <i className="bi bi-info-circle me-1"></i>
                        Format yang didukung:
                        JPG, JPEG, PNG, WebP,
                        dan GIF. Maksimal 5 MB
                        per gambar. Anda dapat
                        memilih beberapa gambar
                        sekaligus.
                      </div>

                      {/* PREVIEW */}

                      {images.length > 0 && (

                        <div className="preview-wrapper">

                          <div className="preview-header">

                            <div className="preview-title">
                              Preview Gambar
                            </div>

                            <div className="preview-count">
                              {images.length} gambar
                            </div>

                          </div>

                          <div className="preview-grid">

                            {images.map(
                              (
                                item,
                                index
                              ) => (

                                <div
                                  key={item.id}
                                  className="preview-card"
                                >

                                  <div className="preview-image-wrapper">

                                    <img
                                      src={
                                        item.preview
                                      }
                                      alt={`Preview ${
                                        index + 1
                                      }`}
                                      className="preview-image"
                                    />

                                    {index === 0 && (

                                      <span className="main-image-badge">
                                        <i className="bi bi-star-fill"></i>
                                        Gambar Utama
                                      </span>

                                    )}

                                    <button
                                      type="button"
                                      className="remove-image-button"
                                      onClick={() =>
                                        removeImage(
                                          item.id
                                        )
                                      }
                                      disabled={
                                        loading
                                      }
                                      title="Hapus gambar"
                                      aria-label="Hapus gambar"
                                    >
                                      <i className="bi bi-trash"></i>
                                    </button>

                                  </div>

                                  <div className="preview-card-body">

                                    <div className="preview-file-name">
                                      {item.file.name}
                                    </div>

                                  </div>

                                </div>

                              )
                            )}

                          </div>

                        </div>

                      )}

                    </div>

                  </div>

                  {/* ======================
                      DESCRIPTION
                  ====================== */}

                  <div className="form-section">

                    <div className="section-heading">

                      <div className="section-heading-icon">
                        <i className="bi bi-card-text"></i>
                      </div>

                      <div className="section-heading-text">

                        <h3>
                          Deskripsi
                        </h3>

                        <p>
                          Jelaskan achievement secara
                          lebih detail.
                        </p>

                      </div>

                    </div>

                    <label className="form-label-custom">
                      Deskripsi Achievement
                    </label>

                    <textarea
                      name="description"
                      className="form-control-custom"
                      rows={6}
                      placeholder="Jelaskan achievement, proses, pencapaian, atau informasi lain yang relevan..."
                      value={
                        form.description
                      }
                      onChange={
                        handleChange
                      }
                    />

                    <small className="field-help">
                      Gunakan deskripsi yang informatif
                      dan mudah dibaca. Teks akan
                      ditampilkan secara justify pada
                      halaman administrasi.
                    </small>

                  </div>

                  {/* ======================
                      FOOTER
                  ====================== */}

                  <div className="form-footer">

                    <div className="form-footer-info">

                      <i className="bi bi-shield-check"></i>

                      <span>
                        Data akan disimpan ke
                        database Achievement.
                      </span>

                    </div>

                    <div className="form-actions">

                      <Link
                        href="/secure-panel/achievements"
                        className="cancel-button"
                      >
                        <i className="bi bi-x-lg"></i>
                        <span>
                          Batal
                        </span>
                      </Link>

                      <button
                        type="submit"
                        className="save-button"
                        disabled={loading}
                      >

                        {loading ? (
                          <>
                            <span
                              className="spinner-border spinner-border-sm"
                              role="status"
                              aria-hidden="true"
                            ></span>

                            <span>
                              Menyimpan...
                            </span>
                          </>
                        ) : (
                          <>
                            <i className="bi bi-check-lg"></i>

                            <span>
                              Simpan Achievement
                            </span>
                          </>
                        )}

                      </button>

                    </div>

                  </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/achievements">

                <i className="bi bi-arrow-left me-1"></i>

                Achievement

              </Link>

            </footer>

          </main>

        </div>

      </div>
    </>
  );
}

