@php
    $embedded = $embedded ?? false;
    $documentCopyLabel = $documentCopyLabel ?? ($documentCopies['original'] ?? 'Original Copy');
    $closeUrl = $closeUrl ?? route('admin.debit-notes.index', request()->except('debit_note'));
    $debitNoteTitle = $debitNoteTitle ?? \App\Helpers\DebitNoteDocumentSettings::title($company->id ?? null);
@endphp

<div class="billing-invoice-preview {{ $embedded ? 'billing-invoice-preview--embedded' : '' }}">
    <div class="billing-invoice-preview__toolbar no-print">
        <div class="billing-invoice-preview__toolbar-left">
            @if(! $embedded)
                <a href="{{ route('admin.debit-notes.index') }}" class="btn btn-light btn-sm border" title="Back to list">
                    <i class="ri-arrow-left-line"></i>
                </a>
            @endif
            <a href="{{ route('admin.debit-notes.create') }}" class="btn btn-primary btn-sm">
                <i class="ri-add-line me-1"></i> New
            </a>
        </div>
        <div class="billing-invoice-preview__actions">
            <a href="{{ route('admin.debit-notes.edit', $debitNote) }}" class="btn btn-panel-action btn-sm">
                <i class="ri-pencil-line"></i><span>Edit</span>
            </a>
            <a href="{{ route('admin.debit-notes.pdf', $debitNote) }}" class="btn btn-panel-action btn-sm" id="debit-note-pdf-download" title="Download PDF">
                <i class="ri-file-pdf-line"></i><span>PDF</span>
            </a>
            <a href="{{ route('admin.debit-notes.print', ['debit_note' => $debitNote, 'copy' => 'original', 'autoprint' => 1]) }}"
               id="debit-note-print-link"
               target="_blank"
               rel="noopener"
               class="btn btn-panel-action btn-sm"
               title="Print (same as PDF — no browser headers)">
                <i class="ri-printer-line"></i><span>Print</span>
            </a>
            <button type="button"
                    class="btn btn-panel-action btn-sm"
                    id="debit-note-get-link-btn"
                    data-share-url="{{ route('admin.debit-notes.share-link', $debitNote) }}"
                    title="Share public link with client">
                <i class="ri-link"></i><span>Get Link</span>
            </button>
            <div class="dropdown">
                <button type="button" class="btn btn-panel-action btn-sm dropdown-toggle" data-bs-toggle="dropdown">
                    <i class="ri-more-2-line"></i><span>More</span>
                </button>
                <ul class="dropdown-menu dropdown-menu-end">
                    <li>
                        <a class="dropdown-item" href="{{ route('admin.debit-notes.create') }}?copy_from={{ $debitNote->id }}">
                            <i class="ri-file-copy-line me-2"></i>Duplicate debit note
                        </a>
                    </li>
                    <li><hr class="dropdown-divider"></li>
                    <li>
                        <form action="{{ route('admin.debit-notes.destroy', $debitNote) }}" method="POST" onsubmit="return confirm('Delete this debit note?')">
                            @csrf @method('DELETE')
                            <button type="submit" class="dropdown-item text-danger">
                                <i class="ri-delete-bin-line me-2"></i>Delete
                            </button>
                        </form>
                    </li>
                </ul>
            </div>
            <a href="{{ route('admin.debit-notes.create') }}?copy_from={{ $debitNote->id }}" class="btn btn-info btn-sm text-white">
                <i class="ri-file-copy-line me-1"></i> Copy
            </a>
            <a href="{{ $closeUrl }}" class="btn btn-light btn-sm border-0" title="Close">
                <i class="ri-close-line fs-18"></i>
            </a>
        </div>
    </div>

    <div class="billing-invoice-preview__body row g-0">
        <div class="col-lg-3 billing-invoice-preview__sidebar no-print">
            <div class="billing-invoice-preview__sidebar-inner">
                <span class="billing-invoice-preview__badge">
                    <i class="ri-eye-line"></i> Live preview
                </span>

                <div class="billing-invoice-preview__panel">
                    <p class="billing-invoice-preview__panel-title">Document copy</p>
                    <select class="form-select form-select-sm" id="document-copy-select">
                        @foreach($documentCopies as $key => $label)
                            <option value="{{ $key }}" @selected($key === 'original')>{{ $label }}</option>
                        @endforeach
                    </select>
                    <p class="billing-invoice-preview__hint">Label shown on the debit note and included in the PDF download.</p>
                    <p class="billing-invoice-preview__hint mb-0">Use <strong>Print</strong> (not the browser Ctrl+P) for a clean PDF-style printout.</p>
                </div>

                <div class="billing-invoice-preview__panel">
                    <p class="billing-invoice-preview__panel-title">Preview zoom</p>
                    <div class="billing-zoom-toggle btn-group w-100" role="group">
                        <button type="button" class="btn btn-sm btn-outline-secondary" data-zoom="75">75%</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary active" data-zoom="100">100%</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary" data-zoom="125">125%</button>
                    </div>
                </div>

                <div class="billing-invoice-preview__panel">
                    <p class="billing-invoice-preview__panel-title">Page size</p>
                    <div class="billing-layout-toggle btn-group w-100" role="group">
                        <button type="button" class="btn btn-sm btn-outline-secondary active" data-layout="a4">A4</button>
                        <button type="button" class="btn btn-sm btn-outline-secondary" data-layout="a5">A5</button>
                    </div>
                </div>

                <div class="d-grid gap-2">
                    <a href="{{ route('admin.preferences.debit-notes') }}" class="btn btn-light btn-sm border text-start">
                        <i class="ri-settings-3-line me-2"></i> Debit Note settings
                    </a>
                </div>
            </div>
        </div>

        <div class="col-lg-9 billing-invoice-preview__main">
            <div class="billing-invoice-preview__canvas-wrap">
                <div class="billing-invoice-preview__canvas p-3 p-lg-4" id="debit-note-preview-canvas" data-zoom="100">
                    @include('admin.debit-notes.partials._document', [
                        'debitNoteTitle' => $debitNoteTitle,
                        'documentCopyLabel' => $documentCopyLabel,
                        'layout' => 'a4',
                        'logoSrc' => $logoSrc ?? \App\Helpers\CompanyMedia::imageSource($company->logo_path ?? null),
                        'signatureSrc' => $signatureSrc ?? \App\Helpers\CompanyMedia::imageSource($company->signature_image_path ?? null),
                    ])
                </div>
            </div>
        </div>
    </div>
</div>

@include('admin.debit-notes.partials.share-link-modal')
