$(document).ready(function () {
    debugger;
    LoadBranch();
    LoadDepartment();
    loadrecordrequest();
    $('.pat-strip').hide();
    check_previlige_tabs(24,'MR_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;
                        
                        }
                    });
                }
            }
        }
    });
}

//Record Request - Filter

$('#req_filter').on('click', function () {
    debugger;
    //if ($.trim($('#req_branch_id').val()) == "") {
    //    $('#Preq_branch_id').show();
    //    $('#Preq_branch_id').fadeOut(8000);
    //    $('#Preq_branch_id').html('Please Select the Branch');
    //    $('#Preq_branch_id').focus();
    //    $('#Preq_branch_id').val('');
    //    return false;
    //}
    var req_branch_id = $('#req_branch_id').val();
    var req_pat_name = $('#req_patient_name').val();
    var req_pat_id = $('#req_patient_id').val();
    var req_dpt = $('#req_department').val();
    var req_sub_dpt = $('#req_sub_department').val();
    var req_date_time = $('#req_date_time').val();
    if (req_date_time == null || req_date_time == '') {
        var r_date = ''
    }
    else {   
    var r_date_split = '', r_date = ''
    r_date_split = req_date_time.split('/');
    r_date = r_date_split[2] + '-' + r_date_split[1] + '-' + r_date_split[0];
    }
    var from_date = $('#txtfrom_date').val();
    if (from_date == null || from_date == '') {
        var f_date = ''
    }
    var to_date = $('#txtto_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];
    }
    loadrecordrequest(req_branch_id, req_pat_name, req_pat_id, req_dpt, req_sub_dpt, r_date, f_date, t_date)
})
$('#reqclear').click(function () {
    reqclear();
    loadrecordrequest();
    LoadBranch();
});
function reqclear() {
    $('#req_branch_id').val(""),
     $('#req_patient_name').val(""),
     $('#req_patient_id').val(""),
      $('#req_department').val(""),
      $('#req_sub_department').val(""),
    $('#req_date_time').val(""),
    $('#txtfrom_date').val(""),
    $('#txtto_date').val("")
}

//Load Record Reqeust - List

