
$(document).ready(function () {
    debugger;
 
    LoadHomeCareTbl();
    LoadHospitalCareTbl();
    LoadIPTbl();
    WardNo();
    LoadInvestigationTbl();
    loadvisit_type();
   
})

function LoadHomeCareTbl() {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata;

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

            Dml_Indicator: "HMS",
            doctor_user_id: $('#hiddenUser_id').val()
        }
    }
    else {
        HomeCaredata = {

            Dml_Indicator: "HMS",
            branch_id: branch_id,
            doctor_user_id: $('#hiddenUser_id').val()
        }
    }

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

            }
            else {

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


                    $('#hc_summary').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        details = '<button class="btn btn-primaryuse btn-xs" onclick=HomeCareDetails("' + emp.sub_category_department_id + '");>Details</button>';

                        trd = trd + "<tr><td>" + j + "</td><td>" + emp.sub_cd_name + "</td><td>" + emp.Total + "</td><td>" + emp.Unschedule + "</td><td>" + emp.schedule + "</td><td>" + emp.assigned + "</td><td>" + emp.completed + "</td><td>" + emp.Pending + "</td><td>" + emp.refundcompleted + "</td><td>" + emp.refund + "</td><td>" + emp.sptotal + "/" + emp.spallottotal + "</td><td>" + details + "</td></tr>";

                    });


                    $('#thhomecarebody').html(trd);
                    //doctor consultation
                    $('#DC_count').html(data1.Table1[0].docconsultation);
                    $('#HCA_count').html(data1.Table1[0].homecarecount);
                    $('#HCP_count').html(data1.Table1[0].healthcare);
                   
                    $('#MU_count').html(data1.Table1[0].mobileusers);
                    $('#LHCA_count').html(data1.Table1[0].labhomecarecount);
                    $('#d_op_bill').html(data1.Table1[0].op_bill);
                    $('#d_ip_bill').html(data1.Table1[0].ip_bill);

                    //home care
                    $('#hc_count').html(data1.Table1[0].homecarecount);
                    $('#hc_unscheduled').html(data1.Table2[0].Unschedule);
                    $('#hc_Schedule').html(data1.Table2[0].Schedule);
                    $('#hc_assigned').html(data1.Table2[0].assigned);
                    $('#hc_completed').html(data1.Table2[0].completed);
                    $('#hc_pending').html(data1.Table2[0].Pending);
                    $('#hc_serv_sold').html(data1.Table3[0].ser_sold);
                    $('#hc_pac_sold').html(data1.Table3[0].pac_sold);

                    $('#hc_summary').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });

                    
                  
                }
                else {
                    console.log(data);
                }

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

function LoadHospitalCareTbl() {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    debugger;
    var HomeCaredata = {

        Dml_Indicator: "HOSS",
        branch_id: branch_id
    }

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

            }
            else {

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

                    debugger;
                 
                    $('#hospitalcaresum').dataTable().fnDestroy();
                    var tr, tr1 = '';
                    $.each(data1.Table, function (i, emp) {

                        details = '<button class="btn btn-primaryuse btn-xs" onclick=HospitalCareDetails("' + emp.sub_category_department_id + '","' + "HODS" + '");>Details</button>';

                        tr = tr+"<tr><td>" + emp.sub_cd_name + "</td><td>" + emp.Total + "</td><td>" + emp.Pending + "</td><td>" + emp.completed + "</td><td>" + details + "</td></tr>";
                  
                    });
                    $('#thhospitalcarebody').html(tr);

                    $('#hospitalcaresum').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });
                    debugger;
                    $('#hospitalcaresumdoc').dataTable().fnDestroy();
                    $.each(data1.Table, function (i, emp) {

                        detail = '<button class="btn btn-primaryuse btn-xs" onclick=HospitalCareDetails("' + emp.sub_category_department_id + '","' + "HOLDS" + '");>Details</button>';

                        tr1 = tr1+"<tr><td>" + '' + "</td><td>" + emp.Total + "</td><td>" + emp.Pending + "</td><td>" + emp.completed + "</td><td>" + detail + "</td></tr>";
                      
                    });
                 
                    $('#thhospitalcardocebody').html(tr1);

                    //hospital care dashboard count
                    $('#hos_app').html(data1.Table2[0].hoscarecount);
                    $('#hos_unscheduled').html(data1.Table1[0].Unschedule);
                    $('#hos_scheduled').html(data1.Table1[0].Schedule);
                    $('#hos_assigned').html(data1.Table1[0].assigned);
                    $('#hos_completed').html(data1.Table1[0].completed);
                    $('#hos_pending').html(data1.Table1[0].Pending);
                    $('#hos_pac_sold').html(data1.Table2[0].pac_sold);
                    $('#hos_ser_sold').html(data1.Table2[0].ser_sold);

                    $('#hospitalcaresumdoc').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });                                                                       
               
                }
                else {
                    console.log(data);
                }

            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}
