$(document).ready(function () {
    debugger;
    LoadBranchForPatientSearchPopup()
    //if (parseInt($('#IPDischarge_Id').val()) != 1)
    //{
    //    $('.hdn_discharge').removeClass('hidden');
    //}
    if (parseInt($('#HFormbuilder_id').val()) == 1)
    {
        $('.FormBuilder_form').removeClass('hide');
    }
    else
    {
        $('.Normal_form').removeClass('hide');
    }
   
   LoadRecordsCound();
});
function LoadBranchForPatientSearchPopup() {
    debugger;
    $('#SelBranchSearchPopup').empty();
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {

                        $('#SelBranchSearchPopup').append(SubDepartment);
                    }
                    else {

                        $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                    }

                    if ($('#hiddenRole_id').val() != 2) {

                        $('#SelBranchSearchPopup').val($('#HospitalBranchID').val());
                        $('#SelBranchSearchPopup').attr('disabled', true);

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

        }
    });
}
function LoadRecordsCound() {

    var objInvestigationHis = [

        {
            Dml_Indicator: "RC",
            patient_id: $("#IPPatient_Id").val(),
            ip_visit_report_id: $("#IPVisitReport_Id").val(),
        },
    ]


    objInvestigationHis = JSON.stringify({ 'objInvestigationHis': objInvestigationHis });

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/InvestigationHis_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: objInvestigationHis,
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    //var count = $('#HRecordCount').val();
                    debugger;
                    if (data1.Table.length > 0) {

                        //if (parseInt(data1.Table[0].type_of_admition)==1) {
                        //    $(".div_InHistory").addClass('hidden');
                        //}
                        //else {
                        //    $(".div_InHistory").removeClass('hidden');
                        //}

                        if (parseInt(data1.Table[0].type_of_admition)==1) {
                            $(".div_OTSchedule").addClass('hidden');
                        }
                        else if (parseInt(data1.Table[0].type_of_admition) == 2 || parseInt(data1.Table[0].type_of_admition) == 3) {
                            $(".div_OTSchedule").removeClass('hidden');
                        }

                        if (parseInt(data1.Table[0].Examinationcount) > 0 || parseInt(data1.Table[0].PerAbdominalExamCount) > 0) {
                            $("#SExamPenting").hide();
                        }
                        else {
                            $("#SExamComplete").hide();
                        }

                        if (parseInt(data1.Table[0].patienthistoryCount) > 0) {
                            $("#SPHistoryPending").hide();
                        }
                        else {
                            $("#SPHistoryComplete").hide();
                        }

                        //if (parseInt(data1.Table[0].InvestigationsHistoryCount) > 0) {
                        //    $("#SInvestPending").hide();
                        //}
                        //else {
                        //    $("#SInvestComplete").hide();
                        //}

                        if (data1.Table.length > 0) {
                            $("#SStoppedMedicines").html("(" + data1.Table[0].PaseMedicine + ")");
                            $("#SActiveMedicines").html("(" + data1.Table[0].StartMedicine + ")");
                        }

                        if (data1.Table1.length > 0) {
                            $("#SInvestigations").html("(" + data1.Table1[0].InvesticationCount + ")");
                            $("#SVitalsMonitoring").html("(" + data1.Table1[0].VitalsCount + ")");
                            $("#SDiabetic_Flow_Sheet").html("(" + data1.Table1[0].DiabeticCount + ")");
                            $("#SNursingNotes").html("(" + data1.Table1[0].NursingCount + ")");
                            $("#SDoctorNotes").html("(" + data1.Table1[0].DoctorNotesCount + ")");
                        }

                        if (data1.Table2.length > 0) {
                            $("#SIPFluids").html("(" + data1.Table2[0].fluidsCount + ")");
                        }

                        if (data1.Table2[0].OperationIntraCount > 0 || data1.Table2[0].OperationSurgeonCount > 0 || data1.Table2[0].OperationPortCount > 0) {
                            $("#SODetailsPending").hide();
                        }
                        else {
                            $("#SODetailsCompleted").hide();
                        }

                        if (data1.Table1[0].FeedbackCount > 0) {
                            $("#SFeedbackPending").hide();
                        }
                        else {
                            $("#SFeedbackComplete").hide();
                        }
                    }
                }
            }

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

    });
}

