<?php
/**
 * Plugin-wide Consent Notice (FREE v4.2.7)
 * REQUIRED: API connection consent (without it plugin can't fetch points / generate labels)
 * OPTIONAL: Diagnostics (telemetry) — anonymous data
 * OPTIONAL: Email subscription (max 1 mail/year)
 *
 * Adapted from PRO v3.5.0 ACTIVATOR/templates/step-consent.php — TRIM wizard wrapper,
 * inline as admin notice. NO new business logic, only wizard navigation removed.
 */

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

// Load legal content (Terms + Privacy HTML helpers)
if ( ! function_exists( 'parcselect_get_terms_of_service_html' ) ) {
    $parcselect_legal_file = plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . 'includes/class-ps-legal.php';
    if ( file_exists( $parcselect_legal_file ) ) {
        require_once $parcselect_legal_file;
    }
}

// Error from previous failed POST attempt
$parcselect_ps_error = get_transient( 'parcselect_consent_error_' . get_current_user_id() );
if ( $parcselect_ps_error ) {
    delete_transient( 'parcselect_consent_error_' . get_current_user_id() );
}
?>

<div class="wizard-step step-consent">

    <div class="step-header">
        <h2><?php esc_html_e('Terms & Privacy', 'parcel-selector'); ?></h2>
        <p class="step-description">
            <?php esc_html_e('Please review and accept our policies to continue.', 'parcel-selector'); ?>
        </p>
    </div>

    <?php if ( $parcselect_ps_error ) : ?>
    <div class="notice notice-error" style="margin:0 0 16px;padding:8px 12px;">
        <p><?php echo esc_html( $parcselect_ps_error ); ?></p>
    </div>
    <?php endif; ?>

    <form id="form-consent-data" class="ps-consent-form ps-no-ajax" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">

        <?php wp_nonce_field( 'parcselect_consent_save', 'parcselect_consent_nonce' ); ?>
        <input type="hidden" name="action" value="parcselect_consent_save">

        <!-- API Connection Consent (REQUIRED for plugin to function) -->
        <div class="consent-box required">
            <label class="consent-label">
                <input type="checkbox" id="terms_accepted" name="terms_accepted" required>
                <span class="consent-text">
                    <?php esc_html_e('I accept the', 'parcel-selector'); ?>
                    <a href="https://parcelselector.com/parcelselector-konstytucja/" target="_blank" class="link-primary">
                        <?php esc_html_e('Terms of Service', 'parcel-selector'); ?>
                    </a>
                    <span class="required-mark">*</span>
                </span>
            </label>
            <div class="consent-preview" style="margin-top:10px;">
                <?php
                if ( function_exists( 'parcselect_get_terms_of_service_html' ) ) {
                    echo wp_kses_post( parcselect_get_terms_of_service_html() );
                }
                ?>
            </div>
        </div>

        <!-- Privacy Policy (REQUIRED) -->
        <div class="consent-box required">
            <label class="consent-label">
                <input type="checkbox" id="privacy_accepted" name="privacy_accepted" required>
                <span class="consent-text">
                    <?php esc_html_e('I accept the', 'parcel-selector'); ?>
                    <a href="https://parcelselector.com/parcelselector-konstytucja/" target="_blank" class="link-primary">
                        <?php esc_html_e('Privacy Policy', 'parcel-selector'); ?>
                    </a>
                    <span class="required-mark">*</span>
                </span>
            </label>
            <div class="consent-preview" style="margin-top:10px;">
                <?php
                if ( function_exists( 'parcselect_get_privacy_policy_html' ) ) {
                    echo wp_kses_post( parcselect_get_privacy_policy_html() );
                }
                ?>
            </div>
        </div>

        <hr class="consent-divider">

        <!-- Diagnostics Consent (OPTIONAL) -->
        <div class="consent-box optional">
            <label class="consent-label">
                <input type="checkbox" id="diagnostics_consent" name="diagnostics_consent">
                <span class="consent-text">
                    <?php esc_html_e('Send diagnostic data to help us improve', 'parcel-selector'); ?>
                    <span class="optional-badge"><?php esc_html_e('Optional', 'parcel-selector'); ?></span>
                </span>
            </label>
            <div class="consent-preview">
                <details>
                    <summary><?php esc_html_e('What diagnostic data includes', 'parcel-selector'); ?></summary>
                    <div class="consent-content">
                        <h4><?php esc_html_e('Additional data we\'d collect:', 'parcel-selector'); ?></h4>
                        <ul>
                            <li>✅ <?php esc_html_e('Active theme name & version', 'parcel-selector'); ?></li>
                            <li>✅ <?php esc_html_e('List of active plugins (names only)', 'parcel-selector'); ?></li>
                            <li>✅ <?php esc_html_e('PHP version, memory limits', 'parcel-selector'); ?></li>
                            <li>✅ <?php esc_html_e('Error logs from our plugin only', 'parcel-selector'); ?></li>
                        </ul>
                        <p><strong><?php esc_html_e('Why?', 'parcel-selector'); ?></strong></p>
                        <ul>
                            <li>🎯 <?php esc_html_e('Prioritize support for popular themes/plugins', 'parcel-selector'); ?></li>
                            <li>🎯 <?php esc_html_e('Detect conflicts before you report them', 'parcel-selector'); ?></li>
                            <li>🎯 <?php esc_html_e('Provide faster, more accurate support', 'parcel-selector'); ?></li>
                        </ul>
                        <p class="note">
                            <?php esc_html_e('You can disable this at any time in Settings → InPost → Privacy', 'parcel-selector'); ?>
                        </p>
                    </div>
                </details>
            </div>
        </div>

        <div class="info-box info-box-success">
            <strong>🔒 <?php esc_html_e('Your data is safe', 'parcel-selector'); ?></strong><br>
            <?php esc_html_e('We use bank-level encryption (SSL/TLS), store data in EU servers (GDPR compliant), and never sell your information. You can request data deletion at any time.', 'parcel-selector'); ?>
        </div>

        <div class="button-group">
            <?php // Wizard "<- Back" button removed: ten template dziala inline jako admin notice, nie ma poprzedniego kroku kreatora ($_ps_prev_url nigdy nie istnial w tym kontekscie). ?>
            <button type="submit" class="button button-primary button-large" id="btn-consent-submit">
                <?php esc_html_e('Accept & Continue →', 'parcel-selector'); ?>
            </button>
        </div>

    </form>

</div>

<?php
// Consent step — enable/disable submit based on required checkboxes. JS attached
// via wp_add_inline_script so the plugin emits no raw <script> from PHP.
if ( ! wp_script_is( 'parcselect-consent-checkbox', 'registered' ) ) {
    wp_register_script( 'parcselect-consent-checkbox', '', array(), PARCSELECT_VERSION, true );
}
wp_enqueue_script( 'parcselect-consent-checkbox' );
wp_add_inline_script(
    'parcselect-consent-checkbox',
    "(function(){var t=document.getElementById('terms_accepted');var p=document.getElementById('privacy_accepted');var b=document.getElementById('btn-consent-submit');if(!t||!p||!b)return;function u(){b.disabled=!(t.checked&&p.checked);}t.addEventListener('change',u);p.addEventListener('change',u);u();})();"
);
?>
