<?php
defined( 'ABSPATH' ) || exit;

if (isset($_REQUEST['w2pshop_product_page_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['w2pshop_product_page_nonce'])), 'w2pshop_product_actions')){
	$cart_item_key = (isset($_REQUEST['cart_item_key'])) ? sanitize_text_field(wp_unslash($_REQUEST['cart_item_key'])) : '';
} else {
	$cart_item_key = '';
}
$is_cart_updated = isset( $is_cart_updated ) && $is_cart_updated;

// Proofing Feature Variables
$proofing_enabled      = ! empty( W2PShopSettings::get_setting( 'w2pshop_core_proofing') ) ? 'true' : 'false';
$proofing_instructions = W2PShopSettings::get_setting( 'w2pshop_proofing_confirmation_text', true );
$confirmation_label    = W2PShopSettings::get_setting( 'w2pshop_confirmation_label', true );
$approve_button_state  = ( ! empty( $proofing_instructions ) ) ? 'disabled' : '';
$bypass_design_upload  = W2PShopSettings::get_product_option( $post->ID, 'w2pshop_bypass_design_and_upload' );

// Set default confirmation label
$confirmation_label = ( empty( $confirmation_label ) ) ? 'I acknowledge the final product' : $confirmation_label;

wp_enqueue_script('w2pshop_product_page_core_js', W2PSHOP_PLUGIN_URL . 'assets/js/w2pshop-product-page-core.js', array('jquery'), W2PSHOPCORE_VERSION, true);
$cart_url = wc_get_cart_url();
if ($is_cart_updated) {
	$cart_url .= ( wp_parse_url( $cart_url, PHP_URL_QUERY ) ? '&' : '?' ) . 'cart_updated=true';
}
$local_data = array(
	'bypass_design_upload' => $bypass_design_upload,
	'is_cart_updated'      => $is_cart_updated ? '1' : '0',
	'cart_url'             => esc_url($cart_url),
	'cart_item_key'        => $cart_item_key,
);
wp_add_inline_script(
	'w2pshop_product_page_core_js',
	'var W2PShopProductPageData = ' . wp_json_encode($local_data) . ';',
	'before'
);

if ( $proofing_enabled ) { ?>
    <div id="w2pshop-overlay" style="display: none; pointer-events: none;"></div>

    <div id="w2pshop-udraw-proof-container" style="display: none; pointer-events: none;">
        <div class="w2pshop-button-container w2pshop-top-bar">
            <button type="button" class="w2pshop-proof-button w2pshop-proof-back-button">
                <?php esc_html_e( 'Back', 'web-to-print-shop-core' ); ?>
            </button>
            <button id="w2pshop-back-button" type="button" class="w2pshop-proof-button w2pshop-proof-back-button" aria-label="<?php esc_attr_e( 'Close', 'web-to-print-shop-core' ); ?>">
                <svg height="1em" viewBox="0 0 384 512" aria-hidden="true">
                    <path d="M342.6 150.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0L192 210.7 86.6 105.4c-12.5-12.5-32.8-12.5-45.3 0s-12.5 32.8 0 45.3L146.7 256 41.4 361.4c-12.5 12.5-12.5 32.8 0 45.3s32.8 12.5 45.3 0L192 301.3 297.4 406.6c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L237.3 256 342.6 150.6z" />
                </svg>
            </button>
        </div>

        <div class="w2pshop-carousel-container">
            <div class="w2pshop-preview-container" id="w2pshop-preview-container">
                <img src="" class="w2pshop-preview-image" alt="<?php esc_attr_e( 'Design Preview', 'web-to-print-shop-core' ); ?>">
            </div>
            <div id="w2pshop-carousel-buttons">
                <a class="prev arrow proofing-prev-button" aria-label="<?php esc_attr_e( 'Previous', 'web-to-print-shop-core' ); ?>">&#10094;</a>
                <a class="next arrow proofing-next-button" aria-label="<?php esc_attr_e( 'Next', 'web-to-print-shop-core' ); ?>">&#10095;</a>
                <div class="slide-numbers">
                    <span class="dot active proofing-dot"></span>
                </div>
            </div>
        </div>
        <div class="w2pshop-instructions">
            <div class="w2pshop-instructions-message">
				<?php
				if ( ! empty( $proofing_instructions ) ) {
					echo wp_kses( $proofing_instructions, W2PShop::get_html_design_tags() );
					?>
					<div class="w2pshop-proof-checkbox-container">
						<input type="checkbox" id="w2pshop_proof_approval" name="w2pshop_proof_approval" value="w2pshop_proof_approval" class="w2pshop-proof-checkbox">
						<label for="w2pshop_proof_approval"><?php echo wp_kses( $confirmation_label, W2PShop::get_html_design_tags() ); ?></label>
					</div>
					<?php
				}
				?>
            </div>
            <div class="w2pshop-button-container">
                <button type="button" class="w2pshop-proof-button" id="w2pshop_approve_button" <?php echo esc_attr( $approve_button_state ); ?>><?php esc_html_e( 'Approve and Add to Cart', 'web-to-print-shop-core' ); ?></button>
            </div>
            <!-- PDF Editor specific buttons -->
            <div class="w2pshop-button-container w2pshop-action-buttons w2pshop-pdfeditor-buttons">
                <button type="button" class="w2pshop-proof-button w2pshop-proof-back-button w2pshop-back-btn">
                    <?php esc_html_e( 'Back', 'web-to-print-shop-core' ); ?>
                </button>
                <button type="button" class="w2pshop-proof-button w2pshop-download-proof-button pdfeditor-download-pdf-btn" id="w2pshop_download_proof_button">
                    <?php esc_html_e( 'Download Print Proof', 'web-to-print-shop-core' ); ?>
                </button>
                <button type="button" class="w2pshop-proof-button w2pshop-approve-btn" id="w2pshop_approve_button_pdfeditor" <?php echo esc_attr( $approve_button_state ); ?>>
                    <?php esc_html_e( 'Looks Good! Add to Cart', 'web-to-print-shop-core' ); ?>
                </button>
            </div>
        </div>
    </div>
<?php } ?>
