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

//Load Branch
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;
                    //$('#pkg_branch_id').attr('disabled', true);
                    $('#fbranch').empty();
                    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>';
                    });
                    if (data1.Table.length == 1) {
                        $('#fbranch').append(SubDepartment);
                    }
                    else {
                        if ($('#hiddenRole_id').val() == 1) {
                            $('#fbranch').append(SubDepartment);
                        }
                        else {
                            $('#fbranch').append(default_opt + SubDepartment);
                        }  
                    }

                    //Only KinderSurge
                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('#fbranch').val($('#HospitalBranchID').val());
                    //    LoadDoctor();
                    //}
                    //else 
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#fbranch').val($('#HospitalBranchID').val());
                        $('#fbranch').attr('disabled', true);
                        LoadDoctor();
                    }
                    else {
                        LoadDoctor();
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$('#fbranch').change(function () {
    LoadDoctor();
});

function LoadDoctor() {
    debugger
    var roleId = $('#hiddenRole_id').val();
    var DoctorId = $('#HiddenDoctorID').val();
    var DoctorData = {
        Dml_Indicator: "HDL",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#fbranch').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CalendarConfiguration/HosiptalSelect',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#txtDoctorName').empty();
            var data1 = JSON.parse(data)
            console.log(data1);
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#txtDoctorName').empty();
                    var default_opt = '<option value="0">All Doctor</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>';
                        }
                        $('#txtDoctorName,.txt_doctor_name').html(default_opt + SelRole);

                        if ($('#hiddenRole_id').val() == 1) {
                            $('#txtDoctorName').val($('#HiddenDoctorID').val());
                            $('#txtDoctorName').attr('disabled', true);
                        }
                    }

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

function AdmissionRequestTbl() {
    debugger

    var ART = $('#txtfrom_date').val();
    if (ART == '') {
        ART = '';
    }
    else {
        var a = ART.split('/');
        ART = a[2] + '-' + a[1] + '-' + a[0];
    }
    var AR_data = {

        roleid: $('#hiddenRole_id').val(),
        DML_indicator: "ARS",
        branch_id:$('#fbranch').val(),
        patient_name: $('#txtPatientName').val(),
        mobile_no: $('#txtMobileNumber').val(),
        doctor_name: $('#txtDoctorName').val(),
        request_date: ART,
        ip_admission_request_id: $('#HospitalBranchID').val()

    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/Ipaddmission_Ins_Upd',
        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") {
                    var data2 = data1.data;
                    var trd = '';
                    $('#Arequestbody').empty();
                    $('#ip_admission_request_table').dataTable().fnClearTable();
                    $('#ip_admission_request_table').dataTable().fnDestroy();
                    $.each(data2.Table, function (j, emp) {

                        Revoke = '<a href="javascript:void(0);" onclick="RevokeDelete(' + emp.ip_admission_request_id + ',3)" title="Decline">Decline</a>';
                        Delete = '<a class="delete_icon" data-id="CD_D1" title="Delete" data-type="role_rights_delete" data-nav="sp_div" data-name="delete_CategoryDepartment_form" onclick="RevokeDelete(' + emp.ip_admission_request_id + ',0);"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';

                        AdmitPage = '<a title="Transfer to IP" onclick="formclick(\'' + emp.patient_id + '\', \'' + emp.OP_visit_report_id + '\', \'' + emp.doctor_id + '\', \'' + emp.branch_id + '\', this)">Transfer to IP</a>';

                        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></div><li>' + Revoke + '</li><li>' + AdmitPage + '</li></ul></div>';


                        trd = trd + "<tr><td>" + emp.RowNo + "</td><td>" + emp.patient_id + "</td><td>" + emp.PatientName + "</td><td>" + emp.patient_phone_number + "</td><td>" + emp.DoctorName + "</td><td>" + emp.Request_Created_Date + "</td><td>" + emp.reason + "</td><td>" + emp.created_name + "</td><td>" + other_info+ "</td></tr>";

                    });


                    $('#Arequestbody').html(trd);
                    datatable('ip_admission_request_table', 'ip_admission_request', 'Legal');
                }
                else {
                    console.log(data);
                }
            }

        }
    });
}

