<?php
/**
 * Cart Page
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/cart/cart.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 7.9.0
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_before_cart' ); ?>

<div class="container">
    <div class="row cs_gap_y_40">
        <div class="col-lg-7">
            <h2 class="cs_fs_36 cs_secondary_font cs_medium mb-0"><?php esc_html_e( 'MY CART', 'glowify-wp' ); ?></h2>
            <div class="cs_height_40 cs_height_lg_40"></div>
            
            <form class="woocommerce-cart-form" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
                <?php do_action( 'woocommerce_before_cart_table' ); ?>
                
                <?php
                $cart_items_count = WC()->cart->get_cart_contents_count();
                if ( $cart_items_count > 0 ) : ?>
                <div class="cs_cart_list_top">
                    <div class="cs_custom_checkbox cs_style_1">
                        <input type="checkbox" id="select_all_items">
                        <span><?php esc_html_e( 'SELECT ALL', 'glowify-wp' ); ?> <b class="cs_medium cs_primary_color">(<?php echo esc_html( $cart_items_count ); ?> <?php echo _n( 'ITEM', 'ITEMS', $cart_items_count, 'glowify-wp' ); ?>)</b></span>
                    </div>
                    <p class="mb-0">
                        <button type="button" class="cs_delete_btn" id="delete_selected_items"><i class="fa-regular fa-trash-can"></i> <?php esc_html_e( 'Delete', 'glowify-wp' ); ?></button>
                    </p>
                </div>
                <?php endif; ?>
                
                <ul class="cs_cart_list cs_mp_0">
                    <?php do_action( 'woocommerce_before_cart_contents' ); ?>

                    <?php
                    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                        $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

                        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
                            $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
                            ?>
                            <li class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">
                                <div class="cs_custom_checkbox cs_style_1 cs_type_1">
                                    <input type="checkbox" class="cart_item_checkbox" data-key="<?php echo esc_attr( $cart_item_key ); ?>">
                                    <span></span>
                                </div>
                                <div class="cs_product_card cs_style_11 cs_radius_10">
                                    <?php if ( $product_permalink ) : ?>
                                        <a href="<?php echo esc_url( $product_permalink ); ?>" class="cs_product_thumb">
                                            <?php echo $_product->get_image(); ?>
                                        </a>
                                    <?php else : ?>
                                        <div class="cs_product_thumb">
                                            <?php echo $_product->get_image(); ?>
                                        </div>
                                    <?php endif; ?>
                                    
                                    <div class="cs_product_card_right">
                                        <?php
                                        // Display product categories if available
                                        $product_cats = get_the_terms( $product_id, 'product_cat' );
                                        if ( $product_cats && ! is_wp_error( $product_cats ) ) {
                                            $first_cat = reset( $product_cats );
                                            ?>
                                            <div class="cs_product_category text-uppercase cs_fs_14 cs_light">
                                                <a href="<?php echo esc_url( get_term_link( $first_cat->term_id, 'product_cat' ) ); ?>"><?php echo esc_html( $first_cat->name ); ?></a>
                                            </div>
                                        <?php } ?>
                                        
                                        <h2 class="cs_product_title cs_fs_24 cs_medium cs_secondary_font">
                                            <?php
                                            if ( ! $product_permalink ) {
                                                echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ) );
                                            } else {
                                                echo wp_kses_post( apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_name() ), $cart_item, $cart_item_key ) );
                                            }
                                            ?>
                                        </h2>
                                        
                                        <?php
                                        // Meta data
                                        echo wc_get_formatted_cart_item_data( $cart_item );
                                        
                                        // Backorder notification
                                        if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
                                            echo wp_kses_post( apply_filters( 'woocommerce_cart_item_backorder_notification', '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'glowify-wp' ) . '</p>', $product_id ) );
                                        }
                                        ?>
                                        
                                        <p class="cs_product_price cs_fs_24 cs_primary_color cs_medium cs_accent_color">
                                            <?php
                                                echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                                            ?>
                                        </p>
                                        
                                        <div class="cs_quantity cs_style_1">
                                            <?php
                                            if ( $_product->is_sold_individually() ) {
                                                $min_quantity = 1;
                                                $max_quantity = 1;
                                                $input_value = 1;
                                            } else {
                                                $min_quantity = 0;
                                                $max_quantity = $_product->get_max_purchase_quantity();
                                                $input_value = $cart_item['quantity'];
                                            }
                                            ?>
                                            <button type="button" class="cs_quantity_button cs_decrement" data-key="<?php echo esc_attr( $cart_item_key ); ?>">
                                                <svg width="12" height="2" viewBox="0 0 12 2" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                    <path d="M0.580007 1.18555C0.425021 1.18215 0.277528 1.11823 0.169117 1.00742C0.0607065 0.896604 0 0.74771 0 0.592687C0 0.437664 0.0607065 0.288839 0.169117 0.178028C0.277528 0.0672157 0.425021 0.00322354 0.580007 -0.000172257H11.249C11.404 0.00322354 11.5515 0.0672157 11.6599 0.178028C11.7683 0.288839 11.829 0.437664 11.829 0.592687C11.829 0.74771 11.7683 0.896604 11.6599 1.00742C11.5515 1.11823 11.404 1.18215 11.249 1.18555H0.580007Z" fill="currentColor"></path>
                                                </svg>                          
                                            </button>
                                            <input type="number" 
                                                id="qty_<?php echo esc_attr( $cart_item_key ); ?>"
                                                class="cs_quantity_input cs_primary_color cs_medium"
                                                name="cart[<?php echo esc_attr( $cart_item_key ); ?>][qty]"
                                                value="<?php echo esc_attr( $input_value ); ?>"
                                                title="<?php esc_attr_e( 'Qty', 'glowify-wp' ); ?>"
                                                min="<?php echo esc_attr( $min_quantity ); ?>"
                                                <?php if ( $max_quantity > 0 ) : ?>
                                                    max="<?php echo esc_attr( $max_quantity ); ?>"
                                                <?php endif; ?>
                                                readonly
                                            />
                                            <button type="button" class="cs_quantity_button cs_increment" data-key="<?php echo esc_attr( $cart_item_key ); ?>">
                                                <svg width="15" height="15" viewBox="0 0 15 15" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                    <path d="M14.4141 6.91406H8.08594V0.585938C8.08594 0.262324 7.82361 0 7.5 0C7.17639 0 6.91406 0.262324 6.91406 0.585938V6.91406H0.585938C0.262324 6.91406 0 7.17639 0 7.5C0 7.82361 0.262324 8.08594 0.585938 8.08594H6.91406V14.4141C6.91406 14.7377 7.17639 15 7.5 15C7.82361 15 8.08594 14.7377 8.08594 14.4141V8.08594H14.4141C14.7377 8.08594 15 7.82361 15 7.5C15 7.17639 14.7377 6.91406 14.4141 6.91406Z" fill="currentColor"></path>
                                                </svg>                          
                                            </button>
                                        </div>
                                        
                                        <?php if ( class_exists( 'YITH_WCWL' ) ) : ?>
                                        <button type="button" class="cs_like_btn cs_center cs_accent_color add_to_wishlist" data-product-id="<?php echo esc_attr( $product_id ); ?>">
                                            <svg width="20" height="18" viewBox="0 0 20 18" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M10 17.8873C9.71527 17.8873 9.44077 17.7842 9.22684 17.5968C8.41888 16.8903 7.63992 16.2264 6.95267 15.6408L6.94916 15.6377C4.93423 13.9207 3.19427 12.4378 1.98364 10.9771C0.630341 9.34409 0 7.79578 0 6.10434C0 4.46097 0.563507 2.94485 1.58661 1.83508C2.62192 0.712189 4.04251 0.09375 5.58716 0.09375C6.74164 0.09375 7.79892 0.45874 8.72955 1.1785C9.19922 1.54181 9.62494 1.98645 10 2.5051C10.3752 1.98645 10.8008 1.54181 11.2706 1.1785C12.2012 0.45874 13.2585 0.09375 14.413 0.09375C15.9575 0.09375 17.3782 0.712189 18.4135 1.83508C19.4366 2.94485 20 4.46097 20 6.10434C20 7.79578 19.3698 9.34409 18.0165 10.9769C16.8059 12.4378 15.0661 13.9205 13.0515 15.6374C12.363 16.224 11.5828 16.8889 10.773 17.5971C10.5592 17.7842 10.2846 17.8873 10 17.8873ZM5.58716 1.26532C4.37363 1.26532 3.25882 1.74963 2.44781 2.62915C1.62476 3.52194 1.17142 4.75607 1.17142 6.10434C1.17142 7.52692 1.70013 8.79919 2.88559 10.2296C4.03137 11.6122 5.73563 13.0645 7.70889 14.7462L7.71255 14.7492C8.4024 15.3371 9.18442 16.0036 9.99832 16.7153C10.8171 16.0023 11.6003 15.3347 12.2916 14.7458C14.2647 13.0642 15.9688 11.6122 17.1146 10.2296C18.2999 8.79919 18.8286 7.52692 18.8286 6.10434C18.8286 4.75607 18.3752 3.52194 17.5522 2.62915C16.7413 1.74963 15.6264 1.26532 14.413 1.26532C13.524 1.26532 12.7078 1.54791 11.9872 2.10516C11.3449 2.60199 10.8975 3.23004 10.6352 3.66949C10.5003 3.89548 10.2629 4.03036 10 4.03036C9.73709 4.03036 9.49966 3.89548 9.36478 3.66949C9.10263 3.23004 8.65524 2.60199 8.01285 2.10516C7.29218 1.54791 6.47598 1.26532 5.58716 1.26532Z" fill="currentColor"></path>
                                            </svg>                            
                                            <svg width="21" height="19" viewBox="0 0 21 19" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M10.4135 18.6287C10.1288 18.6287 9.85431 18.5255 9.64038 18.3381C8.83243 17.6317 8.05347 16.9677 7.36621 16.3821L7.3627 16.3791C5.34778 14.662 3.60782 13.1791 2.39719 11.7184C1.04388 10.0854 0.413544 8.53711 0.413544 6.84567C0.413544 5.2023 0.977051 3.68619 2.00015 2.57642C3.03546 1.45352 4.45605 0.835083 6.0007 0.835083C7.15518 0.835083 8.21246 1.20007 9.1431 1.91983C9.61276 2.28314 10.0385 2.72778 10.4135 3.24643C10.7888 2.72778 11.2143 2.28314 11.6841 1.91983C12.6148 1.20007 13.6721 0.835083 14.8265 0.835083C16.371 0.835083 17.7918 1.45352 18.8271 2.57642C19.8502 3.68619 20.4135 5.2023 20.4135 6.84567C20.4135 8.53711 19.7834 10.0854 18.4301 11.7183C17.2194 13.1791 15.4796 14.6618 13.465 16.3788C12.7765 16.9653 11.9963 17.6303 11.1866 18.3384C10.9728 18.5255 10.6981 18.6287 10.4135 18.6287Z" fill="currentColor"></path>
                                            </svg>                            
                                        </button>
                                        <?php endif; ?>
                                        <?php
										echo apply_filters( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
												'woocommerce_cart_item_remove_link',
												sprintf(
													'<a href="%s" class="remove cs_product_card_close" aria-label="%s" data-product_id="%s" data-product_sku="%s"><svg width="21" height="21" viewBox="0 0 21 21" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                <path d="M1.02556 21C0.822736 21 0.624454 20.9399 0.455797 20.8272C0.287141 20.7146 0.155687 20.5544 0.0780629 20.367C0.000439235 20.1796 -0.0198669 19.9734 0.0197131 19.7745C0.0592931 19.5756 0.156981 19.3929 0.30042 19.2495L19.2493 0.300367C19.4416 0.108045 19.7024 0 19.9744 0C20.2464 0 20.5072 0.108045 20.6996 0.300367C20.8919 0.49269 20.9999 0.753534 20.9999 1.02552C20.9999 1.2975 20.8919 1.55835 20.6996 1.75067L1.75071 20.6998C1.65557 20.7951 1.54253 20.8707 1.41809 20.9222C1.29364 20.9737 1.16025 21.0002 1.02556 21Z" fill="currentColor"/>
                                                <path d="M19.9744 21C19.8397 21.0002 19.7063 20.9737 19.5818 20.9222C19.4574 20.8707 19.3444 20.7951 19.2492 20.6998L0.300364 1.75067C0.108044 1.55835 0 1.2975 0 1.02552C0 0.753534 0.108044 0.49269 0.300364 0.300367C0.492684 0.108045 0.753525 0 1.02551 0C1.29749 0 1.55833 0.108045 1.75065 0.300367L20.6995 19.2495C20.843 19.3929 20.9406 19.5756 20.9802 19.7745C21.0198 19.9734 20.9995 20.1796 20.9219 20.367C20.8442 20.5544 20.7128 20.7146 20.5441 20.8272C20.3755 20.9399 20.1772 21 19.9744 21Z" fill="currentColor"/>
                                                </svg></a>',
													esc_url( wc_get_cart_remove_url( $cart_item_key ) ),
													/* translators: %s is the product name */
													esc_attr( sprintf( __( 'Remove %s from cart', 'glowify-wp' ), wp_strip_all_tags( $_product->get_name()) ) ),
													esc_attr( $product_id ),
													esc_attr( $_product->get_sku() )
												),
												$cart_item_key
											);
										?>
                                    </div>
                                </div>
                            </li>
                            <?php
                        }
                    }
                    ?>

                    <?php do_action( 'woocommerce_cart_contents' ); ?>
                    <?php do_action( 'woocommerce_after_cart_contents' ); ?>
                </ul>
                
                <div class="cs_height_40"></div>
                <div class="actions">
                    <button type="submit" class="cs_btn cs_style_1 cs_fs_18 w-100" name="update_cart" value="<?php esc_attr_e( 'Update cart', 'glowify-wp' ); ?>"><?php esc_html_e( 'UPDATE CART', 'glowify-wp' ); ?></button>
                    <?php do_action( 'woocommerce_cart_actions' ); ?>
                    <?php wp_nonce_field( 'woocommerce-cart', 'woocommerce-cart-nonce' ); ?>
                </div>
                <?php do_action( 'woocommerce_after_cart_table' ); ?>
            </form>
        </div>
        
        <div class="col-xxl-4 col-lg-5 offset-xxl-1">
            <div class="cs_order_card cs_accent_light_bg cs_radius_10">
                <h3 class="cs_order_card_title cs_fs_24 cs_medium cs_secondary_font"><?php esc_html_e( 'YOUR ORDER', 'glowify-wp' ); ?></h3>
                
                <?php if ( wc_coupons_enabled() ) : ?>
                    <h4 class="cs_coupon_title cs_semibold cs_fs_16 cs_secondary_font"><?php esc_html_e( 'Have a Promo Code?', 'glowify-wp' ); ?></h4>
                    <p class="cs_coupon_subtitle cs_light"><?php esc_html_e( 'Enter it here for additional savings.', 'glowify-wp' ); ?></p>
                    <form class="cs_coupon_form woocommerce-coupon-form" method="post">
                        <input type="text" name="coupon_code" class="cs_coupon_input cs_light" placeholder="<?php esc_attr_e( 'Enter Coupon Code', 'glowify-wp' ); ?>">
                        <button type="submit" class="cs_btn cs_style_1 cs_fs_18 cs_coupon_btn cs_medium" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'glowify-wp' ); ?>"><span><?php esc_html_e( 'Get', 'glowify-wp' ); ?></span></button>
                        <?php do_action( 'woocommerce_cart_coupon' ); ?>
                    </form>
                <?php endif; ?>
                
                <div class="cs_height_40 cs_height_lg_30"></div>
                
				<ul class="cs_card_price_list cs_mp_0">
					<li>
						<span class="cs_light"><?php esc_html_e( 'Items', 'glowify-wp' ); ?></span>
						<span class="cs_light"><?php echo WC()->cart->get_cart_contents_count(); ?></span>
					</li>

					<li>
						<span class="cs_light"><?php esc_html_e( 'Subtotal', 'glowify-wp' ); ?></span>
						<span class="cs_semibold cs_primary_color">
						<?php echo wc_price( WC()->cart->get_subtotal() ); ?>
						</span>
					</li>

					<li>
						<span class="cs_light"><?php esc_html_e( 'Total Item Values', 'glowify-wp' ); ?></span>
						<span class="cs_semibold cs_primary_color">
						<?php echo wc_price( WC()->cart->get_subtotal() ); ?>
						</span>
					</li>

					<?php if ( WC()->cart->get_discount_total() > 0 ) : ?>
					<li>
						<span class="cs_light"><?php esc_html_e( 'Total Discount', 'glowify-wp' ); ?></span>
						<span class="cs_semibold cs_primary_color">
						-<?php echo wc_price( WC()->cart->get_discount_total() ); ?>
						</span>
					</li>
					<?php endif; ?>

					<?php if ( WC()->cart->get_shipping_total() > 0 ) : ?>
					<li>
						<span class="cs_light"><?php esc_html_e( 'Shipping Fee', 'glowify-wp' ); ?></span>
						<span class="cs_semibold cs_primary_color">
						<?php echo wc_price( WC()->cart->get_shipping_total() ); ?>
						</span>
					</li>
					<?php endif; ?>

					<li class="cs_total_price">
						<span class="cs_medium cs_fs_24 cs_primary_color"><?php esc_html_e( 'Total', 'glowify-wp' ); ?></span>
						<span class="cs_medium cs_fs_24 cs_primary_color">
						<?php echo WC()->cart->get_total(); ?>
						</span>
					</li>
					</ul>

                
                <div class="cs_height_40 cs_height_lg_30"></div>
                
                <p class="cs_order_note cs_light"><?php esc_html_e( 'Is there anything specific you\'d like to add or any special instructions for your order? Let us know in the box below.', 'glowify-wp' ); ?></p>
                <textarea cols="30" rows="4" id="order_comments" name="order_comments" class="cs_order_note_input cs_light" placeholder="<?php esc_attr_e( 'Notes about your orders', 'glowify-wp' ); ?>"></textarea>
                
                <div class="cs_height_40 cs_height_lg_30"></div>
                
                <?php if ( WC()->cart->is_empty() ) : ?>
                    <a href="<?php echo esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ); ?>" class="cs_btn cs_style_1 cs_fs_18 w-100">
                        <span><?php esc_html_e( 'SHOP NOW', 'glowify-wp' ); ?></span>
                    </a>
                <?php else : ?>
                    <a href="<?php echo esc_url( wc_get_checkout_url() ); ?>" class="cs_btn cs_style_1 cs_fs_18 w-100">
                        <span><?php esc_html_e( 'PROCEED TO CHECKOUT', 'glowify-wp' ); ?></span>
                    </a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php do_action( 'woocommerce_after_cart' ); ?>

