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

@if(isset($paat_alert) && $paat_alert)
    <div class="alert alert-danger" role="alert">
        <h4 class="alert-heading">Attention!</h4>
        <p>Vous ne pourrez pas sauvegarder la fiche projet car aucun PAAT n'est initialisé ou en cours.</p>
    </div>
@endif

@php
    $form_route = "";
    $element_disabled = "";
    if (Request::routeIs("projets.*")) {
        $form_route = "projets.$route";
    } else if (Request::routeIs("bibliotheque_projets.*")) {
        $form_route = "bibliotheque_projets.store";
        $element_disabled = Request::routeIs("bibliotheque_projets.show") ? "disabled" : "";
    }
@endphp

<form method="POST" action="{{ route($form_route, isset($projet) ? $projet->id : '') }}">
    @csrf
    @if ($route == "update") {{ method_field('PUT') }} @endif

    <div class="row custom-frame pb-1">
        <div class="col-sm-2">
            <label class="col-form-label"><span id="reference-fiche">Nouvelle fiche</span></label>
        </div>
        <div class="col-sm-3">
            <div class="multi-step">
                <ul class="multi-step-list">
                    @for($i = 1; $i <= ETAPE_PROJET::NOMBRE_ETAPE; $i++)
                        <li class="multi-step-item progress-{{ $i }} @isset($projet) @if(in_array(ETAPE_PROJET::ETAPES_ORDERED[$i - 1], $projet->etapes())) current @endif @endisset">
                            <div class="item-wrap">
                                <p class="item-title">{{ $i }}</p>
                            </div>
                        </li>
                    @endfor
                </ul>
            </div>
        </div>

        <div class="col-sm-7">
            <div class="row">
                <label for="date_creation" class="col-4 col-sm-3 col-form-label text-right">Créée le</label>
                <div class="col-8 col-sm-3">
                    <input type="text" class="form-control" id="date_creation" value="{{ isset($projet->created_at) ? date('d/m/Y', strtotime($projet->created_at)) : '' }}" readonly>
                </div>
                <label for="date_maj" class="col-4 col-sm-3 col-form-label text-right">Clôturée le</label>
                <div class="col-8 col-sm-3">
                    <input type="text" class="form-control" id="date_maj" readonly>
                </div>
            </div>
        </div>
    </div>


    {{-- Partie gauche de l'écran --}}
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group row">
                <label for="structure" class="col-sm-4 col-form-label required">Structure</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" value="{{ $nom_structure }}" readonly>
                </div>
            </div>

            <div class="form-group row">
                <label for="direction_id" class="col-sm-4 col-form-label required">Etablissement</label>
                <div class="col-sm-8">
                    <select class="form-control" id="direction_id" name="direction_id" required {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($etablissements as $key => $value)
                            <option value="{{ $key }}" @if($key == old('direction_id', $projet->direction_id ?? '')) selected @endif>
                                {{ $value }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="service_id" class="col-sm-4 col-form-label">Service utilisateur</label>
                <div class="col-sm-8">
                    <select class="form-control" id="service_id" name="service_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($services_utilisateurs as $s)
                            <option value="{{ $s->direction_id . '_' . $s->id }}"
                                    @if($s->direction_id . '_' . $s->id == old('service_id', isset($projet) ? $s->direction_id . '_' . $projet->service_id : '')) selected @endif
                                    @isset($projet) @if($s->direction_id != $projet->direction_id) style="display: none" @endif @endisset>
                                {{ $s->nom }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="objet" class="col-sm-4 col-form-label required">Objet du projet</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="objet" value="{{ old('objet', $projet->objet ?? '') }}" required {{ $element_disabled }}>
                </div>
            </div>

            <div class="form-group row">
                <label for="description" class="col-sm-4 col-form-label">Descriptif</label>
                <div class="col-sm-8">
                    <textarea rows="4" class="form-control" name="description" {{ $element_disabled }}>{{ old('description', $projet->description ?? '') }}</textarea>
                </div>
            </div>

            <div class="form-group row">
                <label for="niveau_mutualisation_id" class="col-sm-4 col-form-label required">Mutualisation</label>
                <div class="col-sm-8">
                    <select class="form-control" name="niveau_mutualisation_id" required {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($mutualisations as $key => $value)
                            <option value="{{ $key }}" @if($key == old('niveau_mutualisation_id', $projet->niveau_mutualisation_id ?? '')) selected @endif>{{ $value }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="operateur_mutualisation_id" class="col-sm-4 col-form-label required">Centrale
                    d'achat</label>
                <div class="col-sm-8">
                    <select class="form-control" name="operateur_mutualisation_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($centrales_achats as $key => $value)
                            <option value="{{ $key }}" @if($key == old('operateur_mutualisation_id', $projet->operateur_mutualisation_id ?? '')) selected @endif>{{ $value }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <!-- Budget -->
            <div class="form-group row">
                <label for="budget" class="col-sm-4 col-form-label required">Budget</label>
                <div class="col-sm-8">
                    <select class="form-control" name="budget" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        <option value="{{ PROJET::INVESTISSEMENT }}" @if(PROJET::INVESTISSEMENT == old('budget', $projet->budget ?? '')) selected @endif>{{ PROJET::PROJET_FORMATTED[PROJET::INVESTISSEMENT] }}</option>
                        <option value="{{ PROJET::EXPLOITATION }}" @if(PROJET::EXPLOITATION == old('budget', $projet->budget ?? '')) selected @endif>{{ PROJET::PROJET_FORMATTED[PROJET::EXPLOITATION] }}</option>
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="impact_budgetaire" class="col-sm-4 col-form-label">Impact
                    budgétaire</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input type="number" class="form-control"
                            id="impact_budgetaire" name="impact_budgetaire" min="0" max="100"
                            value="{{ old('impact_budgetaire', $projet->impact_budgetaire ?? '') }}" {{ $element_disabled }}>
                        <div class="input-group-append">
                            <span class="input-group-text">%</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <label for="imputation_budgetaire_id" class="col-sm-4 col-form-label required">Imputation
                    budgétaire</label>
                <div class="col-sm-8">
                    <select class="form-control" name="imputation_budgetaire_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($imputations_budegetaires as $key => $value)
                            <option value="{{ $key }}" @if($key == old('imputation_budgetaire_id', $projet->imputation_budgetaire_id ?? '')) selected @endif>{{ $value }}</option>
                        @endforeach
                    </select>
                </div>
            </div>


            <div class="form-group row">
                <label for="programmation" class="col-sm-4 col-form-label required">Programmation</label>
                <div class="col-sm-8">
                    <select class="form-control" name="programmation" id="programmation" {{ $element_disabled }}>
                        <option value="1" @if(1 == old('programmation', $projet->programmation ?? '')) selected @endif>Action programmée</option>
                        <option value="0" @if(0 == old('programmation', $projet->programmation ?? '')) selected @endif>Action non programmée</option>
                    </select>
                </div>
            </div>


            <div class="form-group row">
                <label for="type_calcul_gains" class="col-4 col-form-label required">Calcul des gains</label>
                <div class="col-8">
                    <select class="form-control" name="type_calcul_gains" id="type_calcul_gains" {{ $element_disabled }}>
                        <option value="{{TYPE_CALCUL_GAINS::ACHATS_RECURRENTS}}" @if(TYPE_CALCUL_GAINS::ACHATS_RECURRENTS == old('type_calcul_gains', $projet->type_calcul_gains ?? '')) selected @endif>Achats récurrents</option>
                        <option value="{{TYPE_CALCUL_GAINS::ACHATS_COMPLEXES}}" @if(TYPE_CALCUL_GAINS::ACHATS_COMPLEXES == old('type_calcul_gains', $projet->type_calcul_gains ?? '')) selected @endif>Achats complexes</option>
                        <option value="{{TYPE_CALCUL_GAINS::ACHATS_INVESTISSEMENT}}" @if(TYPE_CALCUL_GAINS::ACHATS_INVESTISSEMENT == old('type_calcul_gains', $projet->type_calcul_gains ?? '')) selected @endif>Achats ponctuels</option>
                    </select>
                </div>
            </div>


            <div class="form-group row">
                <label for="ondam" class="col-sm-4 col-form-label required">Objectifs de l'Assurance Maladie</label>
                <div class="col-sm-8">
                    <select class="form-control" name="ondam" id="ondam" {{ $element_disabled }}>
                        <option value="1" @if(1 == old('ondam', $projet->ondam ?? '')) selected @endif>ONDAM</option>
                        <option value="0" @if(0 == old('ondam', $projet->ondam ?? '')) selected @endif>Non ONDAM</option>
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="referentiel" class="col-sm-4 col-form-label">{{ $champ_referentiel }}</label>
                <div class="col-sm-8">
                    <select class="form-control" name="referentiel" id="referentiel" {{ $element_disabled }}>
                        <option value="0" @if(0 == old('referentiel', $projet->referentiel ?? '')) selected @endif>Non</option>
                        <option value="1" @if(1 == old('referentiel', $projet->referentiel ?? '')) selected @endif>Oui</option>
                    </select>
                </div>
            </div>

        </div>




        {{-- Partie droite de l'écran --}}
        <div class="col-sm-6">
            <div class="form-group row">
                <label for="famille_id" class="col-sm-4 col-form-label">Famille</label>
                <div class="col-sm-8">
                    <select class="form-control" name="famille_id" id="famille_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($familles as $f)
                            <option value="{{ $f->id }}" @if($f->id == old('famille_id', $projet->famille_id ?? '')) selected @endif>{{ $f->nom }}</option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="domaine_id" class="col-sm-4 col-form-label">Domaine</label>
                <div class="col-sm-8">
                    <select class="form-control" name="domaine_id" id="domaine_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($domaines as $d)
                            <option value="{{ $d->famille_id . '_' . $d->id }}"
                                    @if($d->famille_id . '_' . $d->id == old('domaine_id', isset($projet) ? $d->famille_id . '_' . $projet->domaine_id : '')) selected @endif
                                    @isset($projet) @if($d->famille_id != $projet->famille_id) style="display: none" @endif @endisset>
                                {{ $d->nom }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>


            <div class="form-group row">
                <label for="categorie_id" class="col-sm-4 col-form-label">Catégorie</label>
                <div class="col-sm-8">
                    <select class="form-control" name="categorie_id" id="categorie_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($categories as $c)
                            <option value="{{ $c->domaine_id . '_' . $c->id }}"
                                    @if($c->domaine_id . '_' . $c->id == old('categorie_id', isset($projet) ? $c->domaine_id . '_' . $projet->categorie_id : '')) selected @endif
                                    @isset($projet) @if($c->domaine_id != $projet->domaine_id) style="display: none" @endif @endisset>
                                {{ $c->nom }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>

            <div class="form-group row">
                <label for="segment_id" class="col-sm-4 col-form-label">Segment</label>
                <div class="col-sm-8">
                    <select class="form-control" name="segment_id" id="segment_id" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        @foreach($segments as $s)
                            <option value="{{ $s->categorie_id . '_' . $s->id }}"
                                    @if($s->categorie_id . '_' . $s->id == old('segment_id', isset($projet) ? $s->categorie_id . '_' . $projet->segment_id : '')) selected @endif
                                    @isset($projet) @if($s->categorie_id != $projet->categorie_id) style="display: none" @endif @endisset>
                                {{ $s->nom }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>

            <!-- Renouvellement de marché ? -->
            <div class="form-group row">
                <label for="renouvellement_marche" class="col-sm-4 col-form-label">Type de marché</label>
                <div class="col-sm-8">
                    <select class="form-control" name="renouvellement_marche" id="renouvellement_marche" {{ $element_disabled }}>
                        <option value="">Sélectionner</option>
                        <option value="1" @if(isset($projet) && isset($projet->renouvellement_marche) && $projet->renouvellement_marche) selected @endif>Renouvellement de marché</option>
                        <option value="0" @if(isset($projet) && isset($projet->renouvellement_marche) && !$projet->renouvellement_marche) selected @endif>Nouveau marché</option>
                    </select>
                </div>
            </div>

            <h5>Réf. ancien marché à renouveler</h5>

            <div class="form-group row">
                <label for="libelle_marche_a_renouveler" class="col-sm-4 col-form-label">Libellé de marché</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="libelle_marche_a_renouveler"
                        value="{{ old('libelle_marche_a_renouveler', $projet->libelle_marche_a_renouveler ?? '') }}" {{ $element_disabled }}>
                </div>
            </div>

            <div class="form-group row">
                <label for="numero_marche_a_renouveler" class="col-sm-4 col-form-label">Numéro de marché</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="numero_marche_a_renouveler"
                        value="{{ old('numero_marche_a_renouveler', $projet->numero_marche_a_renouveler ?? '') }}" {{ $element_disabled }}>
                </div>
            </div>

            <h5>Réf. nouveau marché</h5>

            <div class="form-group row">
                <label for="libelle_nouveau_marche" class="col-sm-4 col-form-label">Libellé de marché</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="libelle_nouveau_marche"
                    value="{{ old('libelle_nouveau_marche', $projet->libelle_nouveau_marche ?? '') }}" {{ $element_disabled }}>
                </div>
            </div>

            <div class="form-group row">
                <label for="numero_nouveau_marche" class="col-sm-4 col-form-label">Numéro de marché</label>
                <div class="col-sm-8">
                    <input type="text" class="form-control" name="numero_nouveau_marche"
                    value="{{ old('numero_nouveau_marche', $projet->numero_nouveau_marche ?? '') }}" {{ $element_disabled }}>
                </div>
            </div>

            <h5>Planning du projet</h5>
            <div class="form-group row">
                <label for="date_debut" class="col-sm-4 col-form-label required">Date prévisionnelle de
                    début</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input type="text" class="datepicker form-control" id="date_debut" name="date_debut"
                        value="{{ old('date_debut', isset($projet->date_debut) ? date('d/m/Y', strtotime($projet->date_debut)) : '') }}"
                        {{ $element_disabled }}>
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-calendar3" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd" d="M14 0H2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM1 3.857C1 3.384 1.448 3 2 3h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H2c-.552 0-1-.384-1-.857V3.857z"/>
                                    <path fill-rule="evenodd" d="M6.5 7a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
                                  </svg>
                            </span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-group row">
                <label for="date_fin" class="col-sm-4 col-form-label required">Date prévisionnelle de fin</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input type="text" class="datepicker form-control" id="date_fin" name="date_fin"
                        value="{{ old('date_fin', isset($projet->date_fin) ? date('d/m/Y', strtotime($projet->date_fin)) : '') }}"
                        {{ $element_disabled }}>
                        <div class="input-group-append">
                            <span class="input-group-text">
                                <svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-calendar3" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                                    <path fill-rule="evenodd" d="M14 0H2a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2zM1 3.857C1 3.384 1.448 3 2 3h12c.552 0 1 .384 1 .857v10.286c0 .473-.448.857-1 .857H2c-.552 0-1-.384-1-.857V3.857z"/>
                                    <path fill-rule="evenodd" d="M6.5 7a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm-9 3a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2zm3 0a1 1 0 1 0 0-2 1 1 0 0 0 0 2z"/>
                                  </svg>
                            </span>
                        </div>
                    </div>
                </div>
            </div>


        </div>
        <div class="col-12">
            <div class="form-group row text-right">
                <div class="col-sm-12">
                    @if (Request::is('bibliotheque_projets/*'))
                        <a href="{{ route('bibliotheque_projets.index') }}" class="btn btn-outline-warning">Annuler</a>
                    @else
                        <a href="{{ route('projets.index') }}" class="btn btn-outline-warning">Annuler</a>
                    @endif

                    @if($editable)
                        <button type="submit" class="btn btn-outline-success">
                            Enregistrer
                        </button>
                    @endif
                </div>
            </div>
        </div>
    </div>
</form>

@if(strlen($element_disabled) === 0)
@section('unique-js')
    <script src="{{ asset('js/commons/controls/directions-services.js') }}"></script>
    <script src="{{ asset('js/commons/controls/familles-domaines-categories-segments.js') }}"></script>
@endsection
@endif
