//NursingNotes js//

$(document).ready(function () {
    LoadDoctor();
    ShowNursingNotes();
    ShowRefDoctorNotes();
});

function ShowNursingNotes() {

    var Nursingdata = {
        Dml_Indicator: "S",
        visit_report_id: $('#IPVisitReportId').val(),
        notes_type: 2
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Nursingdata),
        success: function (data) {
            $('#tblDoctorNotes').empty();
            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;

                if (data1.length > 0) {

                    tr = $('<table id="example1" class="table table-responsive table-bordered table-striped dataTable tablecolumnsticky" />');

                    tr.append("<thead class='table-bg'><tr><th>Date & Time</th><th>Notes</th><th>Created By</th><th>Action</th></tr></thead><tbody>");

                    $.each(data1, function (i, emp) {


                        view = "<a href=# title='Edit' class='edit_icon'onclick=loadNursingNotes('" + emp.ip_nursing_notes_id + "','DN');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                        Delete = "<a title='Delete' href='javascript:void(0)'  title='Delete' class='delete_icon' onclick=DeletNotes('" + emp.ip_nursing_notes_id + "','DN');><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                        other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + view + '</li><li>' + Delete + '</li></div></ul></div>';

                        row_data = "<tr><td class='table_name_space'>" + emp.ip_nursing_login_datetime + "</td><td>" + emp.ip_notes + "</td><td>" + emp.NameofNurse + "</td><td>" + other_info + "</td></tr>";
                        tr.append(row_data);
                    });
                    tr.append("</tbody></table>");
                    $('#tblDoctorNotes').html(tr);
                }

            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });

}