function loadrecordrequest(req_branch_id, req_pat_name, req_pat_id, req_dpt, req_sub_dpt, r_date, f_date, t_date) {
    debugger;   
    var values,department = '', branchid = ''

    if (req_dpt != '' || req_dpt != 0 || req_dpt == undefined) {
         department = req_dpt
        //department = $('#HDepartmentId').val()
    }
    else {
        department = $('#HDepartmentId').val()
    }
    if (req_branch_id != '' || req_branch_id != 0) {
        branchid = req_branch_id
    }
    else {
        branchid = $('#HospitalBranchID').val()
    }
    if ($('#hiddenRole_id').val() == 2) {
        values = {
            Dml_Indicator: "LRR",
            Hosptial_id: $('#HospitalId').val(),
            Department: department,
            Branch_id: branchid,
            patient_name: req_pat_name,
            patient_id: req_pat_id,
            subdepartment: req_sub_dpt,
            requested_on: r_date,
            from_date: f_date,
            to_date: t_date,
            flag: 1
        }
    }
    else {
       

        values = {
            Dml_Indicator: "LRR",
            Department: department,
            Hosptial_id: $('#HospitalId').val(),
            Branch_id: branchid,
            patient_name: req_pat_name,
            patient_id: req_pat_id,
            subdepartment: req_sub_dpt,
            requested_on: r_date,
            from_date: f_date,
            to_date: t_date,
            flag: 1
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            debugger;
            console.log(data);
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }

                else {
                    $('#records_requested_table tbody').empty();
                    $('#records_requested_table').dataTable().fnClearTable();
                    $('#records_requested_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>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td>' + emp.name + '</td><td >' + emp.sub_cd_name + '</td><td>' + emp.requestedby + '</td><td>' + emp.created_on + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + '</td></tr>';
                    });
                    $('#txtrecords_requested').html(tr);
                    datatable('records_requested_table', 'Records_Requested_List', 'Legal');
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//record issued list - Filter

$('#issue_filter').on('click', function () {
    debugger;
    //if ($.trim($('#issue_branch_id').val()) == "") {
    //    $('#Pissue_branch_id').show();
    //    $('#Pissue_branch_id').fadeOut(8000);
    //    $('#Pissue_branch_id').html('Please Select the Branch');
    //    $('#Pissue_branch_id').focus();
    //    $('#Pissue_branch_id').val('');
    //    return false;
    //}
    var issue_branchid = $('#issue_branch_id').val();
    var issue_pat_name = $('#issue_patient_name').val();
    var issue_pat_id = $('#issue_patient_id').val();
    var issue_dpt = $('#issue_department').val();
    var issue_sub_dpt = $('#issue_sub_department').val();
    var issue_date_time = $('#issue_date_time').val();
    if (issue_date_time == null || issue_date_time == '') {
        var r_issue_date = ''
    }
    else {
        var r_issue_date_split = '', r_issue_date = ''
        r_issue_date_split = issue_date_time.split('/');
        r_issue_date = r_issue_date_split[2] + '-' + r_issue_date_split[1] + '-' + r_issue_date_split[0];
    }
    var from_date = $('#I_txtfrom_date').val();
    if (from_date == null || from_date == '') {
        var f_issue_date = ''
    }
    var to_date = $('#I_txtto_date').val();
    if (to_date == null || to_date == '') {
        var t_issue_date = ''
    }
    else {        
        var f_issue_date_split = '', f_issue_date = ''
        f_issue_date_split = from_date.split('/');
        f_issue_date = f_issue_date_split[2] + '-' + f_issue_date_split[1] + '-' + f_issue_date_split[0];
       
        var t_issue_date_split = '', t_issue_date = ''
        t_issue_date_split = to_date.split('/');
        t_issue_date = t_issue_date_split[2] + '-' + t_issue_date_split[1] + '-' + t_issue_date_split[0];
    }
    record_issued_list_tab(issue_branchid, issue_pat_name, issue_pat_id, issue_dpt, issue_sub_dpt, r_issue_date, f_issue_date, t_issue_date)
})
$('#issueclear').click(function () {
    issueclear();
    record_issued_list_tab();
    LoadBranch();
});
function issueclear() {
    $('#issue_branch_id').val(""),
     $('#issue_patient_name').val(""),
     $('#issue_patient_id').val(""),
      $('#issue_department').val(""),
      $('#issue_sub_department').val(""),
    $('#issue_date_time').val(""),
    $('#I_txtfrom_date').val(""),
    $('#I_txtto_date').val("")
}

//record issued list - Tab

$("#record_issued_list_tab").click(function () {
    debugger;
    record_issued_list_tab();
});

//load record issued list

function record_issued_list_tab(issue_branchid, issue_pat_name, issue_pat_id, issue_dpt, issue_sub_dpt, r_issue_date, f_issue_date, t_issue_date) {
    debugger;
    var values, department = '', branchid = ''
    if (issue_dpt != '' || issue_dpt != 0) {
        department = issue_dpt
    }
    else {
        department = $('#HDepartmentId').val()
    }
    if (issue_branchid != '' || issue_branchid != 0) {
        branchid = issue_branchid
    }
    else {
        branchid = $('#HospitalBranchID').val()
    }
    if ($('#hiddenRole_id').val() == 2) {
        values = {
            Dml_Indicator: "LRI",
            Hosptial_id: $('#HospitalId').val(),
            Department: department,
            Branchid: branchid,
            patient_name: issue_pat_name,
            patient_id: issue_pat_id,
            subdepartment: issue_sub_dpt,
            requested_on: r_issue_date,
            from_date: f_issue_date,
            to_date: t_issue_date
        }
    }
    else {
            var department = '', branchid = ''
            if (issue_dpt != '' || issue_dpt != 0) {
                department = issue_dpt
            }
            else {
                department = $('#HDepartmentId').val()
            }
            if (issue_branchid != '' || issue_branchid != 0) {
                branchid = issue_branchid
            }
            else {
                branchid = $('#HospitalBranchID').val()
            }
            values = {
                Dml_Indicator: "LRI",
                Hosptial_id: $('#HospitalId').val(),
                Department: department,
                Branchid: branchid,
                patient_name: issue_pat_name,
                patient_id: issue_pat_id,
                subdepartment: issue_sub_dpt,
                requested_on: r_issue_date,
                from_date: f_issue_date,
                to_date: t_issue_date
            }
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(values),
            success: function (data) {
                debugger;
                console.log(data);
                var data1 = JSON.parse(data);
                console.log(data1);
                if (data1.msg == "Success") {
                    debugger;
                    var data1 = data1.data;
                    if (jQuery.isEmptyObject((data1))) {

                    }

                    else {
                        $('#record_issues_table tbody').empty();
                        $('#record_issues_table').dataTable().fnClearTable();
                        $('#record_issues_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>' + 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>' + active_inactive + '</td><td>' + '<i class="fa fa-reply mr-10" aria-hidden="true" data-toggle="modal" data-target="#record_issued_return_action" onclick=returnmrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.department + '","' + emp.subdepartment + '","' + emp.requestid + '","' + emp.mrdid + '")></i><i class="fa fa-exchange" aria-hidden="true" data-toggle="modal" data-target="#record_issued_transfer_action" onclick=transmrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.department + '","' + emp.subdepartment + '","' + emp.requestid + '")></i>' + '</td></tr>';
                        });
                        $('#txtrecord_issued').html(tr);
                        datatable('record_issues_table', 'Record_issued_Table', 'Legal');
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
}

//Other dept req - filter

$('#other_dept_filter').on('click', function () {
    debugger;
    //if ($.trim($('#other_dept_branch_id').val()) == "") {
    //    $('#Pother_dept_branch_id').show();
    //    $('#Pother_dept_branch_id').fadeOut(8000);
    //    $('#Pother_dept_branch_id').html('Please Select the Branch');
    //    $('#Pother_dept_branch_id').focus();
    //    $('#Pother_dept_branch_id').val('');
    //    return false;
    //}
    var other_dept_branch_id = $('#other_dept_branch_id').val();
    var other_dept_pat_name = $('#dept_req_patient_name').val();
    var other_dept_pat_id = $('#dept_req_patient_id').val();
    var other_dept_dpt = $('#dept_req_department').val();
    var other_dept_sub_dpt = $('#dept_req_sub_department').val();
    var other_dept_date_time = $('#dept_req_date_time').val();
    if (other_dept_date_time == null || other_dept_date_time == '') {
        var r_other_dept_date = ''
    }
    else {
        var r_other_dept_date_split = '', r_other_dept_date = ''
        r_other_dept_date_split = other_dept_date_time.split('/');
        r_other_dept_date = r_other_dept_date_split[2] + '-' + r_other_dept_date_split[1] + '-' + r_other_dept_date_split[0];
    }
    var from_date = $('#O_txtfrom_date').val();
    if (from_date == null || from_date == '') {
        var f_other_dept_date = ''
    }
    var to_date = $('#O_txtto_date').val();
    if (to_date == null || to_date == '') {
        var t_other_dept_date = ''
    }
    else {
        var f_other_dept_date_split = '', f_other_dept_date = ''
        f_other_dept_date_split = from_date.split('/');
        f_other_dept_date = f_other_dept_date_split[2] + '-' + f_other_dept_date_split[1] + '-' + f_other_dept_date_split[0];

        var t_other_dept_date_split = '', t_other_dept_date = ''
        t_other_dept_date_split = to_date.split('/');
        t_other_dept_date = t_other_dept_date_split[2] + '-' + t_other_dept_date_split[1] + '-' + t_other_dept_date_split[0];
    }
    loadotherdeparttmentrequest(other_dept_branch_id, other_dept_pat_name, other_dept_pat_id, other_dept_dpt, other_dept_sub_dpt, r_other_dept_date, f_other_dept_date, t_other_dept_date)
});
$('#otherdeptclear').click(function () {
    otherdeptclear();
    loadotherdeparttmentrequest();
    LoadBranch();
});
function otherdeptclear() {
    $('#other_dept_branch_id').val(""),
     $('#dept_req_patient_name').val(""),
     $('#dept_req_patient_id').val(""),
      $('#dept_req_department').val(""),
      $('#dept_req_sub_department').val(""),
    $('#dept_req_date_time').val(""),
    $('#O_txtfrom_date').val(""),
    $('#O_txtto_date').val("")
}

//load other department request

    $("#other_department_requests_list_tab").click(function () {
        debugger;
        loadotherdeparttmentrequest();
    });

    function loadotherdeparttmentrequest(other_dept_branch_id, other_dept_pat_name, other_dept_pat_id, other_dept_dpt, other_dept_sub_dpt, r_other_dept_date, f_other_dept_date, t_other_dept_date) {
        debugger;
        var values,department = '', branchid = ''
        if (other_dept_dpt != '' || other_dept_dpt != 0) {
            department = other_dept_dpt
        }
        else {
            department = $('#HDepartmentId').val()
        }
        if (other_dept_branch_id != '' || other_dept_branch_id != 0) {
            branchid = other_dept_branch_id
        }
        else {
            branchid = $('#HospitalBranchID').val()
        }
        if ($('#hiddenRole_id').val() == 2) {
            values = {
                Dml_Indicator: "LODR",
                Hosptial_id: $('#HospitalId').val(),
                Department: department,
                Branchid: branchid,
                patient_name: other_dept_pat_name,
                patient_id: other_dept_pat_id,
                subdepartment: other_dept_sub_dpt,
                requested_on: r_other_dept_date,
                from_date: f_other_dept_date,
                to_date: t_other_dept_date,
                flag: 2
            }
        }
        else {
            var department = '', branchid =''
            if (other_dept_dpt != '' || other_dept_dpt != 0) {
                department = other_dept_dpt
            }
            else {
                department = $('#HDepartmentId').val()
            }
            if (other_dept_branch_id != '' || other_dept_branch_id != 0) {
                branchid = other_dept_branch_id
            }
            else {
                branchid = $('#HDepartmentId').val()
            }
            values = {
                Dml_Indicator: "LODR",
                Hosptial_id: $('#HospitalId').val(),
                Department: department,
                Branchid: branchid,
                patient_name: other_dept_pat_name,
                patient_id: other_dept_pat_id,
                subdepartment: other_dept_sub_dpt,
                requested_on: r_other_dept_date,
                from_date: f_other_dept_date,
                to_date: t_other_dept_date,
                flag: 2
            }
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(values),
            success: function (data) {
                debugger;
                console.log(data);
                var data1 = JSON.parse(data);
                console.log(data1);
                if (data1.msg == "Success") {
                    debugger;
                    var data1 = data1.data;
                    if (jQuery.isEmptyObject((data1))) {

                    }

                    else {
                        $('#other_department_request_table tbody').empty();
                        $('#other_department_request_table').dataTable().fnClearTable();
                        $('#other_department_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-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.name + '</td><td>' + emp.sub_cd_name + '</td><td>' + emp.requestedby + '</td><td>' + emp.created_date + '</td><td>' + emp.created_time + '</td><td>' + emp.employee_name + '</td><td>' + active_inactive + ' </td><td><i class="fa fa-exchange" aria-hidden="true" data-toggle="modal" data-target="#other_department_request_action" onclick=\'transfermrd(this,"' + emp.patient_id + '","' + emp.branch_id + '","' + emp.sub_category_department_id + '","' + emp.master_category_department_id + '","' + emp.mrdrequestid + '")\'></i></td></tr>';
                        });
                        $('#txtother_department_requests').html(tr);
                        datatable('other_department_request_table', 'other_department_request_table', 'Legal');
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }

//Load Branch

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

                            branch_change();
                            //$('#pkg_branch_id').val(emp.hospital_branch_id);
                        }
                        else {
                            $('#mrd_branch_Name').append(default_opt + SubDepartment);
                            $('#transfer_branch').append(default_opt + SubDepartment);
                            $('#req_branch_id').append(fil_default_opt + SubDepartment);
                            $('#issue_branch_id').append(fil_default_opt + SubDepartment);
                            $('#other_dept_branch_id').append(fil_default_opt + SubDepartment);

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

                            $('#transfer_branch').val($('#HospitalBranchID').val());
                            $('#transfer_branch').attr('disabled', true);

                            $('#req_branch_id').val($('#HospitalBranchID').val());
                            $('#req_branch_id').attr('disabled', true);

                            $('#issue_branch_id').val($('#HospitalBranchID').val());
                            $('#issue_branch_id').attr('disabled', true);

                            $('#other_dept_branch_id').val($('#HospitalBranchID').val());
                            $('#other_dept_branch_id').attr('disabled', true);

                            //$('#add_patient_id').attr('disabled', false)
                            branch_change();
                        }
                    }
                }
                else {
                    console.log(data1);
                }
            }
        });
    }
//-------one master concept----------
    //$('#mrd_branch_Name').change(function () {
    //    debugger;
    //    branch_change($('#mrd_branch_Name').val());
    //    LoadRequestBy($('#mrd_branch_Name').val());

    //    if ($('#mrd_branch_Name').val() != 0) {
    //        $('#add_patient_id').attr('disabled', false)
    //    }
    //    else {
    //        $('#add_patient_id').attr('disabled', true)
    //    }

    //})
    //$('#transfer_branch').change(function () {
    //    debugger;
    //    branch_change($('#transfer_branch').val());
    //})
    //$('#req_branch_id').change(function () {
    //    debugger;
    //    branch_change($('#req_branch_id').val());
    //})
    //$('#issue_branch_id').change(function () {
    //    debugger;
    //    branch_change($('#issue_branch_id').val());
    //})
    //$('#other_dept_branch_id').change(function () {
    //    debugger;
    //    branch_change($('#other_dept_branch_id').val());
    //})
//---------one master concept-----------
    //function branch_change(branch) {
    //    debugger;
  
    //    if ($('#mrd_branch_Name').val() == '' || $('#mrd_branch_Name').val() == 0) {
    //        $('#mrd_department').attr('disabled', true);
    //    }

    //    else {
    //        LoadDepartment(branch);
    //        $('#mrd_department').attr('disabled', false);
    //        $('#mrd_sub_department').empty();
    //    }

    //    if ($('#transfer_branch').val() == '' || $('#transfer_branch').val() == 0) {
    //        $('#transfer_department').attr('disabled', true);
    //    }

    //    else {
    //        LoadDepartment(branch);
    //        $('#transfer_branch').attr('disabled', false);
    //        $('#transfer_department').empty();
    //    }

    //    if ($('#req_branch_id').val() == '' || $('#req_branch_id').val() == 0) {
    //        $('#req_department').attr('disabled', true);
    //    }

    //    else {
    //        LoadDepartment(branch);
    //        $('#req_branch_id').attr('disabled', false);
    //        $('#req_department').empty();
    //    }
    //    if ($('#issue_branch_id').val() == '' || $('#issue_branch_id').val() == 0) {
    //        $('#issue_department').attr('disabled', true);
    //    }

    //    else {
    //        LoadDepartment(branch);
    //        $('#issue_branch_id').attr('disabled', false);
    //        $('#issue_department').empty();
    //    }
    //    if ($('#other_dept_branch_id').val() == '' || $('#other_dept_branch_id').val() == 0) {
    //        $('#dept_req_department').attr('disabled', true);
    //    }

    //    else {
    //        LoadDepartment(branch);
    //        $('#other_dept_branch_id').attr('disabled', false);
    //        $('#dept_req_department').empty();
    //    }
    //}

//load department

//function LoadDepartment(branch) {  //one master concept
function LoadDepartment() {
        var department = {
            Dml_Indicator: "DP"
            //,branch_id: branch      //one master concept
        }
        $.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) {
                var data1 = JSON.parse(data);
                if (data1.msg == "Success") {
                    var data1 = data1.data;
                    console.log(data1)
                    if (jQuery.isEmptyObject((data1))) {
                    }
                    else {
                        var Department;
                        $('#mrd_department').empty();
                        $('#transfer_department').empty();
                        $('#req_department').empty();
                        $('#issue_department').empty();
                        $('#dept_req_department').empty();


                        var default_opt = '<option value="0">--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) {
                            $('#mrd_department').append(Department);
                            $("#mrd_department").change();
                            $('#transfer_department').append(Department);
                            $("#transfer_department").change();
                            $('#req_department').append(Department);                           
                            $("#req_department").change();
                            $('#req_department').append(Department);
                            $("#req_department").change();
                            $('#issue_department').append(Department);
                            $("#issue_department").change();
                            $('#dept_req_department').append(Department);
                            $("#dept_req_department").change();
                        }
                        else {
                            $('#mrd_department').append(default_opt + Department);
                            $('#transfer_department').append(default_opt + Department);
                            $('#req_department').append(default_opt + Department);
                            $('#issue_department').append(default_opt + Department);
                            $('#dept_req_department').append(default_opt + Department);

                        }

                    }
                }
                else {
                    console.log(data1);
                }
            }
        });
    }
    $("#mrd_department").change(function () {
        debugger;
        LoadSubDepartment($('#mrd_department').val(), 0);
        $('#mrd_sub_department').prop('disabled', false);
    })
    $("#transfer_department").change(function () {
        debugger;
        LoadSubDepartment($('#transfer_department').val(), 0);
        $('#transfer_sub_department').prop('disabled', false);
    })
    $("#req_department").change(function () {
        debugger;
        LoadSubDepartment($('#req_department').val(), 0);
        $('#req_sub_department').prop('disabled', false);
    })
    $("#issue_department").change(function () {
        debugger;
        LoadSubDepartment($('#issue_department').val(), 0);
        $('#issue_sub_department').prop('disabled', false);
    })
    $("#dept_req_department").change(function () {
        debugger;
        LoadSubDepartment($('#dept_req_department').val(), 0);
        $('#dept_req_sub_department').prop('disabled', false);
    })

