
$(document).ready(function () {
    ChatTableBind()
    LoadBranch()
});
function ChatTableBind(chat_CBranch,chat_pat_name, chat_pat_id) {
    debugger;
    var ChatData = {
        branch_id: chat_CBranch,
        patient_name: chat_pat_name,
        Patient_id: chat_pat_id,
        Dml_Indicator: 'S'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: "/Chat/GetChat",
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(ChatData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#tbl_billing_body').empty();
                    $('#tbl_billing').dataTable().fnClearTable();
                    $('#tbl_billing').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    //var discharge_action = '<i class="fa fa-file" data-toggle="modal" data-target="#discharge_patient_action" aria-hidden="true"></i>';
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success"></span>';
                        }
                        else if (emp.status == null) {
                            active_inactive = '<span class="label label-warning"></span>';
                        }
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.first_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.user_created_on + '</td><td>' + emp.Subscriber_end_date + '</td><td>' + emp.status + '</td><td>' + emp.Chat_ended_by + '</td><td><div class="onoffswitch"><input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox_product" id="crystalreportonoffswitch_' + j + '" ' + active_inactive + ' ><label class="onoffswitch-label_product" for="crystalreportonoffswitch_' + j + '"><span class="onoffswitch-inner_product"></span><span class="onoffswitch-switch_product"></span></label></div></td></tr>'
                    });
                    $('#tbl_billing_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('tbl_billing', 'tbl_billing', 'Legal');

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

//Load Branch
function LoadBranch() {
    debugger;
    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) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment = "";
                    $('#CBranch').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#CBranch').append(SubDepartment);
                        $('#CBranch').change();

                    }
                    else {
                        $('#CBranch').append(default_opt + SubDepartment);

                    }
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#CBranch').val($('#HospitalBranchID').val());
                        $('#CBranch').attr('disabled', true);
                        loadhospitaldoctor();

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

$('#CBranch').change(function () {
    $('#CDoctor').empty();
    loadhospitaldoctor();
});

function loadhospitaldoctor() {
    var DoctorData = {
        Dml_Indicator: "DL",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#CBranch').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            //$('#directappointment').modal('toggle');
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var tr = '';
                    var SelRole = '';
                    $('#CDoctor').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (data1.length > 0) {
                        for (var i = 0; i < data1.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1[i].doctor_id + '">' + data1[i].first_name + '</option>';
                        }
                        $('#CDoctor').append(default_opt + SelRole);

                        if ($('#hiddenRole_id').val() == 1) {
                            $('#CDoctor').val($('#HiddenDoctorID').val());
                            $('#CDoctor').attr('disabled', true);
                        }
                    }
                }
                else {
                    console.log(data);
                }

            }
        }
    });
}

$('#btn_filter').on('click', function () {
    debugger;
    var chat_CBranch = $('#CBranch').val();
    //var chat_CDoctor = $('#CDoctor').val();
    //var chat_sub_end_date = $('#sub_end_date').val();
    //var chat_sub_start_date = $('#sub_start_date').val();
    //var chat_active_filter = $('#active_filter').val();
    var chat_pat_name = $('#chat_patient_name').val();
    var chat_pat_id = $('#chat_patient_id').val();
    ChatTableBind(chat_CBranch,chat_pat_name, chat_pat_id)
  
})
$('#btnclear').click(function () {
    debugger;
    dlclear();
});
function dlclear() {
    $('#CBranch').val(0),
    $('#CDoctor').val(0),
    $('#sub_end_date').val(""),
    $('#sub_start_date').val(""),
    $('#active_filter').val(0),
    $('#chat_patient_id').val(""),
    $('#chat_patient_name').val("")
    ChatTableBind()
}

