{{define "content"}}
<div class="max-w-7xl mx-auto px-4 sm:px-6 md:px-8">
    <h1 class="text-2xl font-semibold text-gray-900">
      Transport solidaire
    </h1>

    {{if and .ViewState.booking.Data.motivation (isGuaranteedTripMotivation .ViewState.booking.Data.motivation)}}
    <div class="mt-4"><span class="text-sm p-2 bg-co-green text-white rounded-2xl">Trajet garanti : {{.ViewState.booking.Data.motivation}}</span></div>
    {{end}}

    {{ if eq .ViewState.booking.Status "WAITING_CONFIRMATION"}}
    <div class="mt-4"><span class="p-2 text-sm bg-gray-300 rounded-2xl px-4">Attente confirmation</span></div>
    {{ else if eq .ViewState.booking.Status "VALIDATED"}}
    <div class="mt-4"><span class="p-1 text-sm bg-co-green rounded-2xl px-4">Validé</span></div>
    {{ else if eq .ViewState.booking.Status "CANCELLED"}}
    <div class="mt-4"><span class="p-1 text-sm bg-co-red text-white rounded-2xl px-4">Annulé{{if .ViewState.booking.Data.reason}} : {{.ViewState.booking.Data.reason}}{{end}}</span></div>
    {{ end }}

    <div class="mt-6 flex justify-end space-x-3">
      {{if .ViewState.booking.Data.replaced_by}}
      <!-- Booking has been replaced, show link to replacement -->
      <a href="/app/solidarity-transport/bookings/{{.ViewState.booking.Data.replaced_by}}" class="inline-flex">
        <button type="button" class="px-4 py-2 border border-transparent text-sm font-medium rounded-2xl shadow-sm text-white bg-co-blue hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-co-blue">
          Remplacé par →
        </button>
      </a>
      {{else}}
      <!-- Normal action buttons -->
      {{if eq .ViewState.booking.Status "WAITING_CONFIRMATION" }}
      <a href="/app/solidarity-transport/bookings/{{.ViewState.booking.Id}}/confirm" class="inline-flex">
        <button type="button" class="px-4 py-2 border border-transparent text-sm font-medium rounded-2xl shadow-sm text-white bg-co-blue hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-co-blue">
          Confirmer
        </button>
      </a>
      {{end}}

      {{if ne .ViewState.booking.Status "WAITING_CONFIRMATION" }}
      <a href="/app/solidarity-transport/bookings/{{.ViewState.booking.Id}}/waitconfirmation" class="inline-flex">
        <button type="button" class="px-4 py-2 border border-transparent text-sm font-medium rounded-2xl shadow-sm text-white bg-co-blue hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-co-blue">
          Remettre en attente
        </button>
      </a>
      {{end}}

      {{if ne .ViewState.booking.Status "CANCELLED" }}
      {{$dialog := "cancel"}}
      <div x-data="{ {{ $dialog }}: false}">
        <button @click="{{ $dialog }} = !{{ $dialog }}" type="button" class="bg-co-red border-gray-300 border px-4 py-2 text-white items-center text-sm rounded-2xl focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-co-blue">
          Annuler
        </button>
        <div x-show="{{$dialog}}" class="relative z-10" aria-labelledby="modal-title" role="dialog" aria-modal="true">
          <div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity"></div>

          <div class="fixed inset-0 z-10 overflow-y-auto">
            <div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-0">

              <div class="relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full max-w-lg sm:p-6">
                <form method="POST" action="/app/solidarity-transport/bookings/{{.ViewState.booking.Id}}/cancel">
                  <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-co bg-co-blue text-white">
                    {{$.IconSet.Icon "hero:outline/information-circle" "h-6 w-6"}}
                  </div>
                  <div class="mt-3 text-center sm:mt-5">
                    <h3 class="text-lg font-medium leading-6 text-gray-900" id="modal-title">Annuler</h3>
                    <div class="mt-2">
                      <p class="text-sm text-gray-500">Sélectionnez la raison de l'annulation</p>
                    </div>
                    <div>
                      <label for="reason" class="block text-sm font-medium text-gray-700">Raison de l'annulation</label>
                      <div class="mt-1">
                        <select name="reason" id="reason" class="block w-full rounded-2xl border-gray-300 shadow-sm focus:border-co-blue focus:ring-co-blue sm:text-sm">
                          <option value="Trajet annulé par le passager">Trajet annulé par le passager</option>
                          <option value="Trajet non réalisé">Trajet non réalisé</option>
                          <option value="Refusé par le bénévole">Refusé par le bénévole</option>
                          <option value="Autre" selected="selected">Autre</option>
                        </select>
                      </div>
                    </div>
                  </div>
                  <div class="mt-5 sm:mt-6 sm:grid sm:grid-flow-row-dense sm:grid-cols-2">
                    <button @click="{{$dialog}} = !{{$dialog}}" type="button" class="mt-3 inline-flex w-full justify-center rounded-l-2xl border border-gray-300 bg-white px-4 py-2 text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:col-start-1 sm:mt-0 sm:text-sm">Annuler</button>
                    <button type="submit" class="inline-flex w-full justify-center rounded-r-2xl border border-transparent bg-co-blue px-4 py-2 text-base font-medium text-white shadow-sm hover:bg-co-blue focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:col-start-2 sm:text-sm">Confirmer</button>
                  </div>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
      {{end}}

      <a href="/app/journeys/?departure={{json .ViewState.booking.Journey.PassengerPickup}}&destination={{ json .ViewState.booking.Journey.PassengerDrop }}&passengerid={{.ViewState.booking.PassengerId}}" class="inline-flex">
        <button type="button" class="px-4 py-2 border border-transparent text-sm font-medium rounded-2xl shadow-sm bg-white text-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-co-blue">
          Dupliquer
        </button>
      </a>

      {{if eq .ViewState.booking.Status "CANCELLED" }}
      {{$dialogReplace := "replace_driver"}}
      <div x-data="{ {{ $dialogReplace }}: false}">
        <button @click="{{ $dialogReplace }} = !{{ $dialogReplace }}" type="button" class="px-4 py-2 border border-transparent text-sm font-medium rounded-2xl shadow-sm bg-white text-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-offset-gray-100 focus:ring-co-blue">
          Solliciter un autre conducteur
        </button>

        <!-- Modal for replacement drivers -->
        <div x-show="{{$dialogReplace}}" class="relative z-10" aria-labelledby="modal-title" role="dialog" aria-modal="true">
          <div class="fixed inset-0 bg-gray-500 bg-opacity-75 transition-opacity"></div>

          <div class="fixed inset-0 z-10 overflow-y-auto">
            <div class="flex min-h-full items-end justify-center p-4 text-center sm:items-center sm:p-6">

              <div class="relative transform overflow-hidden rounded-lg bg-white px-4 pt-5 pb-4 text-left shadow-xl transition-all sm:my-8 sm:w-full max-w-6xl sm:p-6 m-4">
                <div class="mx-auto flex h-12 w-12 items-center justify-center rounded-co bg-co-blue text-white">
                  {{$.IconSet.Icon "hero:outline/users" "h-6 w-6"}}
                </div>
                <div class="mt-3 text-center sm:mt-5">
                  <h3 class="text-lg font-medium leading-6 text-gray-900" id="modal-title">Conducteurs disponibles</h3>
                  <div class="mt-2">
                    <p class="text-sm text-gray-500">Sélectionnez un conducteur de remplacement</p>
                  </div>
                </div>

                <div class="mt-4">
                  {{if .ViewState.replacement_drivers}}
                  <table class="min-w-full divide-y divide-gray-300">
                    <thead class="bg-gray-50">
                      <tr>
                        <th scope="col" class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6">Conducteur</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Lieu de départ</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Distance conducteur</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Distance passager</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Dernier trajet (-{{$.ViewState.last_trip_days}}j)</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Prix passager</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Indemnité conducteur</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Commentaire</th>
                        <th scope="col" class="px-3 py-3.5 text-left text-sm font-semibold text-gray-900">Profil validé</th>
                        <th scope="col" class="relative py-3.5 pl-3 pr-4 sm:pr-6">
                          <span class="sr-only">Actions</span>
                        </th>
                      </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200 bg-white">
                      {{range $index, $journey := .ViewState.replacement_drivers}}
                      {{$driver := index $.ViewState.replacement_drivers_map $journey.DriverId}}
                      {{$pricing := index $.ViewState.replacement_pricing $journey.Id}}
                      {{$location := index $.ViewState.replacement_locations $journey.Id}}
                      {{$lastTrip := index $.ViewState.driver_last_trips $journey.DriverId}}
                      <tr>
                        <td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
                          {{$driver.Data.first_name}} {{$driver.Data.last_name}}
                        </td>
                        <td class="px-3 py-4 text-sm text-gray-500">
                          {{if $location}}{{$location}}{{else}}-{{end}}
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{$journey.DriverDistance}} km
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{$journey.PassengerDistance}} km
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{if $lastTrip.IsZero}}
                          <span class="text-gray-400">-</span>
                          {{else if eq ($lastTrip.Format "2006-01-02") ($.ViewState.booking.Journey.PassengerPickupDate.Format "2006-01-02")}}
                          <span class="text-red-600 font-semibold">{{ $lastTrip.Format "02/01/2006" }}</span>
                          {{else}}
                          {{ $lastTrip.Format "02/01/2006" }}
                          {{end}}
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{if $pricing}}
                          {{printf "%.2f" $pricing.passenger.amount}} {{$pricing.passenger.currency}}
                          {{else}}-{{end}}
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{if $pricing}}
                          {{printf "%.2f" $pricing.driver.amount}} {{$pricing.driver.currency}}
                          {{else}}-{{end}}
                        </td>
                        <td class="px-3 py-4 text-sm text-gray-500">
                          {{if $driver.Data.other_properties}}{{$driver.Data.other_properties.comment}}{{end}}
                        </td>
                        <td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
                          {{if solidarityDriverValidatedProfile $driver (solidarityDocuments $driver.ID)}}
                          <span class="p-1 px-2 text-xs bg-co-green text-white rounded-2xl">Oui</span>
                          {{else}}
                          <span class="p-1 px-2 text-xs bg-co-red text-white rounded-2xl">Non</span>
                          {{end}}
                        </td>
                        <td class="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
                          <form method="POST" action="/app/solidarity-transport/bookings/{{$.ViewState.booking.Id}}/create-replacement" class="inline">
                            <input type="hidden" name="driver_id" value="{{$journey.DriverId}}" />
                            <input type="hidden" name="journey_id" value="{{$journey.Id}}" />
                            <input type="hidden" name="message" value="{{template "sms_template" (dict "name" ($.ViewState.config.GetString "service_name") "datetime" (timeFormat $journey.PassengerPickupDate.AsTime "02/01/2006 15:04") "baseUrl" ($.ViewState.config.GetString "base_url"))}}" />
                            <button type="submit" class="text-co-blue hover:text-co-blue-dark underline">
                              Solliciter
                            </button>
                          </form>
                        </td>
                      </tr>
                      {{end}}
                    </tbody>
                  </table>
                  {{else}}
                  <div class="text-center py-8">
                    <p class="text-sm text-gray-500">Aucun conducteur disponible pour ce trajet</p>
                  </div>
                  {{end}}
                </div>

                <div class="mt-5 sm:mt-6">
                  <button @click="{{$dialogReplace}} = !{{$dialogReplace}}" type="button" class="inline-flex w-full justify-center rounded-2xl border border-gray-300 bg-white px-4 py-2 text-base font-medium text-gray-700 shadow-sm hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-co-blue focus:ring-offset-2 sm:text-sm">
                    Fermer
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      {{end}}
      {{end}}
    </div>
