<?php
include "../../../wp-config.php";
if (!is_user_logged_in() ) {
    // exit();
}

//// 60 Days License Expiry Alerts ////
echo "<h3>60 Days License Expiry Alerts</h3>";
echo "<strong>Date: </strong>". gmdate("d/m/Y", strtotime('+ 1444 hours')); // UAE +4 hours and date +60 days
echo "<br />";echo "<br />";
$expiry_date = gmdate("Ymd", strtotime('+ 1444 hours')); // UAE +4 hours and date +60 days
query_merge_email("license", $expiry_date,60);
////// 60 Days Establishment Card Expiry Alerts /////
echo "<h3>60 Days Establishment Card Expiry Alerts</h3>";
query_merge_email("establishment_card", $expiry_date,60);

//// 30 Days License Expiry Alerts ////
echo "<h3>30 Days License Expiry Alerts</h3>";
echo "<strong>Date: </strong>". gmdate("d/m/Y", strtotime('+ 724 hours')); // UAE +4 hours and date +30 days
echo "<br />";echo "<br />";
$expiry_date = gmdate("Ymd", strtotime('+ 724 hours')); // UAE +4 hours and date +30 days
query_merge_email("license", $expiry_date,30);
////// 30 Days Establishment Card Expiry Alerts /////
echo "<h3>30 Days Establishment Card Expiry Alerts</h3>";
query_merge_email("establishment_card", $expiry_date,30);


//// 15 Days License Expiry Alerts ////
echo "<h3>15 Days License Expiry Alerts</h3>";
echo "<strong>Date: </strong>". gmdate("d/m/Y", strtotime('+ 364 hours')); // UAE +4 hours and date +15 days
echo "<br />";echo "<br />";
$expiry_date = gmdate("Ymd", strtotime('+ 364 hours')); // UAE +4 hours and date +15 days
query_merge_email("license", $expiry_date,15);
////// 15 Days Establishment Card Expiry Alerts /////
echo "<h3>15 Days Establishment Card Expiry Alerts</h3>";
query_merge_email("establishment_card", $expiry_date,15);


//// 7 Days License Expiry Alerts ////
echo "<h3>7 Days License Expiry Alerts</h3>";
echo "<strong>Date: </strong>". gmdate("d/m/Y", strtotime('+ 172 hours')); // UAE +4 hours and date +7 days
echo "<br />";echo "<br />";
$expiry_date = gmdate("Ymd", strtotime('+ 172 hours')); // UAE +4 hours and date +7 days
query_merge_email("license", $expiry_date,7);
////// 15 Days Establishment Card Expiry Alerts /////
echo "<h3>7 Days Establishment Card Expiry Alerts</h3>";
query_merge_email("establishment_card", $expiry_date,7);

function query_merge_email($type, $expiry_date,$days){
	$args = array(
		'post_type'      => 'cus_rec', 'posts_per_page' => 10000, 'meta_query'     => array(
			array('key'     => $type.'_expiry_date','value'   => $expiry_date,'compare' => '=','type'    => 'NUMERIC',), ),
		);
		// print_r ($args);
		// echo "<br/>";
	$query = new WP_Query($args);

	while ($query->have_posts()) : $query->the_post();
	    $email_subject = "";
	    $email_subject = get_field($days.'_days_'.$type.'_alert_email_subject', 'option');
			$email_subject = str_replace("{name}",get_the_title(), $email_subject);
			$email_subject = str_replace("{email}",get_field("email"), $email_subject);
			$email_subject = str_replace("{address}",get_field("address"), $email_subject);
			$email_subject = str_replace("{cell_number}",get_field("cell_number"), $email_subject);
			$email_subject = str_replace("{company}",get_field("company"), $email_subject);
			$email_subject = str_replace("{license_package}",get_field("license_package"), $email_subject);
			$email_subject = str_replace("{license_type}",get_term( get_field("license_type") )->name, $email_subject);
			$email_subject = str_replace("{license_issue_date}",get_field("license_issue_date"), $email_subject);
			$email_subject = str_replace("{license_expiry_date}",get_field("license_expiry_date"), $email_subject);
			$email_subject = str_replace("{share_holders}",get_term( get_field("share_holders") )->name, $email_subject);
			$email_subject = str_replace("{no_of_visa}",get_field("no_of_visa"), $email_subject);
			$email_subject = str_replace("{visa_type}",get_term( get_field("visa_type") )->name, $email_subject);
			$email_subject = str_replace("{visa_issue_date}",get_field("visa_issue_date"), $email_subject);
			$email_subject = str_replace("{visa_expiry_date}",get_field("visa_expiry_date"), $email_subject);
			$email_subject = str_replace("{establishment_card_issue_date}",get_field("establishment_card_issue_date"), $email_subject);
			$email_subject = str_replace("{establishment_card_expiry_date}",get_field("establishment_card_expiry_date"), $email_subject);
			echo $email_subject;
			
			echo "<br />";
			$email_body = "";
	    $email_body = get_field($days.'_days_'.$type.'_alert_email_body', 'option');
			$email_body = str_replace("{name}",get_the_title(), $email_body);
			$email_body = str_replace("{email}",get_field("email"), $email_body);
			$email_body = str_replace("{address}",get_field("address"), $email_body);
			$email_body = str_replace("{cell_number}",get_field("cell_number"), $email_body);
			$email_body = str_replace("{company}",get_field("company"), $email_body);
			$email_body = str_replace("{license_package}",get_field("license_package"), $email_body);
			$email_body = str_replace("{license_type}",get_term( get_field("license_type") )->name, $email_body);
			$email_body = str_replace("{license_issue_date}",get_field("license_issue_date"), $email_body);
			$email_body = str_replace("{license_expiry_date}",get_field("license_expiry_date"), $email_body);
			$email_body = str_replace("{share_holders}",get_term( get_field("share_holders") )->name, $email_body);
			$email_body = str_replace("{no_of_visa}",get_field("no_of_visa"), $email_body);
			$email_body = str_replace("{visa_type}",get_term( get_field("visa_type") )->name, $email_body);
			$email_body = str_replace("{visa_issue_date}",get_field("visa_issue_date"), $email_body);
			$email_body = str_replace("{visa_expiry_date}",get_field("visa_expiry_date"), $email_body);
			$email_body = str_replace("{establishment_card_issue_date}",get_field("establishment_card_issue_date"), $email_body);
			$email_body = str_replace("{establishment_card_expiry_date}",get_field("establishment_card_expiry_date"), $email_body);
			echo $email_body;

			$headers[] = 'From: '.get_option( 'blogname' ).' <'.get_option( 'admin_email' ).'>';
			$headers[] = 'Content-Type: text/html; charset=UTF-8';
			// wp_mail( $email, $email_subject, $email_body, $headers );
			$comments = get_field('comments');
			$comments = gmdate("d/m/Y", strtotime('+ 4 hours'))." : Alert sent for ".strtoupper(str_replace('_',' ',$type))." ".$days." days before expiry ( ".get_field($type."_expiry_date")." )<br />".$comments;
			update_field('comments', $comments); // update comments of the post

			$cron_job_log = get_field('cron_job_log','option');
			$cron_job_log = gmdate("d/m/Y", strtotime('+ 4 hours'))." : Alert sent to ".get_the_title()." ( ".get_field("email")." ) for ".strtoupper(str_replace('_',' ',$type))." ".$days." days before expiry ( ".get_field($type."_expiry_date")." )<br />".$cron_job_log;
			update_field('cron_job_log', $cron_job_log,'option'); // update cron job log

			echo "<br />";
	endwhile;
}
?>
