

$(document).ready(function () {
    debugger;
    LoadBranch();
    loaddischargelist();
    
    $('.pat-strip').hide();
    check_previlige_tabs(24,'MU_V1')

});
function check_previlige_tabs(formid,tab_id) {
    debugger;
    var RightData = {
        Stringtype: formid,
        Dml_Indicator: "CTABR",
        tab_id:tab_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/User_rights_privileges',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RightData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    var j = 0

                    //$.each(data1.Table, function (i, emp1) {
                    //    $("[data-id=" + emp1.tab_id + "]").removeClass('hidden');
                    //})

                    $.each(data1.Table1, function (i, emp) {
                        if (emp.status == 0) {
                            $("[data-id=" + emp.role_rights_view + "]").addClass('hidden');
                            $("[data-id=" + emp.role_rights_view + "]").removeClass('active');
                        }
                        else if (emp.status == 1) {
                            if (j == 0) {
                                    $("[data-id=" + emp.role_rights_view + "]").removeClass('hidden');
                                    $("[data-id=" + emp.role_rights_view + "]").addClass('in active');
                                    $('#'+emp.tab_name).click();
                            }
                            j=j + 1;
                        }
                    });
                }
            }
        }
    });
}

function loaddischargelist(dis_pat_name, dis_pat_id, dis_ip_number, dis_date, adm_date, f_date, t_date, dis_branch) {
    debugger;
    var user = {
        Dml_Indicator: "DL",
        patient_name:dis_pat_name,
        Patient_id: dis_pat_id,
        hospital_id: $('#HospitalId').val(),
        visit_report_id: dis_ip_number,
        received_on: dis_date,
        Createdon: adm_date,
        from_date: f_date,
        to_date: t_date,
        branch_id: dis_branch

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        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 {
                    $('#discharge_list_table_body').empty();
                    $('#discharge_list_table').dataTable().fnClearTable();
                    $('#discharge_list_table').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.discharge_status == '1') {
                            active_inactive = '<span class="label label-success">NORMAL DISCHARGE</span>';
                        }
                        else if (emp.discharge_status == '2') {
                            active_inactive = '<span class="label label-warning">CANCELLED DISCHARGE</span>';
                        }
                        var bill_gen = '';
                        if (emp.billing_no == '' || emp.billing_no == null) {
                            bill_gen = '<span class="label label-danger">NO</span>';
                        }
                        else {
                            bill_gen = '<span class="label label-success">YES</span>';
                        }

                        //tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td class="tbl-name">' + emp.admission_date + '</td><td>' + emp.patient_type + '</td><td>' + emp.branch_name + '</td><td>' + emp.ip_patient_id + '</td><td>' + emp.PatientName + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.ip_number + '</td><td>' + emp.ip_visit_report_id + '</td><td>' + emp.IP_ward_Bed_floor + '</td><td>' + emp.ip_doctor_name + '</td><td>' + emp.speciality + '</td><td>' + emp.discharge_date + '</td><td>' + emp.day_qty + '</td><td>' + bill_gen + '</td><td>' + active_inactive + '</td><td>' + '<i class="fa fa-file" data-toggle="modal" data-target="#discharge_patient_action" aria-hidden="true" onclick=dladdtomrd(this,"' + emp.ip_patient_id + '","' + emp.branch_id + '","' + emp.visit_reportid + '")></i>' + '</td></tr>';
                        tr = tr + '<tr>' + '<td>' + j + '</td>' + '<td class="tbl-name">' + emp.admission_date + '</td>' + '<td>' + emp.patient_type + '</td>' + '<td>' + emp.branch_name + '</td>' + '<td>' + emp.ip_patient_id + '</td>' + '<td>' + emp.PatientName + '</td>' + '<td>' + emp.age + '</td>' + '<td>' + emp.gender + '</td>' + '<td>' + emp.ip_number + '</td>' + '<td>' + emp.ip_visit_report_id + '</td>' + '<td>' + emp.IP_ward_Bed_floor + '</td>' + '<td>' + emp.ip_doctor_name + '</td>' + '<td>' + emp.speciality + '</td>' + '<td>' + emp.discharge_date + '</td>' + '<td>' + emp.day_qty + '</td>' + '<td>' + bill_gen + '</td>' + '<td>' + active_inactive + '</td>' + '<td>' + '<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">' + '<li>' + '<a onclick="dladdtomrd(this, \'' + emp.ip_patient_id + '\', \'' + emp.branch_id + '\', \'' + emp.visit_reportid + '\')"data-toggle="modal" data-target="#discharge_patient_action" aria-hidden="true">Recieve File</a>' + '</li>' + '</ul>' + '</div>' + '</td>' + '</tr>';

                    });
                    $('#discharge_list_table_body').html(tr);
                    $('#shade_slot').hide();
                  //  $('#discharge_list_table_body').Datatable();
                   datatable('discharge_list_table', 'Discharged_List', 'Legal');

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

function dladdtomrd(event, ip_patient_id, branch_id, visit_reportid) {
    debugger;
    $('#Hpatient_id').val(ip_patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hmrd_visit_reportid').val(visit_reportid);
    // $('#Hmrdid').val(mrdid);
    PatientGeneralDetails(ip_patient_id, branch_id);
}

$('#discharge_list_add_to_mrd').click(function () {
    debugger;
    var user = {
        patient_id: $('#Hpatient_id').val(),
        branch_id: $('#Hbranch_id').val(),
        visit_reportid: $('#Hmrd_visit_reportid').val(),
       // mrdid: $('#Hmrdid').val(),
        Icd_code: $('#discharge_list_icd_code').val(),
        received_by: $('#hiddenRole_id').val(),
        employee_name: $('#discharge_list_employee_name').val(),
        flag: '1',
        Dml_Indicator: 'ISI'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }

    });
    window.location.reload();
});

function cleardata2() {
    $('#discharge_list_icd_code').val("");
    $('#discharge_list_received_by').val("");
    $('#discharge_list_employee_name').val("");
}
$('#discharge_list_cancel').click(function () {
    $('#discharge_list_icd_code').val("");
    $('#discharge_list_received_by').val("");
    $('#discharge_list_employee_name').val("");
})

$('#pending_requests_tab').click(function () {
    debugger;
    loadpendingrequests();
})

