{% block body %}
	<div class="clearfix">
		{% if batchLines | length > 0 %}
			<div class="tableLines clearfix">
					<table  width="50%" class="table table-hover" id="tableLines">
						<thead class="thead-dark">
							<th scope="col" width="7%">{{ 'N° de ligne du BR'| trad }}</th>
							<th scope="col" width="">{{ 'Code article'| trad }}</th>
							<th scope="col" width="">{{ 'Désignation article'| trad }}</th>
							<th scope="col" width="">{{ 'Quantité acceptée'| trad }}</th>
							<th scope="col" width="">{{ 'Reste à répartir'| trad }}</th>
						</thead>
						<tbody>
							{% for line in batchLines %}
								<tr id="line_{{ line.no_ligne_br }}">
                                    <td align="right">{{ line.no_ligne_br }}</td>
                                    <td align="left" class="loupe">{{ line.code_art }}</td>
                                    <td>{{ line.design_sommaire }}</td>
                                    <td align="right">{{ qteByUnite(line.qte_a, line.unite) ~ ' ' ~ line.unite }}</td>
                                    <td align="right" id="line_{{ line.no_ligne_br }}_resteARepartir">{{ qteByUnite(line.resteARepartir, line.unite) }}</td>
                                </tr>
								<tr id="line_{{ line.no_ligne_br }}detail" class="detail_ligne hide_detail">
                                    <td colspan="5">
                                        <table class="table table-hover" id="tableLots">
                                            <thead class="thead-dark">
                                                <th scope="col">{{ 'N° de lot'| trad }}</th>
                                                <th scope="col" width="15%">{{ 'Quantité réceptionnée sur le lot'| trad }}</th>
                                                <th scope="col" width="15%">{{ 'Unité'| trad }}</th>
                                                <th scope="col" width="15%">{{ 'Date de péremption'| trad }}</th>
                                                <th scope="col" width="50px">
                                                    <a class="ajouter_v2  white-tooltip" style="float: right;" onclick="addBatchLine('{{ path('Reception.Lot.ajaxAddBatch', {'line': line.no_ligne_br}) }}', '{{ line.no_ligne_br }}');">
                                                        <span class="infos_tooltip left">
                                                            {{ 'Ajouter un lot'| trad }}
                                                        </span>
                                                    </a>
                                                </th>
                                            </thead>
                                            <tbody>
                                                {% for lot in line.LOTS %}
                                                    <tr id="LINE_LOT_{{ line.no_ligne_br ~ '_' ~ lot.NO_LOT }}">
                                                        <td>
                                                            {{ inputAdvance('ZOOM', {
                                                                'name'                  : 'NO_LOT' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT,
                                                                'pahtZoom'              : path('Reception.Lot.listeLots'),
                                                                'paramZoom'             : "{'filter_egal_CODE_ART' : '" ~ line.code_art ~ "' }",
                                                                'pathValidator'         : path('Reception.ValidatorCreation.saisie'),
                                                                'paramsValidator'       : "{'section' : 'lot', 'line' : '" ~ line.no_ligne_br ~ "', 'field' : 'NO_LOT'}",
                                                                'myFuncValidator'       : 'updateBatch',
                                                                'paramFuncValidator'    : '["NO_LOT' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT ~ '", false]',
                                                                'obligatoire'           : true,
                                                                'value'                 : lot.NO_LOT,
                                                            }) }}
                                                        </td>
                                                        <td>
                                                            {{ inputAdvance('TEXT', {
                                                                'titre'                 : '',
                                                                'name'                  : 'QTE_LOT' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT,
                                                                'pathValidator'         : path('Reception.ValidatorCreation.saisie'),
                                                                'paramsValidator'       : "{'section' : 'lot', 'line' : '" ~ line.no_ligne_br ~ "', 'batch': '" ~ lot.NO_LOT ~ "', 'field' : 'QTE_LOT'}",
                                                                'myFuncValidator'       : 'updateBatch',
                                                                'paramFuncValidator'    : '["QTE_LOT' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT ~ '", false]',
                                                                'obligatoire'           : true,
                                                                'value'                 : qteByUnite(lot.QTE_LOT, line.unite),
                                                                'numerique'             : true,
                                                            }) }}
                                                        </td>
                                                        <td>
                                                            {{ inputAdvance('TEXT', {
                                                                'titre'   : '',
                                                                'name'    : 'UNITE_' ~ line.no_ligne_br ~ '_' ~ lot.NO_LOT,
                                                                'value'   : line.unite,
                                                                'option'  : 'readOnly',
                                                            }) }}
                                                        </td>
                                                        <td>
                                                            {{ inputAdvance('DATE', {
                                                                'titre'                 : '',
                                                                'name'                  : 'DTE_PEREM' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT,
                                                                'pathValidator'         : path('Ajax.validator.date'),
                                                                'paramsValidator'       : "{'dateMin' : '" ~ today ~ "'}",
                                                                'myFuncValidator'       : 'updateBatch',
                                                                'paramFuncValidator'    : '["DTE_PEREM' ~ '-' ~ line.no_ligne_br ~ '-' ~ lot.NO_LOT ~ '"]',
                                                                'value'                 : lot.DTE_PEREM|dateTrad,
                                                                'option'                : (lot.DTE_PEREM_NON_MODIF == 'FALSE') ? '' : 'disabled'
                                                            }) }}
                                                        </td>
                                                        <td>
                                                            <a href="javascript:void(0)" class="action-hover Supprimer_v2" 
                                                                onclick="
                                                                    removeBatchLine(
                                                                        '{{ path('Reception.Lot.ajaxRemoveBatch', {'line': line.no_ligne_br, 'batch': lot.NO_LOT}) ~ '\', ' 
                                                                            ~ line.no_ligne_br ~ ', '
                                                                            ~ '\'' ~ lot.NO_LOT ~ '\'' 
                                                                        }}
                                                                    );"
                                                            ></a>
                                                        </td>
                                                    </tr>
                                                {% endfor %}
                                            </tbody>
                                        </table>
                                    </td>
								</tr>
							{% endfor %}
						</tbody>
					</table>
			</div>
		{% else %}
			<h4>
				{{ noBatchInfo }}
			</h4>
		{% endif %}
	</div>
    <script>
        function addBatchLine(path, line) {
            activeLoader();
            $.ajax({
                dataType : 'html',
                url: path,
                type: "POST",
                success: function(result){
                    $('#tableLots tbody').append(result);
                    desactiveLoader();
                },
                error: function(result){
                    $('#input_NO_LOT-' + line + '-0').addClass('error white-tooltip').attr('data-original-title', result.responseText);
                    desactiveLoader();
                }
            });
        }

        function removeBatchLine(path, line, batch) {
            activeLoader();
            $.ajax({
                dataType : 'json',
                url: path,
                type: "POST",
                success: function(result){
                    $('#LINE_LOT_' + line + '_' + batch).remove();
                    if (result.onlyOneBatch === true) {
                        $('#tableLots tbody').append(result.batch);
                    }
                    if (result.disabledExpirationDate === true) {
                        $('#input_DTE_PEREM-' + line + '-' + value).attr('disabled', 'disabled');
                    }
                    $('#line_' + line + '_resteARepartir').text(result.toBeDistributed);
                    $('#subAriane').html(result.subAriane);
                    desactiveLoader();
                },
                error: function(result){

                }
            });
        }

        function checkUpdateBatch(input, value) {
            let explode = input.split('-');
            let field = explode[0];
            let line = explode[1];
            let batch = explode[2] ?? '';

            if (field == 'NO_LOT') {
                if (document.getElementById('input_NO_LOT-' + line + '-' + value.trim()) != null) {
                    return '{{ "Ce numéro de lot existe déjà pour cette ligne."| trad }}';
                } 
            }

            return true;
        }

        function updateBatch(field, value = '', error = false) {
            if (error == true) {
                if (
                    (document.getElementsByClassName('error').length > 0) 
                        && 
                    (document.getElementsByClassName('error').length != document.getElementsByClassName('nonBloquant').length)
                ) {
                    $('#valid').hide();
                    $('#MsgErreur').show();
                } else {
                    $('#valid').show();
                    $('#MsgErreur').hide();
                }
                return '';
            }

            $('#valid').show();
            $('#MsgErreur').hide();

            if (value == '') {
                value = $('#input_' + field).val();
            }
            let check = checkUpdateBatch(field, value);
            let explode = field.split('-');
            let line = explode[1];
            let batch = explode[2] ?? '';

            if (check === true) {
                $.ajax({
                    dataType : 'json',
                    url: '{{ path('Reception.Lot.ajaxUpdateBatch') }}',
                    data: {field: field, value: value},
                    type: "POST", 
                    success: function(result){
                        $('#LINE_LOT_' + line + '_' + batch).after(result.batch);
                        $('#LINE_LOT_' + line + '_' + batch).remove();
                        if (result.disabledExpirationDate === true) {
                            $('#input_DTE_PEREM-' + line + '-' + value).attr('disabled', 'disabled');
                        }
                        $('#line_' + line + '_resteARepartir').text(result.toBeDistributed);
                        $('#subAriane').html(result.subAriane);
                        desactiveLoader();
                    },
                });
            } else {
                $('#input_'+field).addClass('error white-tooltip').attr('data-original-title', check);
                desactiveLoader();
            }
        }
    </script>
{% endblock %}
