
// Wardmangement JS //
// purpose : create wardmanagement details table //

$(document).ready(function () {
    //CheckCodeTemplate()
    LoadBranch();
    LoadWardCategory();
    //LoadFloor();
    LoadSection();
    LoadWardType();
    LoadWard_ToiletType();
    LoadRooms();
    LoadFeature_List();
});

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

    //LoadWardCategory();
    LoadWardname();
    ShowWard();
});

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

$('#btnclear').click(function () {

    $('#status_type_filter').val('');
    $('#FWardName').val('');

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

    ShowWard();
})

$('#ip_branch_id').change(function () {
    CheckCodeTemplate();

    //Medway all branch concept
    //LoadWardCategory();
    //LoadFloor();
    //LoadSection();
    //LoadWardType();
    //LoadWard_ToiletType();
    //ShowWard();
});

//CheckCodeTemplate
function CheckCodeTemplate() {
    debugger;
    var codeData = {
        //Dml_Indicator: "CT",
        Dml_Indicator: "BCT",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#ip_branch_id').val()
        //branch_id: $('#HospitalBranchID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Code_Templates_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(codeData),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data)
            data1 = data1.data;

            if (data1[0].count1 == 0) {
                alert('' + $('#ip_branch_id  option:selected').html() + ' Not Added Code Template. Please Add the Code Template at SetupIds');
                window.location.href = "/Hospitalsetup"
            }

        }
    });
}

