<?php
/**
 * GT BOGO Engine — White Label Manager
 * @package GT_BOGO_Engine
 * @since   3.10.1
 */
if (!defined('ABSPATH')) exit;
if (!current_user_can('manage_options')) return;

$is_pro = function_exists('gt_bogo_has_pro') ? gt_bogo_has_pro() : false;
$message = ''; $message_type = '';

if ($is_pro && isset($_POST['gtbogo_save_wl']) && check_admin_referer('gtbogo_white_label', 'gtbogo_wl_nonce')) {
    $wl = [
        'enabled'      => isset($_POST['wl_enabled']),
        'plugin_name'  => sanitize_text_field(wp_unslash($_POST['wl_plugin_name']  ?? 'GT BOGO Engine')),
        'menu_label'   => sanitize_text_field(wp_unslash($_POST['wl_menu_label']   ?? 'BOGO Engine')),
        'support_url'  => esc_url_raw(wp_unslash($_POST['wl_support_url']  ?? '')),
        'docs_url'     => esc_url_raw(wp_unslash($_POST['wl_docs_url']     ?? '')),
        'agency_name'  => sanitize_text_field(wp_unslash($_POST['wl_agency_name']  ?? '')),
        'hide_branding'=> isset($_POST['wl_hide_branding']),
        'hide_upgrade' => isset($_POST['wl_hide_upgrade']),
    ];
    update_option('gtbogo_white_label', $wl);
    // Save email settings

    $message = 'White label settings saved.'; $message_type = 'success';
}

$wl = get_option('gtbogo_white_label', [
    'enabled'      => false,
    'plugin_name'  => 'GT BOGO Engine',
    'menu_label'   => 'BOGO Engine',
    'support_url'  => '',
    'docs_url'     => '',
    'agency_name'  => '',
    'hide_branding'=> false,
    'hide_upgrade' => false,
]);

require_once __DIR__ . '/../ui-system/wrapper-start.php';
?>
<div class="gt-page-header gt-animate-in">
    <div>
        <h1 class="gt-page-title">🏷️ White Label Manager</h1>
        <p class="gt-page-subtitle">Rebrand the plugin for your clients — your name, your logo, your support links</p>
    </div>
    <span class="gt-badge <?php echo !empty($wl['enabled']) ? 'gt-badge-active' : 'gt-badge-free'; ?>">
        <?php echo !empty($wl['enabled']) ? '● White Label Active' : '○ Using Default Branding'; ?>
    </span>
</div>

<?php if ($message): ?>
<div class="gt-notice gt-notice-<?php echo esc_attr($message_type); ?> gt-animate-in"><?php echo esc_html($message); ?></div>
<?php endif; ?>

<?php if (!$is_pro): ?>
<div class="gt-pro-gate gt-animate-in">
    <span class="gt-pro-gate-icon">🏷️</span>
    <h2>PRO License Required</h2>
    <p>White Label Manager is an enterprise feature for agencies who resell this plugin under their own brand.</p>
    <div class="gt-flex" style="justify-content:center;gap:12px;flex-wrap:wrap">
        <a href="https://graphictshirts.shop/bogo/" target="_blank" rel="noopener" class="gt-btn gt-btn-primary">Purchase PRO — $149/yr</a>
        <a href="<?php echo esc_url(admin_url('admin.php?page=gt-bogo-engine&tab=license')); ?>" class="gt-btn gt-btn-ghost">Activate License</a>
    </div>
</div>
<?php else: ?>