$('#prfilter').on('click', function () {
    debugger;

    var pr_pat_name = $('#patient_request_patient_name').val();
    var pr_pat_id = $('#patient_request_patient_id').val();
    var pr_dpt_sub = $('#patient_request_department_and_subdepartment').val();
    var pr_branch = $('#pr_Branch').val();
    var rec_date_of_discharge = $('#patient_request_requested_date').val();
    if (rec_date_of_discharge == null || rec_date_of_discharge == '') {
        var pr_discharge_date = ''
    }
    else {
        var dis_date_split = '', pr_discharge_date = ''
        dis_date_split = rec_date_of_discharge.split('/');
        pr_discharge_date = dis_date_split[2] + '-' + dis_date_split[1] + '-' + dis_date_split[0];
    }
    var from_date = $('#pr_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#pr_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loadpendingrequests(pr_pat_name, pr_pat_id, pr_dpt_sub, pr_discharge_date, f_date, t_date, pr_branch)
})

$('#prclear').click(function () {
    fileclear3();
})

function fileclear3() {
    $('#patient_request_patient_name').val("");
    $('#patient_request_patient_id').val("");
    $('#patient_request_department_and_subdepartment').val("");
    $('#patient_request_requested_date').val("");
    $('#pr_from_date').val("");
    $('#pr_to_date').val("");
    loadpendingrequests();
}
function loadpendingrequests(pr_pat_name, pr_pat_id, pr_dpt_sub, pr_discharge_date, f_date, t_date, pr_branch) {
    debugger;
    var user1 = {
        Dml_Indicator: "PR",
        patient_name: pr_pat_name,
        patient_id: pr_pat_id,
        hospital_id: $('#HospitalId').val(),
        department: pr_dpt_sub,
        received_on: pr_discharge_date,
        from_date: f_date,
        to_date: t_date,
        branch_id: pr_branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user1),
        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 {
                    $('#pending_request_table_body').empty();
                    $('#pending_request_table').dataTable().fnClearTable();
                    $('#pending_request_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-warning">Pending Request</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                        }
                        //tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.name + '</td><td>' + emp.sub_code + '</td><td>' + emp.requestedby + '</td><td>' + emp.created_on + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + '</td><td>' + '<i class="fa fa-bug" data-toggle="modal" data-target="#pending_request_action" aria-hidden="true" onclick=\'issuemrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.first_name + '","' + emp.master_category_department_id + '","' + emp.sub_category_department_id + '","' + emp.hosptial_id + '","' + emp.mrdrequestid + '","' + emp.mrdid + '")\'></i><i class="fa fa-eye ml-10" data-toggle="modal" data-target="#File_records_view_action" onclick=filerecordhistory(this,"' + emp.patient_id + '")></i></td></tr>';
                        tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.name + '</td><td>' + emp.sub_code + '</td><td>' + emp.requestedby + '</td><td>' + emp.created_on + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + '</td>' + '<td>' + '<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">' + '<li>' + '<a data-toggle="modal" data-target="#pending_request_action" aria-hidden="true" onclick=\'issuemrd(this, "' + emp.patient_id + '", "' + emp.branch_id + '", "' + emp.first_name + '", "' + emp.master_category_department_id + '", "' + emp.sub_category_department_id + '", "' + emp.hosptial_id + '", "' + emp.mrdrequestid + '", "' + emp.mrdid + '")\'>Issue File</a>' + '</li>' + '<li>' + '<a data-toggle="modal" data-target="#File_records_view_action" onclick=\'filerecordhistory(this, "' + emp.patient_id + '")\'>File Transfer History</a>' + '</li>' + '</ul>' + '</div>' + '</td>' + '</tr>';

                    });
                    $('#pending_request_table_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('pending_request_table', 'Pending Request', 'Legal');

                }
            }
            else {
                console.log(data);
            }
        }
    });
}
function filerecordhistory(event, patient_id) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    loadfilehistory();
}

