$(document).ready(function () {

    LoadBranch();
});


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

                        $('#SelBranchList').append(branchdetails);
                    }
                    else {
                        $('#SelBranch').append(default_opt + branchdetails);
                        $('#SelBranchList').append(default_opt + branchdetails);
                    }

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

                        $('#SelBranchList').val($('#HospitalBranchID').val());
                        $('#SelBranchList').attr('disabled', true);
                        LoadPreOperativeInstructionList();
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

$('#SelBranchList').change(function () {

    LoadPreOperativeInstructionList();

});

function LoadPreOperativeInstructionList() {
    var operativeInstruction = {
        Dml_Indicator: 'S',
        branch_id: $('#SelBranchList').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/PreOperativeInstruction_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(operativeInstruction),
        success: function (data) {
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var c = data1.data
                    $('#tblPreOperativeList').dataTable().fnClearTable();
                    $('#tblPreOperativeList').dataTable().fnDestroy();
                    $('#tblPreOperativeList_body').empty();
                    tr = '';                  
                    if (c.length > 0) {
                        for (var i = 0; i < c.length; i++) {
                            
                            del = '<a class="btn btn-delete btn-xs" onclick="deletepreoperative(' + c[i].pre_operative_instruction_id + ')"><i class="fa fa-trash-o"></i></a>'
                           
                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + c[i].patient_id + '</td><td>' + c[i].first_name + '</td><td>' + c[i].phone_number_1 + '</td><td>' + c[i].admission_date_time + '</td><td>' + c[i].surgery_name + '</td><td>' + del + '</td></tr>';
                        }
                        $('#tblPreOperativeList_body').append(tr);
                    }
                    else {
                        var tr1 = '<tr><td colspan="7"><div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" ></div></td></tr>';
                        $('#tblPreOperativeList_body').append(tr1);
                    }

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

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

    if ($('#txtSearchPatientDetail').val() == "") {
        $('#txtSearchPatientDetail').focus();
        $('#PtxtSearchPatientDetail').show();
        $('#PtxtSearchPatientDetail').fadeOut(8000);
        $('#PtxtSearchPatientDetail').html('Choose patient');
        return false;
    }
    if ($('#SelBranch').val() == "" || $('#SelBranch').val() == 0 || $('#SelBranch').val() == null) {
        $('#SelBranch').focus();
        $('#PSelBranch').show();
        $('#PSelBranch').fadeOut(8000);
        $('#PSelBranch').html('Select Branch');
        return false;
    }
    
    if ($('#txtAdmitDate').val() == "") {
        $('#txtAdmitDate').focus();
        $('#PtxtAdmitDate').show();
        $('#PtxtAdmitDate').fadeOut(8000);
        $('#PtxtAdmitDate').html('Select the Admit date');
        return false;
    }

    if ($('#txtSurgeryName').val() == "") {
        $('#txtSurgeryName').focus();
        $('#PtxtSurgeryName').show();
        $('#PtxtSurgeryName').fadeOut(8000);
        $('#PtxtSurgeryName').html('Enter the Surgery Name');
        return false;
    }

    if ($('#txtdiagnosis').val() == "") {
        $('#txtdiagnosis').focus();
        $('#Ptxtdiagnosis').show();
        $('#Ptxtdiagnosis').fadeOut(8000);
        $('#Ptxtdiagnosis').html('Enter the Diagnosis');
        return false;
    }

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

    var time = $("#txtAdmittime").val();
    var hours = Number(time.match(/^(\d+)/)[1]);
    var minutes = Number(time.match(/:(\d+)/)[1]);
    var AMPM = time.match(/\s(.*)$/)[1];
    if (AMPM == "PM" && hours < 12) hours = hours + 12;
    if (AMPM == "AM" && hours == 12) hours = hours - 12;
    var sHours = hours.toString();
    var sMinutes = minutes.toString();
    if (hours < 10) sHours = "0" + sHours;
    if (minutes < 10) sMinutes = "0" + sMinutes;
    var ATM = sHours + ":" + sMinutes;

    var operativeInstruction = {
        Dml_Indicator: 'I',
        patient_id: $('#txtHiddenSelectedPatientID').val(),
        branch_id: $('#SelBranch').val(),
        diagnosis: $('#txtdiagnosis').val(),
        surgery_name: $('#txtSurgeryName').val(),
        admission_date_time: ADT + " " + ATM,
        drug_allergy_history: $('#txtdrugallergyhistory').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/PreOperativeInstruction_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(operativeInstruction),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    toastr.success("Details Saved Successfully");
                    //var load_data = {
                    //    formid: '64',
                    //    branchid: $('#pkg_branch_id').val(),
                    //    hospitalid: data1.Table[0].hospitalid,
                    //    patientid: data1.Table[0].patientid,
                    //    Estimationno: data1.Table[0].estimation_no
                    //}
                    //$.ajax({
                    //    async: true,
                    //    type: 'POST',
                    //    url: '/reports/PrintCrystalReport',
                    //    contentType: 'application/json; charset=UTF-8',
                    //    data: JSON.stringify(load_data),
                    //    success: function (data) {
                    //        var byteArray = new Uint8Array(data);
                    //        var blob = new Blob([byteArray], { type: "application/pdf" });
                    //        var link = window.URL.createObjectURL(blob);
                    //        // window.open(link, '_blank');
                    //        iframe = document.createElement('iframe'); //load content in an iframe to print later
                    //        document.body.appendChild(iframe);

                    //        iframe.style.display = 'none';
                    //        iframe.src = link;
                    //        iframe.onload = function () {
                    //            setTimeout(function () {
                    //                iframe.focus();
                    //                iframe.contentWindow.print();
                    //            }, 1);
                    //        };
                    //    }
                    //});
                    location.reload();
                }
            }
            else {
                console.log(data);
            }
        }
    });

});



function deletepreoperative(pre_operative_instruction_id) {

    var operativeInstruction = {
        Dml_Indicator: 'D',
        pre_operative_instruction_id: pre_operative_instruction_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/PreOperativeInstruction_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(operativeInstruction),
        success: function (data) {
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    toastr.success("Deleted Successfully");
                    LoadPreOperativeInstructionList();
                }
            }
            else {
                console.log(data1)
            }
        }
    });
}


