@extends("layout.main")
@section("content")
<div class="my-4 btn-group">
	@foreach ($product->get_tree() as $p)
		<a href='/documents/{{ $p->id }}' class="btn btn-secondary"><strong>{{ $p->title }}</strong></a>
	@endforeach
</div>
<a href='<?php print($product->refurl) ?>'><?php print($product->refurl) ?></a><br/>

@if (Auth::can_moderate())
<inlineedittext url='/api/product/{{$product->id}}' field='overview'>{{ $product->overview }}</inlineedittext>
@else
@if ($product->overview != "")
<div class="blockquote">
{!! $product->overview_md() !!}
</div>
@endif
@endif

<table class="table table-hover">

@if (Auth::can_moderate())
<tr>
	<td colspan="{{ 3 + count($product->meta()) }}">
		<div class="w-100 text-end">
			<productcontrols pid="{{ $product->id }}"></productcontrols>
		</div>
	</td>
</tr>
@endif

<tr>
	<th></th>
	<th>Title</th>
	@foreach ($product->meta() as $m)
		<th class="fit">{{ MetaType::name($m) }}</th>
	@endforeach
	<th>Order No</th>
</tr>


@if ($product->parent != null)
	<tr>
		<td class="fit"><a class="btn" href='/documents/{{ $product->parent->id }}'><i class="fa fa-level-up" aria-hidden="true"></a></td>
		@if (Auth::can_moderate())
			<td class="w-100" colspan="{{ 2 + count($product->meta()) }}">
				<dddiv droppable="true" object_id="{{$product->parent->id}}" object_type="product" callback="/api/document/dragdrop">
					<a class="btn w-100 text-start" href='/documents/{{ $product->parent->id }}'>Parent</a>
				</dddiv>
			</td>
		@else
			<td class="w-100" colspan="{{ 2 + count($product->meta()) }}"><a class="btn w-100 text-start" href='/documents/{{ $product->parent->id }}'>Parent</a></td>
		@endif
	</tr>
@endif


@foreach ($product->children as $child)
	<tr>
		<td class="fit"><a class="btn" href='/documents/{{ $child->id }}'>
			@if ($child->title == "Trash")
				<i class="fa fa-trash" aria-hidden="true"></i>
			@else
				<i class="fa fa-folder" aria-hidden="true"></i>
			@endif
		</a></td>
		@if (Auth::can_moderate())
			<td class="w-100">
				<dddiv draggable="true" droppable="true" object_id="{{ $child->id }}" object_type="product" callback="/api/document/dragdrop">
					<a class="btn w-100 text-start" href='/documents/{{$child->id}}' >
						{{$child->title}}
					</a>
				</dddiv>
			</td>
		@else
			<td class="w-100"><a class="btn w-100 text-start" href='/documents/{{$child->id}}'>{{$child->title}}</a></td>
		@endif
		<td colspan="{{ 1 + count($product->meta()) }}" class="fit"></td>
	</tr>
@endforeach

@foreach ($product->documents_sorted_by_meta() as $doc)
	<tr>
		<td class="fit">
			@if ($doc->attachments->count() > 0)
				<a class="btn" href='/document/{{$doc->id}}'><i class="fa fa-file-arrow-down text-success" aria-hidden="true"></i></a>
			@else
				<a class="btn" href='/document/{{$doc->id}}'><i class="fa fa-file-text" aria-hidden="true"></i></a>
			@endif
		</td>
		<td class="w-100">
			@if (Auth::can_moderate())
				<dddiv draggable="true" droppable="true" object_id="{{$doc->id}}" object_type="document" extra_data="{{$product->id}}" callback="/api/document/dragdrop">
					<a class="btn w-100 text-start" href='/document/{{$doc->id}}'>
						<strong>{{$doc->title}}</strong>
						{{$doc->subtitle}}
						<small>{{$doc->subsubtitle}}</small>
						@if ($doc->overview)
							<small><i title="{{ $doc->overview }}" class="ms-1 fa-solid fa-paragraph"></i></small>
						@endif
					</a>
				</dddiv>
			@else
				<a class="btn w-100 text-start" href='/document/{{$doc->id}}'>
					<strong>{{$doc->title}}</strong>
					{{$doc->subtitle}}
					<small>{{$doc->subsubtitle}}</small>
					@if ($doc->overview)
						<small><i title="{{ $doc->overview }}" class="ms-1 fa-solid fa-paragraph"></i></small>
					@endif
				</a>
			@endif
		
		</td>

		@foreach ($product->meta() as $m)
		<td class="fit">
			<a draggable="true" object_id="{{$doc->id}}" pid_from="{{$product->id}}" class="btn" href='/document/{{$doc->id}}'>
			{{ $doc->get_metadata_by_id($m) }}
			</a>
		</td>
		@endforeach
		<td class="fit">
			<a draggable="true" object_id="{{$doc->id}}" pid_from="{{$product->id}}" class="btn" href='/document/{{$doc->id}}'>
				{{ $doc->internal_id }}
			</a>
		</td>
	</tr>
@endforeach
</table>

<div class="text-center">
	<small>
		<i>
			{{ count($product->documents) }} documents
		</i>
	</small>
</div>

@endsection
