@extends('layouts.app')
@section('content')
<main class="nxl-container"><div class="nxl-content"><div class="page-header"><h5>{{ ucwords(str_replace('-',' ',$module)) }} Files — #{{ $record->id }}</h5></div>
<div class="main-content">
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
@if(auth()->user()->is_admin || auth()->user()->can('manage_crm_files'))<div class="card mb-4"><div class="card-header"><h5 class="card-title">Storage and security policy</h5></div><div class="card-body"><form method="POST" action="{{ route('crm-files.settings') }}" class="row g-3">@csrf @method('PUT')
<div class="col-md-3"><label class="form-label">Quota (MB)</label><input type="number" min="1" max="102400" class="form-control" name="quota_mb" value="{{ round($fileSettings->quota_bytes/1024/1024) }}"><small>{{ number_format($storageUsed/1024/1024,2) }} MB currently stored, including trash</small></div>
<div class="col-md-3"><label class="form-label">Duplicate policy</label><select class="form-control" name="duplicate_policy"><option value="reject" @selected($fileSettings->duplicate_policy==='reject')>Reject identical files</option><option value="allow" @selected($fileSettings->duplicate_policy==='allow')>Allow identical files</option></select></div>
<div class="col-md-3"><label class="form-label">Trash retention (days)</label><input type="number" min="1" max="3650" class="form-control" name="trash_retention_days" value="{{ $fileSettings->trash_retention_days }}"></div><div class="col-md-3 d-flex align-items-end"><button class="btn btn-primary">Save policy</button></div>
</form></div></div>@endif
<div class="card mb-4"><div class="card-header"><h5 class="card-title">Upload and relate a file</h5></div><div class="card-body">
<form method="POST" enctype="multipart/form-data" action="{{ route('crm-files.store', [$module,$record->id]) }}" class="row g-3">@csrf
<div class="col-md-3"><label class="form-label">Display name</label><input class="form-control" name="name"></div>
<div class="col-md-2"><label class="form-label">Category</label><input class="form-control" name="category"></div>
<div class="col-md-3"><label class="form-label">Description</label><input class="form-control" name="description"></div>
<div class="col-md-2"><label class="form-label">Access</label><select class="form-control" name="access_level"><option value="business">Business</option><option value="team">Team</option><option value="private">Private</option></select></div>
<div class="col-md-2"><label class="form-label">File</label><input class="form-control" type="file" name="file" required></div>
<div class="col-md-4"><label class="form-label">Related Module</label><select class="form-control" name="related_module" id="relatedModule">
@foreach($relatedRecords as $relatedModule=>$records)<option value="{{ $relatedModule }}" @selected($relatedModule===$module)>{{ ucwords(str_replace('-',' ',$relatedModule)) }}</option>@endforeach
</select></div>
<div class="col-md-6"><label class="form-label">Related Record</label><select class="form-control" name="related_record_id" id="relatedRecord">
@foreach($relatedRecords as $relatedModule=>$records) @foreach($records as $recordId=>$label)
<option value="{{ $recordId }}" data-module="{{ $relatedModule }}" @selected($relatedModule===$module && $recordId===$record->id)>{{ $label }} (#{{ $recordId }})</option>
@endforeach @endforeach
</select></div>
<div class="col-md-2 d-flex align-items-end"><button class="btn btn-primary w-100">Upload</button></div>
</form></div></div>

<div class="card"><x-server-list-toolbar placeholder="Search files by name, category, description, or filename" :filters="['date_from'=>['label'=>'From','type'=>'date'],'date_to'=>['label'=>'To','type'=>'date']]" /><div class="card-body table-responsive"><table class="table align-middle"><thead><tr><th>Name</th><th>Related Record</th><th>Access</th><th>Version</th><th>File</th><th>Uploaded</th><th>Actions</th></tr></thead><tbody>
@forelse($files as $file)<tr class="{{ $file->trashed() ? 'table-secondary' : '' }}">
<td><strong>{{ $file->name }}</strong><div class="small text-muted">{{ $file->category }} · {{ $file->description }}</div></td>
<td>{{ class_basename($file->fileable_type) }} #{{ $file->fileable_id }}</td><td>{{ ucfirst($file->access_level) }}<small class="d-block {{ $file->scan_status==='clean'?'text-success':'text-danger' }}">Scan: {{ ucfirst($file->scan_status) }}</small></td>
<td>v{{ $file->version }} @if($file->is_current)<span class="badge bg-success">Current</span>@endif</td>
<td>{{ $file->original_name }}<div class="small text-muted">{{ number_format($file->size/1024,1) }} KB</div></td>
<td>{{ $file->created_at->format('M d, Y H:i') }}<div class="small">{{ $file->uploader?->name }}</div></td>
<td>
@if($file->trashed())<form method="POST" action="{{ route('crm-files.restore',$file->id) }}">@csrf<button class="btn btn-sm btn-success">Restore</button></form>@if(auth()->user()->is_admin || auth()->user()->can('manage_crm_files'))<form method="POST" action="{{ route('crm-files.force-destroy',$file->id) }}" class="mt-1">@csrf @method('DELETE')<button class="btn btn-sm btn-danger">Delete permanently</button></form>@endif
@else
<div class="d-flex gap-1 flex-wrap"><a class="btn btn-sm btn-light" target="_blank" href="{{ route('crm-files.view',$file) }}">View</a>
<a class="btn btn-sm btn-light" href="{{ route('crm-files.download',$file) }}">Download</a>
@php($previous = $files->first(fn($candidate) => ($candidate->root_file_id ?: $candidate->id) === ($file->root_file_id ?: $file->id) && $candidate->version === $file->version-1))
@if($previous)<a class="btn btn-sm btn-info" href="{{ route('crm-files.compare', [$file,$previous]) }}">Compare</a>@endif
</div>
@if($file->is_current)
<form method="POST" enctype="multipart/form-data" action="{{ route('crm-files.replace',$file) }}" class="d-flex gap-1 mt-2">@csrf
<input class="form-control form-control-sm" type="file" name="file" required><button class="btn btn-sm btn-warning">Replace</button></form>
@endif
<details class="mt-2"><summary class="small c-pointer">Grant access</summary>
<form method="POST" action="{{ route('crm-files.permissions',$file) }}" class="border rounded p-2 mt-1">@csrf
<select class="form-control form-control-sm mb-1" name="user_id">@foreach($users as $user)<option value="{{ $user->id }}">{{ $user->name }}</option>@endforeach</select>
@foreach(['can_view'=>'View','can_download'=>'Download','can_replace'=>'Replace','can_delete'=>'Delete'] as $field=>$label)
<label class="me-2 small"><input type="checkbox" name="{{ $field }}" value="1"> {{ $label }}</label>@endforeach
<button class="btn btn-sm btn-primary">Save</button></form></details>
<form method="POST" action="{{ route('crm-files.destroy',$file) }}" class="mt-2">@csrf @method('DELETE')<button class="btn btn-sm btn-danger">Delete</button></form>
@endif
</td></tr>@empty<tr><td colspan="7">No files uploaded.</td></tr>@endforelse
</tbody></table></div></div></div></div></main>
<script>
document.addEventListener('DOMContentLoaded', () => {
 const moduleSelect=document.getElementById('relatedModule'), recordSelect=document.getElementById('relatedRecord');
 const filter=()=>{ let first=null; [...recordSelect.options].forEach(o=>{o.hidden=o.dataset.module!==moduleSelect.value;if(!o.hidden&&!first)first=o;}); if(first)recordSelect.value=first.value; };
 moduleSelect.addEventListener('change',filter); filter();
});
</script>
@endsection
