
$(document).ready(function () {
    TimeLineDate();
});

//$('#Service_panel').click(function () {
//    debugger;

//    // Check the state of the first section
//    var isFirstSectionOpen = $("#serviceaccordion").is(":visible");

//    if(isFirstSectionOpen==false)
//    {
//        TimeLineDate();
//    }
    
//});

function TimeLineDate() {
    debugger;

    var TimeLinedata = {
        Dml_Indicator: "TLD",
        IP_visit_report_id: $('#IPVisitReport_Id').val()
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(TimeLinedata),
        success: function (data) {
            debugger;
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    $('#ServiceList_Date').html("");
                    //$('#Prescription_Date').html("");
                    ul = '',pul='';
                    ul = '<ul class="ipltimelinedates">'
                    //pul = '<ul class="ipltimelinedates">'
                    $.each(data1, function (i, emp) {

                        
                        ul = ul + '<li><p class="Slistcolor" id="LI_' + i + '" onclick="ServiceDate(this,\'' + emp.Date_value + '\');">' + emp.Date_column + '</p></li>';
                        //pul = pul + '<li><p class="Plistcolor" onclick="PrescriptionDate(this,\'' + emp.Date_value + '\');">' + emp.Date_column + '</p></li>'

                    });
                    ul = ul + '</ul>';
                    //pul = pul + '</ul>';

                    $('#ServiceList_Date').html(ul);
                    //$('#Prescription_Date').html(pul);
                    FirstDateLoad("LI_0", data1[0].Date_value);

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

function ServiceDate(element, date) {

    var allElements = document.querySelectorAll('.Slistcolor');
    allElements.forEach(function (el) {
        el.classList.remove('active');
    });
    element.classList.add('active');

    $('#AllTimeLineTable').removeClass('hide');

    IPTimeLineTable_Load(date);
}

function FirstDateLoad(id, date) {
   
    $('#' + id).addClass('active');

    $('#AllTimeLineTable').removeClass('hide');

    IPTimeLineTable_Load(date);
}

function LoadService(date) {
    debugger;

    var TimeLinedata = {
        Dml_Indicator: "ARSL",
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        from_date: date
    }


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

            }
            else {

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

                    row_data = '';
                    $.each(data1, function (i, emp) {


                        row_data = row_data + "<tr><td>" + emp.created_time + "</td><td>" + emp.service_name + "</td><td>" + emp.added_by + "</td></tr>";
                    });

                    $('#ServiceList_TblBody').append(row_data);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

function LoadPrescription(date) {
    debugger;

    var TimeLinedata = {
        Dml_Indicator: "AMPS",
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        from_date: date
    }


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

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    
                    row_data = '';
                    $.each(data1, function (i, emp) {


                        row_data = row_data + "<tr><td>" + emp.created_time + "</td><td>" + emp.medicine_name + "</td><td>" + emp.ipdoctor_name + "</td><td>" + emp.added_by + "</td></tr>";
                    });

                    $('#Prescription_TblBody').append(row_data);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}


function IPTimeLineTable_Load(date) {

    var Form_Data = {
        Dml_Indicator: "CTV",
        IP_visit_report_id: $('#IPVisitReport_Id').val(),
        from_date: date
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/IPCaseSheet_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Form_Data),
        success: function (data) {
            debugger;
            $('.TimeLineTable').empty();

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

            }
            else {

                if (data2.msg == "Success") {
                    debugger;
                    $('.div_ClsShowTimeLineTable').addClass('hide');
                    var data3 = data2.data;
                    

                    for (var j = 0; j < 20; j++) {
                        var data = "";

                        if (j == 0) {
                             data = data3.Table;
                        }
                        else if (j == 1) {
                             data = data3.Table1;
                        }
                        else if (j == 2) {
                             data = data3.Table2;
                        }
                        else if (j == 3) {
                             data = data3.Table3;
                        }
                        else if (j == 4) {
                             data = data3.Table4;
                        }
                        else if (j == 5) {
                             data = data3.Table5;
                        }
                        else if (j == 6) {
                             data = data3.Table6;
                        }
                        else if (j == 7) {
                             data = data3.Table7;
                        }
                        else if (j ==8) {
                             data = data3.Table8;
                        }
                        else if (j == 9) {
                             data = data3.Table9;
                        }
                        else if (j == 10) {
                             data = data3.Table10;
                        }
                        else if (j == 11) {
                             data = data3.Table11;
                        }
                        else if (j == 12) {
                             data = data3.Table12;
                        }
                        else if (j == 13) {
                             data = data3.Table13;
                        }
                        else if (j == 14) {
                            data = data3.Table14;
                        }
                        else if (j == 15) {
                            data = data3.Table15;
                        }
                        else if (j == 16) {
                            data = data3.Table16;
                        }
                        else if (j == 17) {
                            data = data3.Table17;
                        }
                        else if (j == 18) {
                            data = data3.Table18;
                        }
                        else if (j == 19) {
                            data = data3.Table19;
                        }
                        
                        

                        if (data.length > 0) {

                            $("#div_ShowTimeLineTable_" + j + "").removeClass('hide');

                            // 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_" + j + "";
                            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 k = 0; k < col.length; k++) {
                                    var tabCell = tr.insertCell(-1);
                                    tabCell.innerHTML = data[i][col[k]];
                                }
                            }

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

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

                        }

                    }  

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