<?php
/**
 * Template Name: Contact Page
 * Template Post Type: page
 *
 * Contact page template with form area, contact info sidebar, map, and FAQ.
 *
 * @package Paws_And_Care
 * @since 1.2.6
 */

get_header();

// Get customizer settings
$show_info = get_theme_mod('paws_and_care_contact_page_show_info', true);
$show_emergency = get_theme_mod('paws_and_care_contact_page_show_emergency', true);
$show_map = get_theme_mod('paws_and_care_contact_page_show_map', true);
$show_faq = get_theme_mod('paws_and_care_contact_page_show_faq', true);

// Contact info
$contact_address = get_theme_mod('paws_and_care_contact_address', '123 Pet Care Avenue, Animal City, AC 12345');
$contact_phone = get_theme_mod('paws_and_care_contact_phone', '+1 234 567 890');
$contact_email = get_theme_mod('paws_and_care_contact_email', 'example@example.com');
$contact_hours = get_theme_mod('paws_and_care_contact_hours', 'Mon-Fri: 9AM-6PM, Sat: 9AM-4PM');

// Emergency
$emergency_label = get_theme_mod('paws_and_care_contact_emergency_label', '24/7 Emergency Line');
$emergency_phone = get_theme_mod('paws_and_care_contact_emergency_phone', '+1 234 567 999');
$emergency_text = get_theme_mod('paws_and_care_contact_emergency_text', 'For urgent pet emergencies outside of business hours, please call our emergency hotline.');

// Map
$map_title = get_theme_mod('paws_and_care_contact_map_title', 'Find Us');
$map_embed = get_theme_mod('paws_and_care_contact_map_embed', '');

// FAQ
$faq_title = get_theme_mod('paws_and_care_contact_faq_title', 'Frequently Asked Questions');
$faq_setting = get_theme_mod('paws_and_care_contact_faq_items', paws_and_care_get_faq_default());
$faq_items = is_string($faq_setting) ? json_decode($faq_setting, true) : $faq_setting;
if (empty($faq_items) || !is_array($faq_items)) {
    $faq_items = array();
}
?>

<?php get_template_part('template-parts/page-header'); ?>

<!-- Contact Section -->
<section id="primary-content" class="py-5 contact-section">
    <div class="container">
        <div class="row">
            <!-- Left Column: Page Content (Contact Form) -->
            <div class="col-lg-8 mb-5">
                <div class="card border-0 shadow-sm">
                    <div class="card-body p-5">
                        <?php
                        while (have_posts()):
                            the_post();
                            the_content();
                        endwhile;
                        ?>
                    </div>
                </div>
            </div>

            <!-- Right Column: Contact Info & Emergency -->
            <div class="col-lg-4">
                <?php if ($show_info): ?>
                    <div class="card border-0 shadow-sm mb-4">
                        <div class="card-body p-4">
                            <h5 class="mb-4"><?php esc_html_e('Contact Information', 'paws-and-care'); ?></h5>

                            <?php if ($contact_address): ?>
                                <div class="d-flex mb-4">
                                    <div class="contact-icon-bubble me-3">
                                        <i class="fa-solid fa-location-dot"></i>
                                    </div>
                                    <div>
                                        <h6 class="mb-1 fw-semibold"><?php esc_html_e('Address', 'paws-and-care'); ?></h6>
                                        <p class="mb-0 text-muted"><?php echo nl2br(esc_html($contact_address)); ?></p>
                                    </div>
                                </div>
                            <?php endif; ?>

                            <?php if ($contact_phone): ?>
                                <div class="d-flex mb-4">
                                    <div class="contact-icon-bubble me-3">
                                        <i class="fa-solid fa-phone"></i>
                                    </div>
                                    <div>
                                        <h6 class="mb-1 fw-semibold"><?php esc_html_e('Phone', 'paws-and-care'); ?></h6>
                                        <p class="mb-0 text-muted"><?php echo esc_html($contact_phone); ?></p>
                                    </div>
                                </div>
                            <?php endif; ?>

                            <?php if ($contact_email): ?>
                                <div class="d-flex mb-4">
                                    <div class="contact-icon-bubble me-3">
                                        <i class="fa-solid fa-envelope"></i>
                                    </div>
                                    <div>
                                        <h6 class="mb-1 fw-semibold"><?php esc_html_e('Email', 'paws-and-care'); ?></h6>
                                        <p class="mb-0 text-muted"><?php echo esc_html($contact_email); ?></p>
                                    </div>
                                </div>
                            <?php endif; ?>

                            <?php if ($contact_hours): ?>
                                <div class="d-flex">
                                    <div class="contact-icon-bubble me-3">
                                        <i class="fa-solid fa-clock"></i>
                                    </div>
                                    <div>
                                        <h6 class="mb-1 fw-semibold"><?php esc_html_e('Hours', 'paws-and-care'); ?></h6>
                                        <p class="mb-0 text-muted"><?php echo nl2br(esc_html($contact_hours)); ?></p>
                                    </div>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if ($show_emergency): ?>
                    <div class="card border-0 shadow-sm mb-4">
                        <div class="card-body p-4">
                            <h5 class="mb-4"><?php esc_html_e('Emergency Contact', 'paws-and-care'); ?></h5>
                            <div class="alert alert-warning">
                                <i class="fa-solid fa-triangle-exclamation me-2"></i>
                                <strong><?php echo esc_html($emergency_label); ?>:</strong><br>
                                <?php echo esc_html($emergency_phone); ?>
                            </div>
                            <?php if ($emergency_text): ?>
                                <p class="text-muted mb-0"><?php echo esc_html($emergency_text); ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>