<form method="post" action="">
    <?php wp_nonce_field('gtbogo_white_label', 'gtbogo_wl_nonce'); ?>

    <!-- Master toggle -->
    <div class="gt-card gt-animate-in" style="margin-bottom:20px">
        <div class="gt-setting-row" style="border:none;padding:0">
            <div>
                <div class="gt-setting-label" style="font-size:16px">Enable White Label Mode</div>
                <div class="gt-setting-desc">Activates all white label settings below. Clients see your branding, not GT BOGO Engine.</div>
            </div>
            <label class="gt-toggle">
                <input type="checkbox" name="wl_enabled" value="1" <?php checked(!empty($wl['enabled'])); ?>>
                <span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span>
            </label>
        </div>
    </div>

    <!-- Branding -->
    <div class="gt-card gt-animate-in" style="margin-bottom:20px">
        <div class="gt-card-header">
            <h2 class="gt-card-title">Branding</h2>
        </div>
        <div class="gt-feature-grid gt-feature-grid--2col">
            <div class="gt-form-group" style="margin:0">
                <label class="gt-form-label">Plugin Name</label>
                <input type="text" name="wl_plugin_name" class="gt-input"
                       value="<?php echo esc_attr($wl['plugin_name'] ?? 'GT BOGO Engine'); ?>"
                       placeholder="Your Plugin Name">
                <div class="gt-form-hint">Shown in plugin list and admin menu</div>
            </div>
            <div class="gt-form-group" style="margin:0">
                <label class="gt-form-label">Admin Menu Label</label>
                <input type="text" name="wl_menu_label" class="gt-input"
                       value="<?php echo esc_attr($wl['menu_label'] ?? 'BOGO Engine'); ?>"
                       placeholder="Menu Label">
            </div>
            <div class="gt-form-group" style="margin:0">
                <label class="gt-form-label">Agency / Company Name</label>
                <input type="text" name="wl_agency_name" class="gt-input"
                       value="<?php echo esc_attr($wl['agency_name'] ?? ''); ?>"
                       placeholder="Your Company Name">
                <div class="gt-form-hint">Shown in footer and support links</div>
            </div>
        </div>
    </div>

    <!-- Support Links -->
    <div class="gt-card gt-animate-in" style="margin-bottom:20px">
        <div class="gt-card-header">
            <h2 class="gt-card-title">Support & Documentation Links</h2>
        </div>
        <div class="gt-feature-grid gt-feature-grid--2col">
            <div class="gt-form-group" style="margin:0">
                <label class="gt-form-label">Support URL</label>
                <input type="url" name="wl_support_url" class="gt-input"
                       value="<?php echo esc_attr($wl['support_url'] ?? ''); ?>"
                       placeholder="https://youragency.com/support">
                <div class="gt-form-hint">Replaces all GT BOGO support links</div>
            </div>
            <div class="gt-form-group" style="margin:0">
                <label class="gt-form-label">Documentation URL</label>
                <input type="url" name="wl_docs_url" class="gt-input"
                       value="<?php echo esc_attr($wl['docs_url'] ?? ''); ?>"
                       placeholder="https://youragency.com/docs">
                <div class="gt-form-hint">Replaces documentation links</div>
            </div>
        </div>
    </div>

    <!-- Hide Options -->
    <div class="gt-card gt-animate-in" style="margin-bottom:20px">
        <div class="gt-card-header">
            <h2 class="gt-card-title">Hide Options</h2>
        </div>
        <div style="display:flex;flex-direction:column">
            <div class="gt-setting-row">
                <div>
                    <div class="gt-setting-label">Hide GT BOGO Branding</div>
                    <div class="gt-setting-desc">Removes GT BOGO Engine logos, footer credits, and branding throughout the plugin</div>
                </div>
                <label class="gt-toggle">
                    <input type="checkbox" name="wl_hide_branding" value="1" <?php checked(!empty($wl['hide_branding'])); ?>>
                    <span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span>
                </label>
            </div>
            <div class="gt-setting-row" style="border:none">
                <div>
                    <div class="gt-setting-label">Hide Upgrade / Purchase Prompts</div>
                    <div class="gt-setting-desc">Removes all PRO upgrade CTAs — ideal for client sites already on PRO</div>
                </div>
                <label class="gt-toggle">
                    <input type="checkbox" name="wl_hide_upgrade" value="1" <?php checked(!empty($wl['hide_upgrade'])); ?>>
                    <span class="gt-toggle-track"><span class="gt-toggle-thumb"></span></span>
                </label>
            </div>
        </div>
    </div>

    <div class="gt-card gt-animate-in">
        <div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:16px">
            <div>
                <div style="font-size:14px;font-weight:600;color:var(--gt-white)">Ready to save?</div>
                <div class="gt-text-soft" style="font-size:13px">White label changes take effect after saving. Reload the page to see the updated branding.</div>
            </div>


<script>
document.getElementById('emailAccentPicker').addEventListener('input', function() {
    document.getElementById('emailAccentText').value = this.value;
    document.querySelector('[name=email_accent_color_override]').value = this.value;
});
document.querySelectorAll('[name=email_theme]').forEach(function(radio) {
    radio.addEventListener('change', function() {
        document.querySelectorAll('.gt-theme-swatch').forEach(function(s) {
            s.style.outline = 'none';
        });
        this.closest('.gt-theme-swatch').style.outline = '2px solid var(--gt-red)';
    });
});
</script>

            <button type="submit" name="gtbogo_save_wl" class="gt-btn gt-btn-primary" style="padding:12px 32px">💾 Save White Label Settings</button>
        </div>
    </div>
</form>
<?php endif; ?>

<div class="gt-card gt-animate-in" style="margin-bottom:20px;background:linear-gradient(135deg,rgba(59,130,246,0.06),rgba(7,12,28,0));border-color:rgba(59,130,246,0.15)">
    <div class="gt-card-header"><h2 class="gt-card-title">💡 Agency White Label Guide</h2><span class="gt-text-soft" style="font-size:13px">How to deploy GT BOGO Engine under your own brand</span></div>
    <div class="gt-feature-grid gt-feature-grid--2col" style="gap:12px">
        <div style="background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:16px">
            <div style="font-size:15px;font-weight:700;color:var(--gt-white);margin-bottom:8px">🏷️ Naming Convention</div>
            <p style="font-size:13px;color:var(--gt-soft);line-height:1.6;margin:0">Name the plugin something generic and professional — Promotions Manager or Deals Engine works well. Your agency name in the footer gives you credit without exposing the underlying product to your client.</p>
        </div>
        <div style="background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:16px">
            <div style="font-size:15px;font-weight:700;color:var(--gt-white);margin-bottom:8px">🔗 Support URL Strategy</div>
            <p style="font-size:13px;color:var(--gt-soft);line-height:1.6;margin:0">Point the support URL to your own help desk, not GT BOGO Engine support. You control the client relationship and filter issues before escalating. This also lets you charge a support retainer on top of the plugin fee.</p>
        </div>
        <div style="background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:16px">
            <div style="font-size:15px;font-weight:700;color:var(--gt-white);margin-bottom:8px">💰 Pricing Your White Label</div>
            <p style="font-size:13px;color:var(--gt-soft);line-height:1.6;margin:0">With a $149 per year PRO license you can charge clients $299 to $499 per year for Promotions Manager PRO. Enable Hide Upgrade Prompts so clients never see competing purchase CTAs inside the dashboard.</p>
        </div>
        <div style="background:rgba(255,255,255,0.02);border:1px solid var(--gt-border);border-radius:10px;padding:16px">
            <div style="font-size:15px;font-weight:700;color:var(--gt-white);margin-bottom:8px">📋 Client Onboarding</div>
            <p style="font-size:13px;color:var(--gt-soft);line-height:1.6;margin:0">Use Import/Export to pre-configure rule templates per client vertical — fashion, food, electronics. Export your master config, import it on client sites, and have them live with BOGO deals in under 10 minutes. Charge a setup fee for that time.</p>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../ui-system/wrapper-end.php'; ?>