function formclick(patient_id, visit_report_id, doctor_id, branch_id,event) {
    debugger
    //  CheckFormRights(patient_id, 56, 1, 2)
    $('#Hdpatient_id').val(patient_id);
    $('#Hdvisit_report_id').val(visit_report_id);
    $('#Hddoctor_id').val(doctor_id);
    $('#Hdbranch_id').val(branch_id);
    //$('#forms_popup').modal("toggle");
    $('#formShow').removeAttr('src');
    $('#formShow_header').html('')
    loadserviceForm()
  
    var admitPatientUrl = '/Ipmanagement/admitpatient?Patient_Id=' + patient_id + '&OPVisitReport_Id=' + visit_report_id + '&doctorId=' + doctor_id + '&branch_id=' + branch_id;
    window.location.href = admitPatientUrl;
   
}
//$('#forms_popup').on('hidden.bs.modal', function (e) {
//    debugger
//    var admitPatientUrl = '/Ipmanagement/admitpatient?Patient_Id=' +  $('#Hdpatient_id').val() + '&OPVisitReport_Id=' + $('#Hdvisit_report_id').val() + '&doctorId=' + $('#Hddoctor_id').val() + '&branch_id=' + $('#Hdbranch_id').val();
//    window.location.href = admitPatientUrl;
//});

//load Select Form
function loadserviceForm() {
    debugger;
    var data = {
        Dml_Indicator: 'FSAR'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/ServiceFormMapping_Ins_upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;

            var data2 = JSON.parse(data);
            console.log(data2);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    $('#selectfrom').empty();
                    var option = "";
                    var data1 = data2.data;
                    if (data1.length > 0) {
                        $('#iframeFormpopup').modal("toggle");
                    }
                    var default_option = "<option value='0'>--Select--</option>";
                    $.each(data1, function (i, emp) {
                        option = option + "<option value=" + emp.Formid + " >" + emp.Form_Name + "</option>"
                    })
                    // $('#servicetype_filter').append(default_option + option);
                    $('#selectfrom').append(default_option + option);
                }
            }
        }
    })
}

$('#selectfrom').change(function () {
    debugger;
    var selectid = $('#selectfrom').val()
    selectedOptionText = $(this).find('option:selected').text();

    $('#formShow_header').html(selectedOptionText);

    $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + selectid + '?patientid=' + btoa($('#Hdpatient_id').val()) + '&visit_reportid=' + btoa($('#Hdvisit_report_id').val()) + '')


})

$('#AR_FilterBtn').click(function () {
    AdmissionRequestTbl();
});

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

    $('#txtPatientName').val('');
    $('#txtMobileNumber').val('');
    $('#txtfrom_date').val('');
    if (parseInt($('#hiddenRole_id').val()) != 1) {
        $('#txtDoctorName').val('0');
        //$('#txtDoctorName').empty();
    }

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

function RevokeDelete(request_id, type) {

    var dml = '', text = '';

    if (type == 3) {
        dml = "RAR";
        text = 'Revoke';
    }
    else {
        dml = "DAR";
        text = 'Delete';
    }

    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 ' + text + ' the Admission Request?',
        {
            closeButton: true,
            progressBar: true,
            allowHtml: true,
            timeOut: 0,

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

                    var AR_data = {

                        DML_indicator: dml,
                        ip_admission_request_id: request_id

                    }

                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/Ipmanagement/Ipaddmission_Ins_Upd',
                        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('Patient Admission Request ' + text + ' Successfully',
                                     {
                                         //closeButton: false,
                                         progressBar: true,
                                         allowHtml: true,
                                         timeOut: 0,
                                     });

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

}