@extends(Theme::viewName('layout'))
@section('title', trans('admin.metadata_upgrade.title'))

@section('breadcrumb')
    <li class="breadcrumb-item"><a href="{{ route('home') }}"><i class="fa fa-fw fa-home"></i></a></li>
    <li class="breadcrumb-item"><a href="{{ route('admin') }}">@lang('navigation.breadcrumb.admin')</a></li>
    <li class="breadcrumb-item"><a href="{{ route('albums.index') }}">@lang('navigation.breadcrumb.albums')</a></li>
    <li class="breadcrumb-item"><a href="{{ route('albums.show', ['id' => $album->id]) }}">{{ $album->name }}</a></li>
    <li  class="breadcrumb-item active">@lang('navigation.breadcrumb.metadata_upgrade')</li>
@endsection

@section('content')
    <div class="container">
        <div class="row">
            <div class="col-md-8 ml-md-auto mr-md-auto" id="analyse-album">
                <div v-if="!isCompleted">
                    <div class="card" id="analysis-card" style="display: none;">
                        <div class="card-header">Analysing...</div>
                        <div class="card-body">
                            <p>Your photos are now being analysed.</p>

                            <div class="progress">
                                <div class="progress-bar bg-success" v-bind:style="{ width: successfulPercentage }">
                                </div>
                                <div class="progress-bar bg-danger" v-bind:style="{ width: failedPercentage }">
                                </div>
                            </div>

                            {{-- We display a queue of 3 recently completed items, and 5 up-next items, as well as a counter saying "... and XYZ more" --}}
                            {{-- That's what the 3's and 5's are in the next couple of blocks --}}
                            <div v-for="image in latestCompletedImages" style="margin-top: 20px;">
                                <p class="text-success"><span v-text="image.name"></span> ... <i class="fa fa-fw fa-check"></i></p>
                            </div>
                            <div v-for="image in imagesInProgress.slice(0, 5)" style="margin-top: 20px;">
                                <p><span v-text="image.name"></span> ... <i class="fa fa-fw fa-refresh"></i></p>
                            </div>

                            <div v-if="imagesInProgress.length > 5">
                                <p>@lang('admin.analyse_and_more.and') <span v-text="imagesInProgress.length - 5"></span> @lang('admin.analyse_and_more.others')</p>
                            </div>

                            <div v-for="image in imagesFailed" style="margin-top: 20px;">
                                <p class="text-danger"><span v-text="image.name"></span> ... <i class="fa fa-fw fa-times"></i></p>
                            </div>
                        </div>
                    </div>

                    <div class="card bg-primary" id="confirm-card">
                        <div class="card-header text-white">@yield('title')</div>
                        <div class="card-body bg-light">
                            <p>@lang('admin.metadata_upgrade.confirm', ['name' => $album->name])</p>
                            <p class="text-danger"><b>@lang('admin.metadata_upgrade.warning')</b></p>

                            <div class="text-right">
                                <a href="{{ route('admin.metadataUpgrade', ['id' => $album->id]) }}" class="btn btn-link">@lang('forms.cancel_action')</a>
                                <button id="submit-button" type="submit" class="btn btn-primary"><i class="fa fa-fw fa-check"></i> @lang('forms.continue_action')</button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card" v-if="isCompleted">
                    <div class="card-header">Analysis completed</div>
                    <div class="card-body">
                        <p>Your analysis has completed.</p>

                        <div v-if="numberFailed > 0">
                            <p class="text-danger">@lang('admin.analyse_photos_failed_metadata')</p>
                            <ul class="text-danger" v-for="image in imagesFailed">
                                <li><span v-text="image.name"></span>: <span v-text="image.reason"></span></li>
                            </ul>
                        </div>

                        <div class="text-right">
                            <a class="btn btn-link" href="{{ $album->url() }}">View album</a>
                            <a class="btn btn-primary" href="{{ route('admin.metadataUpgrade') }}"><i class="fa fa-fw fa-chevron-left"></i> Back to metadata upgrade</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
@endsection

@push('scripts')
    <script type="text/javascript">
        var viewModel = new AnalyseAlbumViewModel();
        var app = new Vue(viewModel);

        $(document).ready(function() {
            $('#submit-button').click(function()
            {
                $('#analysis-card').show();
                $('#confirm-card').hide();

                {{-- For each photo to analyse, push an instance of AnalyseImageViewModel to our master view model --}}
                @foreach ($photos as $photo)
                app.analyseImage(new AnalyseImageViewModel({
                    'id': '{{ $photo->id }}',
                    'name': '{!! addslashes($photo->name) !!}',
                    'url': '{{ route('photos.re-analyse', ['id' => $photo->id, 'queue_token' => $queue_token]) }}'
                }));
                @endforeach
            });
        });
    </script>
@endpush