$(document).ready(function () {
   // var myTable;
   // 
    //BindItemTable();
   // selectFunction_old();
});

function selectFunction() {
    $.ajax({
        async: true,
        url: "/DirectPatientEnrollList/RegisterPatientSelect",
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',

        success: function (data) {
            
            var j = 0;
            var urcheck = $('#URight').val();
            var pid, view, total_loop, new_end_limit;
            var limit_val = 50;
            var start_limit = 0; end_limit = data.length, rectoincrease = limit_val;
            var hgg = 1;
           // console.log('total data----'+data.length);
            if (data.length > limit_val) {
                start_limit = 0; end_limit = limit_val;
                if (data.length / end_limit % 1 != 0) {
                    total_loop = '"' + data.length / end_limit + '"';
                    total_loop = total_loop.replace('"', '').split('.');
                    total_loop = Number(total_loop[0]) + Number(1);
                }
                else {
                    total_loop = data.length / end_limit;
                }

                for (k = 0; k < (total_loop); k++) {
                    if (k == 0) {
                     //   console.log('k0----------' + k + ',--------total_loop--------' + total_loop + '------start_limit---------' + start_limit + '------end_limit---------' + end_limit);
                        table_data_append(start_limit, end_limit);
                    }
                    else {
                        var last = k;

                        setTimeout(function (k, total_loop) {
                     //       
                        end_limit = new_end_limit;
                        start_limit = new_start_limit;
                        if (k == (total_loop - 1)) {
                            end_limit = data.length;
                       //     console.log('lastloop-----' + end_limit);
                        }
                         //   console.log('k----------' + k + ',--------total_loop--------' + total_loop + '------start_limit---------' + start_limit + '------end_limit---------' + end_limit);
                            table_data_append(start_limit, end_limit);
                        }, 200*k);
                    }
                }
            }
            else {
                table_data_append(start_limit, end_limit);
            }
            function table_data_append(start_limit, end_limit) {
               // 
               // console.log('functionenter----' + hgg);
              //  hgg++;
                for (var i = start_limit; i < end_limit; i++) {
                    j++;
                    if (urcheck == "1") {
                        pid = "<a href='/casestudy/CaseStudyView?patient_id=" + data[i].patient_id + "'>" + data[i].patient_id + "</a>";
                        view = "<a href='/DirectPatientEnroll/AppointmentSlot?patient_id=" + data[i].patient_id + "&PName=" + data[i].patient_first_name + "'>Book Appointment</a>";
                    }

                    if (urcheck == "2") {
                        pid = "<span>" + data[i].patient_id + "</span>";
                        view = "<span>No Action</span>";
                    }
                    row_data = [j, data[i].last_updated_on, data[i].patient_first_name, pid, data[i].patient_phone_number, data[i].patient_email_id, view];
                    $('#data_tablePatient').dataTable().fnAddData(row_data);
                    if (i == (end_limit - 1)) {
                        new_start_limit = end_limit;
                        new_end_limit = end_limit + rectoincrease;
                    }
                }
            }
        }
    });      
}

function selectFunctionbkp() {
    $.ajax({
        async: true,
        url: "/DirectPatientEnrollList/RegisterPatientSelect",
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',

        success: function (data) {
            var j = 0;

            tr = $('<table id="example1" class="table table-bordered table-striped text-center" />');
            var urcheck = $('#URight').val();
            var pid, view;
            tr.append("<thead><tr><th>S No</th><th>Date</th><th>Patient Name</th><th>Patient ID</th><th>Mobile Number</th><th>Email ID</th><th>Action</th></tr></thead><tbody>");

            $.each(data, function (i, emp) {
                j++;
                if (urcheck == "1") {
                    pid = "<a href='/casestudy/CaseStudyView?patient_id=" + emp.patient_id + "'>" + emp.patient_id + "</a>";
                    view = "<a href='/DirectPatientEnroll/AppointmentSlot?patient_id=" + emp.patient_id + "&PName=" + emp.patient_first_name + "'>Book Appointment</a>";
                }

                if (urcheck == "2") {
                    pid = "<span>" + emp.patient_id + "</span>";
                    view = "<span>No Action</span>";
                }
                row_data = "<tr><td>" + j + "</td><td>" + emp.last_updated_on + "</td><td>" + emp.patient_first_name + "</td><td>" + pid + "</td><td>" + emp.patient_phone_number + "</td><td>" + emp.patient_email_id + "</td><td>" + view + "</td></tr>";
                tr.append(row_data);
            });
            tr.append("</tbody></table>");

            $.when($('#data_tablePatient').append(tr)).then($('#example1').DataTable());


        }
    });
}