//Investigation Tab
function LoadInvestigationTbl() {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata = {

        Dml_Indicator: "IL",
        branch_id:branch_id
    }

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

            }
            else {

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


                    $('#IL_Summary').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        details = '<button class="btn btn-primaryuse btn-xs" onclick=InvestigationDetails("' + emp.doctorid + '");>Details</button>';

                        trd = trd + "<tr><td>" + emp.doctor_name + "</td><td>" + emp.Total + "</td><td>" + emp.completed + "</td><td>" + emp.Pending + "</td><td>" + details + "</td></tr>";

                    });


                    $('#IL_Tblbody').html(trd);

                    $('#pres_count').html(data1.Table1[0].Total);
                    $('#cmpt_count').html(data1.Table1[0].completed);
                    $('#pending_count').html(data1.Table1[0].Pending);
                    //$('#LHCA_count').html(data1.Table1[0].labhomecarecount);
                    $('#IL_Summary').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });



                }
                else {
                    console.log(data);
                }

            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}
//Investigation Details
function InvestigationDetails(id) {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata = {

        Dml_Indicator: "IDS",
        subcategory_department_id: id,
        branch_id: branch_id
    }

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

            }
            else {

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

                    $('#ILdetails_body').empty();
                    $('#IL_Summary_details').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        debugger;

                        var HCstatus = '', HCAction = '';

                        if (emp.work_order_status == 4) {
                            HCstatus = '<span class="label label-success">Completed</span>';
                        }
                        else if (emp.service_name == 5) {
                            HCstatus = '<span class="label label-danger">Cancelled</span>';
                        }
                        else {
                            HCstatus = '<span class="label label-warning">' + emp.statusflag + '</span>';
                        }

                        HCAction = '<button type="button" class="btn btn-primaryuse btn-xs">' + emp.btnflag + '</button>';

                        //trd = trd + "<tr><td>" + emp.service_name + "</td><td>" + emp.service_code + "</td><td>" + emp.package_purchased_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.phone_number_1 + "</td><td>" + emp.patient_id + "</td><td>" + emp.sub_cd_name + "</td><td>" + '' + "</td><td>" + '' + "</td><td>" + emp.scheduledate + "</td><td>" + emp.serviceprovidername + "</td><td>" + HCstatus + "</td><td>" + HCAction + "</td></tr>";
                        trd = trd + "<tr><td>" + emp.work_order_id + "</td><td>" + emp.service_name + "</td><td>" + emp.prescribe_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.patient_id + "</td><td>" + emp.phone_number_1 + "</td><td>" + emp.visit_report_id + "</td><td>" + emp.service_date + "</td><td>" + emp.samplebarcode_id + "</td><td>" + HCstatus + "</td></tr>";

                    });


                    $('#ILdetails_body').html(trd);


                    $('#IL_Summary_details').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });


                }
                else {
                    console.log(data);
                }

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


