$(window).on('load', function() {
    $("#page-loader").fadeOut(500);
});
$(document).on('click','#login-btn', function() {
    event.preventDefault();
    $.ajax({
        url: '/controllers/auth.php',
        type: 'POST',
        data: $('#login-form').serialize(), 
        success: function(data) {
            if(data.success == true) {
                window.location.href = '/';
            } else {
                $('#login-error').html('');
                $('#login-error').html(data.msg);
                $('#login-error').addClass('show-item');
            }
        },
    });
});

$(document).on('click','#logout', function() {
    event.preventDefault();
    $.ajax({
        url: '/controllers/logout.php',
        type: 'POST',
        success: function(data) {
            if(data.success == true) {
                window.location.href = '/login.php';
            } else {
                console.log(data.msg);
            }
        },
    });
});

$(document).on('click','#add_child_btn', function() {
    event.preventDefault();
    $.ajax({
        url: '/controllers/add_child_controller.php',
        type: 'POST',
        data: $('#add_child_form').serialize(), 
        success: function(data) {
            if(data.success == true) {
                window.location.href = '/index.php?page=home';
            } else {
                console.log(data.msg);
            }
        },
    });
});

$(document).on('click','#edit_child_btn', function() {
    event.preventDefault();
    $.ajax({
        url: '/controllers/edit_child_controller.php',
        type: 'POST',
        data: $('#edit_child_form').serialize(), 
        success: function(data) {
            if(data.success == true) {
                window.location.href = '/index.php';
            } else {
                console.log(data.msg);
            }
        },
    });
});

$(document).on('click','.btn-n', function() {
    $('.modal').fadeOut(100);
    $('#backdrop').fadeOut(250);
    $('.p-message').fadeOut(100);
    $('#preview_html').fadeOut(100);
});
$(document).on('click','.ci-btn', function() {
    event.preventDefault();
    $('.modal').fadeIn(250);
    $('#backdrop').fadeIn(100);
    var id = $(this).attr('child-id');
    $('#child_id_modal_input').val(id);
});

$(document).on('click','.preview-btn', function() {
    event.preventDefault();
    var id = $('#child_id_modal_input').val();
    var date = $('#mail_date').val();
    var generate = 0;
    $.ajax({
        url: '/controllers/make_invoice.php',
        type: 'POST',
        data:  { child_id: id, date: date, generate: generate },
        success: function(data) {
            if(data) 
            {
                $('.p-message').fadeIn(100);
                $('#preview_html').fadeIn(100);
                $('#preview_html').html(data);
            }
        },
    });
});

$(document).on('click','.btn-send', function() {
    event.preventDefault();

    $('body').css('pointer-events', 'none');
    var html = $(this).html();
    $(this).html('Sending...');
    $(this).append('<div class="loader-icn"></div>');
    $(this).addClass('flex-btn');
    var button_this = $(this);


    var id = $('#child_id_modal_input').val();
    var date = $('#mail_date').val();
    var generate = 1;
    $.ajax({
        url: '/controllers/make_invoice.php',
        type: 'POST',
        data:  { child_id: id, date: date, generate: generate },
        success: function(resp) {
            resp = JSON.parse(resp);
            console.log(resp.success);
            if(resp.success) {
                $('body').css('pointer-events', 'auto');
                $(button_this).html(''); 
                $(button_this).html(html);
                $(button_this).removeClass('flex-btn');
                $('.modal').fadeOut(100);
                $('#backdrop').fadeOut(250);
            }
            else
            {
                alert(resp.msg);
                $('body').css('pointer-events', 'auto');
                $(button_this).html(''); 
                $(button_this).html(html);
                $(button_this).removeClass('flex-btn');
                $('.modal').fadeOut(100);
                $('#backdrop').fadeOut(250);
            }

        },
        error: function(data) {
            $('body').css('pointer-events', 'auto');
            $(this).html(''); 
            $(this).html(html);
            $(this).removeClass('flex-btn');
        }
    });
});