function LoadBranch() {
    debugger;
    //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 {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    var All_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) {
                        $('#ip_branch_id').append(SubDepartment);
                        //$('#ddWCBranch').append(SubDepartment);//Medway all branch concept
                        $('#Tblbranch_id').append(SubDepartment);
                        CheckCodeTemplate();
                        LoadWardCategory();
                        //LoadFloor();
                        LoadSection();
                        LoadWardType();
                        LoadWard_ToiletType();
                        LoadRooms();
                        LoadFeature_List();
                    }
                    else {
                        if ($('#hiddenRole_id').val() == 1) {
                            //$('#ddWCBranch').append(SubDepartment);//Medway all branch concept
                            $('#Tblbranch_id').append(SubDepartment);
                        }
                        else {
                            //$('#ddWCBranch').append(All_opt + SubDepartment);//Medway all branch concept
                            $('#Tblbranch_id').append(All_opt + SubDepartment);
                        }
                        $('#ip_branch_id').append(default_opt + SubDepartment);
                    }

                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('#ip_branch_id').val($('#HospitalBranchID').val());
                    //    $('#ddWCBranch').val($('#HospitalBranchID').val());
                    //    $('#Tblbranch_id').val($('#HospitalBranchID').val());

                    //    LoadWardCategory();
                    //    LoadFloor();
                    //    LoadSection();
                    //}
                    //else 
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#ip_branch_id').val($('#HospitalBranchID').val());
                        $('#ip_branch_id').attr('disabled', true);
                        //$('#ddWCBranch').val($('#HospitalBranchID').val());//Medway all branch concept
                        //$('#ddWCBranch').attr('disabled', true);//Medway all branch concept
                        $('#Tblbranch_id').val($('#HospitalBranchID').val());
                        $('#Tblbranch_id').attr('disabled', true);

                        CheckCodeTemplate();
                        LoadWardCategory();
                        //LoadFloor();
                        LoadSection();
                        LoadWardType();
                        LoadWardname();
                        LoadWard_ToiletType();
                        LoadRooms();
                        LoadFeature_List();
                    }
                    //LoadWardCategory();
                    ShowWard();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function LoadWardCategory() {
    debugger;
    var Warddata = {
        Dml_Indicator: "WC",
        //branchid: $('#ip_branch_id').val() //Medway all branch concept
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Warddata),
        success: function (data) {
            debugger;
            $('#selWardCategory').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    console.log(data1);
                    var category;
                    $('#selWardCategory').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        category = category + '<option value="' + emp.ward_category_id + '">' + emp.category_name + '</option>';
                    });
                    if (data1.length == 1) {
                        $('#selWardCategory').append(category);
                        $("#selWardCategory").change();
                    }
                    else {
                        $('#selWardCategory').append(default_opt + category);
                    }
                    if ($('#HWardID').val() != '') {
                        $('#selWardCategory').val($('#HdnCategoryid').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function ShowWard() {
    debugger;
    $('#shade_slot').show();
    var Warddata = {

        Dml_Indicator: "S",
        role_id: $('#hiddenRole_id').val(),
        branch_id: $('#Tblbranch_id').val(),
        ward_status: $('#status_type_filter').val(),
        ward_name: $('#FWardName').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/WardManagement_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Warddata),
        success: function (data) {
            debugger;
            $('#tblWardmanagement_body').empty();

            if (jQuery.isEmptyObject(data)) {
                //toastr.success('No Data Found..',
                //      {
                //          //closeButton: false,
                //          progressBar: true,
                //          allowHtml: true,
                //          timeOut: 0,
                //      });
                $('#shade_slot').hide();
            }

            else {
                debugger;
                var data2 = JSON.parse(data);
                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var row_data = '';
                    //tr = $('<table id="example1" class="table table-responsive table-bordered table-striped dataTable " />');

                    //tr.append("<thead><tr><th>S.No</th><th>Ward ID</th><th>Ward Name</th><th>No of beds</th><th>Description</th><th>Notes</th><th>Created Date /Time<th>Ward Status</th><th  class='tbl-action'>Action</th></tr></thead><tbody>");
                    var j = 0;
                    $.each(data1.Table, function (i, emp) {
                        j = i + 1;
                        wardstatus = emp.ward_status;

                        if (emp.ward_status == "Active") {
                            wardstatus = '<span class="label label-success">Active</span>';
                        }
                        else {
                            wardstatus = '<span class="label label-danger">Inactive</span>';
                        }

                        //view = "<a href=#  data-toggle='tooltip data-placement='bottom' title='Edit' class='btn btn-edit btn-xs' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=loadWard(this,'" + emp.wardid + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                        //Bed = "<a  data-toggle='tooltip data-placement='bottom' title='Bed_Management' class='btn  btn-xs btn_r' data-id='BD_A1' data-type='role_rights_add' data-nav='redirect' data-name='/Ipmanagement/bedmangement?Ward_ID=" + emp.wardid + "&Ward_category=" + emp.ward_category_type + "'><i class='fa fa-bed'></i></a>";
                        //Delete = '<button type="button" class="btn btn-delete btn-xs" data-id="CD_D1" data-toggle="tooltip" data-placement="bottom" title="Delete" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_ward_form" onclick="DeleteWard(' + emp.BedOccupied + ',' + emp.wardid + ');"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';

                        view = "<a href=#  title='Edit' class='edit_icon' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=loadWard(this,'" + emp.wardid + "');><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                        Bed = "<a title='Bed_Management' class='btn_r' data-id='BD_A1' data-type='role_rights_add' data-nav='redirect' data-name='/Ipmanagement/bedmangement?Ward_ID=" + emp.wardid + "&Ward_category=" + emp.ward_category_type + "'>Bed Management</a>";
                        Delete = '<a type="button" class="delete_icon" data-id="CD_D1" title="Delete" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_ward_form" onclick="DeleteWard(' + emp.BedOccupied + ',' + emp.wardid + ');"><i class="fa fa-trash-o" aria-hidden="true"></i></a>';


                        if (emp.ward_status == "Inactive") {
                            //active_inactive = "<a href=#  data-toggle='tooltip data-placement='bottom' title='Make Active!' class='btn btn-blue btn-xs' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=wardact_inact('" + emp.wardid + "','active','" + emp.BedOccupied + "');><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";
                            active_inactive = "<a href=#  title='Make Active!' class='inactive_icon' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=wardact_inact('" + emp.wardid + "','active','" + emp.BedOccupied + "');><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";
                        }
                        else {
                            //active_inactive = "<a href=#  data-toggle='tooltip data-placement='bottom' title='Make In Active!' class='btn btn-blue btn-xs' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=wardact_inact('" + emp.wardid + "','inactive','" + emp.BedOccupied + "');><i class=' fa fa-minus-square-o' aria-hidden='true'></i></a>";
                            active_inactive = "<a href=#  title='Make In Active!' class='inactive_icon' data-id='WD_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='edit_ward_form' onclick=wardact_inact('" + emp.wardid + "','inactive','" + emp.BedOccupied + "');><i class=' fa fa-minus-square-o' aria-hidden='true'></i></a>";
                        }
                        if (emp.ward_status == "Inactive") {
                            //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + active_inactive + ' ' + Delete + ' </div></div>';
                            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>' + Delete + '</li><li>' + active_inactive + '</li></div></ul></div>';
                        }
                        else {
                            //other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + view + ' ' + Bed + ' ' + active_inactive + ' ' + Delete + '</div></div>';
                            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><li>' + active_inactive + '</li></div><li> ' + Bed + ' </li></ul></div>';
                        }

                        //var s = emp.feature_list.split(",");
                        //var Feature_list = '-'
                       
                        //if (s.length > 0) {
                        //    var pat_type = [];
                        //    for (var m = 0; m < s.length; m++) {
                        //        if (s[m] != 0) {
                        //            pat_type[m] = $("#ddfeaturelist option[value='" + s[m] + "']").text();
                        //        }
                        //        else {
                        //            pat_type[m] = '-'
                        //        }

                        //    }
                        //    Feature_list = pat_type.toString();
                        //}

                        var s = emp.feature_list.split(",");
                        var Feature_list = '-'

                        if (s.length > 0) {
                            var pat_type = [];
                            for (var m = 0; m < s.length; m++) {
                                if (s[m] != 0) {
                                    pat_type[m] = $("#ddfeaturelist option[value='" + s[m] + "']").text();
                                }
                                else {
                                    pat_type[m] = '-'
                                }

                            }
                            Feature_list = pat_type.toString();
                        }

                        row_data = row_data + "<tr><td>" + j + "</td><td>" + emp.ward_id + "</td><td class='tbl-name'>" + emp.ward_name + "</td><td>" + emp.no_of_beds + "</td><td>" + emp.ward_type_name + "</td><td>" + emp.toilet_type_name + "</td><td>" + emp.room_name + "</td><td>" + Feature_list + "</td><td>" + emp.description + "</td><td>" + emp.notes + "</td><td>" + emp.created_on + "</td><td>" + emp.ot_type_name + "</td><td>" + wardstatus + "</td><td>" + other_info + "</td></tr>";
                        //tr.append(row_data);
                    });
                    //tr.append("</tbody></table>");
                    $('#shade_slot').hide();
                    $('#tblWardmanagement').dataTable().fnClearTable();
                    $('#tblWardmanagement').dataTable().fnDestroy();
                    $('#tblWardmanagement_body').append(row_data);
                    datatable('tblWardmanagement', 'Ward_Management', 'Legal');

                    //var table = $('#example1').DataTable({

                    //});
                    //$('a.toggle-vis').on('click', function (e) {
                    //    e.preventDefault();

                    //    // Get the column API object
                    //    var column = table.column($(this).attr('data-column'));

                    //    // Toggle the visibility
                    //    column.visible(!column.visible());
                    //});

                }
                else {
                    console.log(data);
                }

            }
        },
        error: function (data) {
        }
    });
}