function loadfilehistory() {
    debugger;
    var history = {
        patient_id: $('#Hpatient_id').val(),
        Dml_Indicator: "PRT"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(history),
        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 {
                    //console.log(data);
                    $('#file_records_body').empty();
                    $('#file_records').dataTable().fnClearTable();
                    $('#file_records').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">Active</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                        }
                        tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.dept + '</td><td>' + emp.sub_dept + '</td><td>' + emp.created_on + '</td><td>' + emp.issue_on + '</td><td>' + emp.No_of_day + '</td><td>' + emp.Issued_Dept_and_subdept + '</td><td>' + emp.Issuedby + '</td><td>' + emp.receivedby + '</td></tr>';
                    });
                    $('#file_records_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('file_records', 'file_records_list', 'Legal');

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

function issuemrd(event, patient_id, branch_id, first_name, master_category_department_id, sub_category_department_id, hosptial_id, mrdrequestid, mrdid) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hpatient_name').val(first_name);
    $('#Hdepartment').val(master_category_department_id);
    $('#Hsubdepartment').val(sub_category_department_id);
    $('#Hhospital_id').val(hosptial_id);
    $('#Hmrdrequestid').val(mrdrequestid);
    $('#Hmrdid').val(mrdid);
}

$('#pending_request_issue').click(function () {
    debugger;
    var insert = {
        patient_id: $('#Hpatient_id').val(),
        branchid: $('#Hbranch_id').val(),
        patient_name: $('#Hpatient_name').val(),
        department: $('#Hdepartment').val(),
        subdepartment: $('#Hsubdepartment').val(),
        hospital_id: $('#Hhospitalid').val(),
        issue_by: $('#hiddenRole_id').val(),
        employee_name: $('#pending_request_employee_name').val(),
        mrdrequestid: $('#Hmrdrequestid').val(),
        mrdid:$('#Hmrdid').val(),
        Dml_Indicator: 'LIB',
        flag: 1
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(insert),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
    window.location.reload();
});

function cleardata() {
    $('#pending_request_issued_by').val("");
    $('#pending_request_employee_name').val("");
}

$('#pending_request_cancel').click(function () {
    $('#pending_request_issued_by').val("");
    $('#pending_request_employee_name').val("");
})

$('#records_issued_tab').click(function () {
    debugger;
    loadrecordsissued();
})

$('#rifilter').on('click', function () {
    debugger;

    var ri_pat_name = $('#records_issued_patient_name').val();
    var ri_pat_id = $('#records_issued_patientid').val();
    var ri_dpt_sub = $('#records_issued_received_department_and_subdepartment').val();
    var ri_branch = $('#ri_Branch').val();
    var rec_date_of_discharge = $('#records_issued_issued_date').val();
    if (rec_date_of_discharge == null || rec_date_of_discharge == '') {
        var ri_discharge_date = ''
    }
    else {
        var dis_date_split = '', ri_discharge_date = ''
        dis_date_split = rec_date_of_discharge.split('/');
        ri_discharge_date = dis_date_split[2] + '-' + dis_date_split[1] + '-' + dis_date_split[0];
    }
    var from_date = $('#ri_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#ri_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loadrecordsissued(ri_pat_name, ri_pat_id, ri_dpt_sub, ri_discharge_date, f_date, t_date, ri_branch)
})

$('#riclear').click(function () {
    fileclear4();
    loadrecordsissued();

})

function fileclear4() {
    $('#records_issued_patient_name').val("");
    $('#records_issued_patientid').val("");
    $('#records_issued_received_department_and_subdepartment').val("");
    $('#records_issued_issued_date').val("");
    $('#ri_from_date').val("");
    $('#ri_to_date').val("");
}

function loadrecordsissued(ri_pat_name, ri_pat_id, ri_dpt_sub, ri_discharge_date, f_date, t_date, ri_branch) {
    debugger;
    var user2 = {
        Dml_Indicator: "LRI",
        patient_name: ri_pat_name,
        patient_id: ri_pat_id,
        hospital_id: $('#HospitalId').val(),
        department: ri_dpt_sub,
        received_on: ri_discharge_date,
        from_date: f_date,
        to_date: t_date,
        branch_id: ri_branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user2),
        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 {
                    $('#records_issued_table_body').empty();
                    $('#records_issues_table').dataTable().fnClearTable();
                    $('#records_issues_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        var day_diff = '';
                        if (emp.day_difference <= '2') {
                            day_diff = '<span class="label label-success">2 Days or Less</span>';
                        }
                        else if (emp.day_difference <= '5') {
                            day_diff = '<span class="label label-warning">3-5 Days</span>';
                        }
                        else {
                            day_diff = '<span class="label label-danger"> More than 5 Days</span>';
                        }
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">Issued</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                        }
                        //tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.patient_name + '</td><td>' + emp.name + '</td><td>' + emp.sub_cd_name + '</td><td>' + emp.requested_by + '</td><td>' + emp.requested_on + '</td><td>' + emp.issue_by + '</td><td>' + emp.issue_on + '</td><td>' + emp.filetakenby_employee_name + '</td><td>' + day_diff + '</td><td>' + active_inactive + '</td><td>' + '<i class="fa fa-file" data-toggle="modal" data-target="#records_issued_action"aria-hidden="true" onclick=riaddtomrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.department + '","' + emp.subdepartment + '","' + emp.requestid + '")></i>' + '</td></tr>';
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.patient_name + '</td><td>' + emp.name + '</td><td>' + emp.sub_cd_name + '</td><td>' + emp.requested_by + '</td><td>' + emp.requested_on + '</td><td>' + emp.issue_by + '</td><td>' + emp.issue_on + '</td><td>' + emp.filetakenby_employee_name + '</td><td>' + day_diff + '</td><td>' + active_inactive + '</td>' + '<td>' + '<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">' + '<li>' + '<a data-toggle="modal" data-target="#records_issued_action"aria-hidden="true" onclick=riaddtomrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.department + '","' + emp.subdepartment + '","' + emp.requestid + '")>Recieve File</a>' + '</li>' + '</div>' + '</ul>' + '</div>' + '</td >' + '</tr>';


                    });
                    $('#records_issued_table_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('records_issues_table', 'Records_Issued', 'Legal');

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

function riaddtomrd(event, patient_id, branch_id, department, subdepartment, requestid) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hdepartment').val(department);
    $('#Hsubdepartment').val(subdepartment);
    $('#Hrequestid').val(requestid);
    PatientGeneralDetails(patient_id, branch_id);
}

$('#records_issued_add_to_mrd').click(function () {
    debugger;
    var ins = {
        patient_id: $('#Hpatient_id').val(),
        branchid: $('#Hbranch_id').val(),
        department: $('#Hdepartment').val(),
        subdepartment: $('#Hsubdepartment').val(),
        received_by: $('#hiddenRole_id').val(),
        returned_by: $('#hiddenRole_id').val(),
        employee_name: $('#records_issued_employee_name').val(),
        requestid: $('#Hrequestid').val(),
        Dml_Indicator: 'IS'

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(ins),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
    window.location.reload();
});

function cleardata1() {
    $('#records_issued_received_by').val("");
    $('#records_issued_returned_by').val("");
    $('#records_issued_employee_name').val("");
}

$('#records_issued_cancel').click(function () {
    $('#records_issued_received_by').val("");
    $('#records_issued_returned_by').val("");
    $('#records_issued_employee_name').val("");
})

//$('#all_records_tab').click(function () {
//    debugger;
//    loadallrecords();
//})
//function loadallrecords() {
//    debugger;
//    var list = {
//        Dml_Indicator: "AR"
//    }
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: '/MedicalRecord/insert_update_deleteMrduserpage',
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(list),
//        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 {
//                    $('#all_records_table_body').empty();
//                    $('#all_records_table').dataTable().fnClearTable();
//                    $('#all_records_table').dataTable().fnDestroy();
//                    var j = 0; tr = '';
//                    var b = data1.Table;
//                    $.each(b, function (i, emp) {
//                        j++;
//                        var active_inactive = '';
//                        if (emp.status == '1') {
//                            active_inactive = '<span class="label label-success">Active</span>';
//                        }
//                        else if (emp.status == '2') {
//                            active_inactive = '<span class="label label-warning">Inactive</span>';
//                        }
//                        tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.visit_type + '</td><td>' + emp.wardid + '</td><td>' + emp.bed_id + '</td><td>' + emp.floorid + '</td><td>' + emp.ip_patient_admitdate + '</td><td>' + emp.ip_doctor_id + '</td><td>' + emp.admission_reason + '</td><td>' + emp.ip_surgery_type + '</td><td>' + active_inactive + '</td></tr>';
//                    });
//                    $('#all_records_table_body').html(tr);
//                    $('#shade_slot').hide();
//                    datatable('all_records_table', 'All_Records', 'Legal');

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

//add to mrd
$('#add_btn_new_mrd').on("click", function () {
    debugger;
    var records = {
        patient_id: $('#Hpatientid').val(),
        branch_id: $('#add_new_mrd_branch').val(),
        ICD_Code: $('#txt_enter_icd_code').val(),
        receivedby: $('#hiddenRole_id').val(),
        Dml_Indicator: "IS"
    };
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/insert_update_deleteMrduserpage',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(records),
        success: function (data) {
            debugger;
            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('request Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Request Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
});

function clearRequest() {
    $('#txt_patientid').val("");
    $('#add_new_mrd_branch').val("");
    $('#txt_enter_icd_code').val("");
    $('#txtreceived_by').val("");
    $('.pat-strip').hide();
}

$('#add_btn_cancel_mrd').click(function () {
    $('#txt_patientid').val("");
    $('#add_new_mrd_branch').val("");
    $('#txt_enter_icd_code').val("");
    $('#txtreceived_by').val("");
    $('.pat-strip').hide();
})

//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 = "";
                    $('#dis_Branch').empty();
                    $('#adm_Branch').empty();
                    $('#nyr_Branch').empty();
                    $('#pr_Branch').empty();
                    $('#ri_Branch').empty();
                    $('#all_Branch').empty();
                    $('#admitted_list_branch').empty();
                    $('#all_records_branch').empty();
                    $('#add_new_mrd_branch').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) {
                        $('#add_new_mrd_branch').append(SubDepartment);
                        branch_change();
                        //$('#pkg_branch_id').val(emp.hospital_branch_id);
                        $('#all_records_branch').append(SubDepartment);
                        $('#admitted_list_branch').append(SubDepartment);
                        $('#dis_Branch').append(SubDepartment);
                        $('#adm_Branch').append(SubDepartment);
                        $('#nyr_Branch').append(SubDepartment);
                        $('#pr_Branch').append(SubDepartment);
                        $('#ri_Branch').append(SubDepartment);
                        $('#all_Branch').append(SubDepartment);

                    }
                    else {
                        $('#add_new_mrd_branch').append(default_opt + SubDepartment);
                        // $('#add_patient_id').attr('disabled', true)
                        $('#all_records_branch').append(default_opt + SubDepartment);
                        $('#admitted_list_branch').append(default_opt + SubDepartment);
                        $('#dis_Branch').append(default_opt + SubDepartment);
                        $('#adm_Branch').append(default_opt + SubDepartment);
                        $('#nyr_Branch').append(default_opt + SubDepartment);
                        $('#pr_Branch').append(default_opt + SubDepartment);
                        $('#ri_Branch').append(default_opt + SubDepartment);
                        $('#all_Branch').append(default_opt + SubDepartment);
                    }
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#add_new_mrd_branch').val($('#HospitalBranchID').val());
                     //   $('#add_new_mrd_branch').attr('disabled', true);
                        // $('#add_patient_id').attr('disabled', false)
                        $('#all_records_branch').val($('#HospitalBranchID').val());
                     //   $('#all_records_branch').attr('disabled', true);
                        $('#admitted_list_branch').val($('#HospitalBranchID').val());
                      //  $('#admitted_list_branch').attr('disabled', true);
                        $('#dis_Branch').val($('#HospitalBranchID').val());
                      //  $('#dis_Branch').attr('disabled', true);
                        $('#adm_Branch').val($('#HospitalBranchID').val());
                     //   $('#adm_Branch').attr('disabled', true);
                        $('#nyr_Branch').val($('#HospitalBranchID').val());
                     //   $('#nyr_Branch').attr('disabled', true);
                        $('#pr_Branch').val($('#HospitalBranchID').val());
                     //   $('#pr_Branch').attr('disabled', true);
                        $('#ri_Branch').val($('#HospitalBranchID').val());
                      //  $('#ri_Branch').attr('disabled', true);
                        $('#all_Branch').val($('#HospitalBranchID').val());
                       // $('#all_Branch').attr('disabled', true);



                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$('#add_new_mrd_branch').change(function () {
    debugger;
    branch_change($('#add_new_mrd_branch').val());
    // branch_change($('#admitted_list_branch').val());
    //branch_change($('#all_records_branch').val());
})
$('#all_records_branch').change(function () {
    debugger;
    var branch_val = $('#all_records_branch').val();
    branch_change(branch_val);
})
//$('#admitted_list_branch').change(function () {
//    debugger;
//    branch_change($('#admitted_list_branch').val());
//})
$('#admitted_list_branch').change(function () {
    debugger;
    var branch_val1 = $('#admitted_list_branch').val();
    branch_change(branch_val1);
})
function branch_change(branch) {
    debugger;
    LoadDepartment(branch);
}
//load department
function LoadDepartment(branch) {
    debugger;
    var department = {
        Dml_Indicator: "DP",
        branch_id: branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            debugger
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Department;
                    $('#all_records_department').empty();

                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#all_records_department').append(Department);
                        $("#all_records_department").change();
                        $('#admitted_list_department').append(Department);
                        $("#admitted_list_department").change();

                    }
                    else {
                        $('#all_records_department').append(default_opt + Department);
                        $('#admitted_list_department').append(default_opt + Department);
                        //$('#transfer_department').append(default_opt + Department);
                    }

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
$("#all_records_department").change(function () {
    debugger;
    LoadSubDepartment($('#all_records_department').val(), 0);
    $('#all_records_sub_department').prop('disabled', false);
})
$("#admitted_list_department").change(function () {
    debugger;
    LoadSubDepartment($('#admitted_list_department').val(), 0);
    $('#admitted_list_sub_department').prop('disabled', false);
})
//$("#transfer_department").change(function () {
//    debugger;
//    LoadSubDepartment($('#transfer_department').val(), 0);
//    $('#transfer_sub_department').prop('disabled', false);
//})
//load sub department
function LoadSubDepartment(id, val) {
    debugger;
    var val = val;
    document.getElementById("all_records_sub_department").innerHTML = null;
    document.getElementById("admitted_list_sub_department").innerHTML = null;
    // document.getElementById("transfer_sub_department").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: id
    }
    $.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) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment = '';
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#all_records_sub_department').append(SubDepartment);
                        $("#all_records_sub_department").change();
                        $('#admitted_list_sub_department').append(SubDepartment);
                        $("#admitted_list_sub_department").change();
                        //$('#transfer_sub_department').append(SubDepartment);
                        //$("#transfer_sub_department").change();
                    }
                    else {
                        $('#all_records_sub_department').append(default_opt + SubDepartment);
                        $('#admitted_list_sub_department').append(default_opt + SubDepartment);
                        //$('#transfer_sub_department').append(default_opt + SubDepartment);
                    }
                    if (val != '' && val != 0) {
                        $('#all_records_sub_department').val(val);
                        $('#admitted_list_sub_department').val(val);
                        //$('#transfer_sub_department').val(val);
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$('#admitted_list_tab').click(function () {
    debugger;
    loadadmitlist();
    LoadBranch();
})

$('#admit_filter').on('click', function () {
    debugger;

    var admit_pat_name = $('#admitted_patient_patient_name').val();
    var admit_pat_id = $('#admitted_patient_patientid').val();
    var admit_pat_phn = $('#admitted_patient_patient_mobile_number').val();
    var admit_branch = $('#adm_Branch').val();
    var admit_date_of_Admit = $('#admitted_patient_date_of_admission').val();
    if (admit_date_of_Admit == null || admit_date_of_Admit == '') {
        var r_admit_date = ''
    }
    else {
        var admit_date_split = '', r_admit_date = ''
        admit_date_split = admit_date_of_Admit.split('/');
        r_admit_date = admit_date_split[2] + '-' + admit_date_split[1] + '-' + admit_date_split[0];
    }

    var admit_ip_no = $('#admitted_patient_ipnumber').val();
    var from_date = $('#al_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#al_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loadadmitlist(admit_pat_name, admit_pat_id, admit_pat_phn, r_admit_date, admit_ip_no, f_date, t_date, admit_branch)
})

function admclear() {
    $('#admitted_patient_patient_name').val("");
    $('#admitted_patient_patientid').val("");
    $('#admitted_patient_patient_mobile_number').val("");
    $('#admitted_patient_date_of_admission').val("");
    $('#admitted_patient_ipnumber').val("");
    $('#al_from_date').val("");
    $('#al_to_date').val("");
}

$('#admit_clear').click(function(){
    admclear();
    loadadmitlist();
})
function loadadmitlist(admit_pat_name, admit_pat_id, admit_pat_phn, r_admit_date, admit_ip_no, f_date, t_date, admit_branch) {
    debugger;
    $('#shade_slot').show();
    var admit = {
        Dml_Indicator: "AL",
        patient_name: admit_pat_name,
        patient_id: admit_pat_id,
        hospital_id: $('#HospitalId').val(),
        phone_number: admit_pat_phn,
        admission_date: r_admit_date,
        visit_report_id: admit_ip_no,
        from_date: f_date,
        to_date: t_date,
        branch_id: admit_branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/insert_update_deleteMrduserpage',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(admit),
        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 {
                    $('#admitted_list_table_body').empty();
                    $('#admitted_list_table').dataTable().fnClearTable();
                    $('#admitted_list_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-warning"></span>';
                            tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.ip_patient_admitdate + '</td><td>' + emp.patient_type + '</td><td>' + emp.branch_name + '</td><td>' + emp.ip_patient_id + '</td><td>' + emp.PatientName + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.patient_number + '</td><td>' + emp.Address + '</td><td>' + emp.pincode + '</td><td>' + emp.ip_number + '</td><td>' + emp.ip_visit_report_id + '</td><td>' + emp.ward_name + '</td><td>' + emp.Bed_Name + '</td><td>' + emp.ward_bed_name + '</td><td>' + emp.ip_doctor_name + '</td><td>' + emp.speciality + '</td><td>' + emp.admission_reason + '</td><td>' + emp.ip_operation_name + '</td><td>' + active_inactive + '</td><td><i class="fa fa-bug" data-toggle="modal" data-target="#admitted_patient_action" onclick=admittedlistmrd(this,"' + emp.ip_patient_id + '","' + emp.branch_id + '","' + emp.department + '","' + emp.subdepartment + '","' + emp.mrdid + '")></i></td></tr>';
                        }
                        else if (emp.status == null) {
                            active_inactive = '<span class="label label-success">New Admit</span>';
                            tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.ip_patient_admitdate + '</td><td>' + emp.patient_type + '</td><td>' + emp.branch_name + '</td><td>' + emp.ip_patient_id + '</td><td>' + emp.PatientName + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.patient_number + '</td><td>' + emp.Address + '</td><td>' + emp.pincode + '</td><td>' + emp.ip_number + '</td><td>' + emp.ip_visit_report_id + '</td><td>' + emp.ward_name + '</td><td>' + emp.Bed_Name + '</td><td>' + emp.ward_bed_name + '</td><td>' + emp.ip_doctor_name + '</td><td>' + emp.speciality + '</td><td>' + emp.admission_reason + '</td><td>' + emp.ip_operation_name + '</td><td>' + active_inactive + '</td><td></td></tr>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">issued</span>';
                            tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.ip_patient_admitdate + '</td><td>' + emp.patient_type + '</td><td>' + emp.branch_name + '</td><td>' + emp.ip_patient_id + '</td><td>' + emp.PatientName + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.patient_number + '</td><td>' + emp.Address + '</td><td>' + emp.pincode + '</td><td>' + emp.ip_number + '</td><td>' + emp.ip_visit_report_id + '</td><td>' + emp.ward_name + '</td><td>' + emp.Bed_Name + '</td><td>' + emp.ward_bed_name + '</td><td>' + emp.ip_doctor_name + '</td><td>' + emp.speciality + '</td><td>' + emp.admission_reason + '</td><td>' + emp.ip_operation_name + '</td><td>' + active_inactive + '</td><td></td></tr>';

                        }

                    });
                    $('#admitted_list_table_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('admitted_list_table', 'Admitted_List', 'Legal');

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

        }
    });
}

function admittedlistmrd(event, patient_id, branch_id, department, subdepartment, mrdid) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hdepartment').val(department);
    $('#Hsubdepartment').val(subdepartment);
    $('#Hmrdid').val(mrdid);
    PatientGeneralDetails(patient_id, branch_id);
}

$('#admitted_list_issue').click(function () {
    debugger;
    var all = {
        patient_id: $('#Hpatient_id').val(),
        branchid: $('#admitted_list_branch').val(),
        department: $('#admitted_list_department').val(),
        subdepartment: $('#admitted_list_sub_department').val(),
        issued_by: $('#hiddenRole_id').val(),
        employee_name: $('#admitted_list_employee_name').val(),
        flag: "1",
        mrdid: $('#Hmrdid').val(),
        Dml_Indicator: 'LIB'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(all),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
    window.location.reload();
});

function clearfile1() {
    $('#admitted_list_branch').val("");
    $('#admitted_list_department').val("");
    $('#admitted_list_sub_department').val("");
    $('#admitted_list_issued_by').val("");
    // $('#all_records_received_by').val("");
    $('#admitted_list_employee_name').val("");
}

$('#admitted_list_cancel').click(function () {
    $('#admitted_list_branch').val("");
    $('#admitted_list_department').val("");
    $('#admitted_list_sub_department').val("");
    $('#admitted_list_issued_by').val("");
    // $('#all_records_received_by').val("");
    $('#admitted_list_employee_name').val("");
})

$('#all_records_tab').click(function () {
    debugger;
    loadallrecords();
})
$('#arfilter').on('click', function () {
    debugger;

    var all_pat_name = $('#all_records_patient_name').val();
    var all_pat_id = $('#all_records_patientid').val();
    var all_pat_phn = $('#all_records_patient_mobile_number').val();
    var all_branch = $('#all_Branch').val();
    var rec_date_of_discharge = $('#all_records_received_date_and_time').val();
    if (rec_date_of_discharge == null || rec_date_of_discharge == '') {
        var all_discharge_date = ''
    }
    else {
        var dis_date_split = '', all_discharge_date = ''
        dis_date_split = rec_date_of_discharge.split('/');
        all_discharge_date = dis_date_split[2] + '-' + dis_date_split[1] + '-' + dis_date_split[0];
    }
    var from_date = $('#ar_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#ar_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loadallrecords(all_pat_name, all_pat_id, all_pat_phn, all_discharge_date, f_date, t_date, all_branch)
})

$('#arclear').click(function () {
    fileclear1();
    loadallrecords();
})

function fileclear1() {
    $('#all_records_patient_name').val("");
    $('#all_records_patientid').val("");
    $('#all_records_patient_mobile_number').val("");
    $('#all_records_received_date_and_time').val("");
    $('#ar_from_date').val("");
    $('#ar_to_date').val("");
}
function loadallrecords(all_pat_name, all_pat_id, all_pat_phn, all_discharge_date, f_date, t_date, all_branch) {
    debugger;
    var list = {
        Dml_Indicator: "AR",
        patient_name: all_pat_name,
        patient_id: all_pat_id,
        hospital_id: $('#HospitalId').val(),
        phone_number: all_pat_phn,
        received_on: all_discharge_date,
        from_date: f_date,
        to_date: t_date,
        branch_id: all_branch

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(list),
        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 {
                    $('#all_records_table_body').empty();
                    $('#all_records_table').dataTable().fnClearTable();
                    $('#all_records_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">FILE IN MRD</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">ISSUED</span>';
                        }
                        //tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.phone_number_1 + '</td><td>' + emp.Received_date + '</td><td>' + emp.Received_time + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + '<td class="disp_if"><i class="fa fa-angle-double-down mr-10" onclick=\'icdcodedetails(this,"' + emp.patient_id + '","' + emp.ICD_Code + '","' + emp.ICD_Description + '","' + emp.visit_reportid + '")\'></i><i class="fa fa-bug mr-10" data-toggle="modal" data-target="#all_records_issue_action" onclick=allrecordissue(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.mrdid + '","' + emp.first_name + '")></i><i class="fa fa-eye mr-10" data-toggle="modal" data-target="#all_records_view_action" onclick=allrecordhistory(this,"' + emp.patient_id + '")></i><i class="fa fa-file" data-toggle="modal" data-target="#fupload"></i></td></tr>';
                        tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.age + '</td><td>' + emp.gender + '</td><td>' + emp.phone_number_1 + '</td><td>' + emp.Received_date + '</td><td>' + emp.Received_time + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + '</td><td>' + '<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">' + '<li>' + ' <a onclick=\'icdcodedetails(this, "' + emp.patient_id + '", "' + emp.icd_code + '", "' + emp.icd_description + '", "' + emp.visit_reportid + '")\'>Expand</a>' + '</li>' + '<li>' + '<a data-toggle="modal" data-target="#all_records_issue_action" onclick=\'allrecordissue(this, "' + emp.patient_id + '", "' + emp.branch_id + '", "' + emp.mrdid + '", "' + emp.first_name + '")\'>Issue File</a>' + '</li>' + '<li>' + '<a data-toggle="modal" data-target="#all_records_view_action" onclick=\'allrecordhistory(this, "' + emp.patient_id + '")\'>File Transfer History</a>' + '</li>' + '<li>' + '<a data-toggle="modal" data-target="#fupload">File Upload</a>' + '</li>' + '</ul>' + '</div>' + '</td></tr>';
                       

                    });
                    $('#all_records_table_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('all_records_table', 'All_Records', 'Legal');

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

function allrecordissue(event, patient_id, branch_id, mrdid, first_name) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hmrdid').val(mrdid);
    $('#Hpatient_name').val(first_name);
   
    PatientGeneralDetails(patient_id, branch_id);
}

$('#all_records_issue').click(function () {
    debugger;
    var all = {
        patient_id: $('#Hpatient_id').val(),
        patient_name: $('#Hpatient_name').val(),
        branchid: $('#all_records_branch').val(),
        department: $('#all_records_department').val(),
        subdepartment: $('#all_records_sub_department').val(),
        received_by: $('#hiddenRole_id').val(),
        issued_by: $('#hiddenRole_id').val(),
        employee_name: $('#all_records_employee_name').val(),
        mrdid:$('#Hmrdid').val(),
       // requestid: $('#Hrequestid').val(),
        Dml_Indicator: 'LIB'

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(all),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
    window.location.reload();
});

function clearfile() {
    $('#all_records_branch').val("");
    $('#all_records_department').val("");
    $('#all_records_sub_department').val("");
    $('#all_records_issued_by').val("");
    $('#all_records_received_by').val("");
    $('#all_records_employee_name').val("");
}

$('#all_records_cancel').click(function () {
    $('#all_records_branch').val("");
    $('#all_records_department').val("");
    $('#all_records_sub_department').val("");
    $('#all_records_issued_by').val("");
    $('#all_records_received_by').val("");
    $('#all_records_employee_name').val("");
})

function allrecordhistory(event, patient_id) {
    debugger;
    $('#Hpatient_id').val(patient_id);
    loadhistory();
}

function loadhistory() {
    debugger;
    var history = {
        patient_id: $('#Hpatient_id').val(),
        Dml_Indicator: "ARH"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(history),
        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 {
                    console.log(data);
                    $('#all_records_file_transfer_body').empty();
                    $('#all_records_file_transfer').dataTable().fnClearTable();
                    $('#all_records_file_transfer').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">Active</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                        }
                        tr = tr + '<tr' + j + '"><td>' + j + '</td><td>' + emp.department + '</td><td>' + emp.subdepartment + '</td><td>' + emp.from_datetime + '</td><td>' + emp.to_datetime + '</td><td>' + emp.no_of_days + '</td><td>' + emp.department_subdepartment + '</td><td>' + emp.issue_by + '</td><td>' + emp.received_by + '</td></tr>';
                    });
                    $('#all_records_file_transfer_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('all_records_file_transfer', 'All_file_transfer', 'Legal');

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

$('#not_yet_received_tab').click(function () {
    debugger;
    loadnotreceived();
})

$('#discharged_list_tab').click(function () {
    debugger;
    loaddischargelist();
});


$('#not_yet_receive_filter').on('click', function () {
    debugger;

    var receive_pat_name = $('#not_yet_received_patient_name').val();
    var receive_pat_id = $('#not_yet_received_patientid').val();
    var receive_pat_phn = $('#not_yet_received_patient_mobile_number').val();
    var receive_branch = $('#nyr_Branch').val();
    var rec_date_of_discharge = $('#not_yet_received_date_of_discharge').val();
    if (rec_date_of_discharge == null || rec_date_of_discharge == '') {
        var r_discharge_date = ''
    }
    else {
        var dis_date_split = '', r_discharge_date = ''
        dis_date_split = rec_date_of_discharge.split('/');
        r_discharge_date = dis_date_split[2] + '-' + dis_date_split[1] + '-' + dis_date_split[0];
    }

    var rec_date_of_Admit = $('#not_yet_received_date_of_admission').val();
    if (rec_date_of_Admit == null || rec_date_of_Admit == '') {
        var r_admit_date = ''
    }
    else {
        var admit_date_split = '', r_admit_date = ''
        admit_date_split = rec_date_of_Admit.split('/');
        r_admit_date = admit_date_split[2] + '-' + admit_date_split[1] + '-' + admit_date_split[0];
    }

    var receive_ip_no = $('#not_yet_received_ipnumber').val();
    var from_date = $('#nyr_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#nyr_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loadnotreceived(receive_pat_name, receive_pat_id, receive_pat_phn, r_discharge_date, r_admit_date, receive_ip_no, f_date, t_date, receive_branch)
})
$('#not_yet_receive_clear').click(function () {
    reqclear1();
    loadnotreceived();
});
function reqclear1() {
    $('#not_yet_received_patient_name').val("");
    $('#not_yet_received_patientid').val("");
    $('#not_yet_received_date_of_discharge').val("");
    $('#not_yet_received_patient_mobile_number').val("");
    $('#not_yet_received_date_of_admission').val("");
    $('#not_yet_received_ipnumber').val("");
    $('#nyr_from_date').val("");
    $('#nyr_to_date').val("");
}

function loadnotreceived(receive_pat_name, receive_pat_id, receive_pat_phn, r_discharge_date, r_admit_date, receive_ip_no, f_date, t_date, receive_branch) {
    debugger;
    var receive = {
        Dml_Indicator: "NYR",
        patient_name: receive_pat_name,
        patient_id: receive_pat_id,
        hospital_id: $('#HospitalId').val(),
        phone_number: receive_pat_phn,
        discharge_date: r_discharge_date,
        admission_date: r_admit_date,
        visit_report_id: receive_ip_no,
        from_date: f_date,
        to_date: t_date,
        branch_id: receive_branch
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(receive),
        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 {
                    $('#not_yet_received_table_body').empty();
                    $('#not_yet_received_table').dataTable().fnClearTable();
                    $('#not_yet_received_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++;
                        var active_inactive = '';
                        if (emp.status == '1') {
                            active_inactive = '<span class="label label-success">Active</span>';
                        }
                        else if (emp.status == '2') {
                            active_inactive = '<span class="label label-warning">Inactive</span>';
                        }
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td class="tbl-name">' + emp.admission_date + '</td><td>' + emp.branch_name + '</td><td>' + emp.ip_patient_id + '</td><td>' + emp.PatientName + '</td><td>' + emp.ip_number + '</td><td>' + emp.ip_visit_report_id + '</td><td>' + emp.IP_ward_Bed_floor + '</td><td>' + emp.ip_doctor_name + '</td><td>' + emp.discharge_date + '</td>' + '<td>' + '<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">' + '<li>' + '<a data-toggle="modal" data-target="#yet_to_receive_action" onclick=yetreceivedaddmrd(this,"' + emp.ip_patient_id + '","' + emp.branch_id + '","' + emp.visit_reportid + '")>Recieve File</a>' + '</li>' + '</ul>' + '</div>' + '</td>' + '</tr>';


                    });
                    $('#not_yet_received_table_body').html(tr);
                    $('#shade_slot').hide();
                    datatable('not_yet_received_table', 'Not_yet_received', 'Legal');

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

function yetreceivedaddmrd(event, ip_patient_id, branch_id, visit_reportid) {
    debugger;
    $('#Hpatient_id').val(ip_patient_id);
    $('#Hbranch_id').val(branch_id);
    $('#Hmrd_visit_reportid').val(visit_reportid);
    // $('#Hsubdepartment').val(subdepartment);
    PatientGeneralDetails(ip_patient_id, branch_id);
}

$('#yet_receive_addtomrd').click(function () {
    debugger;
    var all = {
        patient_id: $('#Hpatient_id').val(),
        branch_id: $('#Hbranch_id').val(),
        visit_reportid: $('#Hmrd_visit_reportid').val(),
        // subdepartment: $('#Hsubdepartment').val(),
        Icd_code: $('#not_yet_received_icd_code').val(),
        received_by: $('#hiddenRole_id').val(),
        employee_name: $('#not_yet_received_employee_name').val(),
        flag: '1',
        Dml_Indicator: 'ISI'

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(all),
        success: function (data) {
            debugger;
            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('Return Already Exists',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                    return false;
                }
                else {
                    toastr.success('Return Added Successfully',
                    {
                        //closeButton: false,
                        progressBar: true,
                        allowHtml: true,
                        timeOut: 0,
                    });
                }

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

        },
        error: function (data) {

        }
    });
    window.location.reload();
});

function clearfile2() {
    $('#not_yet_received_icd_code').val("");
    $('#not_yet_received_received_by').val("");
    $('#not_yet_received_employee_name').val("");
}

$('#yet_receive_cancel').click(function () {
    debugger;
    $('#not_yet_received_icd_code').val("");
    $('#not_yet_received_received_by').val("");
    $('#not_yet_received_employee_name').val("");
})

//discharge list filter

$('#dlfilter').on('click', function () {
    debugger;
    var dis_pat_name = $('#discharged_patient_name').val();
    var dis_pat_id = $('#discharged_patient_id').val();
    var dis_ip_number = $('#discharged_ip_number').val();
    var dis_dis_date_time = $('#discharged_date_of_discharge').val();
    var dis_branch = $('#dis_Branch').val();
    if (dis_dis_date_time == null || dis_dis_date_time == '') {
        var dis_date = ''
    }
    else {
        var dis_date_split = '', dis_date = ''
        dis_date_split = dis_dis_date_time.split('/');
        dis_date = dis_date_split[2] + '-' + dis_date_split[1] + '-' + dis_date_split[0];
    }
    var dis_adm_date_time = $('#discharged_date_of_admission').val();
    if (dis_adm_date_time == null || dis_adm_date_time == '') {
        var adm_date = ''
    }
    else {
        var adm_date_split = '', adm_date = ''
        adm_date_split = dis_adm_date_time.split('/');
        adm_date = adm_date_split[2] + '-' + adm_date_split[1] + '-' + adm_date_split[0];
    }
    var from_date = $('#dl_from_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#dl_to_date').val();
    if (to_date == null || to_date == '') {
        var t_date = ''
    }
    else {
        var date_split = '', f_date = ''
        date_split = from_date.split('/');
        f_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];

        var t_date_split = '', t_date = ''
        t_date_split = to_date.split('/');
        t_date = t_date_split[2] + '-' + t_date_split[1] + '-' + t_date_split[0];
    }
    loaddischargelist(dis_pat_name, dis_pat_id, dis_ip_number, dis_date, adm_date, f_date, t_date, dis_branch)
})
$('#dlclear').click(function () {
    dlclear();
    loaddischargelist();
});
function dlclear() {
    $('#discharged_patient_name').val(""),
     $('#discharged_patient_id').val(""),
     $('#discharged_ip_number').val(""),
      $('#discharged_date_of_discharge').val(""),
      $('#discharged_date_of_admission').val(""),
    $('#dl_from_date').val(""),
    $('#dl_to_date').val("")
  //  $('#txtto_date').val("")
}
//Show/hide icd subtable
function icdcodedetails(e, pid, icd, descrip, visitid) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#all_records_table').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(geticdcodedetails(pid, icd, descrip, visitid)).show();
        tr.addClass('shown');
    }

}
function geticdcodedetails(pid, icd, descrip, visitid) {

    debugger;
    $('#shade_slot').show();

    div = $('<div/>')
    .addClass('loading')
    .addClass('table_child')
    .addClass('table-responsive');
    var icdcode_data = {
        patient_id: pid,
        ICD_Code: icd,
        ICD_Description: descrip,
        visit_reportid: visitid,
        Dml_Indicator: 'SCT'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(icdcode_data),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data1 = data.data;
                var tr = '', j = 1;

                if (jQuery.isEmptyObject((data1))) {
                }

                else {
                    div.removeClass('loading')

                    var row = 2;
                    var tr, row_data, j = 1;
                    tr = $('<table class="table table-condensed body-borderless table_borderless">');
                    tr.append('<thead><tr class="info"><th>S.No</th><th>Visit Report ID</th><th>ICD Code</th><th>ICD Description</th></tr></thead><tbody>');
                    $.each(data1.Table, function (i, emp) {
                        row_data = '<tr id="t_' + emp.patient_id + '"><td>' + j + '</td><td>' + emp.visit_reportid + '</td><td>' + emp.ICD_Code + '</td><td>' + emp.ICD_Description + '</td></tr>';
                        j = j + 1;
                        tr.append(row_data);
                    })
                    tr.append("</tbody>");
                    div.append(tr);
                }
            }
            else {
                console.log(data);
            }
        }
    });
    return div;
}

$('#discharge_list_icd_code').autocomplete({
    source: function (request, response) {
        debugger;
        $.ajax({
            async: true,
            url: "/MedicalRecord/AutoCompleteService?SName=" + $('#discharge_list_icd_code').val() + '&P_Dml_Indicator=SICD',
            type: "POST",
            dataType: "json",
            data: { Prefix: request.term },
            success: function (data) {
                debugger;
                //var data2 = JSON.parse(data);
                var data2 = data;
                response($.map(data2, function (item) {
                    debugger;
                    return { label: item.ICD_Code, value: item.ICD_Code, name: item.ICD_Description };
                }))
            }
        })
    },
    minLength: 1,
    select: function (event, ui) {
        debugger;
        var icd_des = ui.item.name;
        $('#dis_icd_description').val(icd_des);
    },
    html: true,
    open: function (event, ui) {
        $(".ui-autocomplete").css("z-index", 1000);
    }
});


$('#dis_icd_description').autocomplete({
    source: function (request, response) {
        debugger;
        $.ajax({
            async: true,
            url: "/MedicalRecord/AutoCompleteService?SName=" + $('#dis_icd_description').val() + '&P_Dml_Indicator=DICD',
            type: "POST",
            dataType: "json",
            data: { Prefix: request.term },
            success: function (data) {
                debugger;
                //var data2 = JSON.parse(data);
                var data2 = data;
                response($.map(data2, function (item) {
                    debugger;
                    return { label: item.ICD_Code, value: item.ICD_Code, name: item.ICD_Description };
                }))
            }
        })
    },
    minLength: 1,
    select: function (event, ui) {
        debugger;
        var icd_des = ui.item.name;
        $('#discharge_list_icd_code').val(icd_des);
    },
    html: true,
    open: function (event, ui) {
        $(".ui-autocomplete").css("z-index", 1000);
    }
});



$('#not_yet_received_icd_code').autocomplete({
    source: function (request, response) {
        debugger;
        $.ajax({
            async: true,
            url: "/MedicalRecord/AutoCompleteService?SName=" + $('#not_yet_received_icd_code').val() + '&P_Dml_Indicator=SICD',
            type: "POST",
            dataType: "json",
            data: { Prefix: request.term },
            success: function (data) {
                debugger;
                //var data2 = JSON.parse(data);
                var data2 = data;
                response($.map(data2, function (item) {
                    debugger;
                    return { label: item.ICD_Code, value: item.ICD_Code, name: item.ICD_Description };
                }))
            }
        })
    },
    minLength: 1,
    select: function (event, ui) {
        debugger;
        var icd_des = ui.item.name;
        $('#nyr_icd_description').val(icd_des);
    },
    html: true,
    open: function (event, ui) {
        $(".ui-autocomplete").css("z-index", 1000);
    }
});








//function allrecordprint(event, patientid) {
//    debugger;
//    $.ajax({
//        url: "/Pdf/GetPdf", // Replace with your actual endpoint URL
//        method: "GET",
//        responseType: "arraybuffer",
//        success: function (response) {
//           // window.open(response, '_blank');
//            displayPdf(response);
//        },
//        error: function (xhr, status, error) {
//            console.error("Error loading PDF:", error);
//        }
//    });
//};


function displayPdf(pdfBytes) {
    var pdfUrl = URL.createObjectURL(new Blob([pdfBytes], { type: "application/pdf" }));

    // Create a new window or tab with the PDF content
    var newWindow = window.open(pdfUrl, '_blank');

    // If you want to embed the PDF in your current page as well, you can use the following:
    // var embed = $("<embed>").attr("src", pdfUrl).attr("type", "application/pdf").attr("width", "100%").attr("height", "600px");
    // $("body").append(embed);
}



//function allrecordprint(event, patientid) {
//    debugger;
//    // Replace 'path_to_your_folder' with the actual path to your folder
//    var folderPath = 'C:/Medway_Files/PABV202200001';

//    $.ajax({
//        url: folderPath,
//        success: function (data) {
//            debugger;
//            $(data).find("a").attr("href", function (i, val) {
//                if (val.match(/\.(pdf|doc|docx)$/)) { // You can specify file extensions to include
//                    $("#fileTable_body").append("<tr><td>" + val + "</td></tr>");
//                }
//            });
//        }
//    });
//}


$('#dis_icd_add').click(function () {
    debugger;
    inserticdtable();
})

$('#nyr_icd_add').click(function () {
    debugger;
    insert_nyr_icdtable();
})

function inserticdtable() {
    debugger;
    var user = {
        Dml_Indicator: 'ICDI',
        mrdid: $('#HICD_id').val(),
        Patient_id: $('#Hpatient_id').val(),
        hospital_id: $('#HospitalId').val(),
        Icd_code: $('#discharge_list_icd_code').val(),
        Mrd_code: $('#dis_icd_description').val(),
        visit_reportid: $('#Hmrd_visit_reportid').val(),
        branch_id: $('#Hbranch_id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            $('#HICD_id').val(0)
            $('#discharge_list_icd_code').val("")
            $('#dis_icd_description').val("")
            loadicdtable();
           // load_nyr_icdtable();
        }
    });
}

function insert_nyr_icdtable() {
    debugger;

    var user = {
        Dml_Indicator: 'ICDI',
        mrdid: $('#Hnyr_ICD_id').val(),
        Patient_id: $('#Hpatient_id').val(),
        hospital_id: $('#HospitalId').val(),
        Icd_code: $('#not_yet_received_icd_code').val(),
        Mrd_code: $('#nyr_icd_description').val(),
        visit_reportid: $('#Hmrd_visit_reportid').val(),
        branch_id: $('#Hbranch_id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            // loadicdtable();
            $('#Hnyr_ICD_id').val(0)
            $('#not_yet_received_icd_code').val("")
            $('#nyr_icd_description').val("")
            load_nyr_icdtable();
        }
    });
}



function loadicdtable() {
    debugger;
    var user = {
        Dml_Indicator: 'LICD',       
        visit_reportid: $('#Hmrd_visit_reportid').val(),     
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        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 {
                    $('#dis_icd_table_body').empty();
                    //$('#dis_icd_table').dataTable().fnClearTable();
                    //$('#dis_icd_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;

                    $.each(b, function (i, emp) {
                        j++;
                        var edit = '';

                        edit = '<i class="fa fa-pencil" data-toggle="modal" data-target="" aria-hidden="true" onclick=icdedit(this,"' + emp.icd_id + '","' + emp.patient_id + '","' + emp.visitreportid + '")></i>'

                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.icd_code + '</td><td>' + emp.icd_description + '</td><td>' + '<i class="fa fa-trash" data-toggle="modal" data-target="" aria-hidden="true" onclick=icddelete(this,"' + emp.icd_id + '")></i>' + '' + edit + '</td></tr>';
                    });
                    $('#dis_icd_table_body').html(tr);
                    $('#shade_slot').hide();
                   // datatable('dis_icd_table', 'Discharged_ICD', 'Legal');

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

function load_nyr_icdtable() {
    debugger;
    var user = {
        Dml_Indicator: 'LICD',
        visit_reportid: $('#Hmrd_visit_reportid').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        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 {
                    $('#nyr_icd_table_body').empty();
                    //$('#dis_icd_table').dataTable().fnClearTable();
                    //$('#dis_icd_table').dataTable().fnDestroy();
                    var j = 0; tr = '';
                    var b = data1.Table;

                    $.each(b, function (i, emp) {
                        j++;
                        var edit = '';

                        edit = '<i class="fa fa-pencil" data-toggle="modal" data-target="" aria-hidden="true" onclick=nyr_icdedit(this,"' + emp.icd_id + '","' + emp.patient_id + '","' + emp.visitreportid + '")></i>'

                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.branch_name + '</td><td>' + emp.icd_code + '</td><td>' + emp.icd_description + '</td><td>' + '<i class="fa fa-trash" data-toggle="modal" data-target="" aria-hidden="true" onclick=nyr_icddelete(this,"' + emp.icd_id + '")></i>' + '' + edit +  ' </td></tr>';
                    });
                    $('#nyr_icd_table_body').html(tr);
                    $('#shade_slot').hide();
                    // datatable('dis_icd_table', 'Discharged_ICD', 'Legal');

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

function icdedit(event, icd_id, patient_id, visitreportid) {
    debugger;
    var user = {
        Dml_Indicator: 'EICD',
        mrdid: icd_id,
        Patient_id: patient_id,
        visit_reportid: visitreportid
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            var emp = data1.data.Table[0]

            $("#discharge_list_icd_code").val(emp.ICD_Code);
            $("#dis_icd_description").val(emp.ICD_Description);
            $("#HICD_id").val(emp.icd_id);

        }
    });
}

function nyr_icdedit(event, icd_id, patient_id, visitreportid) {
    debugger;
    var user = {
        Dml_Indicator: 'EICD',
        mrdid: icd_id,
        Patient_id: patient_id,
        visit_reportid: visitreportid
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            var emp = data1.data.Table[0]
          
            $("#not_yet_received_icd_code").val(emp.ICD_Code);
            $("#nyr_icd_description").val(emp.ICD_Description);
            $("#Hnyr_ICD_id").val(emp.icd_id);

            //$("#discharge_list_icd_code").val(emp.ICD_Code);
            //$("#dis_icd_description").val(emp.ICD_Description);
            //$("#HICD_id").val(emp.icd_id);
         
        }
    });
}


function icddelete(e,icd_id) {
    debugger;
    var user = {
        Dml_Indicator: 'DICD',
        mrdid: icd_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            loadicdtable();
            load_nyr_icdtable();
        }
    });
}

function nyr_icddelete(e, icd_id) {
    debugger;
    var user = {
        Dml_Indicator: 'DICD',
        mrdid: icd_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/MrduserpageList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(user),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            console.log(data1);
            loadicdtable();
            load_nyr_icdtable();
        }
    });
}


$('#documents').change(function () {
    debugger;
   doctorimageupload();
});



function doctorimageupload() {
    debugger;
    var uploadpath = $('#documents').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "png" || fileExtension == "pdf" || fileExtension == "JPG" || fileExtension == "JPEG" || fileExtension == "PNG" || fileExtension == "PDF") {
    }
    else {
        $('#Pdocuments').show();
        $('#Pdocuments').fadeOut(8000);
        $('#Pdocuments').html('Please upload file having extensions only.');
        $('#documents').val('');
        $('#documents').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        var fileUpload = $("#documents").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        //$.when(checkimagedimensionvalidation(fileUpload.files[0], 1024, 512)).
        //done(function (Results) {
        //    if (Results == 'Success') {
                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');
                $.ajax({
                    url: '/DoctorProfile/UploadPhotoFiles',
                    type: "POST",
                    contentType: false,
                    processData: false,
                    data: fileData,
                    success: function (result) {
                        $.each(result, function (i, emp) {
                            $('#profile-photo').empty();
                            $('#profile-photo').show();
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            drloimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Photos/' + emp + '?' + n;
                            $('#profile-photo').append('<a href="' + drloimage + '" target="_blank">Image</a>');
                            $('#drphoto').val(emp);
                            var drphotoSrc = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + "/Photos/" + emp;
                            $('#ProfilePhotoTop,#ProfilePhoto').attr('src', drphotoSrc);
                        });
                    }
                });
            //}
            //else {
            //    $('#documents').val('');
            //    //$('#profile-photo').val('');             
            //}
            //this code is executed when all ajax calls are done
        //});
    }
}




