
//ProcedureNotes js//

$(document).ready(function () {

    ShowProcedureNotes();
});

function ShowProcedureNotes() {

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

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

                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=# class='edit_icon' title='Edit' onclick=loadProcedureNotes('" + emp.ip_nursing_notes_id + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                    Delete = "<a href='javascript:void(0)' title='Delete' class='delete_icon' onclick=DeletNotes('" + emp.ip_nursing_notes_id + "');><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>" + 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>");
                $('#tblProcedureNotes').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 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: 3
        }

        $.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 (data2.msg == "Success") {
                    var data1 = data2.data;

                    if ($("#HNursingNotesID").val() == "") {
                        toastr.success('Procedure Notes Added Successfully',
                                   {
                                       //closeButton: false,
                                       progressBar: true,
                                       allowHtml: true,
                                       timeOut: 0,
                                   });
                    }
                    else {
                        toastr.success('Procedure Notes Updated Successfully',
                                   {
                                       //closeButton: false,
                                       progressBar: true,
                                       allowHtml: true,
                                       timeOut: 0,
                                   });
                    }
                    ClearProcedure();
                    ShowProcedureNotes();
                }
                else {
                    console.log(data);
                }
            },
            error: function (data) {

            }
        });
    }

});

function ClearProcedure() {
    $("#HNursingNotesID").val(""),
    $("#txtLogdate").val(""),
    //$("#txtLogtime").val(""),
    $("#txtNotes").val("")
}

function loadProcedureNotes(val) {
    if ($("#IPDischarge_Id").val() != "2") {
        var Nursingdata = {
            ip_nursing_notes_id: val,
            Dml_Indicator: "ID",
            notes_type: 3
        }

        $.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;

                        $.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 {
                        console.log(data);
                    }
                }
            }
        });
    }

}

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

        status = "'Are You Sure, You want to Delete Procedure 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: "DEL",
                     notes_type: 3,
                     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;

                                 toastr.success('Procedure Notes Deleted Successfully',
                                        {
                                            //closeButton: false,
                                            progressBar: true,
                                            allowHtml: true,
                                            timeOut: 0,
                                        });
                                 ClearProcedure();
                                 ShowProcedureNotes();
                             }
                             else {
                                 console.log(data);
                             }
                         }
                     }
                 });

             });
         }
     });

    }
}