<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}
include_once SFTCY_WORDFORM_PLUGIN_DIR . 'admin/views/pages-header-template.php';
$wordform_pro_license_key = get_option('softcoy_wordform_pro_license_key', '' );
$wordform_pro_license_key = empty( $wordform_pro_license_key ) ? $wordform_pro_license_key : SFTCY_Wordform::wordform_pro_license_key_mask_format( $wordform_pro_license_key );
$wordform_pro_license_data = get_option( 'wordform_pro_license_data', [] );
$renew_license_url = isset( $wordform_pro_license_data['licenseRenewUrl'] )? $wordform_pro_license_data['licenseRenewUrl'] : '';
$update_payment_method = isset( $wordai_pro_license_data['subscriptionPaymentUpdateUrl'] )? $wordai_pro_license_data['subscriptionPaymentUpdateUrl'] : '';
$is_license_expired = isset( $wordform_pro_license_data['isLicenseExpired'] )? $wordform_pro_license_data['isLicenseExpired'] : 0; 
$plugin_expired_datetime = ( isset( $wordform_pro_license_data['subscriptionNextBilledAt'] ) && ! empty( $wordform_pro_license_data['subscriptionNextBilledAt'] ) )? $wordform_pro_license_data['subscriptionNextBilledAt'] : $wordform_pro_license_data['expiredDateTime'];

// echo '<br/><br/><br/></pre>';
// print_r( $wordform_pro_license_data );
// echo '</pre>';
?>
<div class="wrap" style="margin: 1%;">
<h2><i class="fa-solid fa-gear"></i> WordForm Pro License</h2>
<form id="scWordFormLicenseKey" method="post">
	<table>
		<tbody>
            <tr>
                <td colspan="2" style="text-align: end;">
                    <span class="dashicons dashicons-smiley"></span> Any kind of query / help please contact <a href="https://www.softcoy.com/support" target="_blank">support</a>.
                </td>   				                
            </tr>

			<tr>
                <td class="wordform-license-key-label-td">
                    <h2>
                        <i class="fa-solid fa-key"></i>
                        <?php esc_html_e( 'License Key', 'wordform-pro' ); ?>
                    </h2>   					
                </td>
                <td class="wordform-license-key-td">   	
                    <input type="text" value="" name="wordform-pro-license-key" class="wordform-pro-license-key" placeholder="<?php echo esc_attr( strtoupper( $wordform_pro_license_key ) ); ?>" autocomplete="off" />				
                </td>   				
            </tr>
            <tr>
                <td colspan="2" style="text-align: end;">
                    <button type="submit" class="button button-primary button-large save-license-key-btn"><i class="fa fa-save"></i> <?php esc_html_e( 'Save', 'wordform-pro' ); ?></button>
                    <div class="sc-wordform-license-message-settings-info" style="padding: 7px;"></div>
                </td>
            </tr>

            <tr>
                <td colspan="2" style="text-align: end;">                    
                    <?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 );
                        // $expired_date_time = new DateTimeImmutable( $test_expired_date );
                        $interval = $current_date_time->diff( $expired_date_time );                        
                        $remaining_days = (int) $interval->format("%r%a");             
                    }           
                    // Activate License                    
                    if ( ! isset( $remaining_days ) ) {
                        $license_status = '<div class="text-warning"><i class="fa-solid fa-circle-info"></i> Activate License</div>';                        
                        $license_status .= '<small><strong>(To receive new features update & support please Activate your License)<strong></small>';
                    }
                    // Expired License
                    else if ( $remaining_days <= 0 ) {                        
                        $license_status = '<div class="text-error"><i class="fa-solid fa-circle-info"></i> License Expired</div>';
                        $license_status .= '<small><strong>(To receive new features update & support smoothly please keep your Payment method updated)<strong></small>';
                        $license_status .= '<div class="expired-renew-license"><a href="' . $update_payment_method . '" target="_blank">Update Payment Method</a></div>';
                        $license_status .= '<div class="expired-renew-license"><a href="' . $renew_license_url . '" target="_blank">Renew License</a></div>';
                    }
                    // License Active
                    else if ( $remaining_days > 0 ) {
                        if ( $remaining_days <= 60 && $remaining_days > 30 ) {
                            $license_status = '<div class="text-success"><i class="fa-solid fa-check"></i> License Active</div>';
                            $license_status .= '<small><strong>(To receive new features update & support smoothly please keep your Payment method updated)<strong></small>';
                            $license_status .= '<div class="advance-renew-license"><a class="text-info" href="' . $update_payment_method . '" target="_blank">Update Payment Method</a></div>';
                        }
                        else if ( $remaining_days <= 30 ) {
                            $license_status = '<div class="text-success"><i class="fa-solid fa-check"></i> License Active</div>';
                            $license_status .= '<small><strong>(To receive new features update & support smoothly please keep your Payment method updated)<strong></small>';
                            $license_status .= '<div class="advance-renew-license"><a class="text-warning" href="' . $update_payment_method . '" target="_blank">Update Payment Method</a></div>';
                        }
                        else {
                            $license_status = '<div class="text-success"><i class="fa-solid fa-check"></i> License Active</div>';
                        }
                    }

                    echo $license_status;
                    ?>   
                    <br/>                 
                    <div class="license-days-left">Remaining Day(s): <?php echo ( $remaining_days <= 0 ) ? 0 : $remaining_days; ?></div>
                </td>
            </tr>

            <tr>
                <td colspan="2" style="text-align: end;">                    
                    <div class="sc-wordform-license-info-refresh-btn-text"><span class="dashicons dashicons-update"></span> Refresh</div>
                </td>
            </tr>



		</tbody>	
	</table>
</form>	
</div><!-- /.wrap -->