<?php
/**
 * @package Unique Gift Shop
 */

/**
 * Footer
 */
if (! function_exists( 'unique_gift_shop_footer_copyrights' ) ):
    function unique_gift_shop_footer_copyrights() {
        ?>
            <div class="row">
                <div class="copyrights">
                    <p>
                        <?php
                            if("" != esc_html(get_theme_mod( 'unique_gift_shop_footer_copyright_text'))) :
                                echo esc_html(get_theme_mod( 'unique_gift_shop_footer_copyright_text'));
                                if(get_theme_mod('unique_gift_shop_en_footer_credits',true)) :
                                    ?> 
                                    <span class="copyrg-link"><a href="<?php echo esc_url(UNIQUE_GIFT_SHOP_AUT); ?>" target="_blank" rel="nofollow noopener"><?php esc_html_e(' | Gift Shop WordPress Theme','unique-gift-shop') ?></a><?php esc_html_e(' by Legacy Themes','unique-gift-shop') ?></span>
                                    <?php   
                                endif;
                            else :
                                echo date_i18n(
                                    /* translators: Copyright date format, see https://secure.php.net/date */
                                    _x( 'Y', 'copyright date format', 'unique-gift-shop' )
                                );
                                ?>
                                    <a href="<?php echo esc_url( home_url( '/' ) ); ?>"><?php bloginfo( 'name' ); ?></a>
                                    <span class="copyrg-link"><a href="<?php echo esc_url(UNIQUE_GIFT_SHOP_AUT); ?>" target="_blank" rel="nofollow noopener"><?php esc_html_e(' | Gift Shop WordPress Theme','unique-gift-shop') ?></a><?php esc_html_e(' by Legacy Themes','unique-gift-shop') ?></span>
                                <?php
                            endif;
                        ?>
                    </p>
                </div>
            </div>
        <?php    
    }
endif;
add_action( 'unique_gift_shop_action_footer', 'unique_gift_shop_footer_copyrights' );

/**
 * Page Title Settings
 */
