$(document).ready(function () {
    LoadBranch();
    filterbedname();

    if (parseInt($('#hiddenRole_id').val()) == 2) {
        BedDetailsTable();
    }
    //$('#Beddetails_list').DataTable();
});

function LoadBranch() {
    $('#ddBranch').empty();
    $('#Count_Branch').empty();

    //Only KinderSurge
    //var dml = ''
    //if (parseInt($('#hiddenRole_id').val()) == 1) {
    //    dml = 'DLBRAN'
    //}
    //else {
    //    dml = 'LBRAN'
    //}

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        //Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.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) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    var Branch;
                    var default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        Branch = Branch + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });

                    if ($('#hiddenRole_id').val() == 1) {
                        $('#ddBranch').append(Branch);
                        $('#Count_Branch').append(Branch);
                    }
                    else {
                        $('#ddBranch').append(default_opt + Branch);
                        $('#Count_Branch').append(default_opt + Branch);
                    }

                    
                    //Only KinderSurge
                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('#ddBranch').val($('#HospitalBranchID').val());

                    //    $('#Count_Branch').val($('#HospitalBranchID').val());

                    //    BedDetailsTable();
                    //}
                    //else 
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#ddBranch').val($('#HospitalBranchID').val());
                        $('#ddBranch').attr('disabled', true);

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

                        BedDetailsTable();
                    }

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

        }
    });
}

function WardNo() {

    //var DML = 'DHWL'

    //if ($('#ddBranch').val() != "0")
    //{
    //    DML = 'AWL'
    //}

    var Warddata = {
        Dml_Indicator: 'AWL',
        branchid: $('#ddBranch').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;
                    $('#ddselectWard').empty();
                    //$('#FddWard_Name').empty();
                    var default_opt = '<option value="0">All Ward</option>';
                    $.each(data1, function (i, emp) {

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

                    });
                    $('#ddselectWard').append(default_opt + WardNo);
                    //$('#FddWard_Name').append(default_opt + WardNo);
                    filterbedname();
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

$('#ddBranch').change(function () {
    WardNo();
});

function BedDetailsTable()
{
    var AR_data = {

        Dml_Indicator: "BDPL",
        ward_id: $('#ddselectWard').val(),
        branch_id: $('#ddBranch').val(),
        ward_status: $('#FddBed_Status').val(),
        //ward_name: $('#FddWard_Name').val(),
        category_name: $('#FddBed_Name').val()

    }

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

                if (data1.msg == "Success") {

                    var data2 = data1.data;
                    var trd = '', BedCard='';

                    if (data2.Table1.length > 0) {

                        $('#TBeds_count').html(data2.Table1[0].total_bedcount);
                        $('#BedsOccupied').html(data2.Table1[0].bed_Occupied);
                        $('#BedsAvailable').html(data2.Table1[0].bed_Available);
                    }
                    
                   
                    $('#Beddetails_list').dataTable().fnClearTable();
                    $('#Beddetails_list').dataTable().fnDraw();
                    $('#Beddetails_list').dataTable().fnDestroy();

                    
                    $.each(data2.Table, function (j, emp) {

                        var BedIcon = '', BedStatus = '', Gender = '';

                        if (emp.Bed_Status == "Occupied") {
                            BedStatus = '<span class="label btn-primary">' + emp.Bed_Status + '</span>';
                            BedIcon = '<i class="fa fa-bed" aria-hidden="true" style="color:#3c8dbc; font-size:15px;"></i>';
                        }
                        else if (emp.Bed_Status == "Vacant") {
                            BedStatus = '<span class="label label-success">' + emp.Bed_Status + '</span>';
                            BedIcon = '<i class="fa fa-bed" aria-hidden="true" style="color:#00a65a; font-size:15px;"></i>';     
                        }
                        else {
                            BedStatus = '<span class="label label-danger">' + emp.Bed_Status + '</span>';
                            BedIcon = '<i class="fa fa-bed" aria-hidden="true" style="color:#dd4b39; font-size:15px;"></i>';
                        }

                        if (emp.gender == 'Male') {
                            Gender = emp.gender + " " + '<i class="fa fa-male" aria-hidden="true" style="color:#01A6EA; font-size:15px;"></i>';
                        }
                        else if (emp.gender == 'Female') {
                            Gender = emp.gender + " " + '<i class="fa fa-female" aria-hidden="true" style="color:#FFB1CB; font-size:15px;"></i>';
                        }
                        else if (emp.gender == 'Others') {
                            Gender = emp.gender + " " + '<i class="fa fa-transgender" aria-hidden="true" style="color:red; font-size:15px;"></i>';
                        }
                        else {
                            Gender = emp.gender;
                        }

                        BedCard = BedCard + '<div class="bed_card_view">'
    +'<div class="col-sm-6 col-md-3 col-xs-12">'
        + '<div class="pat-box mb-20">'
            +'<div class="row">'
                +'<div class="col-sm-12">'
                    +'<h5> Ward Name</h5>'
                +'</div>'
                +'<div class="col-sm-12 pl-20 patient-idnumber" style="height:13px;">'
                    + '<h5>' + emp.ward_name + '</h5>'
                +'</div>'
                +'<div class="col-sm-12 col-xs-12 pl-20 pat-content">'
                    + '<h6> <span class="pat-heading"> Bed Name : </span> ' + emp.Bed_Name + ' </h6>'
                +'</div>'
                +'<div class="col-sm-12 pl-20 patient-name">'
                    + '<h6> <a class="btn_r" style="cursor:pointer;">Patient Name : ' + emp.PatientName + ' | ' + emp.patient_id + '</a></h6>'
                +'</div>'
            +'</div>' 
            +'<div class="row">'
                +'<div class="col-sm-12 col-xs-12 pat-content">'
                    + '<p> <span class="pat-heading"> Bed Status : </span> '+ BedStatus + " " + BedIcon +'</p>'
                +'</div>'
                +'<div class="col-sm-12 col-xs-12 pat-content">'
                    + '<p> <span class="pat-heading"> Gender : </span> ' + Gender + '</p>'
                +'</div>'
            +'</div>'
          +'</div>'
       +'</div>'
    +'</div>'

                        //AdmitPage = '<a href="/Ipmanagement/admitpatient?Patient_Id=' + emp.patient_id + '&OPVisitReport_Id=' + emp.OP_visit_report_id + '&doctorId=' + emp.doctor_id + '&branch_id=' + emp.branch_id + '" type="button" class="btn btn-xs btn-primaryuse">Transfer to IP</a>';

                        trd = trd + "<tr><td>" + emp.RowNo + "</td><td>" + emp.ward_name + "</td><td>" + emp.floor_name + "/" + emp.section_name + "</td><td>" + emp.Bed_Name + "</td><td>" + emp.patient_id + "</td><td>" + emp.PatientName + "</td><td>" + emp.age + "</td><td>" + Gender + "</td><td>" + emp.visit_report_id + "</td><td>" + emp.DoctorName + "</td><td>" + emp.ip_patient_admitdate + "</td><td>" + BedStatus + " " + BedIcon + "</td></tr>";

                    });

                    debugger;
                    if ($('#bed_table_view').is(":visible")) {
                        $('#bed_table_view').show();
                        $('#BedCard_List').hide();
                    }
                    else {
                        $('#bed_table_view').hide();
                        $('#BedCard_List').show();
                    }
                    
                    $('#BedCard_List').html(BedCard);
                    $('#Beddetails_body').html(trd);
                    datatable('Beddetails_list', 'bed_details', 'Legal');

                   

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

        }
    });
}

