<?php $__env->startSection('content'); ?>
    <div class="container flex-column justify-content-between d-flex">
        <div>
            <h2 class="pb-2">Import Cartographie</h2>
        </div>

        <?php echo $__env->make('layouts.commons.errors', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
        <?php echo $__env->make('layouts.commons.success', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

        <div>
            <h3 class="mt-5">Merci de matcher les entêtes récupérées de votre fichier avec les entêtes Aptimap</h3>
            <h5>Au moins une selection dois être faite pour pour réaliser le matching</h5>
        </div>

        <form id="matching-form" action="<?php echo e(route("import-cartographie.matchingCSV")); ?>" method="POST"
              class="d-flex justify-content-center flex-column">
            <?php echo csrf_field(); ?>
            <div class="row mt-5">
                <div class="col">
                    <div class="mb-5"> Entêtes Aptimap :</div>
                    <?php if(is_array($headersAptimap ?? '') && count($headersAptimap ?? '') > 0): ?>
                        <?php $__currentLoopData = $headersAptimap; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $index => $headerAptimap): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <div class="form-group">
                                <input style="height: 50px; width: 300px" type="text" id="<?php echo e($index); ?>"
                                       class="form-control" placeholder="<?php echo e($headerAptimap); ?>" disabled>
                                <input type="hidden" name="headerAptimap[<?php echo e($index); ?>]" value="<?php echo e($headerAptimap); ?>">
                            </div>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    <?php endif; ?>
                </div>
                <div class="col">
                    <div class="mb-5"> Entêtes du fichier client :</div>
                    <?php if(is_array($headersCustomers ?? '') && count($headersCustomers ?? '') > 0): ?>
                        <?php for($i = 0; $i < count($headersAptimap); $i++): ?>
                            <div class="form-group">
                                <select style="height: 50px"
                                        class="btn border border-dark-subtle p-1 pl-3 pr-3 header-select"
                                        name="headerCustomersSelected[<?php echo e($i); ?>]" id="<?php echo e($i); ?>">
                                    <option value="" disabled selected hidden>Sélectionner un entête</option>
                                    <?php $__currentLoopData = $headersCustomers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $headerCustomersSelected): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option
                                            value="<?php echo e($headerCustomersSelected['id']); ?>"><?php echo e($headerCustomersSelected['nom']); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>
                        <?php endfor; ?>
                    <?php endif; ?>
                </div>
            </div>
            <div class="submit-button-container d-flex b-2 justify-content-center mt-5">
                <button type="submit" id="submit-button-matching" class="btn btn-primary p-3 pl-5 pr-5">Soumettre le
                    matching
                </button>
            </div>
        </form>
    </div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('unique-js'); ?>
    <script>

        document.addEventListener('DOMContentLoaded', function () {
            var submitButton = document.getElementById('submit-button-matching');
            submitButton.disabled = true;

            var selects = document.querySelectorAll('select');
            selects.forEach(function(select) {
                select.addEventListener('change', function() {
                    var atLeastOneSelected = Array.from(selects).some(function(select) {
                        return select.value !== '';
                    });

                    submitButton.disabled = !atLeastOneSelected;
                });
            });
        });

        const selectedHeaders = [];

        const selects = document.querySelectorAll('select');

        selects.forEach(select => {
            select.addEventListener('change', function () {
                const index = selectedHeaders.findIndex(header => header.id === this.id);
                if (index !== -1) {
                    selectedHeaders[index].value = this.value;
                } else {
                    selectedHeaders.push({id: this.id, value: this.value});
                }

                selects.forEach(otherSelect => {
                    otherSelect.querySelectorAll('option').forEach(option => {
                        option.disabled = false;
                    });
                });

                selectedHeaders.forEach(header => {
                    selects.forEach(otherSelect => {
                        if (otherSelect.id !== header.id) {
                            otherSelect.querySelectorAll('option').forEach(option => {
                                if (option.value === header.value) {
                                    option.disabled = true;
                                }
                            });
                        }
                    });
                });
            });
        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /var/www/html/resources/views/parametres/import-cartographie/matching.blade.php ENDPATH**/ ?>