@extends('layouts.app')
@section('title','Document Library')
@section('crumbs','<b>Document Library</b>')
@section('content')
<div class="card"><div class="card-h"><h2>📁 Document Library</h2>
<div class="sp no-print">
<span class="muted">{{ $groups->total() }} document(s) · {{ $totalFiles }} file(s) · {{ number_format($totalBytes/1048576,1) }} MB</span>
@if(perm('docs.create'))<a class="btn btn-sm" href="{{ route('docs-library.create') }}">＋ File document</a>@endif
</div></div>
<div class="card-b">
<form method="GET" action="{{ route('docs-library.index') }}" class="frow no-print" style="margin-bottom:10px">
<input name="q" value="{{ $req->q }}" placeholder="Search filename…" style="max-width:260px">
<select name="owner" style="max-width:200px">
<option value="">All owners</option>
<option value="unfiled" {{ $req->owner==='unfiled'?'selected':'' }}>Unfiled</option>
@foreach($owners as $class => $o)<option value="{{ $class }}" {{ $req->owner===$class?'selected':'' }}>{{ $o[0] }}</option>@endforeach
</select>
<select name="kind" style="max-width:140px">
<option value="">All types</option>
@foreach(['pdf'=>'PDF','image'=>'Image','sheet'=>'Sheet','doc'=>'Doc','other'=>'Other'] as $k=>$l)<option value="{{ $k }}" {{ $req->kind===$k?'selected':'' }}>{{ $l }}</option>@endforeach
</select>
<button class="btn btn-sm">Filter</button>
@if($req->q || $req->owner || $req->kind)<a class="btn btn-sm btn-ghost" href="{{ route('docs-library.index') }}">Reset</a>@endif
</form>
<div class="twrap"><table class="tbl">
<thead><tr><th>Document</th><th>Linked to</th><th>Type</th><th>Size</th><th>Uploaded</th><th>By</th><th class="no-print">Actions</th></tr></thead>
<tbody>
@forelse($groups as $g)
@php $a = $g['latest']; [$ownerLabel, $ownerUrl] = \App\Http\Controllers\DocumentLibraryController::ownerInfo($a); @endphp
<tr>
<td><b>{{ $a->file_name }}</b>
@if($g['count']>1)<span class="bdg b-info" title="{{ $g['count'] }} versions">v{{ $g['count'] }}</span>@endif
@if($g['count']>1)
<details><summary class="muted" style="cursor:pointer;font-size:12px">Version history</summary>
<table class="tbl" style="margin-top:4px"><tbody>
@foreach($g['versions'] as $vi => $v)
<tr><td style="white-space:nowrap">v{{ $g['count']-$vi }}</td><td>{{ $v->created_at->format('d M Y H:i') }}</td>
<td>{{ $v->uploader->name ?? '—' }}</td><td>{{ number_format(($v->size??0)/1024,1) }} KB</td>
<td class="no-print"><a class="btn btn-sm btn-ghost" href="{{ route('docs-library.download',$v->id) }}">⬇</a>
@if(perm('docs.delete'))<form method="POST" action="{{ route('docs-library.destroy',$v->id) }}" data-confirm="Delete v{{ $g['count']-$vi }} of {{ $v->file_name }}?" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-danger">Del</button></form>@endif</td></tr>
@endforeach
</tbody></table></details>
@endif
</td>
<td>@if($ownerUrl)<a href="{{ $ownerUrl }}">{{ $ownerLabel }}</a>@else<span class="muted">{{ $ownerLabel }}</span>@endif</td>
<td>{{ \App\Http\Controllers\DocumentLibraryController::kindOf($a->mime) }}</td>
<td>{{ number_format(($a->size??0)/1024,1) }} KB</td>
<td style="white-space:nowrap">{{ $a->created_at->format('d M Y H:i') }}</td>
<td>{{ $a->uploader->name ?? '—' }}</td>
<td class="no-print" style="white-space:nowrap">
<a class="btn btn-sm btn-ghost" href="{{ route('docs-library.download',$a->id) }}">⬇ Download</a>
@if(perm('docs.delete'))<form method="POST" action="{{ route('docs-library.destroy',$a->id) }}" data-confirm="Delete {{ $a->file_name }} (latest version)?" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-danger">Del</button></form>@endif
</td>
</tr>
@empty
<tr><td colspan="7" class="muted">No documents filed yet. @if(perm('docs.create'))<a href="{{ route('docs-library.create') }}">File the first one →</a>@endif</td></tr>
@endforelse
</tbody></table></div>
@if($groups->hasPages())<div style="margin-top:10px">{{ $groups->links() }}</div>@endif
</div></div>
@endsection
