{{-- List of table revisions --}}

@extends('layouts.app')
@section('title', "Revisions - $table->title")

@php
/** @var \App\Models\Table $table */
/** @var \App\Models\Revision[]|\Illuminate\Support\Collection $revisions */
@endphp

@section('content')
  <div class="row justify-content-start px-3">
    <div class="d-flex w-100 align-items-center">
      @include('table._header-handle')

      <h1 class="mx-3" id="table-title">{{ $table->title }}</h1>
      <a href="{{ $table->viewRoute }}" class="btn btn-outline-primary py-1 btn-sm" @tooltip(Back to Table)>
        @icon(fa-reply, sr:Back to Table)
      </a>
    </div>
  </div>

  <div class="row px-3 border-top pt-3">
    <div class="col-md-12">
      <h2>Table Revisions</h2>

      @php
        $revIds = $revisions->pluck('id')->reverse()->values();
      @endphp

      @foreach($revisions as $i => $revision)
        @php
        $num = count($revisions) - $i;
        $isCurrent = $revision->id == $table->revision_id;
        @endphp
        <div class="x-revision rounded border box-shadow mb-3 p-2" id="revision-{{$revision->id}}">
          <div class="row">
            <div class="col-md-8">
              #{{$num}} –
              <span title="{{$revision->created_at->format('Y-m-d, G:i:s')}}">
                {{ $revision->created_at->diffForHumans() }}
              </span>
              @if($isCurrent)
                <span class="text-muted ml-1">
                  (<a class="link-no-color" href="{{ $table->viewRoute }}">current</a>)
                </span>
              @else
                <span class="text-muted ml-1">
                  (<a class="link-no-color" href="{{ $table->viewRoute }}?rev={{$num}}">see table in this revision</a>)
                </span>
              @endif
            </div>
            <div class="col-md-4 text-right">
              @if($revision->ancestor_id)
                @if ($revIds->contains($revision->ancestor_id))
                  <span class="x-revision-link text-muted" data-id="{{$revision->ancestor_id}}">Based on #{{ 1+$revIds->search($revision->ancestor_id) }}</span>
                @else
                  <span class="text-muted">Forked from elsewhere</span>
                @endif
              @endif
            </div>
          </div>

          <div class="row">
            <div class="col-md-10" style="font-size:140%;">
              {{ $revision->note }}
            </div>
            <div class="col-md-2 text-right">
              @if(!$isCurrent && authed() && user()->ownsTable($table))
                <a href="{{$table->actionRoute('revert-to', ['rev' => $num])}}" class="btn btn-outline-secondary btn-sm">Set as current</a>
              @endif
            </div>
          </div>
        </div>
      @endforeach
    </div>
  </div>
@endsection