@extends('layouts.adminlte')
@section('title', 'Modifica Documento')
@section('page_title', 'Modifica Documento')

@section('content')
<form action="/documenti/{{ $documento->id }}" method="POST">
    @csrf @method('PUT')
    <div class="row">
        <div class="col-md-8">
            <div class="card card-primary">
                <div class="card-header">
                    <h3 class="card-title"><i class="fas fa-file mr-2"></i>Dati Documento</h3>
                </div>
                <div class="card-body">
                    <div class="form-group">
                        <label>Nome *</label>
                        <input type="text" name="nome_file" class="form-control" value="{{ old('nome_file', $documento->nome_file) }}" required>
                    </div>
                    <div class="form-group">
                        <label>Tipologia *</label>
                        <select name="tipologia" class="form-control" required>
                            @foreach($tipologie as $tip)
                                <option value="{{ $tip->nome }}" {{ $documento->tipologia === $tip->nome ? 'selected' : '' }}>{{ $tip->descrizione ?? ucfirst($tip->nome) }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>Contesto *</label>
                        <select name="contesto_tipo" id="contesto_tipo" class="form-control" onchange="aggiornaTarget()">
                            <option value="">Seleziona tipo...</option>
                            <option value="individuo" {{ $documento->visibilita === 'individuo' ? 'selected' : '' }}>Individuo</option>
                            <option value="gruppo" {{ $documento->visibilita === 'gruppo' ? 'selected' : '' }}>Gruppo</option>
                            <option value="evento" {{ $documento->visibilita === 'evento' ? 'selected' : '' }}>Evento</option>
                            <option value="mailing" {{ $documento->visibilita === 'mailing' ? 'selected' : '' }}>Mailing List</option>
                        </select>
                        <input type="hidden" name="visibilita" id="visibilita" value="{{ $documento->visibilita }}">
                    </div>
                    <div class="form-group" id="target_container" style="{{ in_array($documento->visibilita, ['individuo', 'gruppo', 'evento', 'mailing']) ? '' : 'display:none;' }}">
                        <label>Riferimento *</label>
                        <select name="visibilita_target_id" id="visibilita_target_id" class="form-control">
                            @if($documento->visibilita === 'individuo')
                                @foreach($individui as $ind)
                                    <option value="{{ $ind->id }}" {{ $documento->visibilita_target_id == $ind->id ? 'selected' : '' }}>{{ $ind->cognome }} {{ $ind->nome }}</option>
                                @endforeach
                            @elseif($documento->visibilita === 'gruppo')
                                @foreach($gruppi as $grp)
                                    <option value="{{ $grp->id }}" {{ $documento->visibilita_target_id == $grp->id ? 'selected' : '' }}>{{ $grp->nome }}</option>
                                @endforeach
                            @elseif($documento->visibilita === 'evento')
                                @foreach($eventi as $evt)
                                    <option value="{{ $evt->id }}" {{ $documento->visibilita_target_id == $evt->id ? 'selected' : '' }}>{{ $evt->nome_evento }}</option>
                                @endforeach
                            @elseif($documento->visibilita === 'mailing')
                                @foreach($mailingLists as $ml)
                                    <option value="{{ $ml->id }}" {{ $documento->visibilita_target_id == $ml->id ? 'selected' : '' }}>{{ $ml->nome }}</option>
                                @endforeach
                            @endif
                        </select>
                        <input type="hidden" name="visibilita_target_type" id="visibilita_target_type" value="">
                    </div>
                    <div class="form-group">
                        <label>Note</label>
                        <textarea name="note" class="form-control" rows="3">{{ old('note', $documento->note) }}</textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"><i class="fas fa-info-circle mr-2"></i>Informazioni</h3>
                </div>
                <div class="card-body">
                    <table class="table table-sm table-borderless">
                        <tr>
                            <td style="width: 100px;"><strong>File:</strong></td>
                            <td>{{ $documento->file_path }}</td>
                        </tr>
                        <tr>
                            <td><strong>Tipo MIME:</strong></td>
                            <td>{{ $documento->mime_type ?? '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Dimensione:</strong></td>
                            <td>{{ $documento->dimensione ? number_format($documento->dimensione / 1024, 1) . ' KB' : '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Creato:</strong></td>
                            <td>{{ $documento->created_at->format('d/m/Y H:i') }}</td>
                        </tr>
                        @if($documento->user)
                        <tr>
                            <td><strong>Utente:</strong></td>
                            <td>{{ $documento->user->name }}</td>
                        </tr>
                        @endif
                    </table>
                    @if($documento->file_path)
                    <hr>
                    <a href="/documenti/{{ $documento->id }}/download" class="btn btn-primary btn-block">
                        <i class="fas fa-download mr-1"></i> Scarica
                    </a>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <div class="mt-3">
        <button type="submit" class="btn btn-success">
            <i class="fas fa-save mr-1"></i> Salva Modifiche
        </button>
        <a href="/documenti" class="btn btn-secondary">
            <i class="fas fa-arrow-left mr-1"></i> Torna all'elenco
        </a>
    </div>
</form>

<script>
function aggiornaTarget() {
    var tipo = document.getElementById('contesto_tipo').value;
    var container = document.getElementById('target_container');
    var targetSelect = document.getElementById('visibilita_target_id');
    var visibilita = document.getElementById('visibilita');
    var targetType = document.getElementById('visibilita_target_type');

    visibilita.value = tipo;
    targetType.value = '';

    if (tipo) {
        container.style.display = 'block';
        targetSelect.innerHTML = '<option value="">Caricamento...</option>';

        var xhr = new XMLHttpRequest();
        xhr.open('GET', '/documenti/options?tipo=' + tipo, true);
        xhr.onload = function() {
            if (xhr.status === 200) {
                targetSelect.innerHTML = '<option value="">Seleziona...</option>' + xhr.responseText;
            }
        };
        xhr.send();
    } else {
        container.style.display = 'none';
        targetSelect.innerHTML = '<option value="">Seleziona prima il tipo...</option>';
        targetSelect.value = '';
    }
}
</script>
@endsection