"use client";


import { useState } from "react";
import { submitContactForm } from "@/api/contactApi";
import Image from "next/image";

export default function ContactPage() {
  const [activeIndex, setActiveIndex] = useState<number | null>(2);

  const [loading, setLoading] = useState(false);

const [formData, setFormData] = useState({
  contact_name: "",
  phone_number: "",
  email_id: "",
  country: "",
  state: "",
  city: "",
  contacting_for: "",
  message: "",
});

const handleSubmit = async (
  e: React.FormEvent<HTMLFormElement>
) => {
  e.preventDefault();

  try {
    setLoading(true);

    await submitContactForm(formData);

    alert("Contact request submitted successfully!");

    setFormData({
      contact_name: "",
      phone_number: "",
      email_id: "",
      country: "",
      state: "",
      city: "",
      contacting_for: "",
      message: "",
    });
  } catch (error: any) {
    console.error(error);

    alert(
      error?.response?.data?.message ||
        "Failed to submit contact request"
    );
  } finally {
    setLoading(false);
  }
};

const handleChange = (
  e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
) => {
  const { name, value } = e.target;

  setFormData((prev) => ({
    ...prev,
    [name]: value,
  }));
};


  const faqs = [
    { question: "What is Ridoo?", answer: "Ridoo is a vehicle rental platform..." },
    { question: "Who can rent a vehicle from Ridoo?", answer: "Anyone with valid license..." },
    {
      question: "Can we put our car or bike on rent on Ridoo?",
      answer:
        "Yes, you can list your vehicle and earn passive income by renting it to verified users.",
    },
    { question: "How can I trust any customer?", answer: "All customers are verified..." },
    { question: "How can I trust any partner?", answer: "All partners go through KYC..." },
    { question: "What kind of support do I get from Ridoo?", answer: "24/7 support..." },
    { question: "Will I get refund in case of cancellation?", answer: "Refund policy applies..." },
  ];

  return (
    <div className="bg-white min-h-screen px-4 md:px-16 py-10">

      {/* ================= HEADER ================= */}
      <h1 className="text-3xl md:text-5xl font-bold">
        Need Help? We've Got{" "}
        <span className="text-primary relative inline-block">
          Your Back
          <span className="absolute left-0 -bottom-2 w-full h-1 bg-primary rounded-full"></span>
        </span>
      </h1>

      <p className="text-gray-500 mt-2">(FAQs & Contact Us)</p>

      {/* ================= FAQ ================= */}
<div className="mt-6 flex justify-center">
  <div className="w-full max-w-5xl space-y-3">

    {faqs.map((faq, index) => {
      const isActive = activeIndex === index;

      return (
        <div
          key={index}
          className={`rounded-lg px-4 py-3 cursor-pointer transition-all duration-200
          ${isActive ? "bg-[#fff4dc]" : "bg-white hover:bg-[#ececec]"}`}
          onClick={() => setActiveIndex(isActive ? null : index)}
        >
          {/* QUESTION */}
          <div className="flex justify-between items-center">
            <h3 className="text-sm md:text-[15px] font-medium text-gray-800">
              {faq.question}
            </h3>

            <span className="text-[#f4b400] text-xs">
              {isActive ? "▲" : "▼"}
            </span>
          </div>

          {/* ANSWER */}
          {isActive && (
            <p className="text-[13px] text-gray-600 mt-2 leading-relaxed">
              {faq.answer}
            </p>
          )}
        </div>
      );
    })}

  </div>
</div>

      {/* ================= CONTACT SECTION ================= */}
      <div className="mt-16 grid md:grid-cols-2 gap-10 items-center">

        {/* ===== LEFT FORM ===== */}
        <div>
          <p className="text-gray-500 text-sm">Still need help?</p>

          <h2 className="text-4xl font-bold mt-2">
            Get in{" "}
            <span className="text-primary relative inline-block">
              touch
              <span className="absolute left-0 -bottom-2 w-full h-1 bg-primary rounded-full"></span>
            </span>
          </h2>

          <form
  onSubmit={handleSubmit}
  className="mt-8 space-y-6"
>

            <input
  name="contact_name"
  value={formData.contact_name}
  onChange={handleChange}
  placeholder="Contact name"
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <div className="flex gap-4">
              <input
  name="phone_number"
  value={formData.phone_number}
  onChange={handleChange}
  placeholder="Phone number"
  className="w-1/2 border-b border-gray-300 bg-transparent py-2 outline-none"
/>
              <input
  type="email"
  name="email_id"
  value={formData.email_id}
  onChange={handleChange}
  placeholder="Email ID"
  className="w-1/2 border-b border-gray-300 bg-transparent py-2 outline-none"
/>
            </div>

           <input
  name="country"
  value={formData.country}
  onChange={handleChange}
  placeholder="Country"
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <input
  name="state"
  value={formData.state}
  onChange={handleChange}
  placeholder="State"
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <input
  name="city"
  value={formData.city}
  onChange={handleChange}
  placeholder="City"
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <input
  name="contacting_for"
  value={formData.contacting_for}
  onChange={handleChange}
  placeholder="Contacting for"
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <textarea
  name="message"
  value={formData.message}
  onChange={handleChange}
  placeholder="Tell us more here..."
  className="w-full border-b border-gray-300 bg-transparent py-2 outline-none"
/>

            <div className="flex items-center gap-2 text-sm">
              <input type="checkbox" />
              <span>I’m not robot</span>
            </div>

            <button
  type="submit"
  disabled={loading}
  className="bg-primary text-white px-8 py-3 rounded-md mt-4 w-full disabled:opacity-50"
>
  {loading ? "Submitting..." : "SUBMIT"}
</button>

            {/* FOOTER INFO */}
            <div className="text-sm text-gray-500 mt-6">
              <p>Timing: 10AM - 6PM IST (Mon-Sat)</p>
              <p className="mt-2">📞 8762680082</p>
              <p>✉️ support@ridoo.co.in</p>
            </div>
          </form>
        </div>

        {/* ===== RIGHT IMAGE ===== */}
        <div className="relative">
          <div className="absolute -top-4 -right-4 w-full h-full bg-primary rounded-2xl"></div>

          <Image
            alt="Contact Bike"
             width={180}
  height={54}
            src="/contact-bike.webp" // put your image in public folder
            className="relative w-full h-[500px] object-cover rounded-2xl shadow-lg"
          />
        </div>
      </div>
    </div>
  );
}