//load sub department

    function LoadSubDepartment(id, val) {
        debugger;
        var val = val;
        document.getElementById("mrd_sub_department").innerHTML = null;
        document.getElementById("transfer_sub_department").innerHTML = null;
        document.getElementById("req_sub_department").innerHTML = null;
        document.getElementById("issue_sub_department").innerHTML = null;
        document.getElementById("dept_req_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) {
                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) {
                            $('#mrd_sub_department').append(SubDepartment);
                            // $("#mrd_sub_department").change();
                            $('#transfer_sub_department').append(SubDepartment);
                            // $("#transfer_sub_department").change();
                            $('#req_sub_department').append(SubDepartment);
                            // $("#req_sub_department").change();
                            $('#issue_sub_department').append(SubDepartment);
                            $('#dept_req_sub_department').append(SubDepartment);
                        }
                        else {
                            $('#mrd_sub_department').append(default_opt + SubDepartment);
                            $('#transfer_sub_department').append(default_opt + SubDepartment);
                            $('#req_sub_department').append(default_opt + SubDepartment);
                            $('#issue_sub_department').append(default_opt + SubDepartment);
                            $('#dept_req_sub_department').append(default_opt + SubDepartment);

                        }
                        if (val != '' && val != 0) {
                            $('#mrd_sub_department').val(val);
                            $('#transfer_sub_department').val(val);
                            $('#req_sub_department').val(val);
                            $('#issue_sub_department').val(val);
                            $('#dept_req_sub_department').val(val);

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

//Add to request - request button

    $('#txtrequest').on("click", function () {
        debugger;
        if ($.trim($('#mrd_branch_Name').val()) == "") {
            $('#Padd-branch_Name').show();
            $('#Padd-branch_Name').fadeOut(8000);
            $('#Padd-branch_Name').html('Please Select The Branch');
            $('#Padd-branch_Name').focus();
            $('#Padd-branch_Name').val('');
            return false;
        }
        if ($.trim($('#mrd_department').val()) == "") {
            $('#Padd-department').show();
            $('#Padd-department').fadeOut(8000);
            $('#Padd-department').html('Please Select The Department');
            $('#Padd-department').focus();
            $('#Padd-department').val('');
            return false;
        }
        if ($.trim($('#mrd_sub_department').val()) == "") {
            $('#Padd_sub_department').show();
            $('#Padd_sub_department').fadeOut(8000);
            $('#Padd_sub_department').html('Please Select The Sub-Department');
            $('#Padd_sub_department').focus();
            $('#Padd_sub_department').val('');
            return false;
        }
        if ($.trim($('#mrd_employee_name').val()) == "") {
                $('#Pmrd_employee_name').show();
                $('#Pmrd_employee_name').fadeOut(8000);
                $('#Pmrd_employee_name').html('Please Enter Employee Name');
                $('#Pmrd_employee_name').focus();
                $('#Pmrd_employee_name').val('');
                return false;
        }
        var request = {
            patient_id: $('#Hpatientid').val(),
            branch_id: $('#mrd_branch_Name').val(),
            department: $('#mrd_department').val(),
            subdepartment: $('#mrd_sub_department').val(),
            requestedby: $('#hiddenUser_id').val(),
            Created_by: $('#hiddenUser_id').val(),
           // Mrdid: $('#Hmrdid').val(),
            employee_name: $('#mrd_employee_name').val(),
            Dml_Indicator: "IS",
            flag: 1
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(request),
            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,
                           });
                        loadrecordrequest();
                    }

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

            },
            error: function (data) {

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

    function clearRequest() {
        $('#add_patient_id').val(""),
       $('#mrd_branch_Name').val(0),
       $('#mrd_department').val(""),
      $('#mrd_sub_department').val(""),
       $('#mrd_employee_name').val(""),
        $('.pat-strip').hide();
    }
    $('#goback2').click(function () {
        $('#add_patient_id').val(""),
       $('#mrd_branch_Name').val(0),
       $('#mrd_department').val(""),
      $('#mrd_sub_department').val(""),
       $('#mrd_employee_name').val(""),
        $('.pat-strip').hide();
    });

//Add to request - cancel button

    $('#txtcancel').click(function () {
        debugger;
        $('#add_patient_id').val(""),
        $('#mrd_branch_Name').val(0),
        $('#mrd_department').val(""),
       $('#mrd_sub_department').val(""),
        $('#mrd_employee_name').val(""),
       $('.pat-strip').hide();
    });
    function transmrd(event, patient_id, branch_id, department, subdepartment, requestid) {
        debugger;
        $('#Hpatientid').val(patient_id);
        $('#Hbranchid').val(branch_id);
        $('#Hdepartment').val(department);
        $('#Hsubdepartment').val(subdepartment);
        $('#Hrequestid').val(requestid);
    };

// Issue tab - Transfer button

    $('#btntransfer').on("click", function () {
        debugger;
        if ($.trim($('#transfer_department').val()) == "") {
            $('#Ptransfer_department').show();
            $('#Ptransfer_department').fadeOut(8000);
            $('#Ptransfer_department').html('Please Select the Department');
            $('#Ptransfer_department').focus();
            $('#Ptransfer_department').val('');
            return false;
        }
        if ($.trim($('#transfer_sub_department').val()) == "") {
            $('#Ptransfer_sub_department').show();
            $('#Ptransfer_sub_department').fadeOut(8000);
            $('#Ptransfer_sub_department').html('Please Select the Sub-Department');
            $('#Ptransfer_sub_department').focus();
            $('#Ptransfer_sub_department').val('');
            return false;
        }
        if ($.trim($('#transfer_employee_name').val()) == "") {
            $('#Ptransfer_employee_name').show();
            $('#Ptransfer_employee_name').fadeOut(8000);
            $('#Ptransfer_employee_name').html('Please Enter Employee Name');
            $('#Ptransfer_employee_name').focus();
            $('#Ptransfer_employee_name').val('');
            return false;
        }
        var tranfer = {
            patient_id: $('#Hpatientid').val(),
            branch_id: $('#transfer_branch').val(),
            department: $('#transfer_department').val(),
            subdepartment: $('#transfer_sub_department').val(),
            tranferred_by: $('#hiddenUser_id').val(),
            received_by: $('#hiddenRole_id').val(),
            employee_name: $('#transfer_employee_name').val(),
            requestid: $('#Hrequestid').val(),
            Dml_Indicator: "TR"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(tranfer),
            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('Transfer Already Exists',
                            {
                                //closeButton: false,
                                progressBar: true,
                                allowHtml: true,
                                timeOut: 0,
                            });
                        return false;
                    }
                    else {
                        toastr.success('Transfered Successfully',
                           {
                               //closeButton: false,
                               progressBar: true,
                               allowHtml: true,
                               timeOut: 0,
                           });
                    }

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

            },
            error: function (data) {

            }
        });
        window.location.reload();
    });
    function clearTransfer() {
        $('#transfer_branch').val(""),
        $('#transfer_department').val(""),
        $('#transfer_sub_department').val(""),
         $('#transfer_employee_name').val("")
    }
    function goback() {
        $('#transfer_branch').val(""),
        $('#transfer_department').val(""),
        $('#transfer_sub_department').val(""),
         $('#transfer_employee_name').val("")
    }
    $('#btncancel').click(function () {
        $('#transfer_branch').val(""),
         $('#transfer_department').val(""),
         $('#transfer_sub_department').val(""),
          $('#transfer_employee_name').val("")
    });

