<?php
/**
 * Theme footer template
 *
 * @package CWC
 * @since 1.0.0
 */

namespace CWC;

$footerLogo = getFieldValue( 'footer.footer_logo.url' ) ? getFieldValue( 'footer.footer_logo.url' ) : '';
$footerDesc = getFieldValue( 'footer.footer_description' ) ? getFieldValue( 'footer.footer_description' ) : '';

$copyright_text = getFieldValue( 'footer.copyright_text' ) ? getFieldValue( 'footer.copyright_text' ) : 'Copyright &copy; %Y ' . get_bloginfo( 'name' );

$copyright = str_replace( '%Y', gmdate( 'Y' ), $copyright_text );

$locations = get_nav_menu_locations();
$footerNav = ! empty( $locations['footer_navigation'] )
    ? ( wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) ? wp_get_nav_menu_items( (int) $locations['footer_navigation'] ) : array() )
    : array();
?>

	</main>

	<footer role="contentinfo" class="site-footer bg-cwc-blue-02 text-white pt-16 pb-12 text-base">
        <div class="container mx-auto grid grid-cols-4 gap-6">
            <div class="max-w-[40ch]">
                <h2 id="footer-header" class="max-w-64 h-auto">
                    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="site-footer__logo-link h-full w-full">
                        <?php if ( $footerLogo ) { ?>
                            <img src="<?php echo esc_url( $footerLogo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" class="site-footer__logo-image" />
                            <?php
                        } else {
                            echo esc_html( get_bloginfo( 'name' ) );
                        }
                        ?>
                    </a>
                </h2>
            </div>

            <div id="footer-nav">
                <?php if ( $footerNav ) : ?>
                    <nav class="site-footer__nav" aria-label="Footer navigation">
                        <ul class="site-footer__nav-list">
                            <?php foreach ( $footerNav as $item ) : ?>
                                <li class="site-footer__nav-item list-none text-left mb-3 <?php echo esc_attr( implode( ' ', $item->classes ) ); ?>">
                                    <a href="<?php echo esc_url( $item->url ); ?>" class="site-footer__nav-link font-bold">
                                        <?php echo esc_html( $item->title ); ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </nav>
                <?php endif; ?>
            </div>

            <div class="prose-p:text-balance">
                <div id="footer-description" class="" aria-label="Footer description">
                    <?php echo wp_kses_post( $footerDesc ); ?>
                </div>


                <div class="contact">
                    <?php
                    $address = getFieldValue( 'contact_info.address' ) ? getFieldValue( 'contact_info.address' ) : '';
                    $phone   = getFieldValue( 'contact_info.phone' ) ? getFieldValue( 'contact_info.phone' ) : '';
                    $email   = getFieldValue( 'contact_info.email' ) ? getFieldValue( 'contact_info.email' ) : '';
                    ?>

                    <?php if ( $address ) : ?>
                        <div class="contact__item contact__address mt-6">
                            <p><?php echo wp_kses_post( $address ); ?></p>
                        </div>
                        <?php
                    endif;

                    if ( $email ) :
                        ?>
                        <div class="contact__item contact__email mt-4">
                            <p><a href="mailto:<?php echo esc_attr( $email ); ?>" class="text-cwc-blue-01 hover:underline"><?php echo esc_html( $email ); ?></a></p>
                        </div>
                        <?php
                    endif;

                    if ( $phone ) :
                        ?>
                        <div class="contact__item contact__phone mt-4">
                            <p><a href="tel:<?php echo esc_attr( preg_replace( '/\D+/', '', $phone ) ); ?>" class="text-cwc-blue-01 hover:underline"><?php echo esc_html( $phone ); ?></a></p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>

            <div class="text-right flex flex-col justify-end items-end">
                <?php dynamic_sidebar( 'footer-1' ); ?>

                <x-back-to-top></x-back-to-top>

                <div class="social-links mt-12">
                    <?php
                    get_template_part(
                        'views/partials/social-media',
                        null,
                        array(
                            'circle'  => true,
                            'classes' => 'social-icons p-0 text-14px bg-white! text-cwc-blue-02! hover:opacity-80!',
                        )
                    );
                    ?>
                </div>

                <div class="copyright font-light text-white/70 text-right py-3 text-12px">
                    <?php
                    if ( $copyright ) {
                        echo wp_kses_post( $copyright );
                    } else {
                        echo esc_html( '&copy; ' . gmdate( 'Y' ) . ' ' . get_bloginfo( 'name' ) . '.' );
                    }
                    ?>
                </div>
            </div>
        </div>
	</footer>

	<?php wp_footer(); ?>
</body>
</html>