//$('#ddselectWard').change(function () {
//    BedDetailsTable();
//});

$('#ddselectWard').change(function () {

    filterbedname();

    //var Beddata = {
    //    Dml_Indicator: "ABN",
    //    ward_no: $("#ddselectWard").val()
    //}

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

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

    //                var BedName;
    //                var default_opt = '<option value="0">All Bed</option>';
    //                $.each(data1, function (i, emp) {

    //                    BedName = BedName + '<option value="' + emp.bedid + '">' + emp.Bed_Name + '</option>';

    //                });
    //                $('#FddBed_Name').append(default_opt + BedName);
    //            }
    //            else {
    //                console.log(data);
    //            }
    //        }
    //    }
    //});
});

$('#btnfilter').click(function () {
    BedDetailsTable();
});

$('#btnclear').click(function () {
    $('#FddBed_Status').val('');
    $('#ddselectWard').val('0');
    $('#FddBed_Name').val('0');

    if (parseInt($('#hiddenRole_id').val())==2)
    {
        $('#ddBranch').val('0');
    }

    BedDetailsTable();
});

//$('#btnbedclear').click(function () {
//    $('#ddselectWard').val('');
//    $('#ddBranch').val('');
//    BedDetailsTable();
//});


//load Bed Name
 function filterbedname() {
        debugger;
        var filterbed = {
            Dml_Indicator: "FBM",
            branchid: $('#ddBranch').val(),
            ward_no: $('#ddselectWard').val()
        }

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

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

                        var BedName='';
                        var default_opt = '<option value="0">All Bed</option>';
                        $.each(data1, function (i, emp) {

                            BedName = BedName + '<option value="' + emp.bedid + '">' + emp.Bed_Name + '</option>';

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

 $('#BedStatus_Count').click(function () {
     $('#Bed_Details_Count').modal("toggle");
     BedDetails_CountTbl();
 });

 $('#Count_Branch').change(function () {
     BedDetails_CountTbl();
 });

 function BedDetails_CountTbl() {
     var AR_data = {

         Dml_Indicator: "WBSC",
         branch_id: $('#Count_Branch').val()

     }

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

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


                     $('#TblBedDetails_Count').dataTable().fnClearTable();
                     $('#TblBedDetails_Count').dataTable().fnDraw();
                     $('#TblBedDetails_Count').dataTable().fnDestroy();

                     $.each(data2.Table, function (j, emp) {

                         trd = trd + "<tr><td>" + (j + 1) + "</td><td>" + emp.ward_name + "</td><td>" + emp.total + "</td><td>" + emp.bed_Occupied + "</td><td>" + emp.Available + "</td></tr>";

                     });


                     $('#TblBedDetails_CountBody').html(trd);
                     //datatable('TblBedDetails_Count', 'BedDetails_Count', '');
                     $('#TblBedDetails_Count').DataTable();
                 }
                 else {
                     console.log(data);
                 }
             }

         }
     });
 }


