@extends("layout.main")
@section("content")
@if (Auth::can_moderate())
	<jobdisplay source="document:{{ $doc->id }}"></jobdisplay>
@endif
<h1 class="text-center">{{ $doc->title }}</h1>
<h2 class="text-center">{{ $doc->subtitle }}</h2>
<h3 class="text-center">{{ $doc->subsubtitle }}</h3>

<div class="row" id="droppable">
    <div class="col-lg-6 col-12">
	@if (Auth::can_moderate())
		<div class="btn-group">
			<documentedit docid="{{ $doc->id }}"></documentedit>
			<a href="/upload/attachment/{{ $doc->id }}" class="btn btn-primary" title="Upload attachment"><i class="fa-solid fa-file-arrow-up"></i></a>
			<a href="/overview/create/{{ $doc->id }}" class="btn btn-primary" title="Create Overview with AI"><i class="fa-solid fa-wand-magic-sparkles"></i></a>
			<a href="/merge/{{ $doc->id }}" class="btn btn-primary" title="Merge Revisions"><i class="fa fa-link"></i></a>
			<a href="/explode/{{ $doc->id }}" class="btn btn-primary" title="Separate Revisions"><i class="fa fa-unlink"></i></a>
		</div>
	@endif
        <h5 class="text-center">Order Number: {{ $doc->internal_id }}</h5>

		@if (Auth::can_moderate())
			<table class="table">
				<tbody>
					@foreach ($doc->metadata as $meta)
						@php
							$meta->load("metadata");
						@endphp
						<tr>
							<td class="w-25">{{ $meta->metadata->name }}</td>
                        	<td class="w-75"><inlineedit value="{{ $meta->data }}" url="/api/document/{{ $doc->id }}/metadata/{{ $meta->metadata->id }}" field="data"/></td>
							<td style="width: 0"><a class="btn btn-danger px-1 py-0" href="/document/{{ $doc->id }}/metadata/{{ $meta->metadata->id }}/delete">
							<i class="fa fa-trash"></i></a></td>
						</tr>
					@endforeach
				</tbody>
			</table>
			<itemadder list="/api/document/{{ $doc->id }}/available_metadata" target="/api/document/{{ $doc->id }}/metadata" method="PUT"></itemadder>
		@else
			<table class="table">
				<tbody>
					@foreach ($doc->metadata as $meta)
						@php
							$meta->load("metadata");
						@endphp
						<tr>
							<td class="w-25">{{ $meta->metadata->name }}</td>
							<td class="w-75">{{ $meta->data }}</td>
						</tr>
					@endforeach
				</tbody>
			</table>
		@endif

	<hr/>
	@if ($doc->attachments->count() > 0)
		<h5>Attachments</h5>
		<table class="table w-100">
			<thead>
				<th>Filename</th>
				<th>Size</th>
			</thead>
			<tbody>
				@foreach ($doc->attachments as $f)
					<tr>
						<td><a href='/attachment/{{$doc->id}}/{{$f->basename()}}'>{{ $f->basename() }}</a></td>
						<td>{{ $f->size() }}</td>
					</tr>
				@endforeach
			</tbody>
		</table>
	@endif

        <ul class="list-group">
@foreach ($doc->products as $prod)
            <li class="list-group-item">
				@if (Auth::can_moderate())
					<a class="btn btn-danger px-1 py-0" href='/del_docproduct/{{$doc->id}}/{{$prod->id}}'>
						<i class="fa fa-close"></i>
					</a>
					<a class="btn w-90 text-start" href='/documents/{{ $prod->id }}'>
						{{ $prod->full_path }}
					</a>
				@else
					<a class="btn w-100 text-start" href='/documents/{{ $prod->id }}'>
						{{ $prod->full_path }}
					</a>
				@endif
			</li>
@endforeach
		</ul>

		@if ($doc->overview)
			<div class="overflow-y-auto p-3">
				{!! $doc->overview_md() !!}
			</div>
		@endif
	</div>

	<div class="col-lg-6 col-12">

	<div class="row">
	@foreach ($doc->revisions as $rev)
		@component("thumbnail-50", ["rev" => $rev])
		@endcomponent
	@endforeach
	</div>
	</div>


	@if (count($doc->related) > 0)
       	<div class="col-12">
       	    <h5>Related Documents</h5>
           	<table class="table">
				@foreach ($doc->related as $r)
                    <tr>
                        <td>
                            <a href='/document/{{ $r->id }}'>
                                <strong>{{ $r->title }}</strong>
                                {{ $r->subtitle }}
                                <small>{{ $r->subsubtitle }}</small>
                            </a>
                        </td>
                        <td>
                            <a href='/document/{{ $r->id }}'>
								{{ $r->internal_id }}
							</a>
						</td>
                    </tr>
				@endforeach
			</table>
		</div>
	@endif
</div>

@endsection