<?php if ($show_map && !empty($map_embed)): ?>
    <!-- Map Section -->
    <section class="py-5 bg-light map-section">
        <div class="container">
            <div class="row">
                <div class="col-12">
                    <?php if ($map_title): ?>
                        <h3 class="text-center mb-4"><?php echo esc_html($map_title); ?></h3>
                    <?php endif; ?>
                    <div class="card border-0 shadow-sm">
                        <div class="card-body p-0">
                            <div class="map-container"
                                style="height: 400px; border-radius: var(--border-radius-lg); overflow: hidden;">
                                <iframe src="<?php echo esc_url($map_embed); ?>" width="100%" height="100%"
                                    style="border:0;" allowfullscreen="" loading="lazy"
                                    referrerpolicy="no-referrer-when-downgrade">
                                </iframe>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?php endif; ?>

<?php if ($show_faq): ?>
    <!-- FAQ Section -->
    <section class="py-5 faq-section">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-lg-8">
                    <?php if ($faq_title): ?>
                        <h3 class="text-center mb-5"><?php echo esc_html($faq_title); ?></h3>
                    <?php endif; ?>
                    <div class="accordion" id="faqAccordion">
                        <?php
                        $faq_count = 0;
                        foreach ($faq_items as $faq):
                            $faq_count++;
                            $icon = !empty($faq['icon_value']) ? $faq['icon_value'] : 'fa-question-circle';
                            $question = !empty($faq['title']) ? $faq['title'] : '';
                            $answer = !empty($faq['text']) ? $faq['text'] : '';
                            $faq_id = 'faq' . $faq_count;
                            $is_first = ($faq_count === 1);
                            ?>
                            <div class="accordion-item">
                                <h2 class="accordion-header">
                                    <button class="accordion-button<?php echo $is_first ? '' : ' collapsed'; ?>" type="button"
                                        data-bs-toggle="collapse" data-bs-target="#<?php echo esc_attr($faq_id); ?>">
                                        <i class="fa-solid <?php echo esc_attr($icon); ?> text-primary me-3"></i>
                                        <?php echo esc_html($question); ?>
                                    </button>
                                </h2>
                                <div id="<?php echo esc_attr($faq_id); ?>"
                                    class="accordion-collapse collapse<?php echo $is_first ? ' show' : ''; ?>"
                                    data-bs-parent="#faqAccordion">
                                    <div class="accordion-body">
                                        <?php echo nl2br(esc_html($answer)); ?>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>
    </section>
<?php endif; ?>

<?php get_footer(); ?>