<?php
/**
 * Footer Block - Server-Side Render
 * Template: Child Author
 * 
 * @package Author_Website_Templates
 */

if (!defined('ABSPATH')) {
    exit;
}

// Extract attributes
$logo_text = isset($attributes['logoText']) ? $attributes['logoText'] : 'Danielle Steel';
$menu1_slug = isset($attributes['menu1Slug']) ? $attributes['menu1Slug'] : '';
$menu1_title = isset($attributes['menu1Title']) ? $attributes['menu1Title'] : 'Explore';
$menu2_slug = isset($attributes['menu2Slug']) ? $attributes['menu2Slug'] : '';
$menu2_title = isset($attributes['menu2Title']) ? $attributes['menu2Title'] : 'Say Hello!';
$copyright_text = isset($attributes['copyrightText']) ? $attributes['copyrightText'] : 'All the fun reserved.';
$footer_style = isset($attributes['footerStyle']) ? $attributes['footerStyle'] : 'fun';
$bio_text = isset($attributes['bioText']) ? $attributes['bioText'] : '';

$logo_type = isset($attributes['logoType']) ? $attributes['logoType'] : 'custom';
$logo_image_url = isset($attributes['logoImageUrl']) ? $attributes['logoImageUrl'] : '';
$logo_image_id = isset($attributes['logoImageId']) ? $attributes['logoImageId'] : 0;

$show_curve = isset($attributes['showCurve']) ? $attributes['showCurve'] : true;
$curve_color = isset($attributes['curveColor']) ? $attributes['curveColor'] : '#FFF8E7';

$is_elegant = $footer_style === 'elegant';

// Default Colors based on Style
$default_bg = $is_elegant ? '#1e293b' : '#264653';
$default_text = $is_elegant ? '#94a3b8' : '#FFFDF5';
$default_heading = $is_elegant ? '#FFFFFF' : '#FFD166';

// User Overrides
$footer_bg_color = isset($attributes['footerBgColor']) && !empty($attributes['footerBgColor']) ? $attributes['footerBgColor'] : $default_bg;
$text_color = isset($attributes['textColor']) && !empty($attributes['textColor']) ? $attributes['textColor'] : $default_text;
$heading_color = isset($attributes['headingColor']) && !empty($attributes['headingColor']) ? $attributes['headingColor'] : $default_heading;

$footer_title_color = isset($attributes['footerTitleColor']) ? $attributes['footerTitleColor'] : '';
$footer_title_hover_color = isset($attributes['footerTitleHoverColor']) ? $attributes['footerTitleHoverColor'] : '';
$social_icon_color = isset($attributes['socialIconColor']) ? $attributes['socialIconColor'] : '';
$social_icon_hover_color = isset($attributes['socialIconHoverColor']) ? $attributes['socialIconHoverColor'] : '';

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : uniqid('footer-');

// Fetch menu items
$menu1_items = !empty($menu1_slug) ? wp_get_nav_menu_items($menu1_slug) : [];
$menu2_items = !empty($menu2_slug) ? wp_get_nav_menu_items($menu2_slug) : [];

// Get current year
$current_year = date('Y');

// Determine Logo Content
$logo_content = '';
if ($logo_type === 'image' && !empty($logo_image_url)) {
    $logo_content = sprintf(
        '<img src="%s" alt="%s" class="awt-footer-logo-img w-48 mx-auto md:mx-0 h-auto" />',
        esc_url($logo_image_url),
        esc_attr($logo_text)
    );
} elseif ($logo_type === 'site_title') {
    $logo_content = get_bloginfo('name');
} else {
    $logo_content = esc_html($logo_text);
}


// CSS Variables for dynamic styling
$css_vars = "style=\"
    --awt-footer-bg: {$footer_bg_color}; 
    --awt-footer-text: {$text_color}; 
    --awt-footer-heading: {$heading_color}; 
    --awt-title-color: " . ($footer_title_color ? $footer_title_color : 'inherit') . ";
    --awt-title-hover: " . ($footer_title_hover_color ? $footer_title_hover_color : ($footer_title_color ? $footer_title_color : 'inherit')) . ";
    --awt-social-color: " . ($social_icon_color ? $social_icon_color : $text_color) . ";
    --awt-social-hover: " . ($social_icon_hover_color ? $social_icon_hover_color : $heading_color) . ";
    --awt-heading-color: {$heading_color};
    --awt-curve-color: {$curve_color};
    background-color: var(--awt-footer-bg); 
    color: var(--awt-footer-text);
\"";

// Scoped Styles for Hover States
$scoped_style = "<style>
    .block-{$block_id} .awt-footer-title { color: var(--awt-title-color) !important; }
    .block-{$block_id} .awt-footer-title:hover { color: var(--awt-title-hover) !important; }
    .block-{$block_id} .awt-social-icon { color: var(--awt-social-color) !important; }
    .block-{$block_id} .awt-social-icon:hover { color: var(--awt-social-hover) !important; }
    .block-{$block_id} .awt-menu-heading { color: var(--awt-heading-color) !important; }
</style>";

// Get Social Icons from Global Settings
if (function_exists('rswpthemes_awt_get_social_icons')) {
    $social_icons_html = rswpthemes_awt_get_social_icons($footer_style, [
        'color' => $social_icon_color, // Passed for reference, but usually handled by CSS class/vars
    ]);
} else {
    $social_icons_html = '<p class="text-xs opacity-50">Social icons unavailable.</p>';
}

