<?php
session_start();

// Generate CAPTCHA
if (!isset($_SESSION['captcha'])) {
    $_SESSION['captcha'] = rand(1000, 9999);
}

// AJAX CAPTCHA refresh
if (isset($_GET['get_captcha'])) {
    $_SESSION['captcha'] = rand(1000, 9999);
    echo $_SESSION['captcha'];
    exit;
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_once 'process_contact.php';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Mailer | SMTP</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: #f8f9fa;
            min-height: 100vh;
            padding: 0;
            margin: 0;
        }

        .contact-wrapper {
            display: grid;
            grid-template-columns: 1fr 1fr;
            height: 100vh;
            width: 100%;
            background: white;
            overflow: hidden;
        }

        /* Left Side - Image Section */
        .image-section {
            background: linear-gradient(135deg, rgba(253, 184, 19, 0.9) 0%, rgba(255, 215, 0, 0.9) 100%), 
                        url('https://images.unsplash.com/photo-1423666639041-f56000c27a9a?w=800&h=1200&fit=crop');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            height: 100%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            position: relative;
            overflow: hidden;
        }

        .image-section::before {
            content: '';
            position: absolute;
            top: -50%;
            left: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 50px 50px;
            animation: moveBackground 20s linear infinite;
        }

        @keyframes moveBackground {
            0% { transform: translate(0, 0); }
            100% { transform: translate(50px, 50px); }
        }

        .image-container {
            position: relative;
            z-index: 1;
            text-align: center;
            padding: 40px;
        }

        .main-image {
            display: none;
        }

        .image-section h2 {
            color: white;
            font-size: 48px;
            font-weight: 700;
            margin-bottom: 20px;
            text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.3);
        }

        .image-section p {
            color: white;
            font-size: 18px;
            line-height: 1.8;
            max-width: 400px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
        }

        /* Right Side - Form Section */
        .form-section {
            padding: 40px;
            background: white;
            height: 100vh;
            overflow-y: auto;
            display: flex;
            flex-direction: column;
        }

        .form-header {
            text-align: center;
            margin-bottom: 30px;
            flex-shrink: 0;
        }

        .form-header h1 {
            color: #1a1a1a;
            font-size: 28px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .form-header .subtitle {
            color: #666;
            font-size: 13px;
        }

        /* Card Style Form Groups */
        .form-card {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 15px;
            border: 2px solid transparent;
            transition: all 0.3s ease;
        }

        .form-card:hover {
            border-color: #FDB813;
            box-shadow: 0 4px 12px rgba(253, 184, 19, 0.2);
        }

        .form-card label {
            display: block;
            color: #1a1a1a;
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 8px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .form-card label span {
            color: #ff4757;
        }

        .form-card input,
        .form-card select,
        .form-card textarea {
            width: 100%;
            padding: 10px 12px;
            border: none;
            background: white;
            border-radius: 8px;
            font-size: 13px;
            font-family: 'Poppins', sans-serif;
            color: #1a1a1a;
            transition: all 0.3s ease;
        }

        .form-card input:focus,
        .form-card select:focus,
        .form-card textarea:focus {
            outline: none;
            box-shadow: 0 0 0 3px rgba(253, 184, 19, 0.3);
        }

        select {
            cursor: pointer;
            appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23FDB813' d='M6 9L1 4h10z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 15px center;
            padding-right: 40px;
        }

        textarea {
            resize: vertical;
            min-height: 80px;
        }

        /* CAPTCHA Card */
        .captcha-card {
            background: #1a1a1a;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 15px;
            text-align: center;
        }

        .captcha-label {
            color: white;
            font-weight: 600;
            font-size: 12px;
            margin-bottom: 12px;
            display: block;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .captcha-display {
            background: linear-gradient(135deg, #FDB813 0%, #FFD700 100%);
            padding: 15px;
            border-radius: 8px;
            display: inline-block;
            margin-bottom: 12px;
            position: relative;
            min-width: 150px;
        }

        .captcha-code {
            font-size: 28px;
            font-weight: 700;
            color: #1a1a1a;
            letter-spacing: 10px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
        }

        .refresh-btn {
            background: #FDB813;
            color: #1a1a1a;
            border: none;
            padding: 8px 20px;
            border-radius: 6px;
            cursor: pointer;
            font-size: 13px;
            font-weight: 600;
            transition: all 0.3s ease;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }

        .refresh-btn:hover {
            background: #FFD700;
            transform: translateY(-2px);
        }

        .captcha-input {
            margin-top: 12px;
        }

        .captcha-input input {
            width: 100%;
            padding: 10px;
            border: 2px solid #FDB813;
            background: white;
            border-radius: 8px;
            text-align: center;
            font-size: 16px;
            font-weight: 600;
            letter-spacing: 6px;
            color: #1a1a1a;
        }

        /* Checkbox */
        .checkbox-card {
            background: #f8f9fa;
            border-radius: 12px;
            padding: 15px;
            margin-bottom: 15px;
            display: flex;
            align-items: flex-start;
            gap: 10px;
        }

        .checkbox-card input[type="checkbox"] {
            width: 18px;
            height: 18px;
            margin-top: 2px;
            cursor: pointer;
            accent-color: #FDB813;
        }

        .checkbox-card label {
            font-size: 12px;
            color: #666;
            line-height: 1.5;
        }

        /* Submit Button */
        .submit-btn {
            width: 100%;
            background: linear-gradient(135deg, #FDB813 0%, #FFD700 100%);
            color: #1a1a1a;
            border: none;
            padding: 14px;
            border-radius: 12px;
            font-size: 15px;
            font-weight: 700;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            letter-spacing: 1px;
            box-shadow: 0 8px 24px rgba(253, 184, 19, 0.3);
            margin-top: 10px;
        }

        .submit-btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 15px 40px rgba(253, 184, 19, 0.5);
        }

        .submit-btn:active {
            transform: translateY(-1px);
        }

        /* Notification */
        .notification {
            padding: 12px 16px;
            border-radius: 12px;
            margin-bottom: 20px;
            font-size: 13px;
            display: none;
            animation: slideIn 0.5s ease;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .notification.success {
            background: #d4edda;
            border: 2px solid #c3e6cb;
            color: #155724;
            display: block;
        }

        .notification.error {
            background: #f8d7da;
            border: 2px solid #f5c6cb;
            color: #721c24;
            display: block;
        }

        /* Loading State */
        .loading {
            pointer-events: none;
            opacity: 0.6;
        }

        /* Responsive */
        @media (max-width: 1024px) {
            .contact-wrapper {
                grid-template-columns: 1fr;
                height: auto;
                min-height: 100vh;
            }

            .image-section {
                height: 40vh;
                min-height: 300px;
            }

            .image-section h2 {
                font-size: 36px;
            }

            .image-section p {
                font-size: 16px;
                max-width: 90%;
            }

            .form-section {
                height: auto;
                padding: 30px 25px;
            }
        }

        @media (max-width: 768px) {
            .image-section {
                height: 35vh;
                min-height: 250px;
            }

            .image-section h2 {
                font-size: 28px;
            }

            .image-section p {
                font-size: 14px;
            }

            .form-section {
                padding: 25px 20px;
            }

            .form-header h1 {
                font-size: 24px;
            }

            .form-card {
                padding: 12px;
                margin-bottom: 12px;
            }

            .captcha-card {
                padding: 15px;
            }

            .captcha-code {
                font-size: 24px;
                letter-spacing: 8px;
            }

            .submit-btn {
                padding: 12px;
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {
            .image-section {
                height: 30vh;
                min-height: 200px;
            }

            .image-section h2 {
                font-size: 24px;
            }

            .image-section p {
                font-size: 13px;
                padding: 0 15px;
            }

            .form-section {
                padding: 20px 15px;
            }

            .form-header h1 {
                font-size: 20px;
            }

            .form-card label,
            .captcha-label {
                font-size: 11px;
            }

            .form-card input,
            .form-card select,
            .form-card textarea {
                font-size: 12px;
                padding: 9px 10px;
            }

            .captcha-display {
                min-width: 130px;
                padding: 12px;
            }

            .captcha-code {
                font-size: 20px;
                letter-spacing: 6px;
            }

            .refresh-btn {
                padding: 7px 15px;
                font-size: 12px;
            }
        }

        /* Smooth scrollbar */
        .form-section::-webkit-scrollbar {
            width: 8px;
        }

        .form-section::-webkit-scrollbar-track {
            background: #f1f1f1;
        }

        .form-section::-webkit-scrollbar-thumb {
            background: #FDB813;
            border-radius: 4px;
        }

        .form-section::-webkit-scrollbar-thumb:hover {
            background: #FFD700;
        }
    </style>
</head>
<body>
    <div class="contact-wrapper">
        <!-- Left Side - Image Section -->
        <div class="image-section">
            <div class="image-container">
                <h2>Let's Connect!</h2>
                <p>We're here to help and answer any questions you might have. We look forward to hearing from you.</p>
            </div>
        </div>

        <!-- Right Side - Form Section -->
        <div class="form-section">
            <div class="form-header">
                <h1>Get In Touch</h1>
                <p class="subtitle">Fill out the form and we'll be in touch soon</p>
            </div>

            <?php if (isset($_SESSION['message'])): ?>
                <div class="notification <?php echo $_SESSION['msg_type']; ?>">
                    <?php 
                        echo $_SESSION['message']; 
                        unset($_SESSION['message']);
                        unset($_SESSION['msg_type']);
                    ?>
                </div>
            <?php endif; ?>

            <form method="POST" action="" id="contactForm">
                <div class="form-card">
                    <label>Full Name <span>*</span></label>
                    <input type="text" name="name" required placeholder="John Doe">
                </div>

                <div class="form-card">
                    <label>Email Address <span>*</span></label>
                    <input type="email" name="email" required placeholder="john@example.com">
                </div>

                <div class="form-card">
                    <label>Phone Number <span>*</span></label>
                    <input type="tel" name="phone" required placeholder="+1 234 567 8900">
                </div>

                <div class="form-card">
                    <label>Inquiry Type <span>*</span></label>
                    <select name="inquiry_type" required>
                        <option value="">Select an option</option>
                        <option value="General Inquiry">General Inquiry</option>
                        <option value="Product Support">Product Support</option>
                        <option value="Sales & Pricing">Sales & Pricing</option>
                        <option value="Partnership Opportunity">Partnership Opportunity</option>
                        <option value="Technical Assistance">Technical Assistance</option>
                    </select>
                </div>

                <div class="form-card">
                    <label>Message <span>*</span></label>
                    <textarea name="message" required placeholder="Tell us how we can help you..."></textarea>
                </div>

                <div class="captcha-card">
                    <span class="captcha-label">Security Verification <span style="color: #FDB813;">*</span></span>
                    <div class="captcha-display">
                        <div class="captcha-code" id="captchaCode"><?php echo $_SESSION['captcha']; ?></div>
                    </div>
                    <button type="button" class="refresh-btn" onclick="refreshCaptcha()">
                        <span id="refreshIcon">🔄</span> Refresh Code
                    </button>
                    <div class="captcha-input">
                        <input type="text" name="captcha" required placeholder="Enter code" maxlength="4" pattern="\d{4}">
                    </div>
                </div>

                <div class="checkbox-card">
                    <input type="checkbox" id="terms" name="terms" required>
                    <label for="terms">
                        I agree to the terms and conditions and consent to the processing of my personal data for contact purposes.
                    </label>
                </div>

                <button type="submit" class="submit-btn">Send Message</button>
            </form>
        </div>
    </div>

    <script>
        // AJAX CAPTCHA Refresh (no page reload)
        function refreshCaptcha() {
            const captchaCode = document.getElementById('captchaCode');
            const refreshIcon = document.getElementById('refreshIcon');
            
            // Animate refresh
            refreshIcon.style.transform = 'rotate(360deg)';
            refreshIcon.style.transition = 'transform 0.5s ease';
            
            // Fetch new CAPTCHA
            fetch('?get_captcha=1')
                .then(response => response.text())
                .then(data => {
                    captchaCode.textContent = data;
                    setTimeout(() => {
                        refreshIcon.style.transform = 'rotate(0deg)';
                    }, 500);
                })
                .catch(error => {
                    console.error('Error:', error);
                    refreshIcon.style.transform = 'rotate(0deg)';
                });
        }

        // Form validation
        document.getElementById('contactForm').addEventListener('submit', function(e) {
            const phone = document.querySelector('input[name="phone"]').value;
            const phonePattern = /^[+]?[\d\s\-()]+$/;
            
            if (!phonePattern.test(phone)) {
                e.preventDefault();
                alert('Please enter a valid phone number');
                return false;
            }

            // Add loading state
            const submitBtn = document.querySelector('.submit-btn');
            submitBtn.textContent = 'Sending...';
            submitBtn.classList.add('loading');
        });

        // Add smooth animations on scroll
        const formCards = document.querySelectorAll('.form-card, .captcha-card, .checkbox-card');
        formCards.forEach((card, index) => {
            card.style.opacity = '0';
            card.style.transform = 'translateY(20px)';
            
            setTimeout(() => {
                card.style.transition = 'all 0.5s ease';
                card.style.opacity = '1';
                card.style.transform = 'translateY(0)';
            }, 100 * index);
        });
    </script>
</body>
</html>