function HomeCareDetails(id)
{
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata = {

        Dml_Indicator: "HMDS",
        subcategory_department_id: id,
        branch_id:branch_id
    }

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

            }
            else {

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

                    $('#thhomecareDetailsbody').empty();
                    $('#hcDetails_summary').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        debugger;

                        var HCstatus = '', HCAction = '';

                        if (emp.work_order_status == 4)
                        {
                            HCstatus = '<span class="label label-success">Completed</span>';
                        }
                        else if (emp.service_name == 5)
                        {
                            HCstatus = '<span class="label label-danger">Cancelled</span>';
                        }
                        else
                        {
                            HCstatus = '<span class="label label-warning">' + emp.statusflag + '</span>';
                        }

                        HCAction = '<button type="button" class="btn btn-primaryuse btn-xs">' + emp.btnflag + '</button>';

                        //trd = trd + "<tr><td>" + emp.service_name + "</td><td>" + emp.service_code + "</td><td>" + emp.package_purchased_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.phone_number_1 + "</td><td>" + emp.patient_id + "</td><td>" + emp.sub_cd_name + "</td><td>" + '' + "</td><td>" + '' + "</td><td>" + emp.scheduledate + "</td><td>" + emp.serviceprovidername + "</td><td>" + HCstatus + "</td><td>" + HCAction + "</td></tr>";
                        trd = trd + "<tr><td>" + emp.service_name + "</td><td>" + emp.service_code + "</td><td>" + emp.package_purchased_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.phone_number_1 + "</td><td>" + emp.patient_id + "</td><td>" + emp.sub_cd_name + "</td><td>" + '' + "</td><td>" + '' + "</td><td>" + emp.scheduledate + "</td><td>" + emp.serviceprovidername + "</td><td>" + HCstatus + "</td></tr>";

                    });


                    $('#thhomecareDetailsbody').html(trd);


                    $('#hcDetails_summary').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });


                }
                else {
                    console.log(data);
                }

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

function Patient_popup(id,age,gender,Mobile,email) {

    $('#PatientDetailsPop').modal('show');

    $('#P_id').html(id);
    $('#P_name').html($('#Pname_' + id).val());
    $('#P_AgeGender').html(age + ' Yrs / ' + gender);
    $('#P_mobile').html(Mobile);
    $('#P_email').html(email);
    $('#P_address').html($('#Paddress_' + id).val());
    
}

function HospitalCareDetails(id, DML) {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata = {

        Dml_Indicator: DML,
        subcategory_department_id: id,
        branch_id:branch_id
    }

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

            }
            else {

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

                    $('#thHospitalcare_Detailsbody').empty();
                    $('#Hospitalcare_Details').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        debugger;

                        var HCstatus = '', HCAction = '';

                        if (emp.work_order_status == 4) {
                            HCstatus = '<span class="label label-success">Completed</span>';
                        }
                        else if (emp.service_name == 5) {
                            HCstatus = '<span class="label label-danger">Cancelled</span>';
                        }
                        else {
                            HCstatus = '<span class="label label-warning">' + emp.statusflag + '</span>';
                        }

                        HCAction = '<button type="button" class="btn btn-primaryuse btn-xs">' + emp.btnflag + '</button>';

                        //trd = trd + "<tr><td>" + emp.service_name + "</td><td>" + emp.service_code + "</td><td>" + emp.package_purchased_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.phone_number_1 + "</td><td>" + emp.patient_id + "</td><td>" + emp.sub_cd_name + "</td><td>" + '' + "</td><td>" + '' + "</td><td>" + emp.scheduledate + "</td><td>" + emp.serviceprovidername + "</td><td>" + HCstatus + "</td><td>" + HCAction + "</td></tr>";
                        trd = trd + "<tr><td>" + emp.service_name + "</td><td>" + emp.service_code + "</td><td>" + emp.package_purchased_date + "</td><td><input type='hidden' id='Pname_" + emp.patient_id + "' value=" + emp.patient_username + " /><input type='hidden' id='Paddress_" + emp.patient_id + "' value=" + emp.paddress + " /><a onclick=Patient_popup('" + emp.patient_id + "','" + emp.patient_age + "','" + emp.patient_gender + "','" + emp.phone_number_1 + "','" + emp.patient_email + "');>" + emp.patient_username + "</a></td><td>" + emp.phone_number_1 + "</td><td>" + emp.patient_id + "</td><td>" + emp.sub_cd_name + "</td><td>" + '' + "</td><td>" + '' + "</td><td>" + emp.scheduledate + "</td><td>" + emp.serviceprovidername + "</td><td>" + HCstatus + "</td></tr>";

                    });


                    $('#thHospitalcare_Detailsbody').html(trd);


                    $('#Hospitalcare_Details').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });


                }
                else {
                    console.log(data);
                }

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