//Issue tab - return button

    function returnmrd(event, patient_id, branch_id, department, subdepartment, requestid, mrdid) {
        debugger;
        $('#Hpatientid').val(patient_id);
        $('#Hbranchid').val(branch_id);
        $('#Hdepartment').val(department);
        $('#Hsubdepartment').val(subdepartment);
        $('#Hrequestid').val(requestid);
        $('#Hmrdid').val(mrdid);

        PatientGeneralDetails(patient_id, branch_id);
    };
    $('#mrdreturn').on("click", function () {
        debugger;
        if ($.trim($('#return_employee_name').val()) == "") {
            $('#Preturn_employee_name').show();
            $('#Preturn_employee_name').fadeOut(8000);
            $('#Preturn_employee_name').html('Please Enter Employee Name');
            $('#Preturn_employee_name').focus();
            $('#Preturn_employee_name').val('');
            return false;
        }
        var returnmrd = {
            patient_id: $('#Hpatientid').val(),
            branch_id: $('#Hbranchid').val(),
            department: $('#Hdepartment').val(),
            subdepartment: $('#Hsubdepartment').val(),
            returnedby: $('#hiddenUser_id').val(),
            requestid: $('#Hrequestid').val(),
            mrdid: $('#Hmrdid').val(),
            returnby_employee_name: $('#return_employee_name').val(),
            Dml_Indicator: "RI",
            flag: 3
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(returnmrd),
            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('Returned Successfully',
                           {
                               //closeButton: false,
                               progressBar: true,
                               allowHtml: true,
                               timeOut: 0,
                           });
                    }

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

            },
            error: function (data) {

            }
        });
        window.location.reload();
    });
    function clearrequest() {
        $('#return_employee_name').val(""),
        $('.pat-strip').hide();
    }
    function goback() {
        $('#return_employee_name').val(""),
        $('.pat-strip').hide();
    }
    $('#mrdcancel').click(function () {
        $('#return_employee_name').val(""),
        $('.pat-strip').hide();
    });

