
$(document).ready(function () {
    IPCaseSheetTable_Load('CCASH');
});

$('.casesheetlists ul li').click(function () {
    $(".content_hide").hide();
    var targetId = $(this).data("target");
    // Show the corresponding content div
    $("#" + targetId).show();
    $('.casesheetlists ul li').removeClass('active'); // Remove "active" class from all buttons
    $(this).addClass('active'); // Add "active" class to the clicked button

    //if(ip_service=="")
    //{

    //}
});


function IPCaseSheetTable_Load(dml) {
    var userid = '';
    if ($('#hiddenRole_id').val()==2) {
        userid = $('#hiddenRole_id').val()
    }
    else {
        userid = $('#hiddenUser_id').val()
    }
    var Form_Data = {
        Dml_Indicator: dml,
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        from_date: userid
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPCaseSheet_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Form_Data),
        success: function (data) {
            $('#ShowDataTable1').empty();
            $('#ShowDataTable2').empty();
            $('#ShowDataTable3').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    debugger;
                    var data = data2.data.Table;
                    

                    // EXTRACT VALUE FOR HTML HEADER. 
                    var col = [];
                    for (var i = 0; i < data.length; i++) {
                        for (var key in data[i]) {
                            if (col.indexOf(key) === -1) {
                                col.push(key);
                            }
                        }
                    }

                    // CREATE DYNAMIC TABLE.
                    var table = document.createElement("table");
                    table.id = "DynamicTable";
                    table.classList.add("table", "table-bordered", "table-striped", "no-footer", "dataTable");

                    // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

                    var tr = table.insertRow(-1);                   // TABLE ROW.

                    for (var i = 0; i < col.length; i++) {
                        var th = document.createElement("th");      // TABLE HEADER.
                        th.innerHTML = col[i];
                        tr.appendChild(th);
                    }

                    // ADD JSON DATA TO THE TABLE AS ROWS.
                    for (var i = 0; i < data.length; i++) {

                        tr = table.insertRow(-1);

                        for (var j = 0; j < col.length; j++) {
                            var tabCell = tr.insertCell(-1);
                            tabCell.innerHTML = data[i][col[j]];
                        }
                    }

                    // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                    var divContainer = document.getElementById("ShowDataTable1");
                    divContainer.innerHTML = "";
                    divContainer.appendChild(table);

                    //$('#DynamicTable').DataTable();

                    debugger;
                    //Table Two 
                    var data = data2.data.Table1;

                    if (data.length > 0) {

                        // EXTRACT VALUE FOR HTML HEADER. 
                        var col = [];
                        for (var i = 0; i < data.length; i++) {
                            for (var key in data[i]) {
                                if (col.indexOf(key) === -1) {
                                    col.push(key);
                                }
                            }
                        }

                        // CREATE DYNAMIC TABLE.
                        var table = document.createElement("table");
                        table.id = "DynamicTable2";
                        table.classList.add("table", "table-bordered", "table-striped", "no-footer", "dataTable");

                        // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

                        var tr = table.insertRow(-1);                   // TABLE ROW.

                        for (var i = 0; i < col.length; i++) {
                            var th = document.createElement("th");      // TABLE HEADER.
                            th.innerHTML = col[i];
                            tr.appendChild(th);
                        }

                        // ADD JSON DATA TO THE TABLE AS ROWS.
                        for (var i = 0; i < data.length; i++) {

                            tr = table.insertRow(-1);

                            for (var j = 0; j < col.length; j++) {
                                var tabCell = tr.insertCell(-1);
                                tabCell.innerHTML = data[i][col[j]];
                            }
                        }

                        // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                        var divContainer = document.getElementById("ShowDataTable2");
                        divContainer.innerHTML = "";
                        divContainer.appendChild(table);

                        //$('#DynamicTable2').DataTable();

                    }

                    //Table Three 
                    var data = data2.data.Table2;

                    if (data.length > 0) {

                        // EXTRACT VALUE FOR HTML HEADER. 
                        var col = [];
                        for (var i = 0; i < data.length; i++) {
                            for (var key in data[i]) {
                                if (col.indexOf(key) === -1) {
                                    col.push(key);
                                }
                            }
                        }

                        // CREATE DYNAMIC TABLE.
                        var table = document.createElement("table");
                        table.id = "DynamicTable3";
                        table.classList.add("table", "table-bordered", "table-striped", "no-footer", "dataTable");

                        // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE.

                        var tr = table.insertRow(-1);                   // TABLE ROW.

                        for (var i = 0; i < col.length; i++) {
                            var th = document.createElement("th");      // TABLE HEADER.
                            th.innerHTML = col[i];
                            tr.appendChild(th);
                        }

                        // ADD JSON DATA TO THE TABLE AS ROWS.
                        for (var i = 0; i < data.length; i++) {

                            tr = table.insertRow(-1);

                            for (var j = 0; j < col.length; j++) {
                                var tabCell = tr.insertCell(-1);
                                tabCell.innerHTML = data[i][col[j]];
                            }
                        }

                        // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER.
                        var divContainer = document.getElementById("ShowDataTable3");
                        divContainer.innerHTML = "";
                        divContainer.appendChild(table);

                        //$('#DynamicTable2').DataTable();

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

function formprint(formmaster_data_id) {
    debugger;
    var load_data = {
        formid: 163,
        patientid: formmaster_data_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/PrintCrystalReport',
        contentType: 'application/json; charset=UTF-8',
        responseType: 'arraybuffer',
        data: JSON.stringify(load_data),
        success: function (data) {
            console.log(data);
            var byteArray = new Uint8Array(data);
            var blob = new Blob([byteArray], { type: "application/pdf" });
            var link = window.URL.createObjectURL(blob);
            //window.open(link, '_blank');   // to open in new window

            iframe = document.createElement('iframe');
            document.body.appendChild(iframe);

            iframe.style.display = 'none';

            iframe.src = link;
            iframe.onload = function () {
                setTimeout(function () {
                    iframe.focus();
                    iframe.contentWindow.print();
                }, 1);
            };
        }
    });
}