<input type="hidden" value="{{ planning | length == 0? 1:(planning | length) }}" id="countPlanning" />
<input type="hidden" name="calculPlanning" id="calculPlanning" value="{% if newLine.RECEP_AUTO %}0{% else %}1{% endif %}" />
<table class="table table-hover" id="tablePlanning">
    <thead class="thead-dark">
        <tr>
            <td>{{ 'Quantité' | trad }}</td>
            <td>{{ 'Date' | trad }}</td>
            <td width='40px'>
                {% if (newLine.NO_DA == '' or (newLine.NO_DA and param.LIMITACH_DEL == false) or droit.HA_CA_MCPR == true) %}
                    <a {% if lockPlanning == true or newLine.RECEP_AUTO | boolSql %}style="display:none;"{% endif %} id="addPlanning" class="ajouter_v2" onClick="addLignePlanning('{{ path('Commande.ModificationLigneCommande.ajaxAddLignePlanning') }}')" style="float: right;">
                    </a>
                {% endif %}
            </td>
        </tr>
    </thead>
    <tbody class="tablePlanning">
        {% for date in planning %}
            <tr class="planningTR" id="planningTR{{loop.index}}">
                <td align="right">
                    {{ inputAdvance('text', {
                        'name' : 'PLANNING-QTE_COMM-' ~ loop.index,
                        'pathValidator' : path('Ajax.validator.float'),
                        'value'	: qteByUnite(date.QTE_COMM, newLine.UNITE),
                        'paramsValidator' : "{'format':" ~ getConst('formatQTE') ~ "}",
                        'addClass' : 'planningQte',
                        'option' : ((newLine.NO_DA and param.LIMITACH_DEL) and droit.HA_CA_MCPR == false)? 'readOnly':'',
                    }) }}
                </td>
                <td>
                    {{ inputAdvance('date', {
                        'name' : 'PLANNING-DTE_LIV-' ~ loop.index,
                        'pathValidator' : path('Ajax.validator.date'),
                        'value'	: date.DTE_LIV |dateTrad,
                        'paramsValidator' : "{}",
                        'addStyle' : 'dte_planning',
                        'option' : (((newLine.NO_DA and param.LIMITACH_DEL) and droit.HA_CA_MCPR == false) or (lockPlanning == true or newLine.RECEP_AUTO | boolSql))? 'readOnly':'',
                    }) }}
                </td>
                <td>
                    {% if loop.index != 1 and newLine.RECEP_AUTO == false %}
                        {{ hover_action('planningTR' ~ loop.index, 'Supprimer_v2 deletePlanning' , "deletePlanning(" ~ loop.index ~ ");", 'float: right;margin-right:5px;' ~ (lockPlanning == true? 'display:none;':'')~'') }}
                    {% endif %}
                </td>
            </tr>
        {% else %}
            <tr id="planningTR1">
                <td align="right">
                    {{ inputAdvance('text', {
                        'name' : 'PLANNING-QTE_COMM-1',
                        'pathValidator' : path('Ajax.validator.float'),
                        'paramsValidator' : "{'format':" ~ getConst('formatQTE') ~ "}",
                        'addClass' : 'planningQte',
                       'option' : ((newLine.NO_DA and param.LIMITACH_DEL) and droit.HA_CA_MCPR == false)? 'readOnly':'',
                    }) }}
                </td>
                <td>
                    {{ inputAdvance('date', {
                        'name' : 'PLANNING-DTE_LIV-1',
                        'pathValidator' : path('Ajax.validator.date'),
                        'paramsValidator' : "{}",
                        'option' : (((newLine.NO_DA and param.LIMITACH_DEL) and droit.HA_CA_MCPR == false) or (lockPlanning == true or newLine.RECEP_AUTO | boolSql))? 'readOnly':'',
                    }) }}
                </td>
                <td>
                </td>
            </tr>
        {% endfor %}
    </tbody>
</table>
<script>
    function deletePlanning(noLignePlanning)
        {
            $('#planningTR' + noLignePlanning).remove();
            var countTR = $('.tablePlanning >tr').length;

            if (countTR == 1) {
                $('#input_ligne_DTELIVCON').attr("readOnly", false);
                $('#input_ligne_DTELIVCON').val($('#input_PLANNING-DTE_LIV-1').val());
            }
        }

        function addLignePlanning(path)
        {
            $('#input_ligne_DTELIVCON').val('');
            $('#input_ligne_DTELIVCON').attr("readOnly", true);
            var countTR = $('#countPlanning').val();
            countTR = parseInt(countTR);
            $.ajax({
                dataType : 'html',
                url: path,
                data: {'noLigne' : countTR + 1},
                type: "POST",
                success: function(result){
                    $('#tablePlanning').find('tr:last').after(result);
                    $('#countPlanning').val(countTR + 1 );
                    desactiveLoader();
                }
            });

        }

        $('#input_ligne_DTELIVCON').change(function(){
            var countTR = $('.tablePlanning >tr').length;

            if (countTR == 1) {
                $('#input_PLANNING-DTE_LIV-1').val($(this).val());
            }
        });

        $('#input_PLANNING-DTE_LIV-1').change(function(){
            var countTR = $('.tablePlanning >tr').length;

            if (countTR == 1) {
                $('#input_ligne_DTELIVCON').val($(this).val());
            }
        });
</script>