//purpose : wardmanagement Add button click event //
$('#BtnWDMAdd').click(function () {

    if ($.trim($('#ip_branch_id').val()) == 0) {
        $('#Pip_branch_id').show();
        $('#Pip_branch_id').fadeOut(8000);
        $('#Pip_branch_id').html('Select Branch');
        $('#ip_branch_id').focus();
        $('#ip_branch_id').val(0);
        return false;
    }

    if ($.trim($('#txtWardName').val()) == "") {
        $('#PtxtWardName').show();
        $('#PtxtWardName').fadeOut(8000);
        $('#PtxtWardName').html('Enter The Ward Name');
        $('#txtWardName').focus();
        $('#txtWardName').val('');
        return false;
    }

    if ($.trim($('#selWardCategory').val()) == 0) {
        $('#PselWardCategory').show();
        $('#PselWardCategory').fadeOut(8000);
        $('#PselWardCategory').html('Select Ward Category');
        $('#selWardCategory').focus();
        $('#selWardCategory').val(0);
        return false;
    }

    if ($.trim($('#txtNumberofbeds').val()) == "") {
        $('#PtxtNumberofbeds').show();
        $('#PtxtNumberofbeds').fadeOut(8000);
        $('#PtxtNumberofbeds').html('Enter The Number of beds');
        $('#txtNumberofbeds').focus();
        $('#txtNumberofbeds').val('');
        return false;
    }

    if (parseInt($.trim($('#txtNumberofbeds').val())) == 0) {
        $('#PtxtNumberofbeds').show();
        $('#PtxtNumberofbeds').fadeOut(8000);
        $('#PtxtNumberofbeds').html('0 Not Allowed');
        $('#txtNumberofbeds').focus();
        $('#txtNumberofbeds').val('');
        return false;
    }

    if ($.trim($('#txtSelectList').val()) == "") {
        $('#PtxtSelectList').show();
        $('#PtxtSelectList').fadeOut(8000);
        $('#PtxtSelectList').html('Select The Ward Status');
        $('#txtSelectList').focus();
        $('#txtSelectList').val('');
        return false;
    }



    debugger;
    var NoOfBed = parseInt($('#txtNumberofbeds').val());
    var ActiveBedCount = parseInt($('#HActiceBed').val());
    var WardStatus = parseInt($('#txtSelectList').val());

    if (ActiveBedCount > NoOfBed) {
        toastr.warning('Please increase the Bed Count');
        return false;
    }

    if (ActiveBedCount > 0 && WardStatus == 0) {
        toastr.warning('Multiple Bed is Active on this Ward');
        return false;
    }
    var future_list = ($('#ddfeaturelist').val() != null && $('#ddfeaturelist').val() != '') ? $('#ddfeaturelist').val().toString() : $('#ddfeaturelist').val();

    var Warddata = {
        ward_id: $('#HWardID').val(),
        ward_name: $('#txtWardName').val(),
        no_of_beds: $('#txtNumberofbeds').val(),
        description: $('#txtDescription').val(),
        notes: $('#txtNotes').val(),
        Dml_Indicator: "I",
        ward_status: $('#txtSelectList').val(),
        ward_category_type: $('#selWardCategory').val(),
        branch_id: $('#ip_branch_id').val(),
        //floorid: $('#ddFloor').val(),
        sectionid: $('#ddSection').val(),
        ward_typeid: $('#ddWardType').val(),
        toilet_typeid: $('#ddToiletType').val(),
        roomid: $('#ddrooms').val(),
        featurelist: future_list,
        ot_type: $('#dd_OT_Type').val()
    }

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

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

                $.each(data1.Table, function (i, emp) {
                    Error = emp.Message;
                });

                if (Error == 'Already Exists') {
                    toastr.warning('Ward Name Already Exists',
                        {
                            //closeButton: false,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 0,
                        });
                    return false;
                }

                if ($('#HWardID').val() == '') {
                    toastr.success('Ward Details Added Successfully',
                        {
                            //closeButton: false,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 0,
                        });
                }
                else {
                    toastr.success('Ward Details Upadated Successfully',
                       {
                           //closeButton: false,
                           progressBar: true,
                           allowHtml: true,
                           timeOut: 0,
                       });
                }

                clearWard();
                goback();
                ShowWard();
            }
            else {
                console.log(data);
            }

        },
        error: function (data) {

        }
    });

});

