<?php

namespace orderflow\Facebook;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

// Get Facebook instance
$facebook = \orderflow\Facebook\Facebook();

// getAllMetaEventParamName() function is already declared in functions-common.php

// Helper function for pro badge
if ( ! function_exists( 'renderProBadge' ) ) {
    function renderProBadge() {
        echo '<span class="pro-badge">PRO</span>';
    }
}

?>
<?php
// General and WooCommerce Events are automatically enabled when Facebook Pixel is enabled
// Purchase Event Settings
?>

<!-- Purchase Event Settings -->
<div class="orderflow-field-row">
    <div class="orderflow-field-label">Immediately Send Purchase Event
        <p class="ads-settings-description">If enabled, purchase events will be sent immediately when order is placed (if courier ratio is above threshold). If disabled, purchase events will be saved and sent when order status changes to "ads-purchase".</p>
    </div>
    <div class="orderflow-field-input">
        <label class="orderflow-toggle-switch">
            <input type="hidden" name="pys[orderflow_facebook][woo_purchase_immediately_send]" value="0">
            <input type="checkbox" name="pys[orderflow_facebook][woo_purchase_immediately_send]" value="1" id="pys_orderflow_facebook_woo_purchase_immediately_send" <?php checked( $facebook->getOption( 'woo_purchase_immediately_send' ), true ); ?>>
            <span class="orderflow-slider"></span>
        </label>
    </div>
</div>

<div class="orderflow-field-row" id="pys_facebook_woo_purchase_courier_ratio_threshold_panel" style="<?php echo $facebook->getOption('woo_purchase_immediately_send') ? '' : 'display: none;'; ?>">
    <div class="orderflow-field-label">Courier Ratio Threshold (%)
        <p class="ads-settings-description">If courier ratio is above this threshold, purchase event will be sent immediately. If below threshold, event will be saved and sent when order status changes to "ads-purchase".</p>
    </div>
    <div class="orderflow-field-input">
        <?php $facebook->render_number_input( 'woo_purchase_courier_ratio_threshold', 'Enter threshold percentage', false, 100, 0, 0.1 ); ?>
    </div>
</div>

<script>
jQuery(document).ready(function($) {
    // Show/hide courier ratio field based on immediately send toggle
    $('#pys_orderflow_facebook_woo_purchase_immediately_send').on('change', function() {
        if ($(this).is(':checked')) {
            $('#pys_facebook_woo_purchase_courier_ratio_threshold_panel').show();
        } else {
            $('#pys_facebook_woo_purchase_courier_ratio_threshold_panel').hide();
        }
    });
});
</script>


