{% extends app.request.xmlHttpRequest ? 'form.html.twig' : 'base.html.twig' %}
{% import "macros/widgets.html.twig" as widgets %}
{% import "timesheet/actions.html.twig" as actions %}

{% block page_title %}{{ 'timesheet.title'|trans }}{% endblock %}
{% block page_actions %}{{ actions.timesheet(timesheet, 'edit') }}{% endblock %}

{% block main %}
    {% if timesheet.exported %}
        {{ widgets.alert('warning', ('timesheet.locked.warning'|trans({}, 'flashmessages')), ('warning'|trans({}, 'flashmessages')), 'warning') }}
    {% endif %}
    {% set formEditTemplate = app.request.xmlHttpRequest ? 'default/_form_modal.html.twig' : 'default/_form.html.twig' %}
    {% set formOptions = {
        'title': (timesheet.id ? 'timesheet.edit'|trans : 'create'|trans),
        'form': form,
        'back': path('timesheet')
    } %}
    {% embed formEditTemplate with formOptions %}
        {% set length = 6 %}
        {% if form.begin is defined and form.end is defined and form.duration is defined %}
            {% set length = 5 %}
        {% elseif form.begin is defined and form.end is defined and form.duration is not defined %}
            {% set length = 6 %}
        {% elseif form.begin is defined and form.end is not defined and form.duration is defined %}
            {% set length = 10 %}
        {% elseif form.begin is not defined and form.end is defined and form.duration is defined %}
            {% set length = 10 %}
        {% endif %}
        {% block form_body %}
            {% if form.begin is defined or form.end is defined or form.duration is defined %}
            <div class="row">
                {% if form.begin is defined %}
                    <div class="col-md-{{ length }}">
                        {{ form_row(form.begin) }}
                    </div>
                {% endif %}
                {% if form.end is defined %}
                    <div class="col-md-{{ length }}">
                        {{ form_row(form.end) }}
                    </div>
                {% endif %}
                {% if form.duration is defined %}
                    <div class="col-md-2">
                        {{ form_row(form.duration) }}
                    </div>
                {% endif %}
            </div>
            {% endif %}
            {{ form_widget(form) }}
        {% endblock %}
        {% block form_after %}
            {% if form.begin is defined and form.end is defined and form.duration is defined %}
                <script type="text/javascript">
                    $('body').on('blur change', '#timesheet_edit_form_begin', function(ev) {
                        changedBegin($(this).val());
                    });

                    $('body').on('blur change', '#timesheet_edit_form_end', function(ev) {
                        changedEnd($(this).val());
                    });

                    $('body').on('blur change', '#timesheet_edit_form_duration', function(ev) {
                        changedDuration($(this).val());
                    });
                    
                    {#
                        Ruleset: 
                        - invalid begin => skip
                        - empty end => set end to begin (only if duration > 0 = running record)
                        - invalid end => skip
                        - calculate duration 
                    #}
                    function changedBegin(value)
                    {
                        var endField = document.getElementById('timesheet_edit_form_end');
                        var durationField = document.getElementById('timesheet_edit_form_duration');
                        var format = endField.dataset.format;
                        var momentDuration = moment.duration(durationField.value);

                        var momentBegin = moment(value, format);
                        if (!momentBegin.isValid()) {
                            return;
                        }
                        
                        if (endField.value === '' && momentDuration.asSeconds() > 0) {
                            endField.value = value;
                        }

                        var momentEnd = moment(endField.value, format);
                        if (!momentEnd.isValid()) {
                            return;
                        }
                        
                        if (momentEnd.isBefore(momentBegin)) {
                            endField.value = momentBegin.add(momentDuration).format(format);
                        }

                        momentBegin = moment(value, format);
                        momentEnd = moment(endField.value, format);
                        
                        var durationMoment = moment.duration(momentEnd.diff(momentBegin));
                        var hours = Math.floor(durationMoment.asHours());
                        if (hours < 10) {
                            hours = '0' + hours;
                        }
                        
                        durationField.value = hours + ':' + ('0' + durationMoment.minutes()).slice(-2);
                    }

                    {#
                        Ruleset: 
                        - invalid end => skip
                        - empty begin => set begin to end
                        - invalid begin => skip
                        - calculate duration 
                    #}
                    function changedEnd(value)
                    {
                        var beginField = document.getElementById('timesheet_edit_form_begin');
                        var durationField = document.getElementById('timesheet_edit_form_duration');
                        var format = beginField.dataset.format;
                        var momentDuration = moment.duration(durationField.value);

                        var momentEnd = moment(value, format);
                        if (!momentEnd.isValid()) {
                            return;
                        }

                        if (beginField.value === '') {
                            beginField.value = value;
                        }

                        var momentBegin = moment(beginField.value, format);
                        if (!momentBegin.isValid()) {
                            return;
                        }

                        if (momentEnd.isBefore(momentBegin)) {
                            beginField.value = momentEnd.subtract(momentDuration).format(format);
                        }

                        momentBegin = moment(beginField.value, format);
                        momentEnd = moment(value, format);

                        var durationMoment = moment.duration(momentEnd.diff(momentBegin));
                        var hours = Math.floor(durationMoment.asHours());
                        if (hours < 10) {
                            hours = '0' + hours;
                        }
                        
                        durationField.value = hours + ':' + ('0' + durationMoment.minutes()).slice(-2);
                    }

                    {#
                        Ruleset: 
                        - invalid duration => skip
                        - if begin and end are empty: set begin to now and end to duration
                        - if begin is empty and end is not empty: set begin to end minus duration
                        - if begin is not empty and end is empty and duration is > 0 (running records = 0): set end to begin plus duration
                    #}
                    function changedDuration(value)
                    {
                        var momentDuration = moment.duration(value);
                        if (!momentDuration.isValid()) {
                            return;
                        }
                        
                        var beginField = document.getElementById('timesheet_edit_form_begin');
                        var endField = document.getElementById('timesheet_edit_form_end');
                        var format = endField.dataset.format;
                        var begin = beginField.value;
                        var end = endField.value;
                        var duration = momentDuration.asSeconds();

                        if (begin === '' && end === '') {
                            beginField.value = moment().format(format); 
                            endField.value = moment(beginField.value, format).add(duration, 'seconds').format(format); 
                        } else if (begin === '' && end !== '') {
                            beginField.value = moment(end, format).subtract(duration, 'seconds').format(format);
                        } else if (begin !== '' && duration > 0) {
                            endField.value = moment(beginField.value, format).add(duration, 'seconds').format(format); 
                        }
                    }
                </script>
            {% endif %}
        {% endblock %}
    {% endembed %}
{% endblock %}