</div>

<div class="max-w-7xl mx-auto py-8 px-4 sm:px-6 md:px-8">
  <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
    <!-- Booking Details -->
    <div class="bg-white shadow overflow-hidden sm:rounded-lg">
      <div class="px-4 py-5 sm:px-6">
        <h3 class="text-lg leading-6 font-medium text-gray-900">Détails du transport solidaire</h3>
        <p class="mt-1 max-w-2xl text-sm text-gray-500">Informations sur le trajet</p>
      </div>
      <div class="border-t border-gray-200 px-4 py-5 sm:p-0">
        <dl class="sm:divide-y sm:divide-gray-200">
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Conducteur</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              <a class="text-co-blue" href="/app/solidarity-transport/drivers/{{ .ViewState.driver.ID }}">{{ .ViewState.driver.Data.first_name }} {{ .ViewState.driver.Data.last_name }}</a>
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Passager</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              <a class="text-co-blue" href="/app/beneficiaries/{{ .ViewState.passenger.ID }}">{{ .ViewState.passenger.Data.first_name }} {{ .ViewState.passenger.Data.last_name }}</a>
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Départ conducteur</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.DriverDeparture.Properties.label }}
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Prise en charge passager</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.PassengerPickup.Properties.label }}
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Dépose passager</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.PassengerDrop.Properties.label }}
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Arrivée conducteur</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.DriverArrival.Properties.label }}
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Date et heure</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ timeFormat .ViewState.booking.Journey.PassengerPickupDate "02/01/2006 15:04" }}
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Type de trajet</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{if .ViewState.booking.Journey.Noreturn}}
                Aller simple (pas de retour)
              {{else}}
                Aller-retour ({{.ViewState.booking.ReturnWaitingDuration | shortDuration}} d'attente estimée sur place)
              {{end}}
            </dd>
          </div>
          {{if .ViewState.booking.Data.motivation}}
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Motif</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{$motivation := .ViewState.booking.Data.motivation}}
              {{$found := false}}
              {{range .ViewState.booking_motivations}}
                {{if eq .value $motivation}}
                  {{.label}}
                  {{$found = true}}
                {{end}}
              {{end}}
              {{if not $found}}{{$motivation}}{{end}}
            </dd>
          </div>
          {{end}}
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Distance passager</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.PassengerDistance }} km
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Distance conducteur</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              {{ .ViewState.booking.Journey.DriverDistance }} km
            </dd>
          </div>
          <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
            <dt class="text-sm font-medium text-gray-500">Prix (passager)</dt>
            <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
              <span class="font-medium">{{ printf "%.2f" (round2 .ViewState.booking.Journey.Price.Amount) }} {{ .ViewState.booking.Journey.Price.Currency }}</span>
            </dd>
          </div>
          {{if .ViewState.booking.DriverCompensationAmount}}
            {{if gt (round2 .ViewState.booking.DriverCompensationAmount) 0.0}}
            <div class="py-4 sm:py-5 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
              <dt class="text-sm font-medium text-gray-500">Indemnité conducteur</dt>
              <dd class="mt-1 text-sm text-gray-900 sm:mt-0 sm:col-span-2">
                <span class="font-medium">{{ printf "%.2f" (round2 .ViewState.booking.DriverCompensationAmount) }} {{.ViewState.booking.DriverCompensationCurrency}}</span>
              </dd>
            </div>
            {{end}}
          {{end}}
        </dl>
      </div>
    </div>

    <!-- Map -->
    <div class="bg-white shadow overflow-hidden sm:rounded-lg">
      <div class="border-t border-gray-200 h-full">
        <div class="h-full min-h-96">
          {{ template "journey_map" (dict "driver_journey" .ViewState.booking.Journey) }}
        </div>
      </div>
    </div>
  </div>
</div>

{{ end }}
