@extends('layouts.adminlte')
@section('title', $message->subject ?: 'Email')
@section('page_title', 'Visualizza Email')

@section('content')
<div class="row">
    <div class="col-md-3">
        <div class="card card-primary">
            <div class="card-body p-0">
                <div class="list-group list-group-flush">
                    <a href="{{ route('email.compose') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-plus"></i> Nuova Email
                    </a>
                    @foreach($folders as $f)
                    <a href="{{ route('email.index', $f->type) }}" 
                       class="list-group-item list-group-item-action {{ $folder === $f->type ? 'active' : '' }}">
                        <i class="fas {{ $f->icon }}"></i> {{ $f->name }}
                    </a>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <div class="col-md-9">
        <div class="card card-primary card-outline">
            <div class="card-header">
                <h3 class="card-title">{{ $message->subject ?: '(senza oggetto)' }}</h3>
                <div class="card-tools">
                    @if($message->is_starred)
                    <button class="btn btn-xs btn-warning" onclick="toggleStar({{ $message->id }})">
                        <i class="fas fa-star"></i>
                    </button>
                    @else
                    <button class="btn btn-xs btn-default" onclick="toggleStar({{ $message->id }})">
                        <i class="far fa-star"></i>
                    </button>
                    @endif
                    <a href="{{ route('email.compose', ['reply_to' => $message->id]) }}" class="btn btn-xs btn-default">
                        <i class="fas fa-reply"></i> Rispondi
                    </a>
                    <a href="{{ route('email.compose', ['forward' => $message->id]) }}" class="btn btn-xs btn-default">
                        <i class="fas fa-forward"></i> Inoltra
                    </a>
                    <form method="POST" action="{{ route('email.destroy', $message->id) }}" class="d-inline">
                        @csrf @method('DELETE')
                        <button type="submit" class="btn btn-xs btn-danger" onclick="return confirm('Sposta nel cestino?')">
                            <i class="fas fa-trash"></i>
                        </button>
                    </form>
                </div>
            </div>
            <div class="card-body">
                <div class="mailbox-read-info">
                    <h6>
                        <strong>Da:</strong> {{ $message->from_name }} &lt;{{ $message->from_email }}&gt;
                        <span class="mailbox-read-time float-right">
                            @if($message->received_at)
                                {{ $message->received_at->format('d/m/Y H:i') }}
                            @elseif($message->sent_at)
                                {{ $message->sent_at->format('d/m/Y H:i') }}
                            @endif
                        </span>
                    </h6>
                    <h6>
                        <strong>A:</strong> {{ $message->to_email }}
                        @if($message->cc)
                        <br><strong>CC:</strong> {{ $message->cc }}
                        @endif
                    </h6>
                </div>
                <hr>
                <div class="mailbox-read-message">
                    {!! $message->body_for_display !!}
                </div>
                @if($message->attachments->count() > 0)
                <hr>
                <div class="mailbox-attachments">
                    <h4>Allegati:</h4>
                    <ul class="mailbox-attachments-links">
                        @foreach($message->attachments as $attachment)
                        <li>
                            <span class="mailbox-attachment-icon">
                                <i class="fas {{ $attachment->icon }}"></i>
                            </span>
                            <div class="mailbox-attachment-info">
                                <a href="{{ route('email.attachment.download', $attachment->id) }}" class="mailbox-attachment-name">
                                    <i class="fas fa-paperclip"></i> {{ $attachment->filename }}
                                </a>
                                <span class="mailbox-attachment-size">
                                    {{ number_format($attachment->size / 1024, 1) }} KB
                                </span>
                                @if(!$attachment->is_saved_to_documenti)
                                <form method="POST" action="{{ route('email.attachment.save', [$message->id, $attachment->id]) }}" class="d-inline">
                                    @csrf
                                    <button type="submit" class="btn btn-xs btn-success" onclick="return confirm('Salvare allegato nei documenti?')">
                                        <i class="fas fa-save"></i> Salva in Documenti
                                    </button>
                                </form>
                                @else
                                <span class="badge badge-success"><i class="fas fa-check"></i> Salvato</span>
                                @endif
                            </div>
                        </li>
                        @endforeach
                    </ul>
                </div>
                @endif
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
function toggleStar(id) {
    fetch('/email/' + id + '/star', {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}' }
    }).then(() => location.reload());
}
</script>
@endsection