//other department request - Transfer button

    function transfermrd(event, patient_id, branch_id, sub_category_department_id, master_category_department_id, mrdrequestid) {
        debugger;
        $('#Hpatientid').val(patient_id);
        $('#Hbranchid').val(branch_id);
        $('#Hsubdepartment').val(sub_category_department_id);
        $('#Hdepartment').val(master_category_department_id);
        $('#Hmrdrequestid').val(mrdrequestid);
    };
    $('#depttransfer').on("click", function () {
        debugger;
        if ($.trim($('#other_transfer_employee_name').val()) == "") {
            $('#Pother_transfer_employee_name').show();
            $('#Pother_transfer_employee_name').fadeOut(8000);
            $('#Pother_transfer_employee_name').html('Please Enter Employee Name');
            $('#Pother_transfer_employee_name').focus();
            $('#Pother_transfer_employee_name').val('');
            return false;
        }
        var transfermrd = {
            patient_id: $('#Hpatientid').val(),
            branch_id: $('#Hbranchid').val(),
            department: $('#Hdepartment').val(),
            subdepartment: $('#Hsubdepartment').val(),
            transferred_by: $('#hiddenUser_id').val(),
            mrdrequestid: $('#Hmrdrequestid').val(),
            Transfer_by_employee_name : $('#other_transfer_employee_name').val(),
            Dml_Indicator: "ODT"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/MedicalRecord/insert_update_deleteMrddepartmentrequest',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(transfermrd),
            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('Transfer Already Exists',
                            {
                                //closeButton: false,
                                progressBar: true,
                                allowHtml: true,
                                timeOut: 0,
                            });
                        return false;
                    }
                    else {
                        toastr.success('Transfered Successfully',
                           {
                               //closeButton: false,
                               progressBar: true,
                               allowHtml: true,
                               timeOut: 0,
                           });
                    }

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

            },
            error: function (data) {

            }
        });
        window.location.reload();
    });
    function clearDeptTransfer() {
        $('#other_transfer_employee_name').val("")
    }
    function goback() {
        $('#other_transfer_employee_name').val("")
    }
    $('#deptcancel').on("click", function () {
        clear();
    });
    function clear() {
        $('#other_transfer_employee_name').val("")
    }