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


function LoadVisitHistory()
{
    
    var pid = $('#Hdnpatient_id').val();
    var patient_id;
    if (pid == null || pid == "")
    {
        patient_id = '0';
    }
    else
    {
        patient_id = pid;
    }

    

    var visithistory = {
        patient_id: patient_id
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/PatientVisitHistory/select_PatientVisitHistory',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(visithistory),
        success: function (data) {
            //alert(data);
            if (data.length > 2) {

                var data1 = JSON.parse(data);
                var j = 1;
                var z = 1;
                var x = 1;
                $.each(data1, function (i, emp) {

                    var visitdate = emp.visite_date;

                    var visit_id = emp.visit_report_id

                    var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

                    var date = new Date(visitdate);
                    var day = date.getDate();
                    var month1 = date.getMonth();


                    var month = m_names[month1];

                    var suggestion_date = emp.Suggestion_date;
                    var sdate = new Date(suggestion_date);
                    var sug_date = sdate.getDate() + " " + m_names[sdate.getMonth()] + " " + sdate.getFullYear();





                    //alert(sug_date);



                

                    if (j % 2) {
                        tr = $('<li />');
                        tr.append('<div class="timeline-badge">' + day + " " + month + '</div><div class="timeline-panel"><div class="timeline-heading"><h4 class="timeline-title">Followup list</h4><p> ' + emp.visit_report_id + '</p><div> ' + sug_date + '</div></div><div class="timeline-body"><div class="visit-h"><table class="message-user" style="color:#333;"><tbody><tr><td style="width:100px;"><img src="http://placehold.it/80" class="img-circle img-responsive" alt="" style="border-radius:50%; max-width:100%; width:100%;"></td><td><p>' + emp.patient_id + '</p><p>' + emp.patient_first_name + '</p></td></tr></tbody></table><ul class="message-menu" style="color:#333;"><li><a href="#" class="medical-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Medication  <span class="badge pull-right">' + emp.medicine_prescription_count + '</span></a></li><li><a href="#" class="lab-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Lab tests <span class="badge  pull-right">' + emp.lab_record_count + '</span></a></li></ul><div class="padding-wh"><a href="/PatientDashboard/PPDF?visitreportid=' + emp.visit_report_id + '" Target="_blank" " class="text-left">Print</a><a href="#" class="text-right pull-right" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true"> attachments</a></div></div></div></div>');
                        $('#visit-history').append(tr);

                    }
                    else {
                        tr = $('<li class="timeline-inverted" />');
                        tr.append('<div class="timeline-badge warning">' + day + " " + month + '</div><div class="timeline-panel"><div class="timeline-heading"><h4 class="timeline-title">Followup list</h4><p> ' + emp.visit_report_id + '</p><div> ' + sug_date + '</div></div><div class="timeline-body"><div class="visit-h"><table class="message-user" style="color:#333;"><tbody><tr><td style="width:100px;"><img src="http://placehold.it/80" class="img-circle img-responsive" alt="" style="border-radius:50%; max-width:100%; width:100%;"></td><td><p>' + emp.patient_id + '</p><p>' + emp.patient_first_name + '</p></td></tr></tbody></table><ul class="message-menu" style="color:#333;"><li><a href="#" class="medical-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Medication  <span class="badge pull-right">' + emp.medicine_prescription_count + '</span></a></li><li><a href="#" class="lab-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Lab tests <span class="badge  pull-right">' + emp.lab_record_count + '</span></a></li></ul><div class="padding-wh"><a href="/PatientDashboard/PPDF?visitreportid=' + emp.visit_report_id + '" Target="_blank" " class="text-left">Print</a><a href="#" class="text-right pull-right" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true"> attachments</a></div></div></div></div>');
                        $('#visit-history').append(tr);
                    }

                    tr = $('<div class="modal fade" id="pres-lab_' + j + '" tabindex="-1" />');
                    tr.append('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h4 class="modal-title">' + emp.patient_first_name + ' <span style="font-size:12px;">' + emp.visit_report_id + ' <i>' + sug_date + '</i></span> </h4></div><div class="modal-body"><div class="nav-tabs-custom"><ul class="nav nav-tabs"><li class="active"><a href="#personal_' + j + '" data-toggle="tab" aria-expanded="true" class="medical-d">Medication </a></li><li class=""><a href="#medical_' + j + '" data-toggle="tab" aria-expanded="true" class="lab-d">Lab tests</a></li></ul><div class="tab-content "><div class="tab-pane active" id="personal_' + j + '"><table class="table table-bordered table-striped text-center"><thead><tr><td>Medicine</td><td>When to eat </td><td>Dosage </td><td>Days </td><td>M</td><td>A</td><td>N</td></tr></thead><tbody id="medicine_' + emp.visit_report_id + '"></tbody></table></div><div class="tab-pane " id="medical_' + j + '" style="padding:10px;"><table class="table table-bordered table-striped"><thead><tr><td class="text-bold">Lab tests</td></tr></thead><tbody ><tr><td id="lab_' + emp.visit_report_id + '"></td></tr></tbody></table></div></div></div></div><div class="modal-footer"></div></div></div>');
                    $('#modal-popup').append(tr);


                    var visitid = {
                        visit_report_id: visit_id
                    }


                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/PatientVisitHistory/select_Patient_Medicine_Prescription',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(visitid),
                        success: function (data) {
                            //alert(data);


                            var data1 = JSON.parse(data);

                            var morning, afternoon, night, food;
                            //alert(z);
                            $.each(data1, function (i, emp) {

                                if (emp["morning"] == null || emp["morning"] == "") {
                                    morning = '<i class="fa fa-circle"></i>';
                                }
                                else {
                                    morning = '<i class="fa fa-circle text-primary"></i>';
                                }
                                //alert(morning);
                                if (emp["afternoon"] == null || emp["afternoon"] == "") {
                                    afternoon = '<i class="fa fa-circle"></i>';
                                }
                                else {
                                    afternoon = '<i class="fa fa-circle text-primary"></i>';
                                }

                                if (emp["night"] == null || emp["night"] == "") {
                                    night = '<i class="fa fa-circle"></i>';
                                }
                                else {
                                    night = '<i class="fa fa-circle text-primary"></i>';
                                }

                                if (emp["how_to_take"] == "1") {
                                    food = "Post Food";
                                }
                                else {
                                    food = "Pre Food";
                                }

                                tr = $('<tr/>');
                                tr.append('<td>' + emp.medicine_name + '</td><td>' + food + '</td><td>' + emp.medicine_dosage + '</td><td>' + emp.days_to_take + '</td><td>' + morning + '</td><td>' + afternoon + '</td><td>' + night + '</td>');
                                //var a = "#medicine_"+j;

                                $("#medicine_" + emp.visit_report_id).append(tr);
                            });
                            z++;



                        }

                    });


                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/PatientVisitHistory/select_Patient_Lab_Record',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(visitid),
                        success: function (data) {
                            //alert(data);


                            var data1 = JSON.parse(data);

                            $.each(data1, function (i, emp) {
                                var test_name = emp.lab_prescription;
                                var test_type;
                                var lab_test = emp.lab_type;
                                if (lab_test == '1') {
                                    test_type = "Lab";
                                }
                                else if (lab_test == '2') {
                                    test_type = "Scan";
                                }

                                else if (lab_test == '3') {
                                    test_type = "X-Ray";
                                }
                                var test_id = emp.prescription_scan_id;
                                var file="";

                                var prescription = {
                                    prescription_scan_id: test_id
                                }

                                $.ajax({
                                    async: true,
                                    type: 'POST',
                                    url: '/PatientVisitHistory/select_Patient_Lab_Prescription_Record',
                                    contentType: 'application/json; charset=UTF-8',
                                    data: JSON.stringify(prescription),
                                    success: function (data) {
                                        //alert(data);


                                        var data1 = JSON.parse(data);
                                        var pdf = $('#HdnLadRecordPath').val();
                                        
                                        $.each(data1, function (i, emp) {
                                            file += '<a href="' + pdf + '" target="_blank" class="btn btn btn-primary">'+emp.patient_lab_record_path+'</a>';
                                        });

                                        //alert(file);
                                        tr = $('<table class="table table-bordered " />');
                                        tr.append('<tr><td>Test Name</td><td>' + test_name + '</td></tr><tr><td>Test Type</td><td>' + test_type + '</td></tr><tr><td>Files</td><td>' + file + '</td></tr>');
                                        $('#lab_' + emp.visit_report_id).append(tr);
                                    }
                                });
                                
                            });
                            x++;
                        }
                    });



                    j++;

                });
            }
            else
            {
                
                tr = $('<tr/>');
                tr.append('<img src="/Content/img/NoRecordFound.jpg">');
                $('#visit-history').append(tr);
            }
        }
    });
}

