@extends('layouts.app')

@section('title', 'Invoice Details')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-success text-white">
                    <h4 class="card-title mb-0">
                        <i class="fas fa-file-invoice-dollar"></i> Invoice Details
                    </h4>
                    <div class="card-tools">
                        <a href="{{ route('accounts.invoices.pdf', $invoice->id) }}?action=download" class="btn btn-light btn-sm">
                            <i class="fas fa-download"></i> Download PDF
                        </a>
                        <a href="{{ route('accounts.invoices.print', $invoice->id) }}" class="btn btn-light btn-sm" target="_blank">
                            <i class="fas fa-print"></i> Print
                        </a>
                    </div>
                </div>

                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            <i class="fas fa-check-circle"></i> {{ session('success') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    @endif

                    @if(session('show_print_prompt'))
                        <div class="modal fade" id="printPromptModal" tabindex="-1" aria-labelledby="printPromptModalLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="printPromptModalLabel">Invoice Created Successfully!</h5>
                                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                    </div>
                                    <div class="modal-body">
                                        <p>Would you like to print or download the invoice?</p>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Skip</button>
                                        <a href="{{ route('accounts.invoices.pdf', $invoice->id) }}" class="btn btn-primary" target="_blank">
                                            <i class="fas fa-download"></i> Download PDF
                                        </a>
                                        <a href="{{ route('accounts.invoices.pdf', $invoice->id) }}" class="btn btn-info" target="_blank">
                                            <i class="fas fa-print"></i> Print
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <script>
                            document.addEventListener('DOMContentLoaded', function() {
                                var printModal = new bootstrap.Modal(document.getElementById('printPromptModal'));
                                printModal.show();
                            });
                        </script>
                    @endif

                    <div class="row">
                        <div class="col-md-6">
                            <h5 class="text-success">Invoice Information</h5>
                            <table class="table table-borderless">
                                <tr>
                                    <th width="150">Invoice Number:</th>
                                    <td><strong class="text-primary">{{ $invoice->invoice_number }}</strong></td>
                                </tr>
                                <tr>
                                    <th>Invoice Date:</th>
                                    <td>{{ \Carbon\Carbon::parse($invoice->invoice_date)->format('M d, Y') }}</td>
                                </tr>
                                <tr>
                                    <th>Due Date:</th>
                                    <td>{{ $invoice->due_date ? \Carbon\Carbon::parse($invoice->due_date)->format('M d, Y') : 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Status:</th>
                                    <td>
                                        <span class="badge bg-{{ $invoice->status === 'paid' ? 'success' : ($invoice->status === 'sent' ? 'info' : 'secondary') }}">
                                            {{ ucfirst($invoice->status) }}
                                        </span>
                                    </td>
                                </tr>
                            </table>
                        </div>

                        <div class="col-md-6">
                            <h5 class="text-success">Customer Information</h5>
                            <table class="table table-borderless">
                                <tr>
                                    <th width="150">Customer:</th>
                                    <td>{{ $invoice->customer->customer_account ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Customer Code:</th>
                                    <td>{{ $invoice->customer->customer_code ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Created By:</th>
                                    <td>{{ $invoice->creator->name ?? 'N/A' }}</td>
                                </tr>
                                <tr>
                                    <th>Created Date:</th>
                                    <td>{{ $invoice->created_at->format('M d, Y H:i') }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    @if($invoice->description)
                        <div class="row mt-3">
                            <div class="col-12">
                                <h6 class="text-success">Description</h6>
                                <p class="border p-2 rounded">{{ $invoice->description }}</p>
                            </div>
                        </div>
                    @endif

                    <div class="row mt-4">
                        <div class="col-12">
                            <h5 class="text-success">Invoice Items</h5>
                            <div class="table-responsive">
                                <table class="table table-bordered">
                                    <thead class="table-success">
                                        <tr>
                                            <th>Item</th>
                                            <th class="text-center">Quantity</th>
                                            <th class="text-end">Unit Price</th>
                                            <th class="text-center">Discount (%)</th>
                                            <th class="text-end">Line Total</th>
                                        </tr>
                                    </thead>
                                    <tbody id="invoice-items">
                                        @forelse($invoice->items ?? [] as $item)
                                            <tr>
                                                <td>{{ $item->item }}</td>
                                                <td class="text-center">{{ number_format($item->quantity, 2) }}</td>
                                                <td class="text-end">${{ number_format($item->unit_price, 2) }}</td>
                                                <td class="text-center">{{ number_format($item->discount, 2) }}%</td>
                                                <td class="text-end">${{ number_format($item->line_total, 2) }}</td>
                                            </tr>
                                        @empty
                                            <tr>
                                                <td colspan="5" class="text-center text-muted">
                                                    <em>No invoice items found</em>
                                                </td>
                                            </tr>
                                        @endforelse
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>

                    <div class="row mt-4">
                        <div class="col-md-6 offset-md-6">
                            <table class="table table-borderless">
                                <tr>
                                    <th class="text-end">Subtotal:</th>
                                    <td class="text-end">${{ number_format($invoice->subtotal, 2) }}</td>
                                </tr>
                                <tr>
                                    <th class="text-end">Discount:</th>
                                    <td class="text-end">-${{ number_format($invoice->discount_total, 2) }}</td>
                                </tr>
                                <tr>
                                    <th class="text-end">Tax (10%):</th>
                                    <td class="text-end">${{ number_format($invoice->tax_amount, 2) }}</td>
                                </tr>
                                <tr class="border-top">
                                    <th class="text-end h5 text-success">Total:</th>
                                    <td class="text-end h5 text-success">${{ number_format($invoice->total, 2) }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>

                <div class="card-footer">
                    <div class="d-flex justify-content-end">
                        <a href="{{ route('accounts.invoices.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Invoices
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
@media print {
    .card-tools, .card-footer, .alert {
        display: none !important;
    }
    .container-fluid {
        margin: 0;
        padding: 0;
    }
    .card {
        border: none !important;
        box-shadow: none !important;
    }
}
</style>
@endsection
