<?php
// Exit if accessed directly
if (!defined('ABSPATH')) exit;
// Fetch license data
$license_key   = get_option('gamadsmbai_license_key', '');
$token         = get_option('gamadsmbai_license_token', '');
$plan          = get_option('gamadsmbai_license_plan', '');
$expires_at    = get_option('gamadsmbai_expires_at', '');
$activated_at  = get_option('gamadsmbai_activated_at', '');
$status        = get_option('gamadsmbai_license_status', 'inactive');

// Determine license state
$today_ts = strtotime(gmdate('Y-m-d', current_time('timestamp')));
$expiry_state = 'inactive';
$masked_key = $license_key
    ? str_repeat('*', max(0, strlen($license_key) - 4)) . substr($license_key, -4)
    : '';
$expiry_text = "Soon";
if (!empty($license_key) && !empty($expires_at)) {
    $expires_at = trim($expires_at);
    $expiry_ts  = strtotime($expires_at);
    if ($expiry_ts !== false) {
        $days_left = floor(($expiry_ts - $today_ts) / DAY_IN_SECONDS);
        if ($expires_at === gmdate('Y-m-d', current_time('timestamp'))) {
            $expiry_text = 'Today';
        }
        if ($days_left < 0) {
            $expiry_state = 'expired';
        } elseif ($days_left <= 7) {
            $expiry_state = 'expiring';
        } else {
            $expiry_state = 'active';
        }
    }
}

?>

<div class="premium-wrap">
    <!-- License Notice -->
    <?php if ($expiry_state === 'expired'): ?>
        <div class="premium-notice premium-error">
            <strong>⚠ License Expired</strong> — Your license expired on <strong><?php echo esc_html($expires_at); ?></strong>.
            Please renew to continue receiving premium updates and features.
        </div>
    <?php elseif ($expiry_state === 'expiring'): ?>
        <div class="premium-notice premium-warning">
            <strong>⏳ License Expiring <?php echo esc_html($expiry_text) ?></strong> — Expires on <strong><?php echo esc_html($expires_at); ?></strong>.
            Please renew to avoid interruption.
        </div>
    <?php endif; ?>

    <div class="prmm-container">
        <?php if (empty($license_key) || empty($token)): ?>
            <!-- License Input Form -->
            <form class="lic-key-frm">
                <small>Plugin License Key</small>
                <div class="lic-key-inp-box">
                    <input type="text" name="license_key" required>
                </div>
                <div class="msgbox">
                    <svg style="display:none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" width="200" height="200" style="shape-rendering: auto; display: block; background: rgb(255, 255, 255);">
                        <g>
                            <g transform="rotate(0 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.9375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(22.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(45 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.8125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(67.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.75s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(90 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.6875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(112.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(135 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.5625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(157.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.5s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(180 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.4375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(202.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(225 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.3125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(247.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.25s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(270 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.1875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(292.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(315 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="-0.0625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g transform="rotate(337.5 50 50)">
                                <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                    <animate repeatCount="indefinite" begin="0s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                                </rect>
                            </g>
                            <g />
                        </g>
                    </svg>
                    <div id="valdtnmsgbx"></div>
                </div>
                <button>save</button>
            </form>
            <div class="new-lic-key-buy">
                <p>Don't have a license key?</p>
                <?php
                $url = add_query_arg(
                    [
                        'email'   => get_option('admin_email'),
                        'website' => get_site_url(),
                    ],
                    'https://gamadinserter.com/#pricing'
                );
                ?>
                <a target="_blank" href="<?php echo esc_url($url); ?>">
                    Buy Premium
                </a>
            </div>
        <?php else: ?>
            <!-- License Status Box -->
            <div class="lic-status-box">
                <h4>License Status</h4>
                <table class="lic-status-table">
                    <tbody>
                        <tr>
                            <th>Status</th>
                            <td>
                                <?php if ($expiry_state === 'active'): ?>
                                    <span class="status-badge active">Active</span>
                                <?php elseif ($expiry_state === 'expiring'): ?>
                                    <span class="status-badge expiring">Expiring <?php echo esc_html($expiry_text) ?></span>
                                <?php else: ?>
                                    <span class="status-badge expired">Expired</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <tr>
                            <th>License Key</th>
                            <td>
                                <span class="masked-key"><?php echo esc_html($masked_key); ?></span>
                            </td>
                        </tr>
                        <tr>
                            <th>Plan</th>
                            <td>
                                <?php echo esc_html(ucfirst($plan)); ?>
                            </td>
                        </tr>
                        <tr>
                            <th>Activated On</th>
                            <td>
                                <?php echo esc_html($activated_at); ?>
                            </td>
                        </tr>
                        <tr>
                            <th>Expires On</th>
                            <td>
                                <?php echo esc_html($expires_at); ?>
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <div class="lic-action-row">
                <?php if ($expiry_state !== 'active'): ?>
                    <a class="renew-btn" href="https://yourdashboard.com/account" target="_blank">
                        Renew License
                    </a>
                <?php else: ?>
                    <a class="renew-btn disabled" href="javascript:void(0)">
                        License Active
                    </a>
                <?php endif; ?>

                <button type="button" class="remove-lic-btn" id="gam-remove-license" data-status="<?php echo esc_attr($expiry_state); ?>">
                    Revoke License
                </button>

                <button type="button" class="refresh-lic-btn" id="gam-refresh-license">
                    Refresh Status
                </button>
            </div>
            <div class="msgbox">
                <svg style="display:none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 100" preserveAspectRatio="xMidYMid" width="200" height="200" style="shape-rendering: auto; display: block; background: rgb(255, 255, 255);">
                    <g>
                        <g transform="rotate(0 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.9375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(22.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(45 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.8125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(67.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.75s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(90 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.6875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(112.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(135 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.5625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(157.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.5s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(180 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.4375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(202.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.375s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(225 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.3125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(247.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.25s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(270 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.1875s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(292.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.125s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(315 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="-0.0625s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g transform="rotate(337.5 50 50)">
                            <rect fill="#d7d7d7" height="12" width="4" ry="3.6" rx="2" y="28" x="48">
                                <animate repeatCount="indefinite" begin="0s" dur="1s" keyTimes="0;1" values="1;0" attributeName="opacity" />
                            </rect>
                        </g>
                        <g />
                    </g>
                </svg>
                <div id="lic-remove-msg" class="lic-action-msg"></div>
            </div>
        <?php endif; ?>
    </div>
</div>