function LoadIPTbl() {
    var branch_id = ($('#HospitalBranchID').val() == '' ? 0 : $('#HospitalBranchID').val())
    var HomeCaredata = {

        Dml_Indicator: "IPDS",
        branch_id:branch_id
    }

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

            }
            else {

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


                    $('#TblIPdashboard').dataTable().fnDestroy();
                    var trd = '';
                    $.each(data1.Table, function (j, emp) {

                        details = '<a href="">View';

                        //trd = trd + "<tr><td>" + (j + 1) + "</td><td>" + emp.patient_id + "</td><td>" + emp.first_name + "</td><td>" + emp.phone_number + "</td><td>" + emp.gender + "</td><td>" + emp.age + "</td><td>" + emp.type_of_admition + "</td><td>" + emp.department + "</td><td>" + emp.ward_name + "</td><td>" + emp.Bed_Name + "</td><td>" + emp.admidate + "</td><td>" + emp.discharge + "</td><td>" + emp.Noofdays_stay + "</td><td>" + details + "</td></tr>";
                        trd = trd + "<tr><td>" + (j + 1) + "</td><td>" + emp.patient_id + "</td><td>" + emp.first_name + "</td><td>" + emp.phone_number + "</td><td>" + emp.gender + "</td><td>" + emp.age + "</td><td>" + emp.type_of_admition + "</td><td>" + emp.department + "</td><td>" + emp.ward_name + "</td><td>" + emp.Bed_Name + "</td><td>" + emp.admidate + "</td><td>" + emp.discharge + "</td><td>" + emp.Noofdays_stay + "</td></tr>";


                    });

                    $('#ip_active').html(data1.Table1[0].ip_active);
                    $('#ip_new').html(data1.Table1[0].ip_new);
                    $('#ip_bed').html(data1.Table1[0].bed_count);


                    $('#TblIPdashboard_body').html(trd);


                    $('#TblIPdashboard').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    });



                }
                else {
                    console.log(data);
                }

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

function WardNo() {

    var Warddata = {
        Dml_Indicator: "DHWL"
        //branchid: $('#SelBranch').val()
    }

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

            }
            else {

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

                    var WardNo;
                    $('#ddWard').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {

                        WardNo = WardNo + '<option value="' + emp.wardid + '">' + emp.ward_id + "/" + emp.ward_name + '</option>';

                    });
                    $('#ddWard').append(default_opt + WardNo);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}


//load visit type
function loadvisit_type() {
    debugger;
    var data = {
        P_Dml_Indicator: 'DVT'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Service_Master_Ins_upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data);

            if (jQuery.isEmptyObject((data1.data))) {

            }
            else {

                if (data1.msg == "Success") {
                    var data2 = data1.data;
                 
                    $('#op_lbl_name').html(data2[0].visit_type_name + ' ' + 'Bills')
                    if (data2[1].status == 1) {
                        $('#ip_bill_card').removeClass('hidden')
                        $('#ip_lbl_name').html(data2[1].visit_type_name + ' ' + 'Bills')
                    }
                    else {
                        $('#ip_bill_card').addClass('hidden')
                    }
                    $('#homecare_lbl_name').html(data2[2].visit_type_name + ' ' + 'Appointments')
                }
            }
        }
    })
}