// clear wardmanagement screen All textbox value //
function clearWard() {
    $('#txtWardName').val(""),
    $('#txtNumberofbeds').val(""),
    $('#txtDescription').val(""),
    $('#txtNotes').val(""),
    $('#HWardID').val(""),
    $('#txtSelectList').val("1"),
    $('#selWardCategory').val("0"),
    $('#HdnCategoryid').val(''),
    //$('#ip_branch_id').val(0),
    $('#HActiceBed').val('');
    $('#ddFloor').val("0");
    $('#ddSection').val("0");
    $('#ddWardType').val("0");
    $('#ddToiletType').val("0");
    $('#ddrooms').val("0");
    $('#ddfeaturelist').val('').change();
}


// click edit button this function is run and  //
function loadWard(event, val) {
    $('#shade_slot').show();
    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 {
                     $('#ward_management_form').show();
                     $('.table_view').hide();
                     $('#add_new_ward_setup').hide();
                     $('#goback1').hide();
                     $('#goback2').show();
                     var Warddata = {
                         ward_id: val,
                         Dml_Indicator: "ID"
                     }

                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Ipmanagement/WardManagement_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;
                                     $.each(data1.Table, function (i, emp) {
                                         debugger;
                                         $('#ip_branch_id').val(emp.branch_id);
                                         $('#txtWardName').val(emp.ward_name);
                                         $("#txtNumberofbeds").val(emp.no_of_beds);
                                         $("#txtDescription").val(emp.description);
                                         $("#txtNotes").val(emp.notes);
                                         $("#txtSelectList").val(emp.ward_status);
                                         $('#HWardID').val(val);
                                         $('#HActiceBed').val(emp.ActiveBedCount);
                                         $('#selWardCategory').val(emp.ward_category_type);
                                         $('#HdnCategoryid').val(emp.ward_category_type);
                                         $('#Hdnfloorid').val(emp.floorid);
                                         $('#Hdnsectionid').val(emp.sectionid);
                                         $('#HdnWardType').val(emp.ward_typeid);
                                         $('#HdnToiletType').val(emp.toilet_typeid);
                                         $('#Hdnroomid').val(emp.roomid);
                                         $('#Hdnfeature_list').val(emp.feature_list);
                                         $('#dd_OT_Type').val(emp.ot_type);
                                    
                                         LoadWardCategory();
                                         //LoadFloor();
                                         LoadSection();
                                         LoadWardType();
                                         LoadWard_ToiletType();
                                         LoadRooms();
                                         LoadFeature_List();
                                     });
                                 }
                                 else {
                                     console.log(data);
                                 }
                                 $('#shade_slot').hide();
                             }
                         }
                     });
                 }
             }
         }
     });
}