$('#btnAdd').click(function () {
    if ($("#IPDischarge_Id").val() != "2") {

        if ($.trim($("#txtLogdate").val()) == "") {
            $('#PtxtLogdate').show();
            $('#PtxtLogdate').fadeOut(8000);
            $('#PtxtLogdate').html('Select Date');
            $('#txtLogdate').val('');
            $('#txtLogdate').focus();
            return false;
        }

        if ($.trim($("#txtNotes").val()) == "") {
            $('#PtxtNotes').show();
            $('#PtxtNotes').fadeOut(8000);
            $('#PtxtNotes').html('Enter Nursing Notes');
            $('#txtNotes').val('');
            $('#txtNotes').focus();
            return false;
        }

        var LDT = $('#txtLogdate').val();
        if (LDT == '') {
            LDT = '';
        }
        else {
            var a = LDT.split('/');
            LDT = a[2] + '-' + a[1] + '-' + a[0];
        }

        var time = $("#txtLogtime").val();
        var convertedTime = moment(time, 'hh:mm A').format('HH:mm');

        //var time = $("#txtLogtime").val();
        //var hours = Number(time.match(/^(\d+)/)[1]);
        //var minutes = Number(time.match(/:(\d+)/)[1]);
        //var AMPM = time.match(/\s(.*)$/)[1];
        //if (AMPM == "PM" && hours < 12) hours = hours + 12;
        //if (AMPM == "AM" && hours == 12) hours = hours - 12;
        //var sHours = hours.toString();
        //var sMinutes = minutes.toString();
        //if (hours < 10) sHours = "0" + sHours;
        //if (minutes < 10) sMinutes = "0" + sMinutes;
        //var LTM = sHours + ":" + sMinutes;

        var Nursingdata = {
            ip_nursing_notes_id: $("#HNursingNotesID").val(),
            ip_nursing_login_datetime: LDT + " " + convertedTime,
            ip_notes: $("#txtNotes").val(),
            Dml_Indicator: "I",
            visit_report_id: $('#IPVisitReportId').val(),
            patient_id: $('#IPPatient_Id').val(),
            created_by: $('#hiddenUser_id').val(),
            notes_type: 2
            //referral_Doctorid: $('#ddRefDoctor').val()
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(Nursingdata),
            success: function (data) {

                var data2 = JSON.parse(data);
                if (jQuery.isEmptyObject((data2.data))) {

                }
                else {
                    if (data2.msg == "Success") {

                        if ($("#HNursingNotesID").val() == "") {
                            toastr.success('Doctor Notes Added Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                        }
                        else {
                            toastr.success('Doctor Notes Updated Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                        }


                        clearNursing();
                        ShowNursingNotes();
                    }
                    else {
                        console.log(data);
                    }
                }
                //window.location = "/Ipmanagement/ippatientdetails?Patient_Id=" + $("#IPPatient_Id").val() + "&IPVisitReport_Id=" + $("#IPReportId").val() + "";
            },
            error: function (data) {

            }
        });
    }

});

function clearNursing() {
    $("#HNursingNotesID").val(""),
    $("#txtLogdate").val(""),
    //$("#txtLogtime").val(""),
    $("#txtNotes").val("")
    //$('#ddRefDoctor').val('0')
}

function loadNursingNotes(val,dml_type) {
    if ($("#IPDischarge_Id").val() != "2") {

        var dml = '';

        if (dml_type == "DN") {
            dml = "ID";
        }
        else {
            dml = "REFID";
        }

        var Nursingdata = {
            ip_nursing_notes_id: val,
            Dml_Indicator: dml,
            notes_type: 2
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(Nursingdata),
            success: function (data) {
                var data2 = JSON.parse(data);
                if (jQuery.isEmptyObject((data2.data))) {
                }
                else {

                    if (data2.msg == "Success") {
                        var data1 = data2.data;

                        if (dml_type == "DN") {
                            $.each(data1, function (i, emp) {
                                $("#HNursingNotesID").val(emp.ip_nursing_notes_id),
                                $("#txtLogdate").val(emp.ip_nursing_login_date),
                                $("#txtLogtime").val(emp.ip_nursing_login_time),
                                $("#txtNotes").val(emp.ip_notes)
                            });
                        }
                        else {
                            $.each(data1, function (i, emp) {
                                $("#HRefDoctorNotesID").val(emp.doctor_referral_id),
                                $("#txtRefLogdate").val(emp.referred_doctor_date),
                                $("#txtRefLogtime").val(emp.referred_doctor_time),
                                //$("#txtRefNotes").val(emp.notes)
                                $("#ddRefDoctor").val(emp.referred_to_doctor_id)
                            });
                        }

                       
                    }
                    else {
                        console.log(data);
                    }
                }
            }
        });
    }

}

function DeletNotes(val, dml_type) {
    if ($("#IPDischarge_Id").val() != "2") {

        var dml = '', message = '';

        if (dml_type == "DN") {
            dml = "DEL";
            message = 'Doctor';
        }
        else {
            dml = "REFDEL";
            message = 'Referral Doctor';
        }

        status = "'Are You Sure, You want to Delete " + message + " Notes?'";

        toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button> <button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
     {
         closeButton: true,
         progressBar: true,
         allowHtml: true,
         timeOut: 0,

         onShown: function (toast) {
             debugger
             $("#confirmationRevertYes").click(function () {

                 var Nursingdata = {
                     ip_nursing_notes_id: val,
                     Dml_Indicator: dml,
                     notes_type: 2,
                     created_by: $('#hiddenUser_id').val()
                 }

                 $.ajax({
                     async: true,
                     type: 'POST',
                     url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
                     contentType: 'application/json; charset=UTF-8',
                     data: JSON.stringify(Nursingdata),
                     success: function (data) {
                         var data2 = JSON.parse(data);
                         if (jQuery.isEmptyObject((data2.data))) {
                         }
                         else {

                             if (data2.msg == "Success") {
                                 var data1 = data2.data;

                                 if (dml_type == "DN") {
                                     toastr.success('Doctor Notes Deleted Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                                     clearNursing();
                                     ShowNursingNotes();
                                 }
                                 else {
                                     toastr.success('Referral Doctor Notes Deleted Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                                     ClearRefNotes();
                                     ShowRefDoctorNotes();
                                 }

                                
                             }
                             else {
                                 console.log(data);
                             }
                         }
                     }
                 });

             });
         }
     });

    }
}

function LoadDoctor() {
    var DoctorData = {
        Dml_Indicator: "HDL",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#Branch_id').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            $('#ddRefDoctor').empty();
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    var RefDoctor = '';

                    var default_opt = '<option value="0">--Select--</option>';

                    if (data1.length > 0) {
                        for (var i = 0; i < data1.length; i++) {

                            RefDoctor = RefDoctor + '<option value="' + data1[i].doctor_id + '">' + data1[i].first_name + '</option>';
                        }
                        $('#ddRefDoctor').html(default_opt + RefDoctor);

                        if (parseInt($('#hiddenRole_id').val()) == 1) {
                            $("#ddRefDoctor option[value='" + $('#HiddenDoctorID').val() + "']").remove();
                        }
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

// Referral Doctor Notes Start //

$('#BtnRefAdd').click(function () {
    if ($("#IPDischarge_Id").val() != "2") {

        if ($.trim($("#txtRefLogdate").val()) == "") {
            $('#PtxtRefLogdate').show();
            $('#PtxtRefLogdate').fadeOut(8000);
            $('#PtxtRefLogdate').html('Select Date');
            $('#txtRefLogdate').val('');
            $('#txtRefLogdate').focus();
            return false;
        }

        if ($.trim($("#txtRefLogtime").val()) == "") {
            $('#PtxtRefLogtime').show();
            $('#PtxtRefLogtime').fadeOut(8000);
            $('#PtxtRefLogtime').html('Enter Nursing Notes');
            $('#txtRefLogtime').val('');
            $('#txtRefLogtime').focus();
            return false;
        }

        var LDT = $('#txtRefLogdate').val();
        if (LDT == '') {
            LDT = '';
        }
        else {
            var a = LDT.split('/');
            LDT = a[2] + '-' + a[1] + '-' + a[0];
        }

        var time = $("#txtRefLogtime").val();
        var convertedTime = moment(time, 'hh:mm A').format('HH:mm');

        var Nursingdata = {
            ip_nursing_notes_id: $("#HRefDoctorNotesID").val(),
            ip_nursing_login_datetime: LDT + " " + convertedTime,
            //ip_notes: $("#txtRefNotes").val(),
            Dml_Indicator: "RDIN",
            visit_report_id: $('#IPVisitReportId').val(),
            patient_id: $('#IPPatient_Id').val(),
            created_by: $('#hiddenUser_id').val(),
            notes_type: 2,
            referral_Doctorid: $('#ddRefDoctor').val()
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(Nursingdata),
            success: function (data) {

                var data2 = JSON.parse(data);
                if (jQuery.isEmptyObject((data2.data))) {

                }
                else {
                    if (data2.msg == "Success") {

                        if ($("#HRefDoctorNotesID").val() == "") {
                            toastr.success('Referral Doctor Notes Added Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                        }
                        else {
                            toastr.success('Referral Doctor Notes Updated Successfully',
                                       {
                                           //closeButton: false,
                                           progressBar: true,
                                           allowHtml: true,
                                           timeOut: 0,
                                       });
                        }


                        ClearRefNotes();
                        ShowRefDoctorNotes();
                    }
                    else {
                        console.log(data);
                    }
                }
            },
            error: function (data) {

            }
        });
    }

});

function ClearRefNotes() {
    $("#HRefDoctorNotesID").val(""),
    $("#txtRefLogdate").val(""),
    //$("#txtRefNotes").val("")
    $('#ddRefDoctor').val('0')
}

function ShowRefDoctorNotes() {

    var Nursingdata = {
        Dml_Indicator: "RDNS",
        visit_report_id: $('#IPVisitReportId').val(),
        notes_type: 2
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/NursingNotes_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Nursingdata),
        success: function (data) {
            $('#TblRefDoctor').empty();
            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;
                
                if (data1.length > 0) {

                    tr = $('<table id="example1" class="table table-responsive table-bordered table-striped dataTable tablecolumnsticky" />');

                    tr.append("<thead class='table-bg'><tr><th>Date & Time</th><th>Referred By</th><th>Referred To</th><th>Created By</th><th>Action</th></tr></thead><tbody>");

                    $.each(data1, function (i, emp) {


                        view = "<a href=# title='Edit' class='edit_icon' onclick=loadNursingNotes('" + emp.doctor_referral_id + "','REFDN');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                        Delete = "<a href='javascript:void(0)'  title='Delete' class='delete_icon' onclick=DeletNotes('" + emp.doctor_referral_id + "','REFDN');><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                        other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + view + '</li><li>' + Delete + '</li></div></ul></div>';

                        row_data = "<tr><td class='table_name_space'>" + emp.referred_date_time + "</td><td>" + emp.doctor_name + "</td><td>" + emp.referral_doctorname + "</td><td>" + emp.NameofNurse + "</td><td>" + other_info + "</td></tr>";
                        tr.append(row_data);
                    });
                    tr.append("</tbody></table>");
                    $('#TblRefDoctor').html(tr);
                }
            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });

}

// Referral Doctor Notes End //