<?php
 error_reporting(E_ERROR | E_PARSE);
 include ('includes/config/config.php');
 include ('includes/functions.php');
 include('log_entry.php');
?>
<script src="assets/js/jquery-2.1.4.min.js"></script>


<script>
function formatDate(input) {
    // alert(input);
    var datePart = input.match(/\d+/g),
        year = datePart[0],
        month = datePart[1],
        day = datePart[2];
    return day+'/'+month+'/'+year;
}


function open_mail(mail,name,ohc,appointmentId,app_date) {
    app_date=formatDate(app_date);
    var subject='Chronic Medication Reminder.';
    var message="<p>Hello "+name+" ,</p><p>I hope this message finds you well. It is important to keep up with your medication schedule, and we are here to help you do just that. Here is a friendly reminder for your last medication received on date was "+app_date+" .</p><p>Your well-being is important to us, and we are here to support you on your journey to better health.Please visit to your OHC to collect your chronic medications </p><p>Thank you for entrusting us with your healthcare needs. We are here to help you every step of the way. </p><p>Best regards,</p>";
    var ref_type='cronic medication reminder';
    var email_list_to=mail;
    var email_list_cc='';
        send_email(ref_type, appointmentId, email_list_to, email_list_cc, subject,
            message);

}

function send_email(ref_type, ref_identifier, email_list_to, email_list_cc, subject, message) {

$.ajax({
    url: 'send_email.php',
    type: 'POST',
    data: {
        ref_type: ref_type,
        ref_identifier: ref_identifier,
        email_list_to: email_list_to,
        email_list_cc: email_list_cc,
        subject: subject,
        message: message,
    },
    success: function(data) {
            return;


    },
    error: function(data) {
        BootstrapDialog.alert('Error in sending mail');
        return;
    }
});
$('.close').click();
}

</script>
<?php
 begin();

    $query = "SELECT distinct(emp_id) FROM `prescription_master` ";


error_log("sql " . $query);
if (!$result = @mysqli_query($conn,$query)) {
    rollback();
    exit(mysqli_error($conn));
}
$data = array();
if(mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
  
$app_q=" select date(appointment_date) + INTERVAL chronic_day DAY AS DATE,date(appointment_date) as app_date,appointment_id,ohc_type_id,emp_id  from employee_appointment  where appointment_id=(select max(appointment_id) from employee_appointment  where emp_id='".$row['emp_id']."' and chronic_day!=0 ) ";   

if (!$result_app_q = @mysqli_query($conn,$app_q)) {
    rollback();
    exit(mysqli_error($conn));
}
if(mysqli_num_rows($result_app_q) > 0) {
    while ($row_app_q = mysqli_fetch_assoc($result_app_q)) {

        $emp_mail = getTableFieldValue('patient_master','offiial_email_id','id',$row_app_q['emp_id']);

        
        $emp_name = getTableFieldValue('patient_master','patient_name','id',$row_app_q['emp_id']);

        $ohc = getTableFieldValue('ohc_type','ohc_type_name','ohc_type_id',$row_app_q['ohc_type_id']);

        $cronic_date=$row_app_q['DATE'];

        if($cronic_date<date("Y-m-d")){
            
           echo "<script type='text/javascript'>open_mail('".$emp_mail."','".$emp_name."','".$ohc."','".$row_app_q['appointment_id']."','".date($row_app_q['app_date'])."')</script>";
        }
    }        
}
}
$data['status'] = "succes";
$data['message'] = "Assign menu done!";
}
else
{
$data['status'] = 200;
$data['message'] = "Data not found!";
}

commit();
echo json_encode($data);

?>