<?php
@session_start();
include('includes/config.php');

$alert_script = '';

if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['mobile'];
    $message = $_POST['message'];

    $result = submitContactQuery($name, $email, $mobile, $message);

    if($result['success']) {
        $ticket_number = $result['ticket_number'];
        $alert_script = '
            Swal.fire({
                title: "Success! 🎉",
                html: "Your message has been sent successfully!<br><br><strong>Ticket Number:</strong> ' . $ticket_number . '<br><br>We have sent a confirmation to your email.",
                icon: "success",
                confirmButtonColor: "#4bb543"
            }).then(() => {
                document.getElementById("contactForm").reset();
            });
        ';
    } else {
        $alert_script = '
            Swal.fire({
                title: "Error!",
                text: "Failed to send message. Please try again. '.$result['error'].'",
                icon: "error",
                confirmButtonColor: "#dc3545"
            });
        ';
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="icon" href="./template/tngweb/assets/images/favicon-32x32.png" type="image/png" />
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.4.14/dist/sweetalert2.all.min.js"></script>
    <title><?php echo XyZ_val('brand'); ?> | Contact Us</title>
    
    <!-- ========== ENHANCED MODERN CSS (preserves all original classes) ========== -->
    <style>
        /* Reset & Base */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29 0%, #302b63 50%, #24243e 100%);
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            position: relative;
            overflow-x: hidden;
        }
        /* Animated Background Shapes */
        .bg-shape {
            position: fixed;
            filter: blur(80px);
            opacity: 0.35;
            z-index: 0;
            border-radius: 50%;
            animation: floatBg 18s infinite ease-in-out;
            pointer-events: none;
        }
        .shape-1 { top: -150px; left: -150px; width: 400px; height: 400px; background: radial-gradient(circle, #00ffff, #0080ff); }
        .shape-2 { bottom: -120px; right: -120px; width: 500px; height: 500px; background: radial-gradient(circle, #ff00ff, #ff0080); animation-delay: -6s; }
        .shape-3 { top: 40%; left: -80px; width: 300px; height: 300px; background: radial-gradient(circle, #ffd700, #ff6600); animation-delay: -12s; }
        @keyframes floatBg {
            0% { transform: translate(0, 0) scale(1); }
            50% { transform: translate(40px, 30px) scale(1.1); }
            100% { transform: translate(0, 0) scale(1); }
        }
        
        /* Navbar - original classes, upgraded look */
        .navbar {
            position: relative;
            z-index: 100;
            background: rgba(15, 12, 41, 0.7);
            backdrop-filter: blur(12px);
            border-bottom: 1px solid rgba(255,255,255,0.1);
            padding: 1rem 2rem;
        }
        .nav-container {
            max-width: 1300px;
            margin: 0 auto;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
        }
        .logo {
            display: flex;
            align-items: center;
            gap: 12px;
            font-size: 1.5rem;
            font-weight: 700;
            color: white;
            text-decoration: none;
            letter-spacing: -0.5px;
        }
        .logo img {
            height: 40px;
            width: auto;
            filter: drop-shadow(0 2px 5px rgba(0,0,0,0.2));
        }
        .nav-links {
            display: flex;
            gap: 2rem;
            list-style: none;
            align-items: center;
        }
        .nav-links li a {
            color: rgba(255,255,255,0.85);
            text-decoration: none;
            font-weight: 500;
            transition: 0.2s;
            font-size: 0.95rem;
            padding: 0.5rem 0;
        }
        .nav-links li a:hover {
            color: white;
            text-shadow: 0 0 6px rgba(255,255,255,0.5);
        }
        .nav-links li a.active {
            color: #c084fc;
            border-bottom: 2px solid #c084fc;
        }
        @media (max-width: 768px) {
            .navbar { padding: 0.8rem 1.2rem; }
            .nav-links {
                gap: 1rem;
            }
        }
        
        /* Contact Container & Card - enhanced glassmorphism */
        .contact-container {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 2rem 1.5rem;
            position: relative;
            z-index: 10;
        }
        .contact-card {
            background: rgba(255, 255, 255, 0.07);
            backdrop-filter: blur(14px);
            border-radius: 2rem;
            border: 1px solid rgba(255, 255, 255, 0.2);
            box-shadow: 0 25px 45px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255,255,255,0.1);
            padding: 2rem;
            width: 100%;
            max-width: 550px;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
        }
        .contact-card:hover {
            transform: translateY(-4px);
            box-shadow: 0 30px 55px rgba(0, 0, 0, 0.4);
        }
        /* Shine effect on card */
        .contact-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.12), transparent);
            transition: left 0.5s;
            pointer-events: none;
        }
        .contact-card:hover::before {
            left: 100%;
        }
        
        .contact-header {
            text-align: center;
            margin-bottom: 1.8rem;
        }
        .contact-icon {
            font-size: 3rem;
            color: #c084fc;
            margin-bottom: 1rem;
            display: inline-block;
        }
        .contact-header h1 {
            font-size: 1.8rem;
            font-weight: 700;
            background: linear-gradient(135deg, #fff, #c084fc);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
            margin-bottom: 0.5rem;
        }
        .contact-header p {
            color: rgba(255,255,255,0.7);
            font-size: 0.85rem;
        }
        
        .form-group {
            margin-bottom: 1.3rem;
        }
        .form-label {
            display: block;
            color: rgba(255,255,255,0.9);
            font-weight: 500;
            font-size: 0.8rem;
            margin-bottom: 0.5rem;
            letter-spacing: 0.5px;
        }
        .form-control {
            width: 100%;
            background: rgba(255,255,255,0.1);
            border: 1px solid rgba(255,255,255,0.2);
            border-radius: 1.2rem;
            padding: 0.85rem 1rem;
            font-size: 0.9rem;
            color: white;
            font-weight: 500;
            transition: 0.2s;
            outline: none;
            font-family: 'Poppins', sans-serif;
        }
        .form-control:focus {
            border-color: rgba(255,255,255,0.6);
            background: rgba(255,255,255,0.15);
            box-shadow: 0 0 10px rgba(255,255,255,0.15);
        }
        .form-control::placeholder {
            color: rgba(255,255,255,0.4);
        }
        textarea.form-control {
            resize: vertical;
            min-height: 100px;
        }
        
        .submit-btn {
            width: 100%;
            background: linear-gradient(95deg, #7c3aed, #a855f7);
            border: none;
            border-radius: 1.8rem;
            padding: 0.85rem;
            font-weight: 700;
            font-size: 0.95rem;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 0.5rem;
            cursor: pointer;
            transition: 0.2s;
            position: relative;
            overflow: hidden;
            box-shadow: 0 6px 18px rgba(124,58,237,0.3);
        }
        .submit-btn:hover {
            transform: scale(1.02);
            box-shadow: 0 8px 22px rgba(124,58,237,0.5);
        }
        .check-status-link {
            text-align: center;
            margin-top: 1.3rem;
            font-size: 0.8rem;
        }
        .check-status-link a {
            color: #c084fc;
            text-decoration: none;
            font-weight: 600;
        }
        .check-status-link a:hover {
            text-shadow: 0 0 5px #c084fc;
        }
        
        /* Footer */
        .footer {
            text-align: center;
            padding: 1.2rem;
            font-size: 0.7rem;
            color: rgba(255,255,255,0.4);
            z-index: 10;
            position: relative;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        /* Responsive */
        @media (max-width: 600px) {
            .contact-card {
                padding: 1.5rem;
            }
            .contact-header h1 {
                font-size: 1.5rem;
            }
        }
    </style>
</head>

<body>
    <!-- Animated background shapes (added for glamour, does not affect functionality) -->
    <div class="bg-shape shape-1"></div>
    <div class="bg-shape shape-2"></div>
    <div class="bg-shape shape-3"></div>

    <!-- Navigation (original HTML unchanged) -->
    <nav class="navbar">
        <div class="nav-container">
            <a href="index.php" class="logo">
                <img src="<?php echo XyZ_val('logo'); ?>" alt="Logo">
                <?php echo XyZ_val('brand'); ?>
            </a>
            
            <ul class="nav-links">
                <li><a href="index.php">Home</a></li>
                <li><a href="register.php">Register</a></li>
                <li><a href="login.php">Login</a></li>
                <li><a href="contact.php" class="active">Contact</a></li>
            </ul>
        </div>
    </nav>

    <!-- Contact Content -->
    <div class="contact-container">
        <div class="contact-card">
            <div class="contact-header">
                <i class='bx bx-envelope contact-icon'></i>
                <h1>Contact Us</h1>
                <p>We'd love to hear from you. Send us a message!</p>
            </div>
            
            <div class="contact-body">
                <form method="POST" action="" id="contactForm">
                    <div class="form-group">
                        <label for="name" class="form-label">Full Name *</label>
                        <input type="text" name="name" class="form-control" id="name" placeholder="Enter your full name" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="email" class="form-label">Email Address *</label>
                        <input type="email" name="email" class="form-control" id="email" placeholder="Enter your email" required>
                    </div>
                    
                    <div class="form-group">
                        <label for="mobile" class="form-label">Mobile Number *</label>
                        <input type="text" name="mobile" class="form-control" id="mobile" placeholder="Enter your mobile number" required maxlength="10">
                    </div>
                    
                    <div class="form-group">
                        <label for="message" class="form-label">Message *</label>
                        <textarea name="message" class="form-control" id="message" placeholder="Enter your message" required></textarea>
                    </div>
                    
                    <button type="submit" name="submit" class="submit-btn">
                        <i class='bx bx-send'></i> Send Message
                    </button>
                    
                    <div class="check-status-link">
                        <a href="check_status.php">Check existing ticket status</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Footer -->
    <footer class="footer">
        <div class="container">
            <p>&copy; <?php echo date("Y"); ?> <?php echo XyZ_val('brand'); ?>. All rights reserved.</p>
        </div>
    </footer>

    <script>
        // Mobile number validation
        document.getElementById('mobile').addEventListener('input', function(e) {
            this.value = this.value.replace(/[^0-9]/g, '');
        });
        
        // Form validation
        document.getElementById('contactForm').addEventListener('submit', function(e) {
            const name = document.getElementById('name').value.trim();
            const email = document.getElementById('email').value.trim();
            const mobile = document.getElementById('mobile').value.trim();
            const message = document.getElementById('message').value.trim();
            
            if (!name || !email || !mobile || !message) {
                e.preventDefault();
                Swal.fire({
                    title: "Required!",
                    text: "Please fill all required fields",
                    icon: "warning",
                    confirmButtonColor: "#ffc107"
                });
                return;
            }
            
            if (mobile.length !== 10) {
                e.preventDefault();
                Swal.fire({
                    title: "Invalid Mobile!",
                    text: "Please enter a valid 10-digit mobile number",
                    icon: "warning",
                    confirmButtonColor: "#ffc107"
                });
                return;
            }
            
            // Email validation
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            if (!emailRegex.test(email)) {
                e.preventDefault();
                Swal.fire({
                    title: "Invalid Email!",
                    text: "Please enter a valid email address",
                    icon: "warning",
                    confirmButtonColor: "#ffc107"
                });
                return;
            }
        });
        
        // Auto focus on first field
        document.addEventListener('DOMContentLoaded', function() {
            document.getElementById('name').focus();
            
            <?php if (!empty($alert_script)): ?>
            <?php echo $alert_script; ?>
            <?php endif; ?>
        });
    </script>
</body>
</html>