function pdf(id) {
    alert(id);

}
//onclick = pdf(' + emp.visit_report_id + ')


$('#VisitHistory_Filter').click(function () {
    var sdate = $('#datepicker').val();
    var edate = $('#datepicker1').val();

    if ($.trim($('#datepicker').val()) == "") {
        $('#Pdatepicker').show();
        $('#Pdatepicker').fadeOut(8000);
        $('#Pdatepicker').html('Select From date');
        $('#datepicker').focus();
        $('#datepicker').val('');
        return false;
    }
    if ($.trim($('#datepicker1').val()) == "") {
        $('#Pdatepicker1').show();
        $('#Pdatepicker1').fadeOut(8000);
        $('#Pdatepicker1').html('Select To date');
        $('#datepicker1').focus();
        $('#datepicker1').val('');
        return false;
    }
    if(sdate.length > 0 && edate.length > 0)
    {

        var s_date = sdate.split('/');
        var e_date = edate.split('/');

        var startdate = new Date(s_date[1] + "/" + s_date[0] + "/" + s_date[2]);
        var enddate = new Date(e_date[1] + "/" + e_date[0] + "/" + e_date[2]);

        

        if(startdate <= enddate)
        {
            //alert(startdate + "-" + enddate);


            var pid = $('#Hdnpatient_id').val();
            var patient_id;
            if (pid == null || pid == "") {
                patient_id = '0';
            }
            else {
                patient_id = pid;
            }

            var visithistory = {
                patient_id: patient_id,
                s_date: s_date[2] + "-" + s_date[1] + "-" + s_date[0],
                e_date: e_date[2] + "-" + e_date[1] + "-" + e_date[0]
            }

            $.ajax({
                async: true,
                type: 'POST',
                url: '/PatientVisitHistory/select_PatientVisitHistory_Filter',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(visithistory),
                success: function (data) {
                    //alert(data);

                    if (data.length > 2) {
                        var data1 = JSON.parse(data);
                        var j = 1;
                        var z = 1;
                        var x = 1;

                        $('#modal-popup').empty();
                        $('#visit-history').empty();

                        $.each(data1, function (i, emp) {

                            var visitdate = emp.visite_date;

                            var visit_id = emp.visit_report_id

                            var m_names = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

                            var date = new Date(visitdate);
                            var day = date.getDate();
                            var month1 = date.getMonth();


                            var month = m_names[month1];

                            var suggestion_date = emp.Suggestion_date;
                            var sdate = new Date(suggestion_date);
                            var sug_date = sdate.getDate() + " " + m_names[sdate.getMonth()] + " " + sdate.getFullYear();





                            //alert(sug_date);



                        

                            if (j % 2) {
                                tr = $('<li />');
                                tr.append('<div class="timeline-badge">' + day + " " + month + '</div><div class="timeline-panel"><div class="timeline-heading"><h4 class="timeline-title">Followup list</h4><p> ' + emp.visit_report_id + '</p><div> ' + sug_date + '</div></div><div class="timeline-body"><div class="visit-h"><table class="message-user" style="color:#333;"><tbody><tr><td style="width:100px;"><img src="http://placehold.it/80" class="img-circle img-responsive" alt="" style="border-radius:50%; max-width:100%; width:100%;"></td><td><p>' + emp.patient_id + '</p><p>' + emp.patient_first_name + '</p></td></tr></tbody></table><ul class="message-menu" style="color:#333;"><li><a href="#" class="medical-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Medication  <span class="badge pull-right">' + emp.medicine_prescription_count + '</span></a></li><li><a href="#" class="lab-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Lab tests <span class="badge  pull-right">' + emp.lab_record_count + '</span></a></li></ul><div class="padding-wh"><a href="/PatientDashboard/PPDF?visitreportid=' + emp.visit_report_id + '" Target="_blank" " class="text-left">Print</a><a href="#" class="text-right pull-right" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">attachments</a></div></div></div></div>');
                                $('#visit-history').append(tr);

                        }
                        else {
                            tr = $('<li class="timeline-inverted" />');
                            tr.append('<div class="timeline-badge warning">' + day + " " + month + '</div><div class="timeline-panel"><div class="timeline-heading"><h4 class="timeline-title">Followup list</h4><p> ' + emp.visit_report_id + '</p><div> ' + sug_date + '</div></div><div class="timeline-body"><div class="visit-h"><table class="message-user" style="color:#333;"><tbody><tr><td style="width:100px;"><img src="http://placehold.it/80" class="img-circle img-responsive" alt="" style="border-radius:50%; max-width:100%; width:100%;"></td><td><p>' + emp.patient_id + '</p><p>' + emp.patient_first_name + '</p></td></tr></tbody></table><ul class="message-menu" style="color:#333;"><li><a href="#" class="medical-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Medication  <span class="badge pull-right">' + emp.medicine_prescription_count + '</span></a></li><li><a href="#" class="lab-t" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true">Lab tests <span class="badge  pull-right">' + emp.lab_record_count + '</span></a></li></ul><div class="padding-wh"><a href="/PatientDashboard/PPDF?visitreportid=' + emp.visit_report_id + '" Target="_blank" " class="text-left">Print</a><a href="#" class="text-right pull-right" data-toggle="modal" data-target="#pres-lab_' + j + '" data-backdrop="static" data-keyboard="true"> attachments</a></div></div></div></div>');
                            $('#visit-history').append(tr);
                        }

                            tr = $('<div class="modal fade" id="pres-lab_' + j + '" tabindex="-1" />');
                            tr.append('<div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button><h4 class="modal-title">' + emp.patient_first_name + ' <span style="font-size:12px;">' + emp.visit_report_id + ' <i>' + sug_date + '</i></span> </h4></div><div class="modal-body"><div class="nav-tabs-custom"><ul class="nav nav-tabs"><li class="active"><a href="#personal_' + j + '" data-toggle="tab" aria-expanded="true" class="medical-d">Medication </a></li><li class=""><a href="#medical_' + j + '" data-toggle="tab" aria-expanded="true" class="lab-d">Lab tests</a></li></ul><div class="tab-content "><div class="tab-pane active" id="personal_' + j + '"><table class="table table-bordered table-striped text-center"><thead><tr><td>Medicine</td><td>When to eat </td>><td>Dosage </td><td>Days </td><td>M</td><td>A</td><td>N</td></tr></thead><tbody id="medicine_' + emp.visit_report_id + '"></tbody></table></div><div class="tab-pane " id="medical_' + j + '" style="padding:10px;"><table class="table table-bordered table-striped"><thead><tr><td class="text-bold">Lab tests</td></tr></thead><tbody ><tr><td id="lab_' + emp.visit_report_id + '"></td></tr></tbody></table></div></div></div></div><div class="modal-footer"></div></div></div>');
                            $('#modal-popup').append(tr);


                            var visitid = {
                                visit_report_id: visit_id
                            }



                            $.ajax({
                                async: true,
                                type: 'POST',
                                url: '/PatientVisitHistory/select_Patient_Medicine_Prescription',
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(visitid),
                                success: function (data) {
                                    //alert(data);


                                    var data1 = JSON.parse(data);

                                    var morning, afternoon, night, food;
                                    //alert(z);
                                    $.each(data1, function (i, emp) {

                                        if (emp["morning"] == null || emp["morning"] == "") {
                                            morning = '<i class="fa fa-circle"></i>';
                                        }
                                        else {
                                            morning = '<i class="fa fa-circle text-primary"></i>';
                                        }
                                        //alert(morning);
                                        if (emp["afternoon"] == null || emp["afternoon"] == "") {
                                            afternoon = '<i class="fa fa-circle"></i>';
                                        }
                                        else {
                                            afternoon = '<i class="fa fa-circle text-primary"></i>';
                                        }

                                        if (emp["night"] == null || emp["night"] == "") {
                                            night = '<i class="fa fa-circle"></i>';
                                        }
                                        else {
                                            night = '<i class="fa fa-circle text-primary"></i>';
                                        }

                                        if (emp["how_to_take"] == "1") {
                                            food = "Post Food";
                                        }
                                        else {
                                            food = "Pre Food";
                                        }

                                        tr = $('<tr/>');
                                        tr.append('<td>' + emp.medicine_name + '</td><td>' + food + '</td><td>' + emp.medicine_dosage + '</td><td>' + emp.days_to_take + '</td><td>' + morning + '</td><td>' + afternoon + '</td><td>' + night + '</td>');
                                        //var a = "#medicine_"+j;

                                        $("#medicine_" + emp.visit_report_id).append(tr);
                                    });
                                    z++;



                                }

                            });


                            $.ajax({
                                async: true,
                                type: 'POST',
                                url: '/PatientVisitHistory/select_Patient_Lab_Record',
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(visitid),
                                success: function (data) {
                                    //alert(data);


                                    var data1 = JSON.parse(data);

                                    $.each(data1, function (i, emp) {
                                        var test_name = emp.lab_prescription;
                                        var test_type;
                                        var lab_test = emp.lab_type;
                                        if (lab_test == '1')
                                        {
                                            test_type = "Lab";
                                        }
                                        else if (lab_test == '2')
                                        {
                                            test_type = "Scan";
                                        }

                                        else if (lab_test == '3') {
                                            test_type = "X-Ray";
                                        }

                                        var test_id = emp.prescription_scan_id;
                                        var file = "";

                                        var prescription = {
                                            prescription_scan_id: test_id
                                        }

                                        $.ajax({
                                            async: true,
                                            type: 'POST',
                                            url: '/PatientVisitHistory/select_Patient_Lab_Prescription_Record',
                                            contentType: 'application/json; charset=UTF-8',
                                            data: JSON.stringify(prescription),
                                            success: function (data) {
                                                //alert(data);


                                                var data1 = JSON.parse(data);
                                                var pdf = $('#HdnLadRecordPath').val();

                                                $.each(data1, function (i, emp) {
                                                    file += '<a href="' + pdf + '" target="_blank" class="btn btn btn-primary">' + emp.patient_lab_record_path + '</a>';
                                                });

                                                //alert(file);
                                                tr = $('<table class="table table-bordered" />');
                                                tr.append('<tr><td>Test Name</td><td>' + test_name + '</td></tr><tr><td>Test Type</td><td>' + test_type + '</td></tr><tr><td>Files</td><td>' + file + '</td></tr>');
                                                $('#lab_' + emp.visit_report_id).append(tr);
                                            }
                                        });

                                    });
                                    x++;
                                }
                            });



                            j++;

                        });
                    }
                    else
                    {
                        $('#visit-history').empty();
                        tr = $('<tr/>');
                        tr.append('<img src="/Content/img/NoRecordFound.jpg">');
                        $('#visit-history').append(tr);
                    }
                }
            });



            //alert("success");
        }
        else
        {
            toastr.warning("Date Mis-match");
        }
    }
    else
    {
        toastr.warning("Select the Date");
    }
})