?>

<?php echo $scoped_style; ?>

<footer class="awt-child-author-footer pt-20 pb-10 relative block-<?php echo esc_attr($block_id); ?>" <?php echo $css_vars; ?>>
    
    <?php if (!$is_elegant && $show_curve): ?>
    <!-- SVG Wave Decorator (Fun Style Only) -->
    <div class="absolute top-0 left-0 w-full">
        <svg viewBox="0 0 1440 100" fill="none" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none">
            <path d="M0 0H1440V100C1192.5 50.503 919.5 25.503 720 25.503C520.5 25.503 247.5 50.503 0 100V0Z"
                fill="var(--awt-curve-color)" />
        </svg>
    </div>
    <?php endif; ?>

    <div class="awt-container container mx-auto px-6 relative z-10">
        
        <?php if ($is_elegant): ?>
            <!-- ELEGANT LAYOUT -->
            <div class="grid md:grid-cols-3 gap-12 text-center md:text-left">
                <!-- Column 1: Author, Bio, Socials -->
                <div>
                    <a href="#" class="awt-footer-title text-3xl font-serif font-bold mb-6 block" style="color: var(--awt-title-color);">
                        <?php echo $logo_content; ?>
                    </a>
                    
                    <?php if (!empty($bio_text)): ?>
                        <p class="mb-6 opacity-80 text-sm leading-relaxed max-w-sm mx-auto md:mx-0">
                            <?php echo wp_kses_post($bio_text); ?>
                        </p>
                    <?php endif; ?>

                    <div class="flex space-x-6 justify-center md:justify-start">
                         <?php echo $social_icons_html; ?>
                    </div>
                </div>

                <!-- Column 2: Menu 1 -->
                <div>
                     <h5 class="awt-menu-heading font-sans text-xs font-bold uppercase tracking-wider mb-4" style="color: var(--awt-heading-color);"><?php echo esc_html($menu1_title); ?></h5>
                     <?php if (!empty($menu1_items)): ?>
                        <ul class="space-y-3 list-none text-sm">
                            <?php foreach ($menu1_items as $item): ?>
                                <li><a href="<?php echo esc_url($item->url); ?>" class="hover:text-white transition-colors" style="color: var(--awt-footer-text);"><?php echo esc_html($item->title); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>

                <!-- Column 3: Menu 2 -->
                <div>
                     <h5 class="awt-menu-heading font-sans text-xs font-bold uppercase tracking-wider mb-4" style="color: var(--awt-heading-color);"><?php echo esc_html($menu2_title); ?></h5>
                     <?php if (!empty($menu2_items)): ?>
                        <ul class="space-y-3 list-none text-sm">
                            <?php foreach ($menu2_items as $item): ?>
                                <li><a href="<?php echo esc_url($item->url); ?>" class="hover:text-white transition-colors" style="color: var(--awt-footer-text);"><?php echo esc_html($item->title); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </div>
            </div>

            <div class="border-t border-gray-800 mt-12 pt-8 text-center text-xs text-gray-400 opacity-50">
                <p>&copy; <?php echo esc_html($current_year); ?> <?php echo esc_html($logo_text); ?>.
                    <?php echo esc_html($copyright_text); ?></p>
            </div>

        <?php else: ?>
            <!-- FUN LAYOUT (Default) -->
             <div class="grid md:grid-cols-3 gap-12 text-center md:text-left">
                 <!-- Column 1: Logo & Social -->
                 <div>
                    <a href="#" class="awt-footer-title text-3xl font-black mb-6 block" style="color: var(--awt-footer-heading);">
                        <?php echo $logo_content; ?>
                    </a>
                    <div class="flex space-x-6 justify-center md:justify-start">
                         <?php echo $social_icons_html; ?>
                    </div>
                 </div>

                 <!-- Menus (Fun Style) -->
                 <div>
                     <h5 class="awt-menu-heading font-sans text-sm font-bold uppercase tracking-wider mb-4" style="color: var(--awt-heading-color);"><?php echo esc_html($menu1_title); ?></h5>
                     <?php if (!empty($menu1_items)): ?>
                        <ul class="space-y-3 list-none text-center md:text-left p-0">
                            <?php foreach ($menu1_items as $item): ?>
                                <li><a href="<?php echo esc_url($item->url); ?>" class="hover:text-white transition-colors" style="color: var(--awt-footer-text);"><?php echo esc_html($item->title); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                 </div>
                 
                 <div>
                     <h5 class="awt-menu-heading font-sans text-sm font-bold uppercase tracking-wider mb-4" style="color: var(--awt-heading-color);"><?php echo esc_html($menu2_title); ?></h5>
                     <?php if (!empty($menu2_items)): ?>
                        <ul class="space-y-3 list-none text-center md:text-left p-0">
                            <?php foreach ($menu2_items as $item): ?>
                                <li><a href="<?php echo esc_url($item->url); ?>" class="hover:text-white transition-colors" style="color: var(--awt-footer-text);"><?php echo esc_html($item->title); ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                 </div>
             </div>

             <div class="border-t border-cream/20 mt-12 pt-8 text-center">
                 <p class="text-sm" style="color: var(--awt-footer-text);">
                     Copyright &copy; <?php echo $current_year; ?> <?php echo esc_html($logo_text); ?>.
                     <?php echo esc_html($copyright_text); ?>
                 </p>
             </div>

        <?php endif; ?>
    </div>
</footer>