"use client";

import {
  FormEvent,
  useEffect,
  useState,
} from "react";
import Link from "next/link";
import { useParams, useRouter } from "next/navigation";

export default function EditServicePage() {
  const params = useParams();
  const router = useRouter();

  const id = params.id as string;

  const [title, setTitle] = useState("");
  const [slug, setSlug] = useState("");
  const [description, setDescription] = useState("");
  const [icon, setIcon] = useState("");
  const [image, setImage] = useState("");
  const [featured, setFeatured] = useState(false);
  const [active, setActive] = useState(true);

  const [loading, setLoading] = useState(true);
  const [saving, setSaving] = useState(false);
  const [sidebarOpen, setSidebarOpen] = useState(false);

  const generateSlug = (value: string) => {
    return value
      .toLowerCase()
      .trim()
      .replace(/[^a-z0-9\s-]/g, "")
      .replace(/\s+/g, "-")
      .replace(/-+/g, "-");
  };

  const getService = async () => {
    try {
      setLoading(true);

      const response = await fetch(
        `/api/services/${id}`,
        {
          cache: "no-store",
        }
      );

      const result = await response.json();

      if (result.success) {
        const service = result.data;

        setTitle(service.title || "");
        setSlug(service.slug || "");
        setDescription(service.description || "");
        setIcon(service.icon || "");
        setImage(service.image || "");
        setFeatured(Boolean(service.featured));
        setActive(
          service.active !== undefined
            ? Boolean(service.active)
            : true
        );
      } else {
        alert(
          result.message ||
            "Gagal mengambil data Service."
        );

        router.push("/secure-panel/services");
      }
    } catch (error) {
      console.error(
        "GET SERVICE:",
        error
      );

      alert(
        "Terjadi kesalahan saat mengambil data Service."
      );

      router.push("/secure-panel/services");
    } finally {
      setLoading(false);
    }
  };

  useEffect(() => {
    if (id) {
      getService();
    }
  }, [id]);

  const handleTitleChange = (
    value: string
  ) => {
    setTitle(value);
    setSlug(generateSlug(value));
  };

  const handleSubmit = async (
    event: FormEvent<HTMLFormElement>
  ) => {
    event.preventDefault();

    if (!title.trim()) {
      alert("Title wajib diisi.");
      return;
    }

    if (!slug.trim()) {
      alert("Slug wajib diisi.");
      return;
    }

    setSaving(true);

    try {
      const response = await fetch(
        `/api/services/${id}`,
        {
          method: "PUT",
          headers: {
            "Content-Type":
              "application/json",
          },
          body: JSON.stringify({
            title,
            slug,
            description:
              description || null,
            icon: icon || null,
            image: image || null,
            featured,
            active,
          }),
        }
      );

      const result =
        await response.json();

      if (result.success) {
        alert(
          "Service berhasil diperbarui."
        );

        router.push(
          "/secure-panel/services"
        );

        router.refresh();
      } else {
        alert(
          result.message ||
            "Gagal memperbarui Service."
        );
      }
    } catch (error) {
      console.error(
        "UPDATE SERVICE:",
        error
      );

      alert(
        "Terjadi kesalahan saat memperbarui Service."
      );
    } finally {
      setSaving(false);
    }
  };

  if (loading) {
    return (
      <>
        <style>{`
          * {
            box-sizing: border-box;
          }

          .loading-page {
            min-height: 100vh;
            background: #f5f7f9;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 30px;
          }

          .loading-box {
            text-align: center;
          }

          .loading-spinner {
            width: 44px;
            height: 44px;
            border: 3px solid #dcefe4;
            border-top-color: #198754;
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
            margin: 0 auto 16px;
          }

          .loading-title {
            font-size: 15px;
            font-weight: 700;
            color: #344054;
            margin-bottom: 5px;
          }

          .loading-text {
            margin: 0;
            color: #98a2b3;
            font-size: 12px;
          }

          @keyframes spin {
            to {
              transform: rotate(360deg);
            }
          }
        `}</style>

        <div className="loading-page">
          <div className="loading-box">
            <div className="loading-spinner"></div>

            <div className="loading-title">
              Memuat data Service
            </div>

            <p className="loading-text">
              Sedang mengambil informasi dari database...
            </p>
          </div>
        </div>
      </>
    );
  }

  return (
    <>
      <style>{`
        * {
          box-sizing: border-box;
        }

        /* =========================
           LAYOUT
        ========================= */

        .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: 700px;
          text-align: justify;
        }

        /* =========================
           INFO CARD
        ========================= */

        .info-strip {
          background: #ffffff;
          border: 1px solid #eaecf0;
          border-radius: 14px;
          padding: 16px 18px;
          display: flex;
          align-items: flex-start;
          gap: 13px;
          margin-bottom: 20px;
          box-shadow: 0 2px 7px rgba(16, 24, 40, 0.025);
        }

        .info-strip-icon {
          width: 38px;
          height: 38px;
          flex-shrink: 0;
          border-radius: 10px;
          display: flex;
          align-items: center;
          justify-content: center;
          background: #eef4ff;
          color: #3b82f6;
          font-size: 16px;
        }

        .info-strip-content {
          min-width: 0;
        }

        .info-strip-title {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 3px;
        }

        .info-strip-text {
          margin: 0;
          color: #667085;
          font-size: 11px;
          line-height: 1.65;
          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;
        }

        .form-id {
          min-width: 70px;
          height: 38px;
          padding: 0 11px;
          border-radius: 10px;
          background: #f5f7f9;
          color: #667085;
          display: flex;
          align-items: center;
          justify-content: center;
          gap: 6px;
          font-size: 11px;
          font-weight: 700;
          flex-shrink: 0;
        }

        .form-card-body {
          padding: 24px;
        }

        .form-grid {
          display: grid;
          grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
          gap: 22px;
        }

        .form-group {
          min-width: 0;
        }

        .form-group.full {
          grid-column: 1 / -1;
        }

        .form-label-custom {
          display: block;
          margin-bottom: 7px;
          color: #344054;
          font-size: 12px;
          font-weight: 700;
        }

        .required {
          color: #dc3545;
          margin-left: 2px;
        }

        .form-control-custom {
          width: 100%;
          min-height: 44px;
          padding: 10px 13px;
          border: 1px solid #d0d5dd;
          border-radius: 9px;
          background: #ffffff;
          color: #101828;
          font-size: 13px;
          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:hover {
          border-color: #b7c1cd;
        }

        .form-control-custom:focus {
          border-color: #198754;
          box-shadow: 0 0 0 3px rgba(25, 135, 84, 0.1);
        }

        textarea.form-control-custom {
          min-height: 135px;
          resize: vertical;
          line-height: 1.7;
          text-align: justify;
        }

        .form-help {
          margin-top: 6px;
          color: #98a2b3;
          font-size: 10px;
          line-height: 1.6;
        }

        .form-help code {
          color: #198754;
          background: #f0f9f4;
          padding: 2px 5px;
          border-radius: 4px;
          font-size: 10px;
        }

        /* =========================
           ICON PREVIEW
        ========================= */

        .icon-preview-box {
          min-height: 90px;
          margin-top: 10px;
          border: 1px dashed #d0d5dd;
          border-radius: 10px;
          background: #fafbfc;
          display: flex;
          align-items: center;
          gap: 13px;
          padding: 13px;
        }

        .icon-preview {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          background: #e9f7ef;
          color: #198754;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 21px;
          flex-shrink: 0;
        }

        .icon-preview-title {
          color: #344054;
          font-size: 11px;
          font-weight: 700;
          margin-bottom: 3px;
        }

        .icon-preview-text {
          color: #98a2b3;
          font-size: 10px;
          line-height: 1.5;
          word-break: break-word;
        }

        /* =========================
           IMAGE PREVIEW
        ========================= */

        .image-preview {
          margin-top: 10px;
          border: 1px solid #eaecf0;
          border-radius: 11px;
          overflow: hidden;
          background: #f8fafc;
        }

        .image-preview img {
          display: block;
          width: 100%;
          height: 210px;
          object-fit: cover;
        }

        .image-preview-caption {
          padding: 9px 11px;
          color: #98a2b3;
          font-size: 10px;
          line-height: 1.5;
          word-break: break-all;
        }

        /* =========================
           OPTIONS
        ========================= */

        .options-section {
          margin-top: 22px;
          padding-top: 22px;
          border-top: 1px solid #eaecf0;
        }

        .options-title {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 12px;
        }

        .options-grid {
          display: grid;
          grid-template-columns: repeat(2, minmax(0, 1fr));
          gap: 12px;
        }

        .option-card {
          position: relative;
          display: flex;
          align-items: flex-start;
          gap: 11px;
          padding: 14px;
          border: 1px solid #eaecf0;
          border-radius: 11px;
          background: #ffffff;
          transition: all 0.2s ease;
          cursor: pointer;
        }

        .option-card:hover {
          border-color: #cfe8d9;
          background: #fcfffd;
        }

        .option-card.checked {
          border-color: #b7dfc9;
          background: #f5fbf7;
        }

        .option-card input {
          width: 17px;
          height: 17px;
          margin-top: 2px;
          accent-color: #198754;
          flex-shrink: 0;
          cursor: pointer;
        }

        .option-content {
          min-width: 0;
        }

        .option-label {
          color: #344054;
          font-size: 12px;
          font-weight: 700;
          margin-bottom: 3px;
        }

        .option-description {
          color: #98a2b3;
          font-size: 10px;
          line-height: 1.55;
          text-align: justify;
        }

        /* =========================
           BUTTONS
        ========================= */

        .form-actions {
          margin-top: 24px;
          padding-top: 20px;
          border-top: 1px solid #eaecf0;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 15px;
        }

        .form-actions-left,
        .form-actions-right {
          display: flex;
          align-items: center;
          gap: 8px;
        }

        .action-button {
          min-height: 42px;
          padding: 9px 15px;
          border-radius: 9px;
          display: inline-flex;
          align-items: center;
          justify-content: center;
          gap: 8px;
          font-size: 12px;
          font-weight: 600;
          cursor: pointer;
          text-decoration: none;
          transition: all 0.2s ease;
          white-space: nowrap;
        }

        .save-button {
          border: 1px solid #198754;
          background: #198754;
          color: #ffffff;
          box-shadow: 0 6px 16px rgba(25, 135, 84, 0.14);
        }

        .save-button:hover:not(:disabled) {
          background: #157347;
          border-color: #157347;
          color: #ffffff;
          transform: translateY(-1px);
          box-shadow: 0 9px 20px rgba(25, 135, 84, 0.18);
        }

        .cancel-button {
          border: 1px solid #d0d5dd;
          background: #ffffff;
          color: #475467;
        }

        .cancel-button:hover:not(:disabled) {
          background: #f8fafc;
          border-color: #98a2b3;
          color: #344054;
        }

        .action-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;
          }

          .form-grid {
            grid-template-columns: 1fr;
          }

          .form-group.full {
            grid-column: auto;
          }
        }

        /* =========================
           MOBILE
        ========================= */

        @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: 20px;
          }

          .form-actions {
            align-items: stretch;
            flex-direction: column-reverse;
          }

          .form-actions-left,
          .form-actions-right {
            width: 100%;
          }

          .form-actions .action-button {
            flex: 1;
          }
        }

        /* =========================
           SMALL MOBILE
        ========================= */

        @media (max-width: 575.98px) {
          .admin-topbar {
            height: 68px;
            padding: 0 14px;
          }

          .topbar-right {
            gap: 6px;
          }

          .topbar-website,
          .topbar-logout {
            width: 38px;
            height: 38px;
            min-height: 38px;
            padding: 0;
          }

          .topbar-website span,
          .topbar-logout span {
            display: none !important;
          }

          .admin-content {
            padding: 20px 14px;
          }

          .page-title-row {
            align-items: flex-start;
          }

          .page-title-icon {
            width: 43px;
            height: 43px;
            border-radius: 11px;
            font-size: 18px;
          }

          .page-title {
            font-size: 25px;
          }

          .page-description {
            font-size: 12px;
          }

          .info-strip {
            padding: 13px;
          }

          .info-strip-icon {
            width: 34px;
            height: 34px;
            font-size: 14px;
          }

          .form-card-header {
            padding: 17px;
          }

          .form-card-title {
            font-size: 15px;
          }

          .form-card-description {
            font-size: 11px;
          }

          .form-id {
            min-width: 50px;
            height: 34px;
            font-size: 10px;
          }

          .form-card-body {
            padding: 17px;
          }

          .form-grid {
            gap: 17px;
          }

          .form-label-custom {
            font-size: 11px;
          }

          .form-control-custom {
            min-height: 42px;
            font-size: 12px;
          }

          textarea.form-control-custom {
            min-height: 125px;
            font-size: 12px;
          }

          .options-grid {
            grid-template-columns: 1fr;
          }

          .image-preview img {
            height: 180px;
          }

          .form-actions {
            margin-top: 20px;
            padding-top: 17px;
          }

          .form-actions-left,
          .form-actions-right {
            width: 100%;
          }

          .form-actions .action-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>

              <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>

              {/* ACTIVE SERVICES */}

              <Link
                href="/secure-panel/services"
                className="sidebar-link active"
                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-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>

            {/* 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">
                  Service 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/services"
                  className="back-dashboard"
                >
                  <i className="bi bi-arrow-left"></i>
                  <span>
                    Kembali ke Services
                  </span>
                </Link>

                <div className="page-title-row">

                  <div className="page-title-icon">
                    <i className="bi bi-pencil-square"></i>
                  </div>

                  <div>

                    <div className="page-eyebrow">
                      SERVICE MANAGEMENT
                    </div>

                    <h1 className="page-title">
                      Edit Service
                    </h1>

                    <p className="page-description">
                      Perbarui informasi layanan yang
                      ditampilkan pada website portfolio
                      GloVaTech. Pastikan informasi yang
                      diberikan sudah sesuai sebelum
                      menyimpan perubahan.
                    </p>

                  </div>

                </div>

              </div>

            </section>

            {/* INFO */}

            <div className="info-strip">

              <div className="info-strip-icon">
                <i className="bi bi-info-circle"></i>
              </div>

              <div className="info-strip-content">

                <div className="info-strip-title">
                  Mengedit Service #{id}
                </div>

                <p className="info-strip-text">
                  Perubahan yang disimpan akan
                  memperbarui data Service pada
                  database melalui API yang sudah
                  tersedia. Struktur backend dan
                  endpoint tidak diubah.
                </p>

              </div>

            </div>

            {/* FORM CARD */}

            <section className="form-card">

              <div className="form-card-header">

                <div>

                  <div className="form-card-eyebrow">
                    SERVICE INFORMATION
                  </div>

                  <h2 className="form-card-title">
                    Informasi Service
                  </h2>

                  <p className="form-card-description">
                    Kelola nama, URL slug, deskripsi,
                    icon, gambar, serta status service.
                  </p>

                </div>

                <div className="form-id">
                  <i className="bi bi-hash"></i>
                  {id}
                </div>

              </div>

              <form
                onSubmit={handleSubmit}
              >

                <div className="form-card-body">

                  <div className="form-grid">

                    {/* TITLE */}

                    <div className="form-group">

                      <label
                        htmlFor="title"
                        className="form-label-custom"
                      >
                        Title
                        <span className="required">
                          *
                        </span>
                      </label>

                      <input
                        id="title"
                        type="text"
                        className="form-control-custom"
                        value={title}
                        onChange={(event) =>
                          handleTitleChange(
                            event.target.value
                          )
                        }
                        placeholder="Contoh: Web Development"
                      />

                      <div className="form-help">
                        Nama layanan yang akan
                        ditampilkan kepada pengunjung.
                      </div>

                    </div>

                    {/* SLUG */}

                    <div className="form-group">

                      <label
                        htmlFor="slug"
                        className="form-label-custom"
                      >
                        Slug
                        <span className="required">
                          *
                        </span>
                      </label>

                      <input
                        id="slug"
                        type="text"
                        className="form-control-custom"
                        value={slug}
                        onChange={(event) =>
                          setSlug(
                            generateSlug(
                              event.target.value
                            )
                          )
                        }
                        placeholder="web-development"
                      />

                      <div className="form-help">
                        Slug dibuat otomatis dari
                        Title dan digunakan sebagai
                        identitas URL.
                      </div>

                    </div>

                    {/* DESCRIPTION */}

                    <div className="form-group full">

                      <label
                        htmlFor="description"
                        className="form-label-custom"
                      >
                        Description
                      </label>

                      <textarea
                        id="description"
                        className="form-control-custom"
                        rows={5}
                        value={description}
                        onChange={(event) =>
                          setDescription(
                            event.target.value
                          )
                        }
                        placeholder="Jelaskan layanan yang ditawarkan..."
                      />

                      <div className="form-help">
                        Gunakan deskripsi yang singkat,
                        informatif, dan mudah dipahami.
                        Teks pada area ini menggunakan
                        alignment justify.
                      </div>

                    </div>

                    {/* ICON */}

                    <div className="form-group">

                      <label
                        htmlFor="icon"
                        className="form-label-custom"
                      >
                        Icon
                      </label>

                      <input
                        id="icon"
                        type="text"
                        className="form-control-custom"
                        value={icon}
                        onChange={(event) =>
                          setIcon(
                            event.target.value
                          )
                        }
                        placeholder="bi bi-code-slash"
                      />

                      <div className="form-help">
                        Gunakan class Bootstrap Icons,
                        contoh:{" "}
                        <code>
                          bi bi-code-slash
                        </code>
                      </div>

                      <div className="icon-preview-box">

                        <div className="icon-preview">
                          <i
                            className={
                              icon ||
                              "bi bi-gear-wide-connected"
                            }
                          ></i>
                        </div>

                        <div>

                          <div className="icon-preview-title">
                            Icon Preview
                          </div>

                          <div className="icon-preview-text">
                            {icon ||
                              "Belum ada icon. Masukkan class Bootstrap Icons."}
                          </div>

                        </div>

                      </div>

                    </div>

                    {/* IMAGE */}

                    <div className="form-group">

                      <label
                        htmlFor="image"
                        className="form-label-custom"
                      >
                        Image URL
                      </label>

                      <input
                        id="image"
                        type="text"
                        className="form-control-custom"
                        value={image}
                        onChange={(event) =>
                          setImage(
                            event.target.value
                          )
                        }
                        placeholder="https://..."
                      />

                      <div className="form-help">
                        Masukkan URL gambar yang
                        digunakan untuk Service.
                      </div>

                      {image && (
                        <div className="image-preview">

                          <img
                            src={image}
                            alt={
                              title ||
                              "Service preview"
                            }
                            onError={(event) => {
                              event.currentTarget.style.display =
                                "none";
                            }}
                          />

                          <div className="image-preview-caption">
                            Preview gambar Service
                          </div>

                        </div>
                      )}

                    </div>

                  </div>

                  {/* OPTIONS */}

                  <div className="options-section">

                    <div className="options-title">
                      Service Settings
                    </div>

                    <div className="options-grid">

                      {/* FEATURED */}

                      <label
                        htmlFor="featured"
                        className={`option-card ${
                          featured
                            ? "checked"
                            : ""
                        }`}
                      >

                        <input
                          id="featured"
                          type="checkbox"
                          checked={featured}
                          onChange={(event) =>
                            setFeatured(
                              event.target.checked
                            )
                          }
                        />

                        <div className="option-content">

                          <div className="option-label">
                            Jadikan Featured
                          </div>

                          <div className="option-description">
                            Tandai Service sebagai
                            layanan unggulan yang
                            mendapatkan perhatian
                            lebih pada website.
                          </div>

                        </div>

                      </label>

                      {/* ACTIVE */}

                      <label
                        htmlFor="active"
                        className={`option-card ${
                          active
                            ? "checked"
                            : ""
                        }`}
                      >

                        <input
                          id="active"
                          type="checkbox"
                          checked={active}
                          onChange={(event) =>
                            setActive(
                              event.target.checked
                            )
                          }
                        />

                        <div className="option-content">

                          <div className="option-label">
                            Aktifkan Service
                          </div>

                          <div className="option-description">
                            Service aktif dapat
                            ditampilkan pada website,
                            sedangkan Service nonaktif
                            dapat disembunyikan.
                          </div>

                        </div>

                      </label>

                    </div>

                  </div>

                  {/* ACTIONS */}

                  <div className="form-actions">

                    <div className="form-actions-left">

                      <Link
                        href="/secure-panel/services"
                        className="action-button cancel-button"
                      >
                        <i className="bi bi-arrow-left"></i>
                        <span>
                          Kembali
                        </span>
                      </Link>

                    </div>

                    <div className="form-actions-right">

                      <button
                        type="button"
                        className="action-button cancel-button"
                        onClick={() =>
                          router.push(
                            "/secure-panel/services"
                          )
                        }
                        disabled={saving}
                      >
                        <i className="bi bi-x-lg"></i>
                        <span>
                          Batal
                        </span>
                      </button>

                      <button
                        type="submit"
                        className="action-button save-button"
                        disabled={saving}
                      >

                        {saving ? (
                          <>
                            <span
                              className="spinner-border spinner-border-sm"
                              aria-hidden="true"
                            ></span>

                            <span>
                              Menyimpan...
                            </span>
                          </>
                        ) : (
                          <>
                            <i className="bi bi-check-lg"></i>

                            <span>
                              Simpan Perubahan
                            </span>
                          </>
                        )}

                      </button>

                    </div>

                  </div>

                </div>

              </form>

            </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/services">
                <i className="bi bi-gear-wide-connected me-1"></i>
                Services
              </Link>

            </footer>

          </main>

        </div>

      </div>
    </>
  );
}