function IDischargeRequest(event, id) {
    debugger;

    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    $('#divtype').addClass('disabled')
    $('#divsubdis').addClass('disabled')
    // $('#divdptlb').addClass('disabled')

    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         debugger;
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {
                     return;
                 }
                 else {

                     var Patientdata = {
                         Dml_Indicator: 'PRC',
                         IP_visit_report_id: id
                     }

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

                             }
                             else {

                                 if (data2.msg == "Success") {
                                     var data1 = data2.data;
                                     $.each(data1, function (i, emp) {
                                         debugger;
                                         if (emp.request_count > 0) {

                                             if (parseInt($('#HFormbuilder_id').val()) == 1) {
                                                 $('#IPpatienthistory_Id').val('/Ipmanagement/IP_FormBuilder?Patient_Id=' + emp.patient_id + '&IPVisitReport_Id=' + emp.visit_report_id + '&IPDischarge_Id=' + emp.ip_nursing_discharge + '&branch_id=' + emp.branch_id + '&FormHead_id=2');

                                             }
                                             else {
                                                 $('#IPpatienthistory_Id').val('/Ipmanagement/discharge?Patient_Id=' + emp.patient_id + '&IPVisitReport_Id=' + emp.visit_report_id + '&IPDischarge_Id=' + emp.ip_nursing_discharge + '&branch_id=' + emp.branch_id + '');
                                             }
                                         }
                                         else {
                                             toastr.warning('No Requests for this Patient.Please add Requests');
                                         }

                                         //var InitiateDischarge = '<a class="btn_r" style="cursor:pointer;" data-id="IP_IDF1" data-type="role_rights_function" data-nav="redirect" data-name="/Ipmanagement/discharge?Patient_Id=' + emp.ip_patient_id + '&IPVisitReport_Id=' + emp.ip_visit_report_id + '&IPDischarge_Id=' + emp.ip_nursing_discharge + '&branch_id=' + emp.branch_id + '">Initiate Discharge</a>';

                                     });
                                     var ChkForm = CheckFormRights($('#IPPatient_Id').val(), 105, 1, 2);
                                     $.when(ChkForm)
                                     .then(function (data3) {
                                         var data2 = JSON.parse(data3);

                                         if (jQuery.isEmptyObject((data3))) {
                                             formRedirect($('#IPpatienthistory_Id').val())
                                         }
                                         else {
                                             debugger;
                                             if (data2.data.Table[0].StringMsg != "Rights") {
                                                 formRedirect($('#IPpatienthistory_Id').val())
                                             }

                                         }

                                     });

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

                         }
                     });

                 }
             }
         }
     });
}


function ip_formShow(event, patienthistoryid, Patient_Id, form_id) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     check_form(event, patienthistoryid, Patient_Id, form_id)
                 }

             }
         }
     })

   
}


function check_form(event, patienthistoryid, Patient_Id, form_id) {
    debugger;
    var name = $(event).attr("data-name");
    $('#IPpatienthistory_Id').val(name)
    var ChkForm = CheckFormRights(Patient_Id, form_id, 1, 2);
    $.when(ChkForm)
    .then(function (data3) {
        var data2 = JSON.parse(data3);

        if (jQuery.isEmptyObject((data2.data))) {
            formRedirect(name);
        }
        else {
            debugger;
            if (data2.data.Table[0].StringMsg != "Rights") {
                formRedirect(name);
            }

        }

    });

}
function formRedirect(url) {
    window.location=url
}
//Form Close Funtion
$('#formClose').click(function () {
    debugger;
    formRedirect($('#IPpatienthistory_Id').val())
  
})