//category add popup start 

function ShowWardCategory() {

    var Warddata = {

        Dml_Indicator: "WCS",
        role_id: $('#hiddenRole_id').val(),
        //branch_id: $('#ddWCBranch').val() //Medway all branch concept
    }

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

            }
            else {

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

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

                    //tr.append("<thead><tr><th>S.No</th><th>Category Name</th><th>Branch Name</th><th>Status</th><th>Action</th></tr></thead><tbody>"); //Medway all branch concept
                    tr.append("<thead><tr><th>S.No</th><th>Category Name</th><th>Status</th><th>Action</th></tr></thead><tbody>");

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

                        view = "<a href=# class='btn btn-primaryuse btn-xs'onclick=loadWardCategory('" + emp.ward_category_id + "','" + emp.branch_id + "','" + emp.is_active + "');>Category Edit</a>";

                        //row_data = "<tr><td>" + emp.RowNo + "</td><td id='CN_" + emp.ward_category_id + "'>" + emp.category_name + "</td><td>" + emp.branch_name + "</td><td>" + emp.CategoryStatus + "</td><td>" + view + "</td></tr>";//medway all branch concept
                        row_data = "<tr><td>" + emp.RowNo + "</td><td id='CN_" + emp.ward_category_id + "'>" + emp.category_name + "</td><td>" + emp.CategoryStatus + "</td><td>" + view + "</td></tr>";
                        tr.append(row_data);
                    });
                    tr.append("</tbody></table>");
                    $('#tblCategory').html(tr);
                }
                else {
                    console.log(data);
                }

            }
        },
        error: function (data) {
        }
    });

}

