<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
include_once SFTCY_WORDAI_PLUGIN_DIR . 'admin/views/settings-pages-header-template.php';
$wordai_pro_license_key = get_option('softcoy_wordai_pro_license_key', '' );
$wordai_pro_license_key = empty( $wordai_pro_license_key ) ? $wordai_pro_license_key : SftcyWordaiBase::license_key_mask_format( $wordai_pro_license_key );
$wordai_pro_license_data = get_option( 'wordai_pro_license_data', [] );

// echo '<br/><br/><br/><pre>';
// print_r( $wordai_pro_license_data );
// echo '</pre>';

$renew_license_url = isset( $wordai_pro_license_data['licenseRenewUrl'] )? $wordai_pro_license_data['licenseRenewUrl'] : '';
$update_payment_method = isset( $wordai_pro_license_data['subscriptionPaymentUpdateUrl'] )? esc_url_raw( $wordai_pro_license_data['subscriptionPaymentUpdateUrl'] ) : '';
$is_license_expired = isset( $wordai_pro_license_data['isLicenseExpired'] )? $wordai_pro_license_data['isLicenseExpired'] : 0; 
// Every time checking next billed date from payment server info while refreshing - first check 'subscriptionNextBilledAt', if not available then 'expiredDateTime'
$plugin_expired_datetime = ( isset( $wordai_pro_license_data['subscriptionNextBilledAt'] ) && ! empty( $wordai_pro_license_data['subscriptionNextBilledAt'] ) )? $wordai_pro_license_data['subscriptionNextBilledAt'] : $wordai_pro_license_data['expiredDateTime'];
?>

<!-- ==============================================
   Start admin License Page
   ============================================== -->
<div class="wrap">    
    
    <h2 class="license-page-header">
        <i class="fa-solid fa-key"></i> WordAI License & Pro Upgrade
    </h2>

    <form id="scWordAILicenseKey" method="post">
        <table>
            <tbody>
                <!-- Support Info Row -->
                <tr>
                    <td class="license-page-support-info" colspan="2">
                        <span class="dashicons dashicons-smiley"></span>
                        Validate your WordAI Pro license key here, then install the Pro package without leaving WordPress. Any kind of query / help please contact 
                        <a href="https://www.softcoy.com/support" target="_blank">support</a>.
                    </td>
                </tr>

                <!-- License Key Input Row -->
                <tr class="wordai-license-key-row">
                    <td colspan="2">
                        <div class="sc-wordai-license-message-settings-info"></div>
                        <div class="license-key-card">
                            <input type="text" value="" name="wordai-pro-license-key" class="wordai-pro-license-key" placeholder="<?php echo esc_attr( strtoupper( $wordai_pro_license_key ) ); ?>" autocomplete="off" />
                            <div class="license-action-wrapper">
                                <button type="submit" class="button button-primary save-license-key-btn">
                                    <i class="fa-solid fa-floppy-disk"></i> <?php esc_html_e( 'Save', 'wordai-pro' ); ?>
                                </button>
                                <button type="button" class="button button-secondary save-license-key-btn sc-wordai-upgrade-to-pro-btn">
                                    <i class="fa-solid fa-arrow-up-right-dots"></i> <?php esc_html_e( 'Upgrade to Pro', 'wordai-pro' ); ?>
                                </button>
                            </div>
                        </div>
                        <small style="display:block; margin-top:12px; color:#475467;">
                            <?php esc_html_e( 'A successful upgrade installs the latest WordAI Pro package from SOFTCOY, deactivates the free plugin, and activates the Pro plugin with the same saved license key.', 'wordai-pro' ); ?>
                        </small>
                    </td>
                </tr>

                <!-- License Status & Remaining Days Row -->
                <tr class="wordai-license-status-row">
                    <td colspan="2">
                        <div class="license-info-wrapper">
                            <div class="license-status-wrapper">
                                <?php
                                if ( isset( $plugin_expired_datetime ) ) { 
                                    $current_gmt_date_time = gmdate( 'Y-m-d H:i:s' );
                                    $current_date_time = new DateTimeImmutable( $current_gmt_date_time );
                                    $expired_date_time = new DateTimeImmutable( $plugin_expired_datetime );
                                    $interval = $current_date_time->diff( $expired_date_time );                        
                                    $remaining_days = (int) $interval->format("%r%a");                                                 
                                }

                                if ( ! isset( $remaining_days ) ) {
                                    $license_status = '<div class="text-warning"><i class="fa-solid fa-circle-exclamation"></i> Add a valid license key</div>';                        
                                    $license_status .= '<small><strong>(Save a WordAI Pro license key to register this site and unlock one-click upgrade access.)</strong></small>';
                                } else if ( $remaining_days <= 0 ) {
                                    $license_status = '<div class="text-error"><i class="fa-solid fa-circle-xmark"></i> License Expired</div>';
                                    $license_status .= '<small><strong>(Renew the subscription before trying to install or update WordAI Pro.)</strong></small>';
                                    $license_status .= '<div class="expired-renew-license"><a href="' . $update_payment_method . '" target="_blank">Keep payment updated</a></div>';
                                    $license_status .= '<div class="expired-renew-license"><a href="' . $renew_license_url . '" target="_blank">Renew License</a></div>';
                                } else if ( $remaining_days > 0 ) {
                                    if ( $remaining_days <= 60 && $remaining_days > 30 ) {
                                        $license_status = '<div class="text-success"><i class="fa-solid fa-circle-check"></i> License Active</div>';
                                        $license_status .= '<small><strong>(This key is ready for secure Pro install, updates, and support access.)</strong></small>';
                                        $license_status .= '<div class="advance-renew-license"><a class="text-info" href="' . $update_payment_method . '" target="_blank">Keep payment updated</a></div>';
                                    } else if ( $remaining_days <= 30 ) {
                                        $license_status = '<div class="text-success"><i class="fa-solid fa-circle-check"></i> License Active</div>';
                                        $license_status .= '<small><strong>(The license is active, but renewal is close. Refresh the data after billing changes.)</strong></small>';
                                        $license_status .= '<div class="advance-renew-license"><a class="text-warning" href="' . $update_payment_method . '" target="_blank">Keep payment updated</a></div>';
                                    } else {
                                        $license_status = '<div class="text-success"><i class="fa-solid fa-circle-check"></i> License Active</div>';
                                        $license_status .= '<small><strong>(This site is ready to install WordAI Pro from the SOFTCOY delivery endpoint.)</strong></small>';
                                    }
                                }

                                echo $license_status;
                                ?>
                                <div class="license-days-left"><i class="fa-solid fa-calendar-days"></i> Remaining Day(s): <?php echo ( $remaining_days <= 0 ) ? 0 : $remaining_days; ?></div>
                            </div>

                            <!-- Refresh Button -->
                            <div class="sc-wordai-license-info-refresh-btn-text">
                                <span class="dashicons dashicons-update"></span> Refresh
                            </div>
                        </div>
                    </td>
                </tr>
            </tbody>	
        </table>
    </form>	
</div>
<!-- ==============================================
   End admin License Page
   ============================================== -->
