var Norecord = '<div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" style="height:65px; width:72px; border-radius:0px;"><br><div class="no_data_content">No data found.</div></div>';

$(document).ready(function () {
    if ($('#hiddenRole_id').val() != "1") {
        LoadPhysioAdvice();
    }
    else {
        toastr.warning("You have No rights to access this page.Please Contact the Administrator");
        window.location = "/Dashboard";
    }
});

function LoadPhysioAdvice() {
    $('#loaderimg').show();
    if ($('#app_date_current2').html() == 'Today') {
        var d = new Date();
        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();
        date = output;
    }

    else if ($('#app_date_current2').html() == 'Yesterday') {
        date = $('#hndyesterday').val();
    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        date = $('#hndtomorrow').val();
    }
    else {
        date = $('#app_date_current2').html();

    }
    var dates = date.split('/');
    date = dates[2] + '-' + dates[1] + '-' + dates[0];

    var Template = {
        excercise_goal_start_date: date,
        Dml_Indicator: 'PS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/ExcerciseGoalSet',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Template),
        success: function (data) {
            $('#physioAdviceTableBody').empty();
            if (jQuery.isEmptyObject((data))) {
                $('#physioAdviceTableBody').html(Norecord);
            }
            else {
                var data1 = JSON.parse(data);
                var tr = '';
                var Action = '';
                if (data1.length > 0) {
                    for (var i = 0; i < data1.length; i++) {
                        Action = '<a href="javascript:void(0)" class="btn btn-primaryuse btn-sm" data-toggle="modal" data-target="#" id="PhysioApp' + (i + 1) + '"onclick=\'BookPhysioAppointment(\"' + data1[i].first_name + '\",\"' + data1[i].phone_number_1 + '\",\"' + data1[i].patient_id + '\")\';>Book Physio</a>';
                        tr = tr + '<tr><td><span style="color: #999; font-size: 11px;">(' + data1[i].patient_id + ')</span></td><td>' + data1[i].visit_report_id + '</td><td>' + data1[i].first_name + '</td><td>' + data1[i].phone_number_1 + '</td><td>' + data1[i].excercise_description + '</td><td>' + data1[i].excercise_instructions + '</td><td>' + data1[i].created_on + '</td><td>' + data1[i].excercise_duration_per_day + '</td><td>' + Action + '</td></tr>';
                    }

                    $('#physioAdviceTableBody').html(tr);
                }
                else {
                    $('#physioAdviceTableBody').html(Norecord);
                }
            }
            $('#loaderimg').hide();
        }
    });
}

function BookPhysioAppointment(patient_name, phone_number, patient_id) {
    $('#BookPhysioIFrame').attr('src', '/DirectPatientEnroll/AppointmentSlot?patient_id=' + patient_id + '&PName=' + patient_name + '&Type=1');
    $('#bookAppointment').modal();
}

function closeIFrame() {
    $('#bookAppointment').modal('toggle');
}

function datefun() {
    var d = new Date();

    var month = d.getMonth() + 1;
    var day = d.getDate();

    var output = (day < 10 ? '0' : '') + day + '/' +
        (month < 10 ? '0' : '') + month + '/' +
        d.getFullYear();
    $('#hndtoday').val(output);

    //yesterday
    var d = new Date();

    var month = d.getMonth() + 1;
    var day = d.getDate() - 1;

    var output = (day < 10 ? '0' : '') + day + '/' +
        (month < 10 ? '0' : '') + month + '/' +
        d.getFullYear();
    $('#hndyesterday').val(output);

    //tomorrow

    var d = new Date();

    var month = d.getMonth() + 1;
    var day = d.getDate() + 1;

    var output = (day < 10 ? '0' : '') + day + '/' +
        (month < 10 ? '0' : '') + month + '/' +
        d.getFullYear();
    $('#hndtomorrow').val(output);
}



