<?php
error_reporting(E_ERROR | E_PARSE);
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');

function getConfigKey_table($config)
{
    $val = getTableFieldValue('config', 'value', 'key_name', '"' . $config . '"');
    return $val;
}
?>
<?php
$ohc = $_SESSION['current_ohcttype'];
?>
<script src="assets/js/jquery-2.1.4.min.js"></script>

<script>
    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();
    }

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

    function send_template_on_center() {

        var name = 'TEAM OHC';
        var subject = "<?= getConfigKey_table('appointment_reminder_subject') ?>";
        var message = "<p>Dear " + name + ",</p><p>" + "<?= getConfigKey_table('appointment_reminder_contant') ?>" + "</p><p>Regards</p><p>Team ohc</p>";
        var ref_type = 'appointment reminder';
        var email_list_to = '';
        var email_list_cc = '<?= getConfigKey_table('appointment_reminder_cc') ?>';

        send_email(ref_type, '', email_list_to, email_list_cc, subject, message);
    }

    function open_mail(mail, name, ohc, appointmentId, app_date, app_type) {
        app_date = formatDate(app_date);
        var subject = "<?= getConfigKey_table('appointment_reminder_subject') ?>";
        var message = "<p>Dear " + name + ",</p><p>" + "<?= getConfigKey_table('appointment_reminder_contant') ?>" + "</p><p>Regards</p><p>Team " + ohc + "</p>";
        var ref_type = 'appointment reminder';
        var email_list_to = mail;
        var email_list_cc = '';
        send_email(ref_type, appointmentId, email_list_to, email_list_cc, subject, message);
    }
</script>

<?php
begin();
echo "<script type='text/javascript'>send_template_on_center()</script>";
$type_filter = '';

$type_filter_conf = getConfigKey_table('selected_appointment_for_reminder');

if ($type_filter_conf == 'Annual Medical Checkup') $type_filter_conf = 'MED';
else $type_filter_conf = 'OPD';

if ($type_filter_conf != '' && $type_filter_conf != null) $type_filter = ' and app_type="' . $type_filter_conf . '"';
$app_q = "SELECT * FROM `appointment_details` WHERE status = 'BOOKED' AND date(date) < NOW() $type_filter AND ohc_type_id = '$ohc'";

error_log($app_q);
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)) {
        error_log('emppp ' . $row_app_q['emp_id']);
        $emp_mail = getTableFieldValue('patient_master', 'offiial_email_id', 'id', $row_app_q['emp_id']);
        error_log('emppp mail ' . $emp_mail);
        $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']);
        $app_type = '';
        if ($row_app_q['app_type'] == 'MED') $app_type = 'Annual Medical Checkup';
        else $app_type = 'OPD/ Sickness/ Fitness';
        echo "<script type='text/javascript'>open_mail('" . $emp_mail . "','" . $emp_name . "','" . $ohc . "','" . $row_app_q['appointment_id'] . "','" . date($row_app_q['date']) . "','" . $app_type . "')</script>";
    }


    $data['status'] = "success";
    $data['message'] = "Assign menu done!";
} else {
    $data['status'] = 200;
    $data['message'] = "Data not found!";
}

commit();
if ($data['status'] == 'success') {
    echo "<h3>Reminder Sent Successfully</h3><br><button type='button' class='btn btn-info btn-sm save_button' onclick='window.location=`patient_appointment.php`;'><i class='ace-icon fa fa-floppy-o bigger-110'></i>Back </button>";
} else {
    echo json_encode($data);
}
?>