<?php 
$pcfme_license_settings    = (array) get_option('pcfme_license_settings');
$licensing_url               = 'https://api.envato.com/authorization?response_type=code&client_id=sysbasics-license-activation-jvykxxug&redirect_uri=https://www.sysbasics.com/verify-purchase/';
?> 

<table class="widefat pcfme_options_table">

	

	<tr>
		<td><label><?php echo esc_html__('Domain Validation Token','pcfme'); ?></label>
		</td>
		<td>
			<input type="text" class="pcfme_license_key_input" data-toggle="toggle" data-size="xs" name="pcfme_license_settings[license_key]" value="<?php if (isset($pcfme_license_settings['license_key']) ) { echo $pcfme_license_settings['license_key']; } ?>" size="80">


			<?php

			$license_key = '';

			if (isset($pcfme_license_settings['license_key']) ) { 
				$license_key=$pcfme_license_settings['license_key']; 
			}

			
               $activate_button_visible = 'no';
               $domain_status = "inactive";
			
               $input = $_SERVER['SERVER_NAME'];

   
			$input = trim($input, '/');


			if (!preg_match('#^http(s)?://#', $input)) {
				$input = 'http://' . $input;
			}

			$urlParts = parse_url($input);


			$domain_name = preg_replace('/^www\./', '', $urlParts['host']);

			echo '<table class="widefat sysbasics-license-table">';

			

                  echo '<tr><td>'.esc_html__('Domain','pcfme').'</td><td>'.$domain_name.'</td></tr>';

                  if (!isset($license_key) || ($license_key == "")) {
                       echo '<tr><td>'.esc_html__('Activate your token to enable auto updates.Please remember that envato item purchase code is different from domain validation token.','pcfme').'</td></tr>';
			}


			$siteurl = pcfme_get_siteurl();

			

			$json_url = 'https://www.sysbasics.com/wp-json/wp/v2/check-validation?domain='.$domain_name.'&code='.$license_key.'&tid=9799777&siteurl='. $siteurl.'';

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_URL, $json_url);
			$result = curl_exec($ch);
			curl_close($ch);

			$obj = json_decode($result);
			$match_found = $obj->match_found;

			

			if (isset($match_found) && ($match_found == "yes")) {

				$detected_domain = $obj->domain;

				$item_id = $obj->item_id;

				if (($detected_domain == $domain_name) && ($item_id == 9799777)) {

					$supported_until = $obj->supported_until;

                         $usage_validity = $obj->usage_validity;

                         if ($usage_validity == 20500101) {
                         	$usage_validity_txt = esc_html__('Lifetime','pcfme');
                         } else {
                         	$usage_validity_txt = date('Y-m-d',strtotime($usage_validity));
                         }

					$supported_until = date("d M Y",strtotime($supported_until));

					$domain_status = "active";

					delete_option('pcfme_install_e');

					add_option('pcfme_install_e','64');

					echo '<tr><td>'.esc_html__('Status','pcfme').'</td><td>Active</td></tr>';

					echo '<tr><td>'.esc_html__('Support Expiry','pcfme').'</td><td>'.$supported_until.'</td></tr>';

					echo '<tr><td>'.esc_html__('Usage Valid till','pcfme').'</td><td>'.$usage_validity_txt.'</td></tr>';
				}

			} else {
				$reason = $obj->reason;
				echo '<tr><td>'.esc_html__('Error','pcfme').'</td><td>'.$reason.'</td></tr>';

				echo '<tr><td>'.esc_html__('Status','pcfme').'</td><td>Inactive</td></tr>';

				delete_option('pcfme_install_e');

				add_option('pcfme_install_e','32');
			}





            if ($domain_status == "inactive") {

            	

            	

            	echo '<tr><td>'.esc_html__('Instruction','pcfme').'</td><td>'.esc_html__('Please enter valid domain validation token and Save Changes.Make sure you are maping same domain name as shown above.','pcfme').'</td></tr>';

            	?>


            	<tr class="sysbasics_licensing_span">
            		<td><?php echo esc_html__('Tip','pcfme'); ?></td>
            		<td><?php echo esc_html__('Please Remember that Domain Validation Key is different from envato item purchase code.If you do not have Domain Validation Key click on Get Domain Validation Key button.','pcfme'); ?></td>

            	</tr>

            	<?php

            	delete_option('pcfme_domain_validated');
            } 
             
             echo '</table>';
			

            $is_domain_validated = get_option("pcfme_domain_validated");



            if ((!isset($is_domain_validated) || ($is_domain_validated != "yes")) && ($activate_button_visible != 'yes') || (($domain_status == "inactive") )) {

            	if ($domain_status != "active") {
            		echo '<button href="#" class="btn btn-success pcfme_activate_license"><span class="dashicons dashicons-saved"></span>'.esc_html__('Activate','pcfme').'</button>';
            	}
            	
            }      

			?>

			<a target="_blank" href="#" data-toggle="modal" data-target="#sysbasics_get_validation_tkn" class="sysbasics-token-page btn btn-warning"> 
			<span class="dashicons dashicons-admin-network sysbasics-manage-token"></span>              
				<?php 

				if ($domain_status == "active") {
                    echo esc_html__('Manage Tokens','pcfme');
				} else {
					echo esc_html__('Get Validation Token','pcfme');
				}
				?>
					
			</a>
			<?php

			if ((!isset($is_domain_validated) || ($is_domain_validated != "yes")) && ($activate_button_visible != 'yes') || (($domain_status == "inactive") )) {

            	if ($domain_status != "active") {
            		echo '&emsp;<a target="_blank" href="https://sysbasics.com/go/license-doc/" class="btn btn-success sysbasics-license-doc"><span class="dashicons dashicons-admin-settings"></span>&emsp;'.esc_html__('Documentation','pcfme').'</a>&emsp;';

            		echo '<a href="https://www.sysbasics.com/support/" target="_blank" class="btn btn-primary sysbasics-license-doc"><span class="dashicons  dashicons-admin-tools"></span>&emsp;'.esc_html__('Contact Support','pcfme').'</a>&emsp;';
            	}
            	
            }
            ?>	
			
			
		</td>
	</tr>

	
</table>
<div class="modal fade" id="sysbasics_get_validation_tkn" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">

			<div class="modal-body">

				<p class="sysbasics_purchased_through"><?php echo esc_html__( 'Purchased from' ,'customize-my-account-for-woocommerce-pro'); ?></p>

				

				<a type="button" target="_blank" href="https://www.sysbasics.com/my-account/orders" name="submit" id="wcmamtx_frontend_link" class="btn btn-success wcmamtx_frontend_link" >
					<span class="dashicons dashicons-lock"></span>
					<?php echo esc_html__( 'SYSBASICS.com' ,'customize-my-account-for-woocommerce-pro'); ?>
				</a>

				<a type="button" target="_blank" href="<?php echo $licensing_url; ?>" name="submit" id="wcmamtx_frontend_link" class="btn btn-primary wcmamtx_frontend_link" >
					<span class="dashicons dashicons-lock"></span>
					<?php echo esc_html__( 'Envato Market (CodeCanyon)' ,'customize-my-account-for-woocommerce-pro'); ?>
				</a>



			</div>
			<div class="modal-footer">

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