'use client';

import { useState, useEffect } from 'react';
import { contactConfig } from '../../../lib/useContact';

interface ContactInfo {
  title: string;
  company: string;
  address: string;
  email: string;
  phone: string;
  form: {
    name: string;
    phone: string;
    message: string;
    submit: string;
  };
}

interface ContactSectionProps {
  contact: ContactInfo;
  language: string;
}

export default function ContactSection({ contact, language }: ContactSectionProps) {
  const [formData, setFormData] = useState({
    name: '',
    phone: '',
    message: '',
  });
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [submitStatus, setSubmitStatus] = useState<'idle' | 'success' | 'error'>('idle');
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
  }, []);

  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
    const { name, value } = e.target;
    setFormData((prev) => ({
      ...prev,
      [name]: value,
    }));
  };

  const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setIsSubmitting(true);
    setSubmitStatus('idle');

    try {
      const response = await fetch('https://formspree.io/f/mldbwvwo', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          name: formData.name,
          phone: formData.phone,
          message: formData.message,
          language: language,
          source: 'website_contact_form',
        }),
      });

      if (response.ok) {
        setSubmitStatus('success');
        setFormData({ name: '', phone: '', message: '' });

        // Show success message for 5 seconds
        setTimeout(() => {
          setSubmitStatus('idle');
        }, 5000);
      } else {
        setSubmitStatus('error');
      }
    } catch (error) {
      console.error('Form submission error:', error);
      setSubmitStatus('error');
    } finally {
      setIsSubmitting(false);
    }
  };

  return (
    <section className="py-8 sm:py-12 md:py-16 bg-gray-50">
      <div className="max-w-6xl mx-auto px-4">
        <h2 className="text-2xl sm:text-3xl font-bold text-center mb-8 sm:mb-12">
          {contact.title}
        </h2>

        <div className="grid grid-cols-1 lg:grid-cols-2 gap-6 sm:gap-8">
          <div className="bg-orange-500 text-white p-6 sm:p-8 rounded-lg">
            <h3 className="text-xl sm:text-2xl font-bold mb-4 sm:mb-6">
              {contact.company}
            </h3>
            <div className="space-y-3 sm:space-y-4">
              <div className="flex items-start">
                <span className="mr-3 text-lg">📍</span>
                <span className="text-sm sm:text-base">
                  {contact.address}
                </span>
              </div>
              <div className="flex items-center">
                <span className="mr-3 text-lg">✉️</span>
                <span className="text-sm sm:text-base break-all">
                  {contact.email}
                </span>
              </div>
              <div className="flex items-center">
                <span className="mr-3 text-lg">📞</span>
                <span className="text-sm sm:text-base">{contact.phone}</span>
              </div>
            </div>

            <div className="flex space-x-4 mt-6 sm:mt-8">
              <div className="flex space-x-2">
                {/* Telegram图标 */}
                <a
                  href={contactConfig.telegram}
                  className="text-white hover:text-blue-300 transition-colors"
                  title="Telegram"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <svg
                    className="w-5 h-5"
                    fill="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path d="M11.944 0A12 12 0 0 0 0 12a12 12 0 0 0 12 12 12 12 0 0 0 12-12A12 12 0 0 0 12 0a12 12 0 0 0-.056 0zm4.962 7.224c.1-.002.321.023.465.14a.506.506 0 0 1 .171.325c.016.093.036.306.02.472-.18 1.898-.962 6.502-1.36 8.627-.168.9-.499 1.201-.82 1.23-.696.065-1.225-.46-1.9-.902-1.056-.693-1.653-1.124-2.678-1.8-1.185-.78-.417-1.21.258-1.91.177-.184 3.247-2.977 3.307-3.23.007-.032.014-.15-.056-.212s-.174-.041-.249-.024c-.106.024-1.793 1.14-5.061 3.345-.48.33-.913.49-1.302.48-.428-.008-1.252-.241-1.865-.44-.752-.245-1.349-.374-1.297-.789.027-.216.325-.437.893-.663 3.498-1.524 5.83-2.529 6.998-3.014 3.332-1.386 4.025-1.627 4.476-1.635z"/>
                  </svg>
                </a>

                {/* WhatsApp图标 */}
                <a
                  href={contactConfig.whatsapp}
                  className="text-white hover:text-green-300 transition-colors"
                  title="WhatsApp"
                  target="_blank"
                  rel="noopener noreferrer"
                >
                  <svg
                    className="w-5 h-5"
                    fill="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0020.464 3.488" />
                  </svg>
                </a>

                {/* Facebook图标 - 注释保留 */}
                {/* 
                <a
                  href="#"
                  className="text-white hover:text-blue-300 transition-colors"
                  title="Facebook"
                >
                  <svg
                    className="w-5 h-5"
                    fill="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z" />
                  </svg>
                </a>
                */}

                {/* 电话图标 */}
                <a
                  href={contactConfig.phoneHref}
                  className="text-white hover:text-blue-300 transition-colors"
                  title="电话联系"
                >
                  <svg
                    className="w-5 h-5"
                    fill="currentColor"
                    viewBox="0 0 24 24"
                  >
                    <path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z" />
                  </svg>
                </a>
              </div>
            </div>
          </div>

          <div className="bg-blue-600 text-white p-6 sm:p-8 rounded-lg">
            <form className="space-y-4 sm:space-y-6" onSubmit={handleSubmit}>
              <div>
                <input
                  type="text"
                  name="name"
                  value={formData.name}
                  onChange={handleInputChange}
                  placeholder={contact.form.name}
                  className="w-full p-3 border-b border-white bg-transparent placeholder-white focus:outline-none text-sm sm:text-base"
                  required
                />
              </div>
              <div>
                <input
                  type="tel"
                  name="phone"
                  value={formData.phone}
                  onChange={handleInputChange}
                  placeholder={contact.form.phone}
                  className="w-full p-3 border-b border-white bg-transparent placeholder-white focus:outline-none text-sm sm:text-base"
                  required
                />
              </div>
              <div>
                <textarea
                  name="message"
                  value={formData.message}
                  onChange={handleInputChange}
                  placeholder={contact.form.message}
                  rows={4}
                  className="w-full p-3 border-b border-white bg-transparent placeholder-white focus:outline-none resize-none text-sm sm:text-base"
                  required
                />
              </div>

              {/* Status Messages - Only show after component is mounted to prevent hydration errors */}
              {mounted && submitStatus === 'success' && (
                <div className="bg-green-500 text-white p-3 rounded text-sm">
                  {language === 'zh-CN'
                    ? '消息发送成功！我们会尽快与您联系。'
                    : language === 'zh-TW'
                      ? '消息發送成功！我們會盡快與您聯繫。'
                      : 'Message sent successfully! We will contact you soon.'}
                </div>
              )}

              {mounted && submitStatus === 'error' && (
                <div className="bg-red-500 text-white p-3 rounded text-sm">
                  {language === 'zh-CN'
                    ? '发送失败，请稍后重试。'
                    : language === 'zh-TW'
                      ? '發送失敗，請稍後重試。'
                      : 'Failed to send message. Please try again later.'}
                </div>
              )}

              <button
                type="submit"
                disabled={isSubmitting}
                className={`px-6 sm:px-8 py-3 rounded font-bold w-full sm:w-auto text-sm sm:text-base transition-colors ${
                  isSubmitting
                    ? 'bg-gray-300 text-gray-500 cursor-not-allowed'
                    : 'bg-white text-blue-600 hover:bg-gray-100'
                }`}
              >
                {mounted && isSubmitting
                  ? language === 'zh-CN'
                    ? '发送中...'
                    : language === 'zh-TW'
                      ? '發送中...'
                      : 'Sending...'
                  : contact.form.submit}
              </button>
            </form>
          </div>
        </div>
      </div>
    </section>
  );
} 