<?php include('techsyn_header.php'); ?>
<?php include('form/add_config_appointment_reminder.php'); ?>

<!-- Main Content Container for side bar and body-->

<div class="main-container ace-save-state" id="main-container">
    <script type="text/javascript">
        try {
            ace.settings.loadState('main-container')
        } catch (e) {}
    </script>
    <?php include('techsyn_sidebar.php'); ?>

    <!--breadcrumb-->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="breadcrumbs ace-save-state" id="breadcrumbs">
                <ul class="breadcrumb">


                    <li class="#">Patient Management</li>
                    <li class="active">Employee Appointment</li>
                </ul>
            </div>
            <!-- End of breadcrumb -->

            <div class="page-content">
                <div id="flexigridDiv" class="table-responsive">
                    <form name="export_form" method="post" id="export_form" action="">

                        <input type="hidden" name="pdf_action" id="pdf_action" value="pdf_appointment_list.php" />
                        <input type="hidden" name="excel_action" id="excel_action" value="excel_appointment_list.php" />

                    </form>

                    <form name="f1" method="post" action="" id="flex_form_city">
                        <div id="flex1" style="width:100%">
                        </div>
                        <input type="hidden" name="flex_city_id" id="flex_city_id" />
                    </form>
                </div>
            </div>



        </div><!-- /.page-content -->
    </div>
</div><!-- /.main-content -->