<script>
jQuery(document).ready(function($) {
    // Select all items checkbox
    $('#select_all_items').on('change', function() {
        $('.cart_item_checkbox').prop('checked', $(this).prop('checked'));
    });
    
    // Increment quantity
    $('.cs_increment').on('click', function() {
        var key = $(this).data('key');
        var $input = $('#qty_' + key);
        var currentValue = parseInt($input.val());
        var max = $input.attr('max') ? parseInt($input.attr('max')) : '';
        
        if (max === '' || currentValue < max) {
            $input.val(currentValue + 1).change();
            setTimeout(function() {
                $('[name="update_cart"]').trigger('click');
            }, 300);
        }
    });
    
    // Decrement quantity
    $('.cs_decrement').on('click', function() {
        var key = $(this).data('key');
        var $input = $('#qty_' + key);
        var currentValue = parseInt($input.val());
        var min = $input.attr('min') ? parseInt($input.attr('min')) : 1;
        
        if (currentValue > min) {
            $input.val(currentValue - 1).change();
            setTimeout(function() {
                $('[name="update_cart"]').trigger('click');
            }, 300);
        }
    });
    
    
    // Delete selected items
    $('#delete_selected_items').on('click', function() {
        var selectedItems = [];
        $('.cart_item_checkbox:checked').each(function() {
            selectedItems.push($(this).data('key'));
        });
        
        if (selectedItems.length > 0) {
            // Get the AJAX URL - fallback to admin-ajax.php if wc_cart_params is not defined
            var ajaxUrl = (typeof wc_cart_params !== 'undefined') ? 
                wc_cart_params.ajax_url : '<?php echo admin_url('admin-ajax.php'); ?>';
            
            // AJAX call to remove selected items
            $.ajax({
                type: 'POST',
                url: ajaxUrl,
                data: {
                    action: 'remove_selected_cart_items',
                    cart_items: selectedItems,
                    security: '<?php echo wp_create_nonce('remove-cart-items'); ?>'
                },
                success: function(response) {
                    window.location.reload();
                },
                error: function(xhr, status, error) {
                    console.error('Error removing items:', error);
                    alert('There was an error removing items. Please try again.');
                }
            });
        } else {
            alert('Please select items to delete.');
        }
    });
});
</script>