//Medway all Branch concept
//$('#ddWCBranch').change(function () {
//    ShowWardCategory();
//});

$('#BtnCategory').click(function () {

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

    //Medway all Branch concept
    //if ($('#hiddenRole_id').val() == 2)
    //{
    //    $('#ddWCBranch').val('0');
    //}
    //else {
    //    $('#ddWCBranch').val($('#HospitalBranchID').val());
    //}
    $('#ddWCStatus').val(1);    
    

    ShowWardCategory();
});

function loadWardCategory(category_id, branch_id, is_active) {

    $('#HdnCategoryid').val(category_id);
    $('#txtCategoryName').val($('#CN_' + category_id).html());
    //$('#ddWCBranch').val(branch_id);//Medway all Branch concept
    $('#ddWCStatus').val(is_active);


}

$('#Btn_Csubmit').click(function () {

    if ($.trim($('#txtCategoryName').val()) == "") {
        $('#PtxtCategoryName').show();
        $('#PtxtCategoryName').fadeOut(8000);
        $('#PtxtCategoryName').html('Enter The Category Name');
        $('#txtCategoryName').focus();
        $('#txtCategoryName').val('');
        return false;
    }

    //Medway all branch concept
    //if ($.trim($('#ddWCBranch').val()) == "0") {
    //    $('#PddWCBranch').show();
    //    $('#PddWCBranch').fadeOut(8000);
    //    $('#PddWCBranch').html('Select Branch');
    //    $('#ddWCBranch').focus();
    //    $('#ddWCBranch').val('0');
    //    return false;
    //}


    var Warddata = {
        category_id: $('#HdnCategoryid').val(),
        category_name: $('#txtCategoryName').val(),
        Dml_Indicator: "WCI",
        //branch_id: $('#ddWCBranch').val(),//Medway all branch concept
        category_status: $('#ddWCStatus').val()
    }

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

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

                var Error = '';

                $.each(data1.Table, function (i, emp) {
                    Error = emp.message;
                });

                if (Error == 'Already Exists') {
                    toastr.warning('Category Name Already Exists',
                        {
                            //closeButton: false,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 0,
                        });
                    return false;
                }

                if ($('#HdnCategoryid').val() == '') {
                    toastr.success('Category Added Successfully',
                        {
                            //closeButton: false,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 0,
                        });
                }
                else {
                    toastr.success('Category Updated Successfully',
                       {
                           //closeButton: false,
                           progressBar: true,
                           allowHtml: true,
                           timeOut: 0,
                       });
                }
                ShowWardCategory();
                $('#HdnCategoryid').val('');
                $('#txtCategoryName').val('');

            }
            else {
                console.log(data);
            }

        },
        error: function (data) {

        }
    });

});

$('#BthC_colse').click(function () {
    $('#HdnCategoryid').val('');
    $('#txtCategoryName').val('');
    LoadWardCategory();
});