<script type="text/javascript">
    $(function() {
        var w = screen.width * .90;

        var h = 0;
        h = (window.innerHeight - ($("#navbar").height() + $(".breadcrumbs").height())) * .75;
        $("#flex1").flexigrid({
            url: 'patient_appointment_script.php',
            dataType: 'json',
            colModel: [{
                    display: 'Sr no.',
                    name: 'count',
                    width: w * .04,
                    sortable: false,
                    align: 'left'
                },


                {
                    display: 'Appointment Date',
                    name: 'date',
                    width: w * .08,
                    sortable: true,
                    align: 'left'
                },
                {
                    display: 'Time',
                    name: 'time',
                    width: w * .1,
                    sortable: true,
                    align: 'left'
                },
                {
                    display: 'Patient Name',
                    name: 'patient_name',
                    width: w * .12,
                    sortable: true,
                    align: 'left'
                },

                {
                    display: 'EMP Code',
                    name: 'emp_code',
                    width: w * .08,
                    sortable: true,
                    align: 'left'
                },

                {
                    display: 'Age',
                    name: 'dob',
                    width: w * .08,
                    sortable: true,
                    align: 'left'
                },
                {
                    display: 'Grade',
                    name: 'emp_grade',
                    width: w * .08,
                    sortable: true,
                    align: 'left'
                },

                {
                    display: 'Doctor Name',
                    name: 'doctor_name',
                    width: w * .12,
                    sortable: true,
                    align: 'left'
                },
                {
                    display: 'Appointment Type',
                    name: 'app_type',
                    width: w * .12,
                    sortable: true,
                    align: 'left'
                },

                {
                    display: 'Status',
                    name: 'status',
                    width: w * .12,
                    sortable: true,
                    align: 'left'
                },



                {
                    display: '',
                    name: '',
                    width: w * .1,
                    sortable: true,
                    align: 'left'
                },


            ],
            buttons: [

                {
                    name: 'PDF',
                    bclass: 'print',
                    onpress: pdf
                },
                {
                    separator: true
                },
                {
                    name: 'Excel',
                    bclass: 'print_excel',
                    onpress: excel
                },
                {
                    separator: true
                },
                {
                    name: 'Add Appointment',
                    bclass: 'add',
                    onpress: add
                },
                {
                    separator: true
                },
                {
                    name: 'Update Multiple Appointments',
                    bclass: 'add',
                    onpress: addApp
                },
                {
                    separator: true
                },
                {
                    name: 'Send Pending Reminder',
                    bclass: 'add',
                    onpress: reminder_pending
                },
                {
                    separator: true
                }, {
                    separator: true
                }, {
                    name: 'Configer Reminder',
                    bclass: 'add',
                    onpress: config_reminder
                }, {
                    separator: true
                }, {
                    name: 'Send Configer Reminder',
                    bclass: 'add',
                    onpress: reminder
                },
            ],
            searchitems: [{
                    display: 'EMP code',
                    name: 'emp_code',
                    isdefault: true,
                },

                {
                    display: 'Patient Name',
                    name: 'patient_name',
                    isdefault: false,
                },


            ],
            sortname: "date",
            sortorder: "desc",
            usepager: true, //pagination

            useRp: true,
            rp: 50, //records per page
            showTableToggleBtn: false, //toggle button for the whole table
            resizable: true,
            //width: w,
            height: h,
            singleSelect: true
        });
    });

    function excel()

    {

        window.open('ajax_excel.php', 'Contractor List');

    }

    function pdf() {
        //window.open('pdf_manage_item.php');

        window.open('ajax_pdf.php', 'Contractor List');
    }


    function add() {
        $(".save_button").show();
        $(".add_app").val("");



        $(".add_app").prop('readonly', false);

        $('#modal-add-add_app').modal("show");
    }

    function delete_section(cityId) {
        $("#flex_city_id").val(cityId);
        $("#city_id").val(cityId);
        $("#status").val('CANCELLED');
        BootstrapDialog.confirm('Are You Sure To Delete The Employee Appointment?', function(result) {
            if (result) {

                open_item('del');

            }
        });

    }

    function reminder() {
        window.open('pending_appointment_reminder_before.php', 'Appointment reminder');
    }

    function reminder_pending() {
        window.open('pending_appointment_reminder.php', 'Appointment pending reminder');
    }

    function config_reminder() {

        $('#modal-add-config').modal("show");
    }



    function open_section(cityId, action) {

        if (action == "V") {
            $(".save_button").hide();
            $("#city").prop('disabled', true);
            $("#AC_cost").prop('disabled', true);
            $("#AC_amb_charge").prop('disabled', true);
            $("#NON_AC_cost").prop('disabled', true);
            $("#NON_AC_amb_charge").prop('disabled', true);


        } else {
            $("#city").prop('disabled', false);
            $("#AC_cost").prop('disabled', false);
            $("#AC_amb_charge").prop('disabled', false);
            $("#NON_AC_cost").prop('disabled', false);
            $("#NON_AC_amb_charge").prop('disabled', false);

            $(".save_button").show();
        }
        $.ajax({
            url: 'select_app_data.php?cityId=' + cityId + ' ',
            type: 'POST',
            success: function(data) {

                var data = $.parseJSON(data);

                if (data.app_type == 'FIT') {
                    data.app_type = 'Sickness/Fitness';
                } else if (data.app_type == 'MED') {
                    data.app_type = 'Annual Medical Checkup';
                }
                $("#city_id").val(data.id);
                $("#appointment_date").val(data.date);
                $("#time").val(data.slot);
                $("#patient_name").val(data.patient_name);
                $("#app_doctor_name").val(data.doctor_name);
                $("#app_type").val(data.app_type);
                $("#status").val(data.status);




                $('#modal-add-city').modal("show");
            },
            error: function(data) {
                BootstrapDialog.alert('Error Populating city details');
                return;
            }
        });
    }


    function loadSBUList(bu_id, selectId) {
        $("#" + selectId).empty();
        $.ajax({
            url: 'getDynamicData.php?tableName=department&where_clause_field=bu_id&where_clause_value=' + bu_id,
            type: 'GET',
            success: function(data) {

                var data = $.parseJSON(data);

                for (i = 0; i < data.length; i++) {
                    var o = new Option(data[i].dept_name, data[i].dept_id)
                    $("#" + selectId).append(o);
                }

            },
            error: function(data) {
                BootstrapDialog.alert('Error Populating Section');
                return;
            }
        });
    }

    function addApp() {
        location.href = "bulk_appointments.php";
    }


    function save_section() {
        open_item('save');

    }


    function open_item(type) {

        var appointmentId = $('#city_id').val();

        $.ajax({
            url: 'select_app_data.php',
            type: "POST",
            data: {
                'cityId': appointmentId,
            },
            dataType: 'json',
            success: function(data) {
                var app_id = data.id;

                var link = '"' + data.app_root + '"';

                var emailContent = "";
                var status = '';
                var sub_status = '';
                if ($('#status').val() == 'BOOKED' && appointmentId == '') {
                    sub_status = 'pending';
                    status = "<font color=red>pending</font> ";
                } else if ($('#status').val() == 'BOOKED' && appointmentId != '') {
                    sub_status = 'confirmed';
                    status = "<font color=green>confirmed</font> ";
                } else if ($('#status').val() == 'DRP') {
                    sub_status = 'pending';
                    status = "<font color=red>Pending</font>";
                } else if ($('#status').val() == 'DRA') {
                    sub_status = 'completed';
                    status = "<font color=green>completed</font> ";
                } else if ($('#status').val() == 'CANCELLED') {
                    sub_status = 'cancelled';
                    status = " <font color=red>cancelled</font>";
                }

                if (data.app_type == 'FIT') {
                    data.app_type = 'Sickness/Fitness';
                } else if (data.app_type == 'MED') {
                    data.app_type = 'Annual Medical Checkup';
                }

                var email_list_to = data.emp_mail;
                var email_list_cc = data.doc_mail;
                // var app_root = '<?php echo getConfigKey('APP_ROOT') ?>';
                // var link = "" + app_root + "/opd_form_pdf.php?appointment_id_pdf=" + app_id;
                ref_type = "Appointments Booked";
                if (type == 'del') {
                    var subject = 'You missed your ' + data.app_type + ' appointment in ' + data.ohc_type + ' OHC on ' + data
                        .date + '.';
                    emailContent +=
                        "<p>Dear Sir/Madam,</p><p>You have missed your " + data.app_type + " appointment in " + data.ohc_type + " OHC on " + data
                        .date + ".Request you to reschedule your appointment by clicking on below link.</p>"
                } else if ($('#status').val() == 'CANCELLED') {
                    var subject = 'Your ' + data.app_type + ' got ' + sub_status + ' in ' + data.ohc_type + ' OHC on ' + data
                        .date + '.';
                    emailContent +=
                        "<p>Dear Sir/Madam,</p><p>We regret to inform you that your  " + data.app_type + " in " + data.ohc_type + " OHC got " +
                        status +
                        " due to administrativ reasons. Apologies for inconvenience caused.</p><br><p>Request you to reschedule your appointment by clicking on below link.</p>"
                } else {
                    var subject = 'Your ' + data.app_type + ' got ' + sub_status + ' in ' + data.ohc_type + ' OHC on ' + data
                        .date + '.';
                    emailContent +=
                        "<p>Dear Sir/Madam,</p><p>Your " + data.app_type + " in " + data.ohc_type + " ohc got " +
                        status +
                        ".<br>Below are more details of your booking  </p><br><table class=`table-bordered` width=`100%`>";




                    emailContent += "<tr><td>Date: " + data.date + "</td></tr><tr><td>Time: " + data.slot + '-' + data.slot_end +
                        "</td></tr><tr><td> Patient Name:" +
                        data.patient_name +
                        // "</td></tr><tr><td>Doc Name: " +
                        // data.doctor_name +
                        "</td></tr><tr><td>Appointment Type: " + data.app_type +
                        "</td></tr><tr><td>status: " + status +
                        "</td></tr><tr><td>Remarks:" + data.remarks + "</td></tr>";
                    emailContent +=
                        "</tbody></table>";
                    // emailContent += "<br><a href=`" + link +
                    //     "`> Details</a> ";
                }
                if ($('#status').val() == 'CANCELLED' || type == 'del') {

                    emailContent += "<br><a href=" + link + "> Book a appointment</a> ";


                }

                emailContent += "<p> Regards,</p><p> Team OHC</p>";

                message = emailContent;


                var id = appointmentId;
                message = emailContent;

                send_email(ref_type, appointmentId, email_list_to, email_list_cc, subject,
                    message, type);
            },
            error: function(data) {
                BootstrapDialog.alert('Error In Pulling Id');
                return id;
            }

        });
    }

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

        $.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) {

                if (type == 'del') {
                    $.ajax({
                        url: 'delete_emp_appointment.php',
                        type: "POST",
                        data: $("#flex_form_city").serialize(),
                        success: function(data) {
                            if (data.indexOf("SUCCESS") != -1) {
                                BootstrapDialog.alert('Employee Appointment Deleted Successfully.');
                                $("#flex1").flexReload();

                            } else {
                                BootstrapDialog.alert('Error Deleting Employee Appointment');
                            }
                            $("#ajaxSpinnerContainer").css('display', "none");
                            return;
                        },
                        error: function(data) {
                            BootstrapDialog.alert('Error Deleting Complaint');
                            return;
                        }
                    });
                } else if (type == 'save') {
                    $.ajax({
                        url: 'save_app_status.php ',
                        type: "POST",
                        data: $("#city_form").serialize(),

                        success: function(data) {
                            $("#ajaxSpinnerContainer").css('display', "none");

                            BootstrapDialog.alert('Appointment Approved Successfully.');
                            $("#flex1").flexReload();



                            return;
                        },
                        error: function(data) {
                            BootstrapDialog.alert('Error Saving Appointment');
                            return;
                        }

                    });
                    $('.close').click();
                }

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

    function is_medical(type) {
        $('#appointment_book_date').val('');
        var content = "<option value=''>Select Slot</option>";
        var content1 = "<option value=''>Select Doctor</option>";
        $('#doctor_slot').html(content);
        $('#doctor_name').html(content1);

        if (type == 'MED') {

            $('#doc_div').hide();

        } else {
            $('#doc_div').show();
        }
    }

    function getSlotCount() {

        var appointment_book_date = $('#appointment_book_date').val();
        var slot = $('#doctor_slot').val();
        var app_type = $('#appointment_booking_type').val();
        var doc = $('#doctor_name').val();

        var contant = "<h6 style='color:red'>No Available Slots</h6>";
        if (appointment_book_date == '' || appointment_book_date == null) {
            contant = "<h6 style='color:red'>Please Select Date</h6>";
        } else if (slot == '' || slot == null) {
            contant = "<h6 style='color:red'>Please Select Slot</h6>";
        } else if (slot != '' && slot != null && appointment_book_date != '' && appointment_book_date != null) {
            $.ajax({
                url: 'get_availabe_slots.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    appointment_book_date: appointment_book_date,
                    slot: slot,
                    app_type: app_type,
                    doc: doc,
                },
                success(data) {
                    if (app_type == 'OPD') {
                        contant = "";
                    } else {

                        if (data.slot_available > 0) {
                            contant = "<h6 style='color:green'>" + data.slot_available + " slots are available </h6>";

                        } else {
                            contant = "<h6 style='color:red'>No Available Slots</h6>";

                        }
                    }
                    $('#available_slot').val(data.slot_available);
                    $('#slot_count').html(contant);
                    return;
                },
                error: function(data) {
                    alert('Error in Getting Appointment slot');
                    return;
                }
            });

        }

        $('#available_slot').val(0);
        $('#slot_count').html(contant);
    }

    function getDocs(appointment_book_date, doc) {
        //   alert(appointment_book_date);
        if ($('#appointment_booking_type').val() == 'MED') {
            getMedicalSlot('');
        } else {
            $.ajax({
                url: 'select_appointment_doc.php',
                type: 'POST',
                dataType: 'json',
                data: {
                    appointment_book_date: appointment_book_date
                },
                success(data) {

                    var docs_opt = '';
                    docs_opt += "<option disabled selected value='' >Select Doctor</option>";
                    for (var i = 0; i < data.count; i++) {
                        docs_opt += "<option  value='" + data[i]['emp_id'] + "'";
                        if (data[i]['emp_id'] == doc) {
                            docs_opt += ' selected '
                        }
                        docs_opt += " >" + data[i]['patient_name'] +
                            "</option>";
                    }
                    if (data.count == 0) {
                        var contant_x = "<h6 style='color:red'>No Available Slots</h6>";;
                        $('#available_slot').val(0);
                        $('#slot_count').html(contant_x);
                        var docs_opt2 = "<option disabled selected value='' >Select Slot</option>";
                        $('#doctor_slot').html(docs_opt2);
                    }
                    $('#doctor_name').html(docs_opt);

                },
                error: function(data) {
                    alert('Error in Getting slot');
                    return;
                }
            });
        }
    }

    function getMedicalSlot(slot) {

        var appointment_book_date = $('#appointment_book_date').val();

        $.ajax({
            url: 'select_medical_appointment_slots.php',
            type: 'POST',
            dataType: 'json',
            data: {
                appointment_book_date: appointment_book_date,

            },
            success(data) {

                var docs_opt = '';
                docs_opt += "<option disabled selected value='' >Select Slot</option>";
                for (var i = 0; i < data.count; i++) {
                    docs_opt += "<option  value='" + data[i]['slot_id'] + "' ";
                    if (data[i]['status'] == 'BOOKED') {
                        docs_opt += " disabled ";
                    }

                    if (data[i]['slot_id'] == slot) {
                        docs_opt += " selected ";
                    }

                    docs_opt += " >" + data[i]['slot'] + " to " + data[i]['slot_end'] + "</option>";
                }
                if (data.count == 0) {
                    var contant_x = "<h6 style='color:red'>No Available Slots</h6>";;
                    $('#available_slot').val(0);
                    $('#slot_count').html(contant_x);
                }
                $('#doctor_slot').html(docs_opt);
            },
            error: function(data) {
                alert('Error in Getting Appointment slot');
                return;
            }
        });


    }

    function getSlots(doc, slot) {

        var appointment_book_date = $('#appointment_book_date').val();
        var app_type = $('#appointment_booking_type').val();
        $.ajax({
            url: 'select_appointment_slots.php',
            type: 'POST',
            dataType: 'json',
            data: {
                appointment_book_date: appointment_book_date,
                doc: doc,
                app_type: app_type,
            },
            success(data) {

                var docs_opt = '';
                docs_opt += "<option disabled selected value='' >Select Slot</option>";
                for (var i = 0; i < data.count; i++) {
                    docs_opt += "<option  value='" + data[i]['slot_id'] + "' ";
                    if (data[i]['slot_available'] <= 0) {
                        docs_opt += " disabled ";
                    }

                    if (data[i]['slot_id'] == slot) {
                        docs_opt += " selected ";
                    }

                    docs_opt += " >" + data[i]['slot'] + " to " + data[i]['slot_end'] + "</option>";
                }
                $('#doctor_slot').html(docs_opt);
            },
            error: function(data) {
                BootstrapDialog.alert('Error in Getting Appointment slot');
                return;
            }
        });

    }

    function save_appointment() {
        $("#ajaxSpinnerContainer").css('display', "block");
        var app_type = $('#appointment_booking_type').val();
        var date = $('#appointment_book_date').val();
        var doc = $('#doctor_name').val();
        var slot = $('#doctor_slot').val();
        var p_id = $('#patient_name_cc').val();
        var available_slot = $('#available_slot').val();

        if (app_type == '' || app_type == null) {
            BootstrapDialog.alert('Please Select Appointment Type...!');

        } else if (date == '' || date == null) {
            BootstrapDialog.alert('Please Select Appointment Date...!');

        } else if ((doc == '' || doc == null) && (app_type != 'MED')) {
            BootstrapDialog.alert('Please Select Doctor...!');


        } else if ((p_id == '' || p_id == null)) {
            BootstrapDialog.alert('Please Select Patient...!');


        } else if (slot == '' || slot == null) {
            BootstrapDialog.alert('Please Select Slot...!');
        } else if (available_slot == 0) {
            BootstrapDialog.alert('There Is No Slot Availabe.Please Select Appointment For Anoter Slot..!');

        } else {
            $('#save_app').prop('disabled', true);

            var previous_appointment = alreadyHaveAppointment();

            if (app_type == 'MED') {
                app_type = 'Annual Medical Checkup'
            } else {
                app_type = 'OPD/ Sickness/ Fitness for selected Date'
            }
            if (previous_appointment == true) {
                $('#save_app').prop('disabled', false);
                BootstrapDialog.alert('Patient have already an appintment for ' + app_type +
                    '.Please check previous appointments ..!');
                return false;
            } else {
                $.ajax({
                    url: 'save_appointment.php',
                    type: "POST",
                    async: false,
                    data: $("#add_app_form").serialize(),
                    dataType: 'json',
                    success: function(data) {
                        $('#city_id').val(data.app_id);
                        open_item('save');

                    },
                    error: function(data) {
                        alert('Error Updating Details');
                        return;
                    }
                });
            }
        }


    }

    function alreadyHaveAppointment() {
        var id = $('#patient_name_cc').val();
        var app_type = $('#appointment_booking_type').val();
        var app_date = $('#appointment_book_date').val();
        var have_app = false;
        var appointment_book_id = $('#appointment_book_id').val();
        if (appointment_book_id == '' || appointment_book_id == null) {
            $.ajax({
                url: 'select_previous_appointment.php',
                type: 'POST',
                async: false,
                dataType: 'json',
                data: {
                    id: id,
                    app_type: app_type,
                    app_date: app_date,
                },
                success(data) {

                    if (data.count > 0) {
                        have_app = true;
                    }

                },
                error: function(data) {
                    alert('Error in Getting Appointment slot');
                    return;
                }
            });
        }
        return have_app;
    }
</script>
<?php include('form/send_mail.php'); ?>
<?php include('techsyn_footer.php'); ?>
<?php include('show_appointment.php'); ?>
<?php include('book_appointment.php'); ?>