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



function LoadBranch() {
    $('#SelBranch').empty();
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    $('#SelBranch').append(default_opt + SubDepartment);

                    if ($('#hiddenRole_id').val() != 2) {
                        $('#SelBranch').val($('#HospitalBranchID').val());
                        $('#SelBranch').attr('disabled', true);
                        LoadDoctor();
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

function LoadDoctor() {
    var roleId = $('#hiddenRole_id').val();

    var DoctorData = {
        Dml_Indicator: "HDL",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#SelBranch').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data1 = JSON.parse(data)
                console.log(data1);
                if (data1.msg = "Success") {
                    var tr = '';
                    var SelRole = '';
                    $('#SelDoctor').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (data1.data.length > 0) {
                        for (var i = 0; i < data1.data.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data1.data[i].doctor_id + '">' + data1.data[i].first_name + '</option>';
                        }

                        $('#SelDoctor').append(default_opt + SelRole);


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

            }

        }
    });
}



function GetChatConfiguration() {
    $('#shade_slot').show();
   
    var DoctorId = '';
    if ($('#hiddenRole_id').val() == 2 || $('#hiddenRole_id').val() == 3) {
        DoctorId = $('#SelDoctor').val();
    }
    else {
        DoctorId = $('#myHiddenVar').val();
    }

    var Configuration = {
        doctor_id: DoctorId,
        indicator_id: 'S'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/ChatConfiguration/GetChatConfiguration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Configuration),
        success: function (data) {
            var data = JSON.parse(data);
            if (jQuery.isEmptyObject((data))) {
                $('#shade_slot').hide();
                var appointment = '<div class=\"table-responsive \"><table id=\"chatConfig\" class=\"ur-cal-conf-table table-responsive table table-bordered table-striped dataTable \" cellspacing=\"0\" width=\"100%\"><thead><tr><th>S No</th><th>Name</th><th>Patient Id</th><th>Subscription Start Date</th><th>Subscription End Date</th><th>Status</th><th>Chat Enabled By</th><th>Action</th></tr></thead><tbody>';
            }
            else {
                if (data.msg == "Success") {
                    var data = data.data;
                    debugger;
                    var appointment = '<div class=\"table-responsive \"><table id=\"chatConfig\" class=\"ur-cal-conf-table table-responsive table table-bordered table-striped dataTable \" cellspacing=\"0\" width=\"100%\"><thead><tr><th>S No</th><th>Name</th><th>Patient Id</th><th>Subscription Start Date</th><th>Subscription End Date</th><th>Status</th><th>Chat Enabled By</th><th>Action</th></tr></thead><tbody>';
                    $.each(data, function (i, emp) {
                        appointment = appointment + '<tr><td>' + (i + 1) + '</td><td class="tbl-name">' + emp.patient_first_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.subscription_start_date + '</td><td>' + emp.subscription_end_date + '</td><td>' + (emp.type == 1 ? "Paid" : "Complimentary") + '</td><td></td>';
                       
                        appointment = appointment + '<td><div class="onoffswitch"><input type="checkbox" name="onoffswitch" onchange="StatusChange(this,' + '\'' + emp.doctor_id + '\',\'' + emp.patient_id + '\');" class="onoffswitch-checkbox" id="myonoffswitch' + (i + 1) + '"';
                        if (emp.chatstatus == 1) {
                            appointment = appointment + 'checked';
                        }
                        appointment = appointment + '><label class="onoffswitch-label" for="myonoffswitch' + (i + 1) + '"><span class="onoffswitch-inner"></span><span class="onoffswitch-switch"></span></label></div></td>';
                      
                    });

                    appointment = appointment + '</tbody></table></div>';
                    $('#shade_slot').hide();
                    //$('#Configuration').html(appointment);
                    $.when($('#Configuration').html(appointment)).then($('#chatConfig').DataTable());


                }
                else {
                    console.log(data);
                }
            }
        }
    });
}
function StatusChange(obj, Doctor_Id, Patient_ID) {

    if ($('#hiddenRole_id').val() == 2) {
        $('#HiddenChatPatientid').val(Patient_ID);
        $('#HiddenChatDoctorid').val(Doctor_Id);
        $('#addComplimentarySubscription').modal('show');
    }
    else {
        toastr.warning('You dont have rights to enable the subscription. Please contact the administrator');
    }
}


function AddComplimentary(){
    debugger;
    if ($.trim($('#txtChatValidDays').val()) == "") {
        $('#PtxtChatValidDays').show();
        $('#PtxtChatValidDays').fadeOut(8000);
        $('#PtxtChatValidDays').html('Enter No of Valid Days');
        $('#txtChatValidDays').focus();
        $('#txtChatValidDays').val('');
        return false;
    }


    var Configuration = {
        doctor_id: $('#HiddenChatDoctorid').val(),
        Patient_id: $('#HiddenChatPatientid').val(),
        indicator_id: 'S',
        chat_valid_days: $('#txtChatValidDays').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/ChatConfiguration/GetChatConfiguration',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Configuration),
        success: function (data) {
            var data = JSON.parse(data);
            GetChatConfiguration();
        }
    });

}
