<?php
/**
 * Template for displaying Contact page
 *
 * @package Nihongo
 */

// Handle form submission
$message = '';
$message_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Verify nonce
    if (!isset($_POST['nihongo_contact_nonce']) || !wp_verify_nonce($_POST['nihongo_contact_nonce'], 'nihongo_contact')) {
        $message = nihongo_translate('error');
        $message_type = 'error';
    } else {
        // Honeypot field (bots will often fill this)
        $honeypot = isset($_POST['company']) ? trim((string) $_POST['company']) : '';
        if ($honeypot !== '') {
            $message = nihongo_translate('error');
            $message_type = 'error';
        } else {
            // Simple rate limiting per IP (2 minutes)
            $remote_ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown';
            $rate_limit_key = 'nihongo_contact_rate_' . md5($remote_ip);
            $is_rate_limited = get_transient($rate_limit_key);
            if ($is_rate_limited) {
                $message = nihongo_translate('error');
                $message_type = 'error';
            } else {
                set_transient($rate_limit_key, time(), MINUTE_IN_SECONDS * 2);

                // Sanitize inputs
                $name = isset($_POST['name']) ? sanitize_text_field($_POST['name']) : '';
                $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
                $phone = isset($_POST['phone']) ? sanitize_text_field($_POST['phone']) : '';
                $inquiry_type = isset($_POST['inquiry_type']) ? sanitize_text_field($_POST['inquiry_type']) : '';
                $subject = isset($_POST['subject']) ? sanitize_text_field($_POST['subject']) : '';
                $message_text = isset($_POST['message']) ? sanitize_textarea_field($_POST['message']) : '';

                // Basic validation
                if (empty($name) || empty($email) || empty($message_text)) {
                    $message = nihongo_translate('fill_required_fields');
                    $message_type = 'error';
                } elseif (!is_email($email)) {
                    $message = nihongo_translate('enter_valid_email');
                    $message_type = 'error';
                } else {
                    // Success (email sending handled elsewhere or can be added with wp_mail)
                    $message = nihongo_translate('thank_you_inquiry');
                    $message_type = 'success';
                }
            }
        }
    }
}

// Get inquiry type from URL parameter
$default_inquiry_type = isset($_GET['type']) ? sanitize_text_field($_GET['type']) : '';
?>

<?php get_header(); ?>