$(document).on('keyup','#user_email, #user_password', function() {
    $('#login-error').removeClass('show-item');
    $('#login-error').html('&nbsp;');
});

$(document).on('click','.action-btn', function() {
    if(!$(this).hasClass('show')) {
        $(this).addClass('show');
    }
    else {
        $(this).removeClass('show');
    }
});

$(document).ready(function() {
   if($('#regular').attr('checked')) 
   {
       $('.regular-options').show();
       $('.drop-in-options').hide();
   }
   else
   {
       if($('#drop-in').attr('checked'))
       {
              $('.drop-in-options').show();
              $('.regular-options').hide();
       }
   }

    var extra_days = new MaterialDatepicker('#extra_days', {
        orientation: "portrait",
        theme: "light",
        color: "#7F56DA",
        outputFormat: 'MM-DD-YYYY',
        inputFormat: 'MM-DD-YYYY'
    });

    var days_attended = new MaterialDatepicker('#days_attended', {
        orientation: "portrait",
        theme: "light",
        color: "#7F56DA",
        outputFormat: 'MM-DD-YYYY',
        inputFormat: 'MM-DD-YYYY'
    });

    var extra_hours = new MaterialDatepicker('#extra_hours_date', {
        orientation: "portrait",
        theme: "light",
        color: "#7F56DA",
        outputFormat: 'MM-DD-YYYY',
        inputFormat: 'MM-DD-YYYY'
    });

    var mail_date = new MaterialDatepicker('#mail_date', {
        orientation: "portrait",
        theme: "light",
        color: "#7F56DA",
        outputFormat: 'MM-DD-YYYY',
        inputFormat: 'MM-DD-YYYY',
        zIndex: 10005,
    });
$(document).on('click','#mail_date', function() {
    var h = $('#mail_date').innerHeight();
    var pos = $('#mail_date').offset();
    var toop = pos.top.toFixed(1);
    var offset = parseFloat(h.toFixed(1));
    var toop2 = parseFloat(toop) + offset;
    var leeft = pos.left.toFixed(1);
    
    toop2 = toop2 + "px";
    leeft = leeft + "px";

    $('.mp-picker').css('top',toop2);
    $('.mp-picker').css('left',leeft);
});
    if($('#extra_hours_div').length)
    {
        addHours_init();
    }
    
    if($('#extra_days_div').length)
    {
        addDays_init();
    }

    if($('#days_attended_div').length)
    {
        addDaysAttended_init();
    }

});

$(document).on('click','#regular', function() {
    $('.regular-options').show();
    $('.drop-in-options').hide();
});
$(document).on('click','#drop-in', function() {
    $('.drop-in-options').show();
    $('.regular-options').hide();
});