function Lableftarrow() {

    if ($('#app_date_current2').html() == 'Today') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);
    }
    else if ($('#app_date_current2').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);

    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);
    }


    var displaydate = $('#app_date_current2').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];
            $('#app_date_current2').html(date);

        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '30' + '/' + '11' + '/' + displaydates[2];
                $('#app_date_current2').html(date);

            }
            else if (displaydate == '01') {
                date = parseInt(displaydates[1]) + parseInt(1);
                var year = (displaydates[2]) - (1);
                date = '31' + '/' + '12' + '/' + year;
                $('#app_date_current2').html(date);

            }
            else if (displaydate == '03') {
                date = parseInt(displaydates[1]) + parseInt(1);
                date = '28' + '/' + '02' + '/' + displaydates[2];
                $('#app_date_current2').html(date);

            }
            else {
                date = displaydates[1] - 1;
                if (date < 10) {
                    date = '0' + date;
                }
                date = '30' + '/' + date + '/' + displaydates[2];
                $('#app_date_current2').html(date);

            }
        }

    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + date + '/' + displaydates[2];

            $('#app_date_current2').html(date);
        }
    }
    else {
        if (displaydates[0] > 1) {
            date = displaydates[0] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
        else {
            date = displaydates[1] - 1;
            if (date < 10) {
                date = '0' + date;
            }
            date = '31' + '/' + '01' + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
    }
    if (date == $('#hndtoday').val()) {

        $('#app_date_current2').html('Today');

    }
    if (date == $('#hndyesterday').val()) {

        $('#app_date_current2').html('Yesterday');

    }
    if (date == $('#hndtomorrow').val()) {

        $('#app_date_current2').html('Tomorrow');

    }
    LoadPhysioAdvice();
}


function Labrightarrow() {
    if ($('#app_date_current2').html() == 'Today') {


        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate();

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);

    }


    else if ($('#app_date_current2').html() == 'Yesterday') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() - 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);

    }
    else if ($('#app_date_current2').html() == 'Tomorrow') {
        var d = new Date();

        var month = d.getMonth() + 1;
        var day = d.getDate() + 1;

        var output = (day < 10 ? '0' : '') + day + '/' +
            (month < 10 ? '0' : '') + month + '/' +
            d.getFullYear();

        $('#app_date_current2').html(output);

    }
    var displaydate = $('#app_date_current2').html();
    var date = '';
    var month = '';
    var displaydates = displaydate.split("/");
    displaydate = displaydates[1];
    if (displaydate == '01' || displaydate == '03' || displaydate == '05' || displaydate == '07' || displaydate == '08' || displaydate == '10' || displaydate == '12') {
        if (displaydates[0] < 31) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];

            $('#app_date_current2').html(date);
        }
        else {
            if (displaydate == '12') {
                date = parseInt(displaydates[1]) + parseInt(1);
                year = parseInt(displaydates[2]) + parseInt(1);
                date = '01' + '/' + '01' + '/' + year;

                $('#app_date_current2').html(date);

            }
            else {
                date = parseInt(displaydates[1]) + parseInt(1);
                if (date < 10) {
                    date = '0' + date;
                }
                date = '01' + '/' + date + '/' + displaydates[2];

                $('#app_date_current2').html(date);

            }
        }
    }
    else if (displaydate == '04' || displaydate == '06' || displaydate == '09' || displaydate == '11') {
        if (displaydates[0] < 30) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
    }
    else {
        if (displaydates[0] < 28) {
            date = parseInt(displaydates[0]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = date + '/' + displaydates[1] + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
        else {
            date = parseInt(displaydates[1]) + parseInt(1);
            if (date < 10) {
                date = '0' + date;
            }
            date = '01' + '/' + date + '/' + displaydates[2];

            $('#app_date_current2').html(date);

        }
    }
    if ($('#app_date_current2').html() == $('#hndtoday').val()) {

        $('#app_date_current2').html('Today');

    }
    if ($('#app_date_current2').html() == $('#hndyesterday').val()) {

        $('#app_date_current2').html('Yesterday');

    }
    if ($('#app_date_current2').html() == $('#hndtomorrow').val()) {

        $('#app_date_current2').html('Tomorrow');

    }
    LoadPhysioAdvice();
}


$('#datepicker1').change(function () {
    var date = $('#datepicker1').val();
    $('#app_date_current2').html(date);

    LoadPhysioAdvice();
});