function selectFunction_try() {
    $.ajax({
        async: true,
        url: "/DirectPatientEnrollList/RegisterPatientSelect",
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',

        success: function (data) {
            var j = 0;

            tr = $('<table id="example1" class="table table-bordered table-striped text-center" />');
            var urcheck = $('#URight').val();
            var pid, view;
            tr.append("<thead><tr><th>S No</th><th>Date</th><th>Patient Name</th><th>Patient ID</th><th>Mobile Number</th><th>Email ID</th><th>Action</th></tr></thead><tbody>");

            $.each(data, function (i, emp) {
                j++;
                if (j <10) {

                    if (urcheck == "1") {
                        pid = "<a href='/casestudy/CaseStudyView?patient_id=" + emp.patient_id + "'>" + emp.patient_id + "</a>";
                        view = "<a href='/DirectPatientEnroll/AppointmentSlot?patient_id=" + emp.patient_id + "&PName=" + emp.patient_first_name + "'>Book Appointment</a>";
                    }

                    if (urcheck == "2") {
                        pid = "<span>" + emp.patient_id + "</span>";
                        view = "<span>No Action</span>";
                    }
                    row_data = "<tr><td>" + j + "</td><td>" + emp.last_updated_on + "</td><td>" + emp.patient_first_name + "</td><td>" + pid + "</td><td>" + emp.patient_phone_number + "</td><td>" + emp.patient_email_id + "</td><td>" + view + "</td></tr>";
                    tr.append(row_data);
                    $.when($('#data_tablePatient').append(tr)).then($('#example1').DataTable());
                }
                if (j>10) {
                    if (urcheck == "1") {
                        pid = "<a href='/casestudy/CaseStudyView?patient_id=" + emp.patient_id + "'>" + emp.patient_id + "</a>";
                        view = "<a href='/DirectPatientEnroll/AppointmentSlot?patient_id=" + emp.patient_id + "&PName=" + emp.patient_first_name + "'>Book Appointment</a>";
                    }

                    if (urcheck == "2") {
                        pid = "<span>" + emp.patient_id + "</span>";
                        view = "<span>No Action</span>";
                    }
                    row_data = "<tr><td>" + j + "</td><td>" + emp.last_updated_on + "</td><td>" + emp.patient_first_name + "</td><td>" + pid + "</td><td>" + emp.patient_phone_number + "</td><td>" + emp.patient_email_id + "</td><td>" + view + "</td></tr>";
                    tr.append(row_data);
                }
            });
            tr.append("</tbody></table>");

            $.when($('#data_tablePatient').append(tr)).then($('#example1').DataTable());


        }
    });
}

function selectFunction_newtry() {
    
    $.ajax({
        async: true,
        url: "/DirectPatientEnrollList/RegisterPatientSelect",
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',

        success: function (data) {
            var j = 0, row_datahtml = [];
            var urcheck = $('#URight').val();
            var pid, view, row_data='';
            //$.each(data, function (i, emp)
            for (var i = 0; i < data.length; i++) 
            {
                j++;
                if (urcheck == "1") {
                    pid = "<a href='/casestudy/CaseStudyView?patient_id=" + data[i].patient_id + "'>" + data[i].patient_id + "</a>";
                    view = "<a href='/DirectPatientEnroll/AppointmentSlot?patient_id=" + data[i].patient_id + "&PName=" + data[i].patient_first_name + "'>Book Appointment</a>";
                }

                if (urcheck == "2") {
                    pid = "<span>" + data[i].patient_id + "</span>";
                    view = "<span>No Action</span>";
                }
                row_datahtml.push("<tr><td>" + j + "</td><td>" + data[i].last_updated_on + "</td><td>" + data[i].patient_first_name + "</td><td>" + pid + "</td><td>" + data[i].patient_phone_number + "</td><td>" + data[i].patient_email_id + "</td><td>" + view + "</td></tr>");
               // $('#data_tablePatient').append('<tbody>' + row_data + '</tbody>');
            }
            $('#temp').append('<tbody>' + row_datahtml.join('') + '</tbody>').DataTable();

             //tr.append("</tbody></table>");

             //$.when($('#data_tablePatient').append(tr)).then($('#example1').DataTable());


        }
    });
}



