function LoadFloor() {
    var Warddata = {
        Dml_Indicator: "WFS",
        //branch_id: $('#ip_branch_id').val()//Medway all branch concept
    }

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

            }
            else {

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


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

                        Floor = Floor + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddFloor').append(default_opt + Floor);
                    if ($('#HWardID').val() != '') {
                        $('#ddFloor').val($('#Hdnfloorid').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function LoadSection() {
    var Warddata = {
        Dml_Indicator: "SECS",
        hospital_id: $('#HospitalId').val()
        //branch_id: $('#ip_branch_id').val()//Medway all branch concept
    }

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

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var Section;
                    $('#ddSection').empty();

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

                        Section = Section + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddSection').append(default_opt + Section);
                    if ($('#HWardID').val() != '') {
                        $('#ddSection').val($('#Hdnsectionid').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}
function LoadRooms() {
    var Warddata = {
        Dml_Indicator: "ROOM",
        hospital_id: $('#HospitalId').val()
    }

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

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var Section;
                    $('#ddrooms').empty();

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

                        Section = Section + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddrooms').append(default_opt + Section);
                    if ($('#HWardID').val() != '') {
                       $('#ddrooms').val($('#Hdnroomid').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function LoadFeature_List() {
    debugger;
    var Warddata = {
        Dml_Indicator: "LFL"
    }

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

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var Section;
                    //$('#ddfeaturelist').val('').change();
                    $('#ddfeaturelist').empty();

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

                        Section = Section + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddfeaturelist').append(default_opt + Section);

                    if ($('#HWardID').val() != '') {
                        // $('#ddfeaturelist').val($('#Hdnfeature_list').val());
                        var featureListValues = $('#Hdnfeature_list').val();
                        var selectValues = [];
                        var valuesArray = featureListValues.split(',');
                        for (var i = 0; i < featureListValues.length; i++) {
                            var value = valuesArray[i];

                           
                            if ($('#ddfeaturelist option[value="' + value + '"]').length > 0) {
                                selectValues.push(value); 
                            }
                        }

                        $('#ddfeaturelist').val(selectValues).trigger('change');
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function wardact_inact(val, Dml_Indicator, count) {
    debugger;

    $("#HWardID").val(val);
    var status = "";
    if (Dml_Indicator == 'active') {
        status = "Are You Sure, You Want to Activate?";
    }
    else {

        if (count > 0) {
            toastr.warning("The More Bed Occupied this ward.So not In-activate this ward.");
            setTimeout(function () {
            }, 1000);
            return false;
        }

        status = "Are You Sure, You Want to In-activate?";
    }
    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 () {
                  debugger
                  var Parameter = {
                      Dml_Indicator: Dml_Indicator,
                      ward_id: $('#HWardID').val(),
                  }

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

                          }
                          else {

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

                                  if (Dml_Indicator == 'active') {
                                      toastr.success("Activated Successfully");
                                      setTimeout(function () {
                                          location.reload();
                                      }, 1000);
                                  }
                                  else {
                                      toastr.success("In-activated Successfully");
                                      setTimeout(function () {
                                          location.reload();
                                      }, 1000);
                                  }

                                  //IpOperationsLoad();
                                  //LoadBranch();
                              }
                              else {
                                  console.log(data);
                              }
                          }
                      }
                  });

              });
          }
      });

}

function DeleteWard(Count, wardid) {

    if (Count > 0) {
        toastr.warning("The More Bed Occupied this ward.So not Delete this ward.");
        setTimeout(function () {
        }, 1000);
        return false;
    }

    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>", 'Are You sure,You want to Delete the ward?',
        {
            closeButton: true,
            progressBar: true,
            allowHtml: true,
            timeOut: 0,

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

                    var AR_data = {

                        Dml_Indicator: "WMDEL",
                        ward_id: wardid

                    }

                    $.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) {

                            var data1 = JSON.parse(data);
                            if (jQuery.isEmptyObject((data1))) {
                            }
                            else {

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

                                    toastr.success('Ward Deleted Successfully',
                                     {
                                         //closeButton: false,
                                         progressBar: true,
                                         allowHtml: true,
                                         timeOut: 0,
                                     });

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

}

function LoadWardType() {
    var Warddata = {
        Dml_Indicator: "WRDT",
        //branch_id: $('#ip_branch_id').val()//Medway all branch concept
    }

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

            }
            else {

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


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

                        Floor = Floor + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddWardType').append(default_opt + Floor);
                    if ($('#HWardID').val() != '') {
                        $('#ddWardType').val($('#HdnWardType').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function LoadWard_ToiletType() {
    var Warddata = {
        Dml_Indicator: "TOIT",
        hospital_id: $('#HospitalId').val()
        //branch_id: $('#ip_branch_id').val()//Medway all branch concept
    }

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

            }
            else {

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


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

                        Floor = Floor + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddToiletType').append(default_opt + Floor);
                    if ($('#HWardID').val() != '') {
                        $('#ddToiletType').val($('#HdnToiletType').val());
                    }
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}


function LoadWardname() {
    var Warddata = {
        Dml_Indicator: "WN",
        branch_id: $('#Tblbranch_id').val()
    }

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

            }
            else {

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


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

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

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




