@extends('layouts.app')

@section('title', 'Purchase Invoice - ' . $purchase->purchase_code)

@section('actions')
    <a href="{{ route('purchases.index') }}" class="btn btn-secondary">
        <i class="fas fa-arrow-left"></i> Back
    </a>
    <button onclick="window.print()" class="btn btn-primary">
        <i class="fas fa-print"></i> Print
    </button>
    <a href="{{ route('purchases.print-invoice', $purchase) }}" target="_blank" class="btn btn-success">
        <i class="fas fa-file-pdf"></i> PDF
    </a>
@endsection

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <!-- Invoice Card -->
            <div class="card">
                <div class="card-body">
                    <!-- Company Header -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <h2 class="text-primary">Purchase Invoice</h2>
                            <h4 class="mb-0">{{ $purchase->purchase_code }}</h4>
                            <p class="text-muted mb-0">Date: {{ $purchase->purchase_date->format('d M, Y') }}</p>
                            
                            @if($purchase->status == 'received')
                            <p class="text-muted">Received: {{ $purchase->received_date ? $purchase->received_date->format('d M, Y') : '' }}</p>
                            @endif
                        </div>
                        <div class="col-md-6 text-right">
                            <h4 class="mb-0">{{ config('app.name', 'Laravel') }}</h4>
                            <p class="text-muted mb-0">Purchase Management System</p>
                            <p class="text-muted mb-0">{{ date('d M, Y h:i A') }}</p>
                        </div>
                    </div>
                    
                    <!-- Supplier & Company Info -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <h5>Supplier Information:</h5>
                            <div class="bg-light p-3 rounded">
                                <strong>{{ $purchase->supplier->name ?? 'N/A' }}</strong><br>
                                @if($purchase->supplier->company_name)
                                {{ $purchase->supplier->company_name }}<br>
                                @endif
                                Phone: {{ $purchase->supplier->phone ?? 'N/A' }}<br>
                                Email: {{ $purchase->supplier->email ?? 'N/A' }}<br>
                                Address: {{ $purchase->supplier->address ?? 'N/A' }}
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <h5>Purchase Information:</h5>
                            <div class="bg-light p-3 rounded">
                                <strong>Purchase Code:</strong> {{ $purchase->purchase_code }}<br>
                                <strong>Reference:</strong> {{ $purchase->reference_no ?? 'N/A' }}<br>
                                <strong>Warehouse:</strong> {{ $purchase->warehouse->name ?? 'Main Warehouse' }}<br>
                                <strong>Created By:</strong> {{ $purchase->creator->name ?? 'System' }}<br>
                                <strong>Payment Method:</strong> {{ ucfirst($purchase->payment_method) }}
                            </div>
                        </div>
                    </div>
                    
                    <!-- Items Table -->
                    <div class="table-responsive mb-4">
                        <table class="table table-bordered">
                            <thead class="thead-dark">
                                <tr>
                                    <th>#</th>
                                    <th>Product</th>
                                    <th>Code</th>
                                    <th>Quantity</th>
                                    <th>Unit Cost</th>
                                    <th>Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($purchase->items as $index => $item)
                                <tr>
                                    <td>{{ $index + 1 }}</td>
                                    <td>
                                        <strong>{{ $item->product_name }}</strong>
                                        @if($item->batch_no)
                                        <br><small class="text-muted">Batch: {{ $item->batch_no }}</small>
                                        @endif
                                        @if($item->expiry_date)
                                        <br><small class="text-muted">Expiry: {{ $item->expiry_date->format('d M, Y') }}</small>
                                        @endif
                                    </td>
                                    <td>{{ $item->product_code }}</td>
                                    <td>{{ number_format($item->quantity, 2) }}</td>
                                    <td class="text-right">৳{{ number_format($item->unit_cost, 2) }}</td>
                                    <td class="text-right">৳{{ number_format($item->total_cost, 2) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td colspan="4" rowspan="5">
                                        <strong>Notes:</strong><br>
                                        {{ $purchase->notes ?? 'No notes available.' }}
                                    </td>
                                    <td class="text-right"><strong>Subtotal:</strong></td>
                                    <td class="text-right">৳{{ number_format($purchase->subtotal, 2) }}</td>
                                </tr>
                                
                                @if($purchase->discount_amount > 0)
                                <tr>
                                    <td class="text-right"><strong>Discount:</strong></td>
                                    <td class="text-right text-danger">-৳{{ number_format($purchase->discount_amount, 2) }}</td>
                                </tr>
                                @endif
                                
                                @if($purchase->tax_amount > 0)
                                <tr>
                                    <td class="text-right"><strong>Tax ({{ $purchase->tax_rate ?? 0 }}%):</strong></td>
                                    <td class="text-right">+৳{{ number_format($purchase->tax_amount, 2) }}</td>
                                </tr>
                                @endif
                                
                                @if($purchase->shipping_cost > 0)
                                <tr>
                                    <td class="text-right"><strong>Shipping:</strong></td>
                                    <td class="text-right">+৳{{ number_format($purchase->shipping_cost, 2) }}</td>
                                </tr>
                                @endif
                                
                                <tr>
                                    <td class="text-right"><strong>Grand Total:</strong></td>
                                    <td class="text-right text-success">
                                        <h4 class="mb-0">৳{{ number_format($purchase->grand_total, 2) }}</h4>
                                    </td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    
                    <!-- Payment Summary -->
                    <div class="row">
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">Payment Summary</h5>
                                </div>
                                <div class="card-body">
                                    <div class="row">
                                        <div class="col-6">
                                            <strong>Grand Total:</strong><br>
                                            <strong>Paid Amount:</strong><br>
                                            <strong>Due Amount:</strong>
                                        </div>
                                        <div class="col-6 text-right">
                                            ৳{{ number_format($purchase->grand_total, 2) }}<br>
                                            ৳{{ number_format($purchase->paid_amount, 2) }}<br>
                                            <span class="{{ $purchase->due_amount > 0 ? 'text-danger' : 'text-success' }}">
                                                ৳{{ number_format($purchase->due_amount, 2) }}
                                            </span>
                                        </div>
                                    </div>
                                    
                                    <hr>
                                    
                                    <div class="text-center">
                                        <h4>
                                            <span class="badge badge-{{ $purchase->payment_status == 'paid' ? 'success' : ($purchase->payment_status == 'partial' ? 'warning' : 'danger') }}">
                                                Payment Status: {{ strtoupper($purchase->payment_status) }}
                                            </span>
                                        </h4>
                                        
                                        <h4>
                                            <span class="badge badge-{{ $purchase->status == 'received' ? 'success' : ($purchase->status == 'pending' ? 'warning' : ($purchase->status == 'cancelled' ? 'danger' : 'info')) }}">
                                                Purchase Status: {{ strtoupper($purchase->status) }}
                                            </span>
                                        </h4>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="card">
                                <div class="card-header">
                                    <h5 class="mb-0">Authorized Signatures</h5>
                                </div>
                                <div class="card-body text-center">
                                    <div class="row">
                                        <div class="col-6">
                                            <div class="signature-box">
                                                <p class="mb-1">____________________</p>
                                                <p class="text-muted">Supplier Signature</p>
                                            </div>
                                        </div>
                                        <div class="col-6">
                                            <div class="signature-box">
                                                <p class="mb-1">____________________</p>
                                                <p class="text-muted">Authorized Signature</p>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <hr>
                                    
                                    <div class="mt-3">
                                        <p class="text-muted mb-1">Thank you for your business!</p>
                                        <p class="text-muted mb-0">This is a computer generated invoice.</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>
    @media print {
        .card-header, .btn, .actions {
            display: none !important;
        }
        
        .card {
            border: none !important;
        }
        
        body {
            background: white !important;
        }
        
        .container-fluid {
            padding: 0 !important;
        }
    }
    
    .signature-box {
        min-height: 80px;
        padding: 15px;
        border: 1px dashed #ddd;
        border-radius: 5px;
    }
    
    .table-bordered th,
    .table-bordered td {
        border: 1px solid #dee2e6;
    }
    
    .table thead th {
        background-color: #f8f9fa;
        font-weight: 600;
    }
</style>
@endpush