<main id="main" class="site-main">
    <!-- Hero Section -->
    <section class="contact-hero">
        <div class="contact-hero-background">
            <img src="<?php echo get_template_directory_uri(); ?>/assets/img/cobg.jpg" alt="<?php echo nihongo_translate('contact_background_alt'); ?>" class="contact-bg-image">
            <div class="contact-hero-overlay"></div>
            <div class="contact-particles"></div>
        </div>
        <div class="container">
            <div class="contact-hero-content">
                <h1 class="contact-hero-title" data-aos="fade-down" data-aos-delay="100"><?php echo nihongo_translate('contact_title'); ?></h1>
                <p class="contact-hero-subtitle" data-aos="fade-up" data-aos-delay="200"><?php echo nihongo_translate('contact_subtitle'); ?></p>
                <div class="contact-hero-decoration" data-aos="zoom-in" data-aos-delay="300">
                    <div class="decoration-line"></div>
                    <div class="decoration-dot"></div>
                    <div class="decoration-line"></div>
                </div>
            </div>
        </div>
    </section>

    <!-- Modern Contact Form Section -->
    <section class="modern-contact-section">
        <div class="container">
            <div class="modern-form-container">
                <div class="modern-form-card">
                    <?php if ($message): ?>
                        <div class="modern-alert modern-alert-<?php echo $message_type; ?>">
                            <div class="alert-icon"><?php echo $message_type === 'success' ? '✅' : '⚠️'; ?></div>
                            <div class="alert-content">
                                <h4><?php echo $message_type === 'success' ? nihongo_translate('success') : nihongo_translate('error'); ?></h4>
                                <p><?php echo esc_html($message); ?></p>
                            </div>
                        </div>
                    <?php endif; ?>
                    
                    <div class="form-header">
                        <h2 class="modern-form-title"><?php echo nihongo_translate('get_in_touch'); ?></h2>
                        <p class="modern-form-subtitle"><?php echo nihongo_translate('contact_description'); ?></p>
                    </div>
                    
                    <form method="POST" action="" class="modern-form" id="contactForm">
                        <?php wp_nonce_field('nihongo_contact', 'nihongo_contact_nonce'); ?>
                        <input type="text" name="company" id="company" value="" style="display:none !important" tabindex="-1" autocomplete="off" aria-hidden="true">
                        <div class="form-row">
                            <div class="form-group modern-form-group">
                                <label for="name" class="field-label"><?php echo nihongo_translate('full_name'); ?></label>
                                <div class="yard-value"><?php echo nihongo_translate('name_validation_text'); ?></div>
                                <div class="input-container">
                                    <input type="text" id="name" name="name" class="modern-input" required 
                                           value="<?php echo esc_attr($_POST['name'] ?? ''); ?>"
                                           pattern="[A-Za-z\s]{2,50}"
                                           title="Please enter a valid name (2-50 characters, letters and spaces only)">
                                    <div class="input-border"></div>
                                </div>
                                <div class="validation-message" id="nameValidation"></div>
                            </div>
                            
                            <div class="form-group modern-form-group">
                                <label for="email" class="field-label"><?php echo nihongo_translate('email_address'); ?></label>
                                <div class="yard-value"><?php echo nihongo_translate('email_validation_text'); ?></div>
                                <div class="input-container">
                                    <input type="email" id="email" name="email" class="modern-input" required 
                                           value="<?php echo esc_attr($_POST['email'] ?? ''); ?>"
                                           pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
                                           title="Please enter a valid email address">
                                    <div class="input-border"></div>
                                </div>
                                <div class="validation-message" id="emailValidation"></div>
                            </div>
                        </div>
                        
                        <div class="form-row">
                            <div class="form-group modern-form-group">
                                <label for="phone" class="field-label"><?php echo nihongo_translate('phone_number'); ?></label>
                                <div class="yard-value"><?php echo nihongo_translate('phone_validation_text_alt'); ?></div>
                                <div class="input-container">
                                    <input type="tel" id="phone" name="phone" class="modern-input" 
                                           value="<?php echo esc_attr($_POST['phone'] ?? ''); ?>"
                                           pattern="[\+]?[0-9\s\-\(\)]{10,20}"
                                           title="Please enter a valid phone number">
                                    <div class="input-border"></div>
                                </div>
                                <div class="validation-message" id="phoneValidation"></div>
                            </div>
                            
                            <div class="form-group modern-form-group">
                                <label for="inquiry_type" class="field-label"><?php echo nihongo_translate('inquiry_type'); ?></label>
                                <div class="yard-value"><?php echo nihongo_translate('select_inquiry_type_text'); ?></div>
                                <div class="input-container select-container">
                                    <select id="inquiry_type" name="inquiry_type" class="modern-select" required>
                                        <option value=""><?php echo nihongo_translate('select_inquiry_type'); ?></option>
                                        <option value="course" <?php echo ($_POST['inquiry_type'] ?? $default_inquiry_type) === 'course' ? 'selected' : ''; ?>><?php echo nihongo_translate('about_courses'); ?></option>
                                        <option value="j-bridge-company" <?php echo ($_POST['inquiry_type'] ?? $default_inquiry_type) === 'company' ? 'selected' : ''; ?>><?php echo nihongo_translate('j_bridge_companies'); ?></option>
                                        <option value="j-bridge-jobseeker" <?php echo ($_POST['inquiry_type'] ?? $default_inquiry_type) === 'jobseeker' ? 'selected' : ''; ?>><?php echo nihongo_translate('j_bridge_jobseekers'); ?></option>
                                        <option value="pricing" <?php echo ($_POST['inquiry_type'] ?? '') === 'pricing' ? 'selected' : ''; ?>><?php echo nihongo_translate('about_pricing'); ?></option>
                                        <option value="general" <?php echo ($_POST['inquiry_type'] ?? '') === 'general' ? 'selected' : ''; ?>><?php echo nihongo_translate('other'); ?></option>
                                    </select>
                                    <div class="input-border"></div>
                                </div>
                                <div class="validation-message" id="inquiryValidation"></div>
                            </div>
                        </div>
                        
                        <div class="form-group modern-form-group">
                            <label for="subject" class="field-label"><?php echo nihongo_translate('subject'); ?></label>
                            <div class="yard-value"><?php echo nihongo_translate('subject_validation_text_alt'); ?></div>
                            <div class="input-container">
                                <input type="text" id="subject" name="subject" class="modern-input" 
                                       value="<?php echo esc_attr($_POST['subject'] ?? ''); ?>"
                                       pattern="[A-Za-z0-9\s\-\.]{5,100}"
                                       title="Please enter a subject (5-100 characters)">
                                <div class="input-border"></div>
                            </div>
                            <div class="validation-message" id="subjectValidation"></div>
                        </div>
                        
                        <div class="form-group modern-form-group">
                            <label for="message" class="field-label"><?php echo nihongo_translate('message'); ?></label>
                            <div class="yard-value"><?php echo nihongo_translate('message_validation_text_alt'); ?></div>
                            <div class="input-container textarea-container">
                                <textarea id="message" name="message" class="modern-textarea" required 
                                          placeholder="<?php echo nihongo_translate('message_placeholder'); ?>"
                                          minlength="10"
                                          maxlength="1000"><?php echo esc_textarea($_POST['message'] ?? ''); ?></textarea>
                                <div class="input-border"></div>
                                <div class="character-count">
                                    <span id="messageCount">0</span>/1000 <?php echo nihongo_translate('character_count'); ?>
                                </div>
                            </div>
                            <div class="validation-message" id="messageValidation"></div>
                        </div>
                        
                        <div class="form-actions">
                            <button type="submit" class="modern-submit-btn" id="submitBtn">
                                <span class="btn-content">
                                    <span class="btn-text"><?php echo nihongo_translate('send_message'); ?></span>
                                </span>
                                <div class="btn-loader">
                                    <div class="spinner"></div>
                                </div>
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>

    <!-- Corporate Information Section -->
    <section class="corporate-info-section">
        <div class="corporate-info-background">
            <img src="<?php echo get_template_directory_uri(); ?>/assets/img/map.png" alt="<?php echo nihongo_translate('map_background_alt'); ?>" class="corporate-bg-image">
            <div class="corporate-info-overlay"></div>
        </div>
        <div class="container">
            <h2 class="section-title corporate-section-title" data-aos="fade-down"><?php echo nihongo_translate('corporate_information'); ?></h2>
            
            <div class="corporate-info-simple">
                <!-- Left Side Information -->
                <div class="corporate-left-info" data-aos="fade-right" data-aos-delay="100">
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('corporate_name'); ?></h3>
                        <p class="info-description">
                            <strong><?php echo nihongo_translate('corporate_name_value'); ?></strong><br>
                            <?php echo nihongo_translate('corporate_name_alt'); ?>
                        </p>
                    </div>
                    
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('location'); ?></h3>
                        <p class="info-description">
                            3F, 6th-Matsushima Bldg.,<br>
                            10-10 Motoyoyogi-cho, Shibuya-ku,<br>
                            Tokyo 151-0062, Japan<br>
                            [Postal Code: 151-0062] 10-10 Motoyoyogi-cho, Shibuya-ku, Tokyo, 6th Matsushima Building 3F
                        </p>
                    </div>
                    
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('corporate_number'); ?></h3>
                        <p class="info-description">
                            <strong>T4010001160053</strong><br>
                            (Qualified Invoice Registration Number)
                        </p>
                    </div>
                </div>
                
                <!-- Right Side Information -->
                <div class="corporate-right-info" data-aos="fade-left" data-aos-delay="100">
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('telephone'); ?></h3>
                        <p class="info-description">
                            <strong>050-5490-2492</strong>
                        </p>
                    </div>
                    
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('editor_in_chief'); ?></h3>
                        <p class="info-description">
                            <strong>TARUISHI Koji</strong><br>
                            [Koji Taruishi, CEO & Editor-in-Chief]
                        </p>
                    </div>
                    
                    <div class="info-item">
                        <h3 class="info-title"><?php echo nihongo_translate('established'); ?></h3>
                        <p class="info-description">
                            <strong>1986</strong><br>
                            [Founded] 1986
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- FAQ Section -->
    <section class="faq-section">
        <div class="container">
            <h2 class="section-title faq-section-title" data-aos="fade-down"><?php echo nihongo_translate('frequently_asked_questions'); ?></h2>
            <p class="section-subtitle faq-section-subtitle" data-aos="fade-up"><?php echo nihongo_translate('check_before_contacting'); ?></p>
            
            <div class="faq-layout">
                <!-- Left Questions -->
                <div class="faq-left">
                    <div class="faq-question" data-aos="fade-right" data-aos-delay="100" data-faq="1">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_free_trial'); ?></h3>
                    </div>
                    
                    <div class="faq-question" data-aos="fade-right" data-aos-delay="150" data-faq="2">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_fee_structure'); ?></h3>
                    </div>
                    
                    <div class="faq-question" data-aos="fade-right" data-aos-delay="200" data-faq="3">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_online_only'); ?></h3>
                    </div>
                </div>
                
                <!-- Center Image with Answer Overlay -->
                <div class="faq-center">
                    <div class="faq-image-container" data-aos="zoom-in" data-aos-delay="300">
                        <div class="faq-image">
                            <img src="<?php echo get_template_directory_uri(); ?>/assets/img/qu.jpg" alt="<?php echo nihongo_translate('faq_question_image_alt'); ?>" class="faq-bg-image">
                            
                            <div class="question-bubble" id="questionBubble">
                                <div class="bubble-content">
                                    <h3 class="bubble-title" id="questionTitle"><?php echo nihongo_translate('select_a_question'); ?></h3>
                                </div>
                                <div class="bubble-tail"></div>
                            </div>
                            <div class="answer-bubble" id="answerBubble">
                                <div class="bubble-content">
                                    <p class="bubble-text" id="answerText"><?php echo nihongo_translate('click_question_for_answer'); ?></p>
                                </div>
                                <div class="bubble-tail"></div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Right Questions -->
                <div class="faq-right">
                    <div class="faq-question" data-aos="fade-left" data-aos-delay="100" data-faq="4">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_beginners_okay'); ?></h3>
                    </div>
                    
                    <div class="faq-question" data-aos="fade-left" data-aos-delay="150" data-faq="5">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_j_bridge_fees'); ?></h3>
                    </div>
                    
                    <div class="faq-question" data-aos="fade-left" data-aos-delay="200" data-faq="6">
                        <h3 class="question-title"><?php echo nihongo_translate('faq_visa_support'); ?></h3>
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<?php get_footer(); ?>
