@extends('layouts.app')

@section('content')
    <div class="container flex-column justify-content-between d-flex">
        <div>
            <h2 class="pb-2">Import Cartographie</h2>
        </div>

        @include('layouts.commons.errors')
        @include('layouts.commons.success')

        <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="{{ route("import-cartographie.matchingCSV") }}" method="POST"
              class="d-flex justify-content-center flex-column">
            @csrf
            <div class="row mt-5">
                <div class="col">
                    <div class="mb-5"> Entêtes Aptimap :</div>
                    @if(is_array($headersAptimap ?? '') && count($headersAptimap ?? '') > 0)
                        @foreach($headersAptimap as $index => $headerAptimap)
                            <div class="form-group">
                                <input style="height: 50px; width: 300px" type="text" id="{{ $index }}"
                                       class="form-control" placeholder="{{ $headerAptimap }}" disabled>
                                <input type="hidden" name="headerAptimap[{{ $index }}]" value="{{ $headerAptimap }}">
                            </div>
                        @endforeach
                    @endif
                </div>
                <div class="col">
                    <div class="mb-5"> Entêtes du fichier client :</div>
                    @if(is_array($headersCustomers ?? '') && count($headersCustomers ?? '') > 0)
                        @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[{{ $i }}]" id="{{ $i }}">
                                    <option value="" disabled selected hidden>Sélectionner un entête</option>
                                    @foreach($headersCustomers as $headerCustomersSelected)
                                        <option
                                            value="{{ $headerCustomersSelected['id'] }}">{{ $headerCustomersSelected['nom'] }}</option>
                                    @endforeach
                                </select>
                            </div>
                        @endfor
                    @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>

@endsection

@section('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>
@endsection