if ( ! function_exists( 'unique_gift_shop_show_page_title' ) ) :
    function unique_gift_shop_show_page_title() {
        if ( ! is_front_page() ) {
            ?>
            <div class="page-title"> 
                <div class="content-section img-overlay">
                    <div class="container">
                        <div class="row text-center">
                            <div class="col-md-12">
                                <div class="section-title"> 
                                    <?php
                                    // WooCommerce Pages
                                    if ( function_exists( 'is_shop' ) && is_shop() ) {
                                        // Shop Page
                                        echo '<h1 class="main-title">' . esc_html__( 'Shop Page', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( function_exists( 'is_product' ) && is_product() ) {
                                        // Single Product Page
                                        echo '<h1 class="main-title">' . esc_html__( 'Single Product Page', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( function_exists( 'is_checkout' ) && is_checkout() ) {
                                        // Checkout Page
                                        echo '<h1 class="main-title">' . esc_html__( 'Checkout', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( function_exists( 'is_account_page' ) && is_account_page() ) {
                                        // My Account Page
                                        echo '<h1 class="main-title">' . esc_html__( 'My Account', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( is_home() ) {
                                        // Blog Index Page
                                        echo '<h1 class="main-title">' . esc_html__( 'Blog Page', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( is_singular( 'post' ) ) {
                                        // Single Blog Post
                                        echo '<h1 class="main-title">' . get_the_title() . '</h1>';

                                    } elseif ( is_category() ) {
                                        echo '<h1 class="main-title">' . single_cat_title( '', false ) . '</h1>';

                                    } elseif ( is_tag() ) {
                                        echo '<h1 class="main-title">' . single_tag_title( '', false ) . '</h1>';

                                    } elseif ( is_author() ) {
                                        echo '<h1 class="main-title">' . get_the_author() . '</h1>';

                                    } elseif ( is_archive() ) {
                                        echo '<h1 class="main-title">' . get_the_archive_title() . '</h1>';

                                    } elseif ( is_search() ) {
                                        echo '<h1 class="main-title">' . esc_html__( 'Search Results', 'unique-gift-shop' ) . '</h1>';

                                    } elseif ( is_404() ) {
                                        echo '<h1 class="main-title">' . esc_html__( 'Page Not Found', 'unique-gift-shop' ) . '</h1>';

                                    } else {
                                        // Default for all other pages
                                        echo '<h1 class="main-title">' . get_the_title() . '</h1>';
                                    }
                                    ?>
                                </div>                      
                            </div>
                        </div>
                    </div>  
                </div>
            </div>  <!-- End page-title --> 
            <?php
        }
    }
endif;

// Register page title action globally
add_action( 'unique_gift_shop_get_page_title', 'unique_gift_shop_show_page_title' );

/**
 * Home Banner Section
 */
if (! function_exists( 'unique_gift_shop_home_banner_section' ) ):
    function unique_gift_shop_home_banner_section() {
        ?>
        <section id="main-banner-wrap">
            <div class="container">
                <div class="owl-carousel">
                    <?php $unique_gift_shop_banner_count = get_theme_mod("unique_gift_shop_banner_slider_increase");
                        for ($i = 1; $i <= $unique_gift_shop_banner_count; $i++) { ?>
                        <div class="bnr-slide-box">
                            <div class="row">
                              <div class="col-lg-4 col-md-5 col-12">
                                  <div class="banner-side-margin position-relative">
                                    <div class="main-banner-inner-box pe-lg-5">
                                        <?php
                                        $unique_gift_shop_alignment_class = get_theme_mod( 'unique_gift_shop_slider_content_alignment', 'center' );
                                        ?>
                                        <div class="p-3 main-banner-content-box content-<?php echo esc_attr( $unique_gift_shop_alignment_class ); ?>">
                                            <?php
                                            $unique_gift_shop_banner_heading = get_theme_mod( 'unique_gift_shop_banner_heading' . $i, '' );
                                            if ( ! empty( $unique_gift_shop_banner_heading ) ) { ?>
                                                <h2 class="bnr-hd1 pt-2 mb-2"><?php echo esc_html( unique_gift_shop_get_text_excerpt( $unique_gift_shop_banner_heading, 4 ) ); ?></h2>
                                            <?php } ?>    
                                            <?php
                                            $unique_gift_shop_banner_text = get_theme_mod( 'unique_gift_shop_banner_text' . $i, '' );
                                            if ( ! empty( $unique_gift_shop_banner_text ) ) { ?>
                                                <p class="bnr-txt pb-3 pt-2"><?php echo esc_html( unique_gift_shop_get_text_excerpt( $unique_gift_shop_banner_text, 15 ) ); ?></p>
                                            <?php } ?>

                                            <div class="btn-box-slid">
                                                <?php
                                                $unique_gift_shop_banner_button_link = get_theme_mod( 'unique_gift_shop_banner_button_link'.$i, '' );
                                                    if ( ! empty( $unique_gift_shop_banner_button_link ) ) { ?>
                                                    <a class="btn-slid btn" href="<?php echo esc_url( $unique_gift_shop_banner_button_link ); ?>"><?php echo esc_html('Discover','unique-gift-shop'); ?></a>
                                                <?php } ?>
                                            </div>
                                        </div>
                                    </div>
                                  </div>
                              </div>
                              <div class="col-lg-3 col-md-2 col-12">
                                <?php
                                $unique_gift_shop_banner_image = get_theme_mod( 'unique_gift_shop_banner_image'.$i, '' );
                                if ( ! empty( $unique_gift_shop_banner_image ) ) { ?>
                                <div class="banr-center-img pb-4 pt-2 pb-lg-0">
                                    <img src="<?php echo esc_url( $unique_gift_shop_banner_image ); ?>">
                                </div>
                                <?php } ?>
                              </div>
                              <div class="col-lg-5 col-md-5 col-12 text-center text-lg-start text-md-start">
                                  <div class="right-text-box px-5 mx-lg-5">
                                     <?php
                                    $unique_gift_shop_banner_ri8_text1 = get_theme_mod( 'unique_gift_shop_banner_ri8_text1' . $i, '' );
                                    if ( ! empty( $unique_gift_shop_banner_ri8_text1 ) ) { ?>
                                        <p class="bnr-right-txt1"><?php echo esc_html( unique_gift_shop_get_text_excerpt( $unique_gift_shop_banner_ri8_text1, 25 ) ); ?></p>
                                    <?php } ?>

                                    <?php
                                    $unique_gift_shop_banner_ri8_text2 = get_theme_mod( 'unique_gift_shop_banner_ri8_text2' . $i, '' );
                                    if ( ! empty( $unique_gift_shop_banner_ri8_text2 ) ) { ?>
                                        <p class="bnr-right-txt2"><?php echo esc_html( unique_gift_shop_get_text_excerpt( $unique_gift_shop_banner_ri8_text2, 25 ) ); ?></p>
                                    <?php } ?> 
                                  </div>
                              </div>
                            </div>
                        </div>
                    <?php } ?>
                </div>
            </div>
        </section>
        <?php
    }
endif;
add_action( 'unique_gift_shop_action_home_banner', 'unique_gift_shop_home_banner_section' );

/**
 * Home product Section
 */
if (! function_exists( 'unique_gift_shop_home_product_section' ) ):
    function unique_gift_shop_home_product_section() {
        ?>
        <section id="product-wrap" class="pt-5">
            <div class="container">
                <div class="produ-head-box text-center">
                    <?php
                        $unique_gift_shop_product_main_heading = get_theme_mod( 'unique_gift_shop_product_main_heading', '' );
                        if ( ! empty( $unique_gift_shop_product_main_heading ) ) { ?>
                            <h2 class="prod-main-hd py-4 mb-5"><?php echo esc_html( $unique_gift_shop_product_main_heading ); ?></h2>
                    <?php } ?>
                </div>
                <?php if ( class_exists( 'WooCommerce' ) ) { ?>
                    <div class="product-box">
                        <div class="owl-carousel">
                            <?php
                                $unique_gift_shop_selected_category = get_theme_mod('unique_gift_shop_product_category', '');
                                
                                $unique_gift_shop_product_count = get_theme_mod('unique_gift_shop_product_count', 4);
                                $args = array(
                                    'post_type'      => 'product',
                                    'posts_per_page' => $unique_gift_shop_product_count,
                                    'order'          => 'ASC',
                                    'tax_query'      => !empty($unique_gift_shop_selected_category) ? array(
                                        array(
                                            'taxonomy' => 'product_cat',
                                            'field'    => 'slug',
                                            'terms'    => $unique_gift_shop_selected_category,),) : '',
                                );
                                $loop = new WP_Query( $args );
                                while ( $loop->have_posts() ) : $loop->the_post(); global $product;
                            ?> 
                            <div class="product-inn inner-project text-center mb-3">
                                <div class="prod-img-box">
                                    <?php if ( $product->is_on_sale() ) : 
                                    $regular_price = (float) $product->get_regular_price();
                                    $sale_price    = (float) $product->get_sale_price();

                                    if ( $regular_price > 0 && $sale_price < $regular_price ) {
                                        $discount = round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 );
                                        ?>
                                        <span class="sale-tag"><?php echo esc_html( $discount ); ?>% off</span>
                                    <?php }
                                    endif; ?>
                                    <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, ''); else echo '<img src="'.esc_url(wc_placeholder_img_src()).'" />'; ?>
                                </div>
                                <div class="prod-content-box p-3 my-2 mx-1">
                                    <div class="row">
                                        <div class="col-lg-9 col-md-9 col-12 align-self-center">
                                            <h3 class="product-name mb-0 pt-1 text-lg-start"><a href="<?php echo esc_url(get_permalink( $loop->post->ID )); ?>"><?php the_title(); ?></a></h3>
                                        </div>
                                        <div class="col-lg-3 col-md-3 col-12 align-self-center rtng">
                                            <div class="prod-star-rating">
                                                <?php
                                                $average = $product->get_average_rating();
                                                if ( $average > 0 ) {
                                                    echo '<span class="single-star"><i class="bi bi-star-fill me-1"></i></span>';
                                                    echo '<span class="rating-number ml-1">' . esc_html( number_format( $average, 1 ) ) . '</span>';
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="price-prod">
                                        <?php
                                        if ( $product->is_on_sale() ) {
                                            echo '<span class="sale-price">' . wc_price( $product->get_sale_price() ) . '</span> ';
                                            echo '<span class="regular-price"><del>' . wc_price( $product->get_regular_price() ) . '</del></span>';
                                        } else {
                                            echo '<span class="regular-price">' . wc_price( $product->get_price() ) . '</span>';
                                        }
                                        ?>
                                    </div>
                                    
                                    <div class="prod-cart-btn py-2">
                                        <?php woocommerce_template_loop_add_to_cart(); ?>
                                    </div>
                                </div>
                            </div>                                
                            <?php endwhile; wp_reset_postdata(); ?>
                        </div>
                    </div>
                <?php } ?>            
            </div>
        </section>
        <?php    
    }
endif;
add_action( 'unique_gift_shop_action_home_product', 'unique_gift_shop_home_product_section' );

/**
 * Home page another adding Section
 */
if (! function_exists( 'unique_gift_shop_home_extra_section' ) ):
    function unique_gift_shop_home_extra_section() {
        ?>
        <div id="custom-home-extra-content" class="py-3">
            <div class="container">
              <?php while ( have_posts() ) : the_post(); ?>
                <?php the_content(); ?>
              <?php endwhile; ?>
            </div>
        </div>
        <?php    
    }
endif;
add_action( 'unique_gift_shop_action_home_extra', 'unique_gift_shop_home_extra_section' );