/**
 * Created by IOAN on 9/15/2015.
 */
$(document).ready(function(){

    $('#alu').on('click', function(){
        var target = $(this).data('target');
        $(target).collapse('toggle');
    });

    $('body').click(function (event) {
        // only do this if navigation is visible, otherwise you see jump in navigation while collapse() is called
        if (!($(event.target).is('#navbar *')) && $(".navbar-collapse").is(":visible") && $(".navbar-toggle").is(":visible")) {
            $('.navbar-collapse').collapse('toggle');
        }
    });

    // Date Range Init
    var from_date = $('#from_date').val();
    var to_date = $('#to_date').val();
    $('#reportrange span').html(moment(from_date).format('D/M/YYYY') + ' - ' + moment(to_date).format('D/M/YYYY'));

    $('#reportrange').daterangepicker({
        "parentEl": "reportrange-container",
        "autoApply": false,
        format: 'DD/MM/YYYY',
        startDate: moment().subtract(1, 'days'),
        endDate: moment().subtract(1, 'days'),
        minDate: moment().startOf('year').subtract(1, 'year'),
        maxDate: moment().subtract(0, 'day'),
        dateLimit: { months: 36 },
        showDropdowns: true,
        showWeekNumbers: true,
        timePicker: false,
        timePickerIncrement: 1,
        timePicker12Hour: true,
        ranges: {
            'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
            'Last 7 Days': [moment().subtract(7, 'days'), moment().subtract(1, 'days')],
            'Last 28 Days': [moment().subtract(28, 'days'), moment().subtract(1, 'days')],
            'This Month': [moment().startOf('month'), moment().endOf('month')],
            'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
            'Current Year': [moment().startOf('year'), moment().subtract(1, 'days')],
            'Last Year': [moment().startOf('year').subtract(1, 'year'), moment().endOf('year').subtract(1, 'year')]
        },
        opens: 'right',
        drops: 'down',
        buttonClasses: ['btn', 'btn-sm'],
        applyClass: 'btn-primary',
        cancelClass: 'btn-default',
        separator: ' to ',
        locale: {
            applyLabel: 'Apply',
            cancelLabel: 'Cancel',
            fromLabel: 'From',
            toLabel: 'To',
            customRangeLabel: 'Custom Range',
            daysOfWeek: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr','Sa'],
            monthNames: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            firstDay: 1
        }
    }, function(start, end, label) {
        //console.log(start.toISOString(), end.toISOString(), label);
        $('#from_date').val(start.format('YYYY-MM-DD'));
        $('#to_date').val(end.format('YYYY-MM-DD'));
        $('#reportrange span').html(start.format('D/M/YYYY') + ' - ' + end.format('D/M/YYYY'));
    });

    $('#reportrange').on('apply.daterangepicker', function(ev, picker) {
        console.log(ev);
    });

});