<?php

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

if ( ! class_exists( 'SBUY_Shortcode' ) ) {
    /**
     * Class Shortcode
     *
     * @since 1.0.0
     */
    class SBUY_Shortcode {

        /**
         * Constructor
         *
         * @since 1.0.0
         */
        public function __construct() {
            add_shortcode( 'simply-buy-cart', array( $this, 'cart_render' ) );
            add_shortcode( 'simply-buy-shopping-cart', array( $this, 'shopping_cart_render' ) );
            add_shortcode( 'simply-buy-checkout', array( $this, 'checkout_render' ) );
            add_shortcode( 'simply-buy-fee-show', array( $this, 'fee_show' ) );
        }

        /**
         * Render the cart page shotcode
         *
         * @since 1.0.0
         */
        public function cart_render() {

            $token                = get_option( 'sbuy_backlink_text' );
            $backlink_text        = sbuy_get_backlink_text( $token );
            $show_branding_link   = (bool) sbuy_get_options( 'show_branding', 'sbuy_settings_branding' );
            $show_branding_link   = ( $show_branding_link && ! sbuy_has_pro_plugin_access() );

            ob_start();
            ?>
            <div class="sb-cart-page-wrap">
                <div class="main-area-wrap">
                    <div class="page-content">
                        <?php dynamic_sidebar( 'simply-buy-cart-description' ); ?>
                    </div>
                    <div class="searchbox-area">
                        <div class="searchbox-wrap">
                            <select id="product-search"></select>
                            <div id="dummy-searchbox-wrap">
                                <div class="sbuy-search-row">
                                    <input type="text" id="dummy-searchbox"
                                           placeholder="<?php esc_attr_e( 'Search for item...', 'simply-buy' ); ?>"/>
                                    <button type="button" class="sbuy-search-trigger" aria-label="<?php esc_attr_e( 'Search for item...', 'simply-buy' ); ?>">
                                        <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
                                            <circle cx="11" cy="11" r="7"></circle>
                                            <line x1="21" y1="21" x2="16.65" y2="16.65"></line>
                                        </svg>
                                    </button>
                                </div>
                                <?php
                                if ( $show_branding_link ) { ?>
                                    <p>
                                        <a href="https://www.profitclicker.com" target="_blank">
                                            <?php echo esc_html( $backlink_text ); ?>
                                        </a>
                                    </p>
                                <?php } ?>
                            </div>
                        </div>
                        <div id="product-serch-result"></div>
                    </div>
                </div>
            </div>
            <?php
            return ob_get_clean();
        }

        /**
         * Render the shopping cart shortcode
         *
         * @since 1.0.0
         */
        public function shopping_cart_render() {

            $cookie_name = sbuy_get_cookie_name();

            ob_start();
            echo '<div id="sidebar">
			<div class="cart">
				<h3>' . esc_html__( 'Selected items', 'simply-buy' ) . '</h3>';
            echo '<p id="product-cart-empty-message">' . esc_html__( 'No item selected', 'simply-buy' ) . '</p>';
            echo '<div id="product-basket">';
            if ( isset( $_COOKIE[ $cookie_name ] ) ) {
                $sbuy_cookie = sbuy_convert_cookie_string_to_array( sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) );

                if ( ! empty( $sbuy_cookie ) ) {
                    static $subtotal = 0;

                    // Display newest items first (cart cookie itself stays chronological).
                    foreach ( array_reverse( $sbuy_cookie ) as $index => $product ) {
                        if ( isset( $product->id ) ) {
                            $product_id      = $product->id;
                            $product_quntity = $product->quantity;
                            $product_obj     = get_post( $product_id );
                            $type            = get_post_type( $product_id );
                            $status          = get_post_status( $product_id );
                            if ( ! empty( $product_obj ) && 'sbuy_product' === $type && 'publish' === $status ) {
                                $product_title = $product_obj->post_title;
                                $simplybuy     = json_decode( get_post_meta( $product_id, '_simplybuy', true ) );
                                $product_price = isset( $simplybuy->price ) ? esc_html( $simplybuy->price ) : '';

                                $subtotal += sbuy_price_unformat( $product_price ) * $product_quntity;

                                echo '<div class="item" data-productid="' . esc_attr( $product_id ) . '">';
                                echo '<div class="description">';
                                echo '<p class="cart-title">' . esc_html( $product_title ) . '</p>';
                                echo '<p class="cart-amount"><span class="quantity">' . esc_html( $product_quntity ) . '</span> x <span class="price"><span class="amount">' . esc_html( sbuy_price_format( $product_price ) ) . '</span> <span class="currency">' . esc_html( SBUY_CORE_CURRENCY_SYMBOL ) . '</span></span></p>';
                                echo '</div>';
                                echo '<span class="remove-product"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg></span>';
                                echo '</div>';
                            }
                        }
                    }
                }
                echo '</div>';
                if ( $subtotal > 0 ) {
                    echo '<div id="sbuy-cart-footer">';
                    echo '<p id="cart-subtotal">' . sprintf( '<span class="subtotal-text">%1$s</span> <span class="subtotal-amount"><span class="amount">%2$s</span> <span class="currency">%3$s</span></span>', esc_html__( 'Subtotal:', 'simply-buy' ), esc_html( sbuy_price_format( $subtotal ) ), esc_html( SBUY_CORE_CURRENCY_SYMBOL ) ) . '</p>';
                    echo '<button id="checkout-button" class="sb-button"><a href="' . esc_url( get_permalink( sbuy_get_localized_page_id( 'simply_buy_checkout_page' ) ) ) . '">' . esc_html__( 'Go to checkout', 'simply-buy' ) . '</a></button>';
                    echo '</div>';
                }
            }
            echo '</div>
			</div>
		</div>';

            return ob_get_clean();
        }

        /**
         * Render the checkout page shortcode
         *
         * @since 1.0.0
         */
        public function checkout_render() {

            $cookie_name = sbuy_get_cookie_name();

            ob_start();
            ?>
            <div id="sb-checkout-page">
                <?php
                // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                if ( isset( $_COOKIE[ $cookie_name ] ) || isset( $_GET['token'] ) ) {
                    $sbuy_cookie             = isset( $_COOKIE[ $cookie_name ] ) ? sbuy_convert_cookie_string_to_array( sanitize_text_field( wp_unslash( $_COOKIE[ $cookie_name ] ) ) ) : [];
                    $minimum_pickup_surchage = sbuy_get_options( 'minimum_pickup', 'sbuy_settings_finance' );

                    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
                    $sbuy_token_params = isset( $_GET['token'] ) ? sbuy_token_open( sanitize_text_field( wp_unslash( $_GET['token'] ) ) ) : [];
                    ?>
                    <div style="overflow-x:auto">
                        <table id="details-table">
                            <thead>
                            <tr>
                                <?php if ( ! empty( $sbuy_cookie ) ) { ?>
                                    <th></th><?php } ?>
                                <th><?php echo esc_html__( 'Product', 'simply-buy' ); ?></th>
                                <th><?php echo esc_html__( 'Price', 'simply-buy' ); ?></th>
                                <th><?php echo esc_html__( 'Number', 'simply-buy' ); ?></th>
                                <th><?php echo esc_html__( 'Subtotal', 'simply-buy' ); ?></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            static $total = 0;
                            if ( ! empty( $sbuy_cookie ) ) {
                                // Match the cart sidebar order: newest items first.
                                foreach ( array_reverse( $sbuy_cookie ) as $product ) {
                                    if ( isset( $product->id ) ) {
                                        $product_id  = $product->id;
                                        $product_obj = get_post( $product_id );
                                        $type        = get_post_type( $product_id );
                                        $status      = get_post_status( $product_id );
                                        if ( ! empty( $product_obj ) && 'sbuy_product' === $type && 'publish' === $status ) {
                                            $product_title    = $product_obj->post_title;
                                            $simplybuy        = json_decode( get_post_meta( $product_id, '_simplybuy', true ) );
                                            $product_price    = isset( $simplybuy->price ) ? esc_html( $simplybuy->price ) : '';
                                            $product_quantity = $product->quantity;
                                            $subtotal         = sbuy_price_unformat( $product_price ) * $product_quantity;
                                            $total            += $subtotal;
                                            ?>
                                            <tr data-product-id="<?php echo esc_html( $product_id ); ?>">
                                                <td class="remove-product-cell">
                                                    <span class="remove-product">
                                                        <svg xmlns="http://www.w3.org/2000/svg"
                                                             width="16" height="16"
                                                             viewBox="0 0 24 24" fill="none"
                                                             stroke="currentColor"
                                                             stroke-width="2"
                                                             stroke-linecap="round"
                                                             stroke-linejoin="round"
                                                             aria-hidden="true">
                                                            <line x1="18"
                                                                  y1="6"
                                                                  x2="6"
                                                                  y2="18"/>
                                                            <line
                                                                    x1="6" y1="6" x2="18" y2="18"/>
                                                        </svg>
                                                    </span></td>
                                                <td><?php echo esc_html( $product_title ); ?></td>
                                                <td class="price"><span
                                                            class="amount"><?php echo esc_html( sbuy_price_format( $product_price ) ); ?></span><span><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span>
                                                </td>
                                                <td>
                                                    <div class="quantity-area">
                                                        <span class="minus">-</span>
                                                        <input type="number" data-quantity="<?php echo esc_attr( $product_id ); ?>" min="1"
                                                               value="<?php echo esc_html( $product_quantity ); ?>"/>
                                                        <span class="plus">+</span>
                                                    </div>
                                                </td>
                                                <td id="details-subtotal-<?php echo esc_attr( $product_id ); ?>"><span
                                                            class="amount"><?php echo esc_html( sbuy_price_format( $subtotal ) ); ?></span><span
                                                            class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                }
                            } else if ( isset( $sbuy_token_params['price'] ) ) {
                                $total += floatval( $sbuy_token_params['price'] ); ?>
                                <tr data-product-id="0">
                                    <td><?php echo esc_html__( 'Custom Order', 'simply-buy' ); ?></td>
                                    <td class="price"><span
                                                class="amount"><?php echo esc_html( sbuy_price_format( $sbuy_token_params['price'] ) ); ?></span><span><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span>
                                    </td>
                                    <td>1</td>
                                    <td id="details-subtotal-0"><span
                                                class="amount"><?php echo esc_html( sbuy_price_format( $sbuy_token_params['price'] ) ); ?></span><span
                                                class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                                </tr>
                            <?php } ?>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="<?php echo ! empty( $sbuy_cookie ) ? '3' : '2'; ?>"></td>
                                <td><?php echo wp_sprintf( '<p>%1$s</p><p class="small-text">(%2$s)</p>', esc_html__( 'Total', 'simply-buy' ), esc_html__( 'VAT included', 'simply-buy' ) ); ?></td>
                                <td id="details-total"><span class="amount"><?php echo esc_html( sbuy_price_format( $total ) ); ?></span><span
                                            class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                            </tr>
                            <tr class="extra-row">
                                <td colspan="5"></td>
                            </tr>
                            </tfoot>
                        </table>
                    </div>

                    <?php
                    // @since 1.3.3 — Second "Back to cart" button between cart table
                    // and customer info, so visitors don't need to use the browser back arrow.
                    $cart_page_permalink_top = ( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ) ? get_permalink( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ) : '#';
                    ?>
                    <div class="sb-back-top-wrap" style="margin: 1.25rem 0;">
                        <button id="back-top" class="sb-button sb-back"><a href="<?php echo esc_url( $cart_page_permalink_top ); ?>"><?php echo sbuy_svg_icon( 'angles-left' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?><?php echo esc_html__( 'Back to cart', 'simply-buy' ); ?></a></button>
                    </div>

                    <?php if ( sbuy_has_pro_access() && (float) str_replace( ',', '.', (string) $minimum_pickup_surchage ) > 0 ) { ?>
                        <p class="help">
                            <?php echo sbuy_svg_icon( 'circle-info' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
                            <?php
                            /* translators: 1: minimum pickup surcharge amount, 2: currency symbol */
                            echo wp_sprintf( esc_html__( 'Please note that the minimum pickup value is %1$s%2$s', 'simply-buy' ), esc_html( $minimum_pickup_surchage ), esc_html( SBUY_CORE_CURRENCY_SYMBOL ) );
                            ?>
                        </p>
                    <?php } ?>

                    <div id="information-area">
                        <div id="customer-information-area">
                            <h3><?php echo esc_html__( 'Customer information', 'simply-buy' ); ?></h3>

                            <details id="company-name-area" class="formfield formfield__full sbuy-collapsible"<?php echo ( isset( $user->company ) && ! empty( $user->company ) ) ? ' open' : ''; ?>>
                                <summary class="sbuy-collapsible__summary"><?php echo esc_html__( 'Company', 'simply-buy' ); ?></summary>
                                <div class="sbuy-collapsible__body">
                                    <input type="text" id="company-name" name="company-name"
                                           aria-label="<?php echo esc_attr__( 'Company', 'simply-buy' ); ?>"
                                           value="<?php echo isset( $user->company ) && ! empty( $user->company ) ? esc_html( $user->company ) : ''; ?>" <?php echo isset( $user->company ) && ! empty( $user->company ) ? 'disabled' : ''; ?> />
                                </div>
                            </details>

                            <div id="first-name-area" class="formfield formfield__half">
                                <label for="first-name"><?php echo esc_html__( 'First name', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="first-name" name="first-name"
                                       value="<?php echo isset( $user->first_name ) && ! empty( $user->first_name ) ? esc_html( $user->first_name ) : ''; ?>" <?php echo isset( $user->first_name ) && ! empty( $user->first_name ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="last-name-area" class="formfield formfield__half formfield__right">
                                <label for="last-name"><?php echo esc_html__( 'Last name', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="last-name" name="last-name"
                                       value="<?php echo isset( $user->last_name ) && ! empty( $user->last_name ) ? esc_html( $user->last_name ) : ''; ?>" <?php echo isset( $user->last_name ) && ! empty( $user->last_name ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="street-area" class="formfield formfield__full">
                                <label for="street"><?php echo esc_html__( 'Street no.', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="street" name="street1"
                                       value="<?php echo isset( $user->street ) && ! empty( $user->street ) ? esc_html( $user->street ) : ''; ?>" <?php echo isset( $user->street ) && ! empty( $user->street ) ? 'disabled' : ''; ?>
                                       required/>
                                <div class="formfield__spacer"></div>
                                <input type="text" name="street2"/>
                            </div>

                            <div id="postal-code-area" class="formfield formfield__half">
                                <label for="postal-code"><?php echo esc_html__( 'Postal code', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="postal-code" name="postal-code"
                                       value="<?php echo isset( $user->postal_code ) && ! empty( $user->postal_code ) ? esc_html( $user->postal_code ) : ''; ?>" <?php echo isset( $user->postal_code ) && ! empty( $user->postal_code ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="city-area" class="formfield formfield__half formfield__right">
                                <label for="city"><?php echo esc_html__( 'Place / city', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="city" name="city"
                                       value="<?php echo isset( $user->city ) && ! empty( $user->city ) ? esc_html( $user->city ) : ''; ?>" <?php echo isset( $user->city ) && ! empty( $user->city ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="phone-area" class="formfield formfield__half">
                                <label for="phone"><?php echo esc_html__( 'Phone', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="phone" name="phone"
                                       value="<?php echo isset( $user->phone ) && ! empty( $user->phone ) ? esc_html( $user->phone ) : ''; ?>" <?php echo isset( $user->phone ) && ! empty( $user->phone ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="email-area" class="formfield formfield__half formfield__right">
                                <label for="email"><?php echo esc_html__( 'Email address', 'simply-buy' ); ?><span>*</span></label>
                                <input type="email" id="email" name="email"
                                       value="<?php echo isset( $user->user_email ) && ! empty( $user->user_email ) ? esc_html( $user->user_email ) : ''; ?>" <?php echo isset( $user->user_email ) && ! empty( $user->user_email ) ? 'disabled' : ''; ?>
                                       required/>
                            </div>

                            <div id="billing-address-area" class="formfield formfield__full">
                                <input type="checkbox" id="billing-address" name="billing-address"/>
                                <label for="billing-address"><?php echo esc_html__( 'Provide separate billing address?', 'simply-buy' ); ?></label>
                            </div>

                            <div id="billing-address-information" style="display: none;">
                                <div id="billing-company-name-area" class="formfield formfield__full">
                                    <label for="billing-company-name"><?php echo esc_html__( 'Company', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-company-name" name="billing-company-name"/>
                                </div>

                                <div id="billing-first-name-area" class="formfield formfield__half">
                                    <label for="billing-first-name"><?php echo esc_html__( 'First name', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-first-name" name="billing-first-name"/>
                                </div>

                                <div id="billing-last-name-area" class="formfield formfield__half formfield__right">
                                    <label for="billing-last-name"><?php echo esc_html__( 'Last name', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-last-name" name="billing-last-name"/>
                                </div>

                                <div id="billing-street-area" class="formfield formfield__full">
                                    <label for="billing-street"><?php echo esc_html__( 'Street no.', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-street" name="billing-street"/>
                                </div>

                                <div id="billing-postal-code-area" class="formfield formfield__half">
                                    <label for="billing-postal-code"><?php echo esc_html__( 'Postal code', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-postal-code" name="billing-postal-code"/>
                                </div>

                                <div id="billing-city-area" class="formfield formfield__half formfield__right">
                                    <label for="billing-city"><?php echo esc_html__( 'Place / city', 'simply-buy' ); ?></label>
                                    <input type="text" id="billing-city" name="billing-city"/>
                                </div>

                            </div>
                        </div>
                        <div id="additional-information-area">
                            <h3><?php echo esc_html__( 'Additional information', 'simply-buy' ); ?></h3>

                            <div id="elevator-area" class="formfield formfield__half">
                                <label for="elevator"><?php echo esc_html__( 'Elevator available?', 'simply-buy' ); ?><span>*</span></label>
                                <select name="elevator" id="elevator" required>
                                    <option disabled selected><?php echo esc_html__( 'Please select', 'simply-buy' ); ?></option>
                                    <option value="yes"><?php echo esc_html__( 'Yes', 'simply-buy' ); ?></option>
                                    <option value="no"><?php echo esc_html__( 'No', 'simply-buy' ); ?></option>
                                </select>
                            </div>

                            <div id="floor-area" class="formfield formfield__half formfield__right" style="display: none">
                                <label for="floor"><?php echo esc_html__( 'floor', 'simply-buy' ); ?><span>*</span></label>
                                <select name="floor" id="floor">
                                    <option value="" disabled selected><?php echo esc_html__( 'Please select', 'simply-buy' ); ?></option>
                                    <option value="-1"><?php echo esc_html__( 'Celler', 'simply-buy' ); ?></option>
                                    <option value="0"><?php echo esc_html__( 'Ground floor', 'simply-buy' ); ?></option>
                                    <?php
                                    $max_floor = 20;
                                    echo '<option value="0">' . esc_html__( 'Ground floor', 'simply-buy' ) . '</option>';

                                    for ( $i = 1; $i <= $max_floor; $i ++ ) {
                                        $suffix_key = sbuy_get_ordinal_suffix_en( $i ); // 'st' | 'nd' | 'rd' | 'th'

                                        switch ( $suffix_key ) {
                                            case 'st':
                                                $suffix_i18n = esc_html_x( 'st', 'ordinal suffix: 1st', 'simply-buy' );
                                                break;
                                            case 'nd':
                                                $suffix_i18n = esc_html_x( 'nd', 'ordinal suffix: 2nd', 'simply-buy' );
                                                break;
                                            case 'rd':
                                                $suffix_i18n = esc_html_x( 'rd', 'ordinal suffix: 3rd', 'simply-buy' );
                                                break;
                                            default:
                                                $suffix_i18n = esc_html_x( 'th', 'ordinal suffix: 4th, 5th...', 'simply-buy' );
                                        }

                                        /* translators: 1: floor number (e.g. 1), 2: ordinal suffix (e.g. "st"), 3: the word "floor". Example: "1st floor". */
                                        $pattern = esc_html__( '%1$s%2$s %3$s', 'simply-buy' );

                                        $floor_text = wp_sprintf( $pattern, number_format_i18n( $i ), $suffix_i18n, esc_html__( 'floor', 'simply-buy' ) );

                                        echo '<option value="' . esc_attr( $i ) . '">' . esc_html( $floor_text ) . '</option>';
                                    }
                                    ?>
                                    <option value="20+"><?php echo esc_html__( '20th floor and above', 'simply-buy' ); ?></option>
                                </select>
                            </div>

                            <div id="walkway-area" class="formfield formfield__full">
                                <label for="walkway"><?php echo esc_html__( 'Walkway between house entrance & parking lot', 'simply-buy' ); ?>
                                    <span>*</span></label>
                                <select name="walkway" id="walkway" required>
                                    <option disabled selected><?php echo esc_html__( 'Please select', 'simply-buy' ); ?></option>
                                    <option value="0"><?php echo esc_html__( 'upto 15 meters', 'simply-buy' ); ?></option>
                                    <option value="1"><?php echo esc_html__( '15 to 50 meters', 'simply-buy' ); ?></option>
                                    <option value="2"><?php echo esc_html__( 'more than 50 meters', 'simply-buy' ); ?></option>
                                </select>
                                <span class="help"><?php echo esc_html__( 'A surcharge applies for a distance of 50 metres or more', 'simply-buy' ); ?></span>
                            </div>

                            <div id="doorbell-area" class="formfield formfield__full">
                                <label for="doorbell"><?php echo esc_html__( 'Name on the doorbell', 'simply-buy' ); ?><span>*</span></label>
                                <input type="text" id="doorbell" name="doorbell" required/>
                            </div>

                            <details id="note-area" class="formfield formfield__full sbuy-collapsible"<?php echo ! empty( $sbuy_token_params['notes'] ) ? ' open' : ''; ?>>
                                <summary class="sbuy-collapsible__summary"><?php echo esc_html__( 'Notes', 'simply-buy' ); ?></summary>
                                <div class="sbuy-collapsible__body">
                                    <textarea name="note"
                                              id="note"
                                              aria-label="<?php echo esc_attr__( 'Notes', 'simply-buy' ); ?>" <?php echo isset( $sbuy_token_params['notes'] ) ? 'disabled' : ''; ?>><?php echo esc_html( $sbuy_token_params['notes'] ?? '' ); ?></textarea>
                                </div>
                            </details>

                            <?php
                            $custom_checkout = sbuy_get_options( 'enable_custom_checkbox', 'sbuy_settings_checkout' );

                            if ( $custom_checkout ) {
                                $custom_checkout_label        = sbuy_get_options( 'custom_checkbox_label_text', 'sbuy_settings_checkout' );
                                $custom_checkbox_tooltip_html = sbuy_get_options( 'custom_checkbox_tooltip_html', 'sbuy_settings_checkout' );
                                ?>
                                <div id="planting-area" class="formfield formfield__full">
                                    <input type="checkbox" id="planting" name="planting" value="planting"/>
                                    <label for="planting"><?php echo esc_html( $custom_checkout_label ); ?></label>
                                    <p class="help help__large">
                                        <?php
                                        echo wp_sprintf( '%1$s %2$s', sbuy_svg_icon( 'circle-info' ), wp_kses_post( $custom_checkbox_tooltip_html ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                                        ?>
                                    </p>
                                </div>
                            <?php } ?>
                        </div>
                    </div>

                    <div id="date-slot-area">
                        <div id="date-area" class="formfield">
                            <label for="date"><?php echo esc_html__( 'Date', 'simply-buy' ); ?><span>*</span></label>
                            <input type="text" id="date" <?php echo isset( $sbuy_token_params['date'] ) ? 'disabled' : ''; ?>
                                   value="<?php echo isset( $sbuy_token_params['date'] ) ? esc_html( gmdate( 'F j, Y', strtotime( $sbuy_token_params['date'] ) ) ) : ''; ?>"/>
                        </div>
                        <div id="slot-area" class="formfield">
                            <label for="slot"><?php echo esc_html__( 'Time', 'simply-buy' ); ?><span>*</span></label>
                            <select name="slot" id="slot" required>
                                <?php
                                if ( isset( $sbuy_token_params['date'] ) ) {
                                    $day  = gmdate( 'w', strtotime( $sbuy_token_params['date'] ) );
                                    $time = sbuy_get_day_time_shift( $day, $sbuy_token_params['slot'] ) ?? '';
                                    if ( $sbuy_token_params['slot'] === 'morning' ) {
                                        $message = wp_sprintf( '%s %s', esc_html__( 'Morning', 'simply-buy' ), esc_html( $time ) );
                                    } else {
                                        $message = wp_sprintf( '%s %s', esc_html__( 'Afternoon', 'simply-buy' ), esc_html( $time ) );
                                    }
                                    ?>
                                    <option value="<?php echo esc_html( $sbuy_token_params['slot'] ) ?>" disabled
                                            selected><?php echo esc_html( $message ); ?></option>
                                    }
                                <?php } else { ?>
                                    <option value="select-date-first" disabled
                                            selected><?php echo esc_html__( 'Please select a day first', 'simply-buy' ); ?></option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>

                    <?php
                    $next_day_booking_message_html = sbuy_get_options( 'next_day_booking_message_html', 'sbuy_settings_checkout' );

                    if ( $next_day_booking_message_html ) {
                        ?>
                        <p class="help">
                            <?php echo sbuy_svg_icon( 'circle-info' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> <?php echo wp_kses_post( $next_day_booking_message_html ); ?>
                        </p>
                    <?php } ?>

                    <div style="overflow-x: auto">
                        <table id="summary-table">
                            <thead>
                            <tr>
                                <th><?php echo esc_html__( 'Product', 'simply-buy' ); ?></th>
                                <th><?php echo esc_html__( 'Subtotal', 'simply-buy' ); ?></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            if ( ! empty( $sbuy_cookie ) ) {
                                static $summary_total = 0;
                                foreach ( $sbuy_cookie as $product ) {
                                    $product_id  = $product->id;
                                    $product_obj = get_post( $product_id );
                                    $type        = get_post_type( $product_id );
                                    $status      = get_post_status( $product_id );
                                    if ( ! empty( $product_obj ) && 'sbuy_product' === $type && 'publish' === $status ) {
                                        $product_title   = $product_obj->post_title;
                                        $simplybuy       = json_decode( get_post_meta( $product_id, '_simplybuy', true ) );
                                        $product_price   = isset( $simplybuy->price ) ? esc_html( $simplybuy->price ) : '';
                                        $product_quntity = $product->quantity;
                                        $subtotal        = sbuy_price_unformat( $product_price ) * $product_quntity;
                                        $summary_total   += $subtotal;
                                        ?>
                                        <tr id="<?php echo wp_sprintf( 'product-%s', esc_attr( $product_id ) ); ?>"
                                            data-productid=<?php echo esc_attr( $product_id ); ?>>
                                            <td class="product-name-quantity"><span class="name"><?php echo esc_html( $product_title ); ?></span> x
                                                <span
                                                        id="<?php echo wp_sprintf( 'summary-quantity-%s', esc_attr( $product_id ) ); ?>"
                                                        class="quantity"><?php echo esc_html( $product_quntity ); ?></span></td>
                                            <td id="<?php echo wp_sprintf( 'summary-subtotal-%s', esc_attr( $product_id ) ); ?>"><span
                                                        class="amount"><?php echo esc_html( sbuy_price_format( $subtotal ) ); ?></span> <span
                                                        class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                                        </tr>
                                        <?php
                                    }
                                }
                            } else if ( isset( $sbuy_token_params['price'] ) ) {
                                $summary_total = 0;
                                $summary_total += intval( $sbuy_token_params['price'] );
                                ?>
                                <tr id="" data-productid="0">
                                    <td class="product-name-quantity"><span
                                                class="name"><?php echo esc_html__( 'Custom Order', 'simply-buy' ); ?></span></td>
                                    <td id=""><span
                                                class="amount"><?php echo esc_html( sbuy_price_format( $sbuy_token_params['price'] ) ); ?></span>
                                        <span
                                                class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                                </tr>
                            <?php }
                            ?>
                            <tfoot>
                            <?php
                            if ( sbuy_has_pro_access() ) {
                                $minimum_pickup_surchage_fee = (float) $minimum_pickup_surchage - $summary_total;

                                if ( $minimum_pickup_surchage_fee > 0 ) {
                                    $summary_total += $minimum_pickup_surchage_fee;
                                    ?>
                                    <tr id="min-order-surcharge-area">
                                        <td class="label"><?php echo esc_html__( 'Surcharge to the minimum pick-up price:', 'simply-buy' ); ?></td>
                                        <td class="price"><span
                                                    class="amount"><?php echo esc_html( sbuy_price_format( $minimum_pickup_surchage_fee ) ); ?></span><span
                                                    class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?>

                            <tr id="elevator-walkway-sucharge-area">
                                <td class="label"><?php echo esc_html__( 'Difficulty surcharge', 'simply-buy' ); ?></td>
                                <td class="price"><span class="amount"></span> <span
                                            class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                            </tr>
                            <tr>
                                <td><?php echo esc_html__( 'Total', 'simply-buy' ); ?></td>
                                <td id="summary-total"><span class="amount"><?php echo esc_html( sbuy_price_format( $summary_total ) ); ?></span><span
                                            class="currency"><?php echo esc_html( SBUY_CORE_CURRENCY_SYMBOL ); ?></span></td>
                            </tr>
                            </tfoot>

                            </tbody>
                        </table>
                    </div>
                    <p class="help"><?php echo sbuy_svg_icon( 'circle-info' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> <?php echo esc_html__( 'Payment is made on site via cash', 'simply-buy' ); ?></p>

                    <div class="submit-area">
                        <?php
                        $privacy_notice = sbuy_get_options( 'enable_privacy_notice', 'sbuy_settings_privacy' );

                        if ( $privacy_notice ) {
                            $privacy_notice_html   = sbuy_get_options( 'privacy_notice_html', 'sbuy_settings_privacy' );
                            $privacy_checkbox_html = sbuy_get_options( 'privacy_checkbox_html', 'sbuy_settings_privacy' );
                            ?>
                            <p class="help"><?php echo wp_kses_post( $privacy_notice_html ); ?></p>

                            <div id="terms-area" class="formfield formfield__full">
                                <input type="checkbox" id="terms" name="terms" value="terms"/>
                                <label for="terms" class="help"><?php echo wp_kses_post( $privacy_checkbox_html ); ?></label>
                            </div>
                        <?php } ?>

                        <div class="button-wrap" <?php echo ! $privacy_notice ? 'style="margin-top: 0"' : ''; ?>>
                            <?php
                            $cart_page_permalink = ( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ) ? get_permalink( sbuy_get_localized_page_id( 'simply_buy_cart_page' ) ) : '#';
                            ?>
                            <button id="back" class="sb-button sb-back"><a href="<?php echo esc_url( $cart_page_permalink ); ?>"><?php echo sbuy_svg_icon( 'angles-left' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?><?php echo esc_html__( 'Back to cart', 'simply-buy' ); ?></a></button>
                            <button id="submit" class="sb-button" type="button"><a href="javascript:void(0)" onclick="return false;"><?php echo sbuy_svg_icon( 'paper-plane' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> <?php echo esc_html__( 'Book now for a fee', 'simply-buy' ); ?></a>
                            </button>
                            <span class="sb-submit-error"><?php echo esc_html__( 'Required fields are missing', 'simply-buy' ); ?></span>
                        </div>
                    </div>

                    <?php
                }
                ?>
            </div>
            <?php
            return ob_get_clean();
        }

        /**
         * Render the fee_show shortcode
         *
         * @return string
         *
         * @since 1.0.0
         */
        public function fee_show(): string {
            if ( ! sbuy_has_pro_access() ) {
                return '';
            }

            return SBUY_CORE_CURRENCY_SYMBOL . '&nbsp;' . sbuy_get_options( 'minimum_pickup', 'sbuy_settings_finance' );
        }
    }
}
