<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery -- Settings are read from a plugin-owned table.
if (! defined('ABSPATH')) exit; // Exit if accessed directly

// Get existing values for edit mode
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- GET parameter used for popup ID retrieval, value is sanitized
$instant_popup_builder_edit_id = isset($_GET['edit_id']) && !empty($_GET['edit_id']) ? absint(sanitize_text_field(wp_unslash($_GET['edit_id']))) : 0;
$instant_popup_builder_advance_data = null;

if ($instant_popup_builder_edit_id > 0) {
    global $wpdb;
    $instant_popup_builder_popup_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}instant_popup_builder WHERE id=%d", $instant_popup_builder_edit_id));
    if ($instant_popup_builder_popup_data) {
        $instant_popup_builder_popup_data = $instant_popup_builder_popup_data[0];
        $instant_popup_builder_advance_data = isset($instant_popup_builder_popup_data->advance) ? json_decode($instant_popup_builder_popup_data->advance) : null;
    }
}

// Set default values or load from existing data
$instant_popup_builder_age_verification_method = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->age_verification_method) ? $instant_popup_builder_advance_data->age_verification_method : 'checkbox';
$instant_popup_builder_age_verification_minimum_age = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->age_verification_minimum_age) ? $instant_popup_builder_advance_data->age_verification_minimum_age : 18;
$instant_popup_builder_age_verification_compliance_text = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->age_verification_compliance_text) ? $instant_popup_builder_advance_data->age_verification_compliance_text : '';
$instant_popup_builder_age_verification_cookie_duration = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->age_verification_cookie_duration) ? $instant_popup_builder_advance_data->age_verification_cookie_duration : 30;
$instant_popup_builder_age_verification_failure_redirect_url = $instant_popup_builder_advance_data && isset($instant_popup_builder_advance_data->age_verification_failure_redirect_url) ? $instant_popup_builder_advance_data->age_verification_failure_redirect_url : '';
?>
<!-- Age Verification General Settings -->
<div class="ipb-section ipb-age-verification-settings">
 <div class="ipb-section-header">
    <h3>Age Verification Setting</h3>
</div>
<div class="ipb-section-content" data-section="display">
<div class="design_background">
    <div class="ipb_background_wrapper">

        <div class="background_field">
            <div class="label">Verification Method</div>
            <div class="input">
                <select name="age_verification_method" id="age_verification_method">
                    <option value="checkbox" <?php selected($instant_popup_builder_age_verification_method, 'checkbox'); ?>>Checkbox Confirmation</option>
                    <option value="date_of_birth" <?php selected($instant_popup_builder_age_verification_method, 'date_of_birth'); ?>>Date of Birth</option>
                    <option value="yes_no" <?php selected($instant_popup_builder_age_verification_method, 'yes_no'); ?>>Yes/No Buttons</option>
                    <option value="dropdown" <?php selected($instant_popup_builder_age_verification_method, 'dropdown'); ?>>Dropdown Selection</option>
                    <option value="minimal" <?php selected($instant_popup_builder_age_verification_method, 'minimal'); ?>>Minimal Design</option>
                    <option value="modern" <?php selected($instant_popup_builder_age_verification_method, 'modern'); ?>>Modern Design</option>
                </select>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Minimum Age</div>
            <div class="input">
                <input type="number" name="age_verification_minimum_age" id="age_verification_minimum_age" value="<?php echo esc_attr($instant_popup_builder_age_verification_minimum_age); ?>" min="13" max="25">
            </div>
        </div>

        <div class="background_field">
            <div class="label">Compliance Message</div>
            <div class="input">
                <textarea name="age_verification_compliance_text" id="age_verification_compliance_text" rows="3" placeholder="This website contains age-restricted content. By entering, you confirm that you are of legal age."><?php echo esc_textarea($instant_popup_builder_age_verification_compliance_text); ?></textarea>
            </div>
        </div>

        <div class="background_field">
            <div class="label">Cookie Duration (Days)</div>
            <div class="input">
                <input type="number" name="age_verification_cookie_duration" id="age_verification_cookie_duration" value="<?php echo esc_attr($instant_popup_builder_age_verification_cookie_duration); ?>" min="1" max="365">
            </div>
        </div>

        <div class="background_field">
            <div class="label">Failure Redirect URL</div>
            <div class="input">
                <input type="url" name="age_verification_failure_redirect_url" id="age_verification_failure_redirect_url" placeholder="https://example.com/under-age" value="<?php echo esc_url($instant_popup_builder_age_verification_failure_redirect_url); ?>">
            </div>
        </div>

    </div>
</div>
</div>
</div>

<style>
.ipb-age-verification-settings .ipb-section-content {
    padding: 16px;
}

.ipb-age-verification-settings .ipb_background_wrapper {
    margin: 0;
    padding: 0;
}

.ipb-age-verification-settings .background_field {
    display: grid;
    grid-template-columns: minmax(220px, 260px) minmax(0, 1fr);
    align-items: start;
    gap: 16px;
    margin: 0 0 16px;
    width: 100%;
}

.ipb-age-verification-settings .background_field:last-child {
    margin-bottom: 0;
}

.ipb-age-verification-settings .background_field .label {
    margin: 0;
    font-size: 14px;
    font-weight: 600;
    line-height: 1.4;
    color: #1d2327;
}

.ipb-age-verification-settings .background_field .input {
    width: 100%;
}

.ipb-age-verification-settings #age_verification_method {
    min-width: 320px;
    width: min(420px, 100%);
}

.ipb-age-verification-settings #age_verification_minimum_age,
.ipb-age-verification-settings #age_verification_cookie_duration {
    min-width: 140px;
    width: min(180px, 100%);
}

.ipb-age-verification-settings #age_verification_compliance_text {
    min-width: 320px;
    width: min(420px, 100%);
    min-height: 100px;
    resize: vertical;
}

.ipb-age-verification-settings #age_verification_failure_redirect_url {
    min-width: 420px;
    width: min(560px, 100%);
}

@media (max-width: 782px) {
    .ipb-age-verification-settings .background_field {
        grid-template-columns: 1fr;
        gap: 8px;
    }

    .ipb-age-verification-settings #age_verification_method,
    .ipb-age-verification-settings #age_verification_minimum_age,
    .ipb-age-verification-settings #age_verification_cookie_duration,
    .ipb-age-verification-settings #age_verification_compliance_text,
    .ipb-age-verification-settings #age_verification_failure_redirect_url {
        min-width: 0;
        width: 100%;
    }
}
</style>