function addDays_init() 
{
    var extra_days_json = $('#extra_days_json').val();
    var extra_days_json_obj = JSON.parse(extra_days_json);
    var extra_days_div = $('#extra_days_div');
    var extra_days_div_html = '';
    extra_days_div.html('');
    if(extra_days_json_obj.length >0)
    {
        $('#extra_days_div').removeClass('empty-dates')
    }
    else
    {
        $('#extra_days_div').addClass('empty-dates')
    }
    for(var i=0; i<extra_days_json_obj.length; i++)
    {
        var day = extra_days_json_obj[i].day;
        var day_arr = day.split('-');
        var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

        const d = new Date(day_new);
        var day = moment(d).format('ddd, Do MMM YYYY');


        extra_days_div_html += '<div days_id="'+i+'" class="date_wrapper">';
        extra_days_div_html += '<div class="days_text">';
        extra_days_div_html +=  day+ ' - $' + extra_days_json_obj[i].rate;
        extra_days_div_html += '</div>';
        extra_days_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
        extra_days_div_html += '<i class="bi bi-x"></i>';
        extra_days_div_html += '</div>';
        extra_days_div_html += '</div>';
    }
    extra_days_div.append(extra_days_div_html);
}
function addDays() 
{
    event.preventDefault();
    var extra_days_val = $('#extra_days').val();
    var daily_rate_val = $('#daily_rate').val();
    if(extra_days_val && daily_rate_val)    
    {
        var extra_days_json = $('#extra_days_json').val();
        var extra_days_json_obj = JSON.parse(extra_days_json);
        extra_days_json_obj.push({'day':extra_days_val, 'rate':daily_rate_val});
        var extra_days_json_str = JSON.stringify(extra_days_json_obj);
        $('#extra_days_json').val(extra_days_json_str);
        var extra_days_div = $('#extra_days_div');
        var extra_days_div_html = '';
        extra_days_div.html('');
        if(extra_days_json_obj.length >0)
        {
            $('#extra_days_div').removeClass('empty-dates')
        }
        else
        {
            $('#extra_days_div').addClass('empty-dates')
        }
        for(var i=0; i<extra_days_json_obj.length; i++)
        {
            var day = extra_days_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new)
            var day = moment(d).format('ddd, Do MMM YYYY');

            extra_days_div_html += '<div days_id="'+i+'" class="date_wrapper">';
            extra_days_div_html += '<div class="days_text">';
            extra_days_div_html += day + ' - $' + extra_days_json_obj[i].rate;
            extra_days_div_html += '</div>';
            extra_days_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
            extra_days_div_html += '<i class="bi bi-x"></i>';
            extra_days_div_html += '</div>';
            extra_days_div_html += '</div>';
        }
        extra_days_div.append(extra_days_div_html);
    }
}
function deleteDays(index) {
    //remove index from extra_days_json
    var extra_days_json = $('#extra_days_json').val();
    var extra_days_json_obj = JSON.parse(extra_days_json);
    extra_days_json_obj.splice(index, 1);
    var extra_days_json_str = JSON.stringify(extra_days_json_obj);
    $('#extra_days_json').val(extra_days_json_str);
    
    var extra_days_div = $('#extra_days_div');
        var extra_days_div_html = '';
        extra_days_div.html('');
        if(extra_days_json_obj.length >0)
        {
            $('#extra_days_div').removeClass('empty-dates')
        }
        else
        {
            $('#extra_days_div').addClass('empty-dates')
        }
        for(var i=0; i<extra_days_json_obj.length; i++)
        {

            var day = extra_days_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new);
            var day = moment(d).format('ddd, Do MMM YYYY');

            extra_days_div_html += '<div days_id="'+i+'" class="date_wrapper">';
            extra_days_div_html += '<div class="days_text">';
            extra_days_div_html += day + ' - $' + extra_days_json_obj[i].rate;
            extra_days_div_html += '</div>';
            extra_days_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
            extra_days_div_html += '<i class="bi bi-x"></i>';
            extra_days_div_html += '</div>';
            extra_days_div_html += '</div>';
        }
        extra_days_div.append(extra_days_div_html);
}
function addHours_init() 
{
    var extra_hours_json = $('#extra_hours_json').val();
    var extra_hours_json_obj = JSON.parse(extra_hours_json);
    var extra_hours_div = $('#extra_hours_div');
    var extra_hours_div_html = '';
    extra_hours_div.html('');
    if(extra_hours_json_obj.length >0)
    {
        $('#extra_hours_div').removeClass('empty-dates')
    }
    else
    {
        $('#extra_hours_div').addClass('empty-dates')
    }
    for(var i=0; i<extra_hours_json_obj.length; i++)
    {
        var day = extra_hours_json_obj[i].day;
        var day_arr = day.split('-');
        var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

        const d = new Date(day_new);
        var day = moment(d).format('ddd, Do MMM YYYY');

        extra_hours_div_html += '<div hours_id="'+i+'" class="hour_wrapper">';
        extra_hours_div_html += '<div class="hours_text">';
        extra_hours_div_html += day + ' - ' + extra_hours_json_obj[i].hours + 'hr(s) - $' + extra_hours_json_obj[i].rate;
        extra_hours_div_html += '</div>';
        extra_hours_div_html += '<div onclick="deleteHours('+i+')" class="delete_hours">';
        extra_hours_div_html += '<i class="bi bi-x"></i>';
        extra_hours_div_html += '</div>';
        extra_hours_div_html += '</div>';
    }
    extra_hours_div.append(extra_hours_div_html);
}
function addHours() 
{
    event.preventDefault();
    var extra_hours_date_val = $('#extra_hours_date').val();
    var extra_hours_val = $('#extra_hours').val();
    var hourly_rate_val = $('#hourly_rate').val();
    if(extra_hours_val && hourly_rate_val && extra_hours_date_val)
    {
        var extra_hours_json = $('#extra_hours_json').val();
        var extra_hours_json_obj = JSON.parse(extra_hours_json);
        extra_hours_json_obj.push({'day':extra_hours_date_val, 'hours':extra_hours_val, 'rate':hourly_rate_val});
        var extra_hours_json_str = JSON.stringify(extra_hours_json_obj);
        $('#extra_hours_json').val(extra_hours_json_str);
        var extra_hours_div = $('#extra_hours_div');
        var extra_hours_div_html = '';
        extra_hours_div.html('');
        if(extra_hours_json_obj.length >0)
        {
            $('#extra_hours_div').removeClass('empty-dates')
        }
        else
        {
            $('#extra_hours_div').addClass('empty-dates')
        }
        for(var i=0; i<extra_hours_json_obj.length; i++)
        {
            var day = extra_hours_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new);
            var day = moment(d).format('ddd, Do MMM YYYY');

            extra_hours_div_html += '<div hours_id="'+i+'" class="hour_wrapper">';
            extra_hours_div_html += '<div class="hours_text">';
            extra_hours_div_html += day + ' - ' + extra_hours_json_obj[i].hours + 'hr(s) - $' + extra_hours_json_obj[i].rate;
            extra_hours_div_html += '</div>';
            extra_hours_div_html += '<div onclick="deleteHours('+i+')" class="delete_hours">';
            extra_hours_div_html += '<i class="bi bi-x"></i>';
            extra_hours_div_html += '</div>';
            extra_hours_div_html += '</div>';
        }
        extra_hours_div.append(extra_hours_div_html);
    }
}
function deleteHours(index) {
    //remove index from extra_days_json
    var extra_hours_json = $('#extra_hours_json').val();
    var extra_hours_json_obj = JSON.parse(extra_hours_json);
    extra_hours_json_obj.splice(index, 1);
    var extra_hours_json_str = JSON.stringify(extra_hours_json_obj);
    $('#extra_hours_json').val(extra_hours_json_str);
    
    var extra_hours_div = $('#extra_hours_div');
        var extra_hours_div_html = '';
        extra_hours_div.html('');
        if(extra_hours_json_obj.length >0)
        {
            $('#extra_hours_div').removeClass('empty-dates')
        }
        else
        {
            $('#extra_hours_div').addClass('empty-dates')
        }
        for(var i=0; i<extra_hours_json_obj.length; i++)
        {
            var day = extra_hours_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new);
            var day = moment(d).format('ddd, Do MMM YYYY');

            extra_hours_div_html += '<div hours_id="'+i+'" class="hour_wrapper">';
            extra_hours_div_html += '<div class="hours_text">';
            extra_hours_div_html += day + ' - ' + extra_hours_json_obj[i].hours + 'hr(s) - $' + extra_hours_json_obj[i].rate;
            extra_hours_div_html += '</div>';
            extra_hours_div_html += '<div onclick="deleteHours('+i+')" class="delete_hours">';
            extra_hours_div_html += '<i class="bi bi-x"></i>';
            extra_hours_div_html += '</div>';
            extra_hours_div_html += '</div>';
        }
        extra_hours_div.append(extra_hours_div_html);
}
function addDaysAttended_init() 
{
    var days_attended_json = $('#days_attended_json').val();
    var days_attended_json_obj = JSON.parse(days_attended_json);
    var days_attended_div = $('#days_attended_div');
    var days_attended_div_html = '';
    days_attended_div.html('');
    if(days_attended_json_obj.length >0)
    {
        $('#days_attended_div').removeClass('empty-dates')
    }
    else
    {
        $('#days_attended_div').addClass('empty-dates')
    }
    for(var i=0; i<days_attended_json_obj.length; i++)
    {
        var day = days_attended_json_obj[i].day;
        var day_arr = day.split('-');
        var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

        const d = new Date(day_new);
        var day = moment(d).format('ddd, Do MMM YYYY');

        days_attended_div_html += '<div days_id="'+i+'" class="date_wrapper">';
        days_attended_div_html += '<div class="days_text">';
        days_attended_div_html +=  day + ' - $' + days_attended_json_obj[i].rate;
        days_attended_div_html += '</div>';
        days_attended_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
        days_attended_div_html += '<i class="bi bi-x"></i>';
        days_attended_div_html += '</div>';
        days_attended_div_html += '</div>';
    }
    days_attended_div.append(days_attended_div_html);
}
function addDaysAttended() 
{
    event.preventDefault();
    var days_attended_val = $('#days_attended').val();
    var daily_rate_val = $('#daily_rate').val();
    if(days_attended_val && daily_rate_val)    
    {
        var days_attended_json = $('#days_attended_json').val();
        var days_attended_json_obj = JSON.parse(days_attended_json);
        days_attended_json_obj.push({'day':days_attended_val, 'rate':daily_rate_val});
        var days_attended_json_str = JSON.stringify(days_attended_json_obj);
        $('#days_attended_json').val(days_attended_json_str);
        
        var days_attended_div = $('#days_attended_div');
        var days_attended_div_html = '';
        days_attended_div.html('');
        if(days_attended_json_obj.length >0)
        {
            $('#days_attended_div').removeClass('empty-dates')
        }
        else
        {
            $('#days_attended_div').addClass('empty-dates')
        }
        for(var i=0; i<days_attended_json_obj.length; i++)
        {
            var day = days_attended_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new);
            var day = moment(d).format('ddd, Do MMM YYYY');

            days_attended_div_html += '<div days_id="'+i+'" class="date_wrapper">';
            days_attended_div_html += '<div class="days_text">';
            days_attended_div_html += day + ' - $' + days_attended_json_obj[i].rate;
            days_attended_div_html += '</div>';
            days_attended_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
            days_attended_div_html += '<i class="bi bi-x"></i>';
            days_attended_div_html += '</div>';
            days_attended_div_html += '</div>';
        }
        days_attended_div.append(days_attended_div_html);
    }
}
function deleteDaysAttended(index) {
    //remove index from extra_days_json
    var days_attended_json = $('#days_attended_json').val();
    var days_attended_json_obj = JSON.parse(days_attended_json);
    days_attended_json_obj.splice(index, 1);
    var days_attended_json_str = JSON.stringify(days_attended_json_obj);
    $('#days_attended_json').val(days_attended_json_str);
    
    var days_attended_div = $('#days_attended_div');
        var days_attended_div_html = '';
        days_attended_div.html('');
        if(days_attended_json_obj.length >0)
        {
            $('#days_attended_div').removeClass('empty-dates')
        }
        else
        {
            $('#days_attended_div').addClass('empty-dates')
        }
        for(var i=0; i<days_attended_json_obj.length; i++)
        {
            var day = days_attended_json_obj[i].day;
            var day_arr = day.split('-');
            var day_new = day_arr[2] + '/' + day_arr[0] + '/' + day_arr[1];

            const d = new Date(day_new);
            var day = moment(d).format('ddd, Do MMM YYYY');

            days_attended_div_html += '<div days_id="'+i+'" class="date_wrapper">';
            days_attended_div_html += '<div class="days_text">';
            days_attended_div_html += day + ' - $' + days_attended_json_obj[i].rate;
            days_attended_div_html += '</div>';
            days_attended_div_html += '<div onclick="deleteDays('+i+')" class="delete_day">';
            days_attended_div_html += '<i class="bi bi-x"></i>';
            days_attended_div_html += '</div>';
            days_attended_div_html += '</div>';
        }
        days_attended_div.append(days_attended_div_html);
}