@extends('layouts.admin-simple')

@section('title', 'View Asset Receipt')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.fixed-assets.incoming.index') }}">Incoming</a></li>
    <li class="breadcrumb-item active">{{ $incoming->receipt_number }}</li>
@endsection

@section('page-title')
    <div class="d-flex justify-content-between align-items-center w-100">
        <div class="page-title-left">
            <h3 class="mb-1">{{ $incoming->receipt_number }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Asset receipt from {{ $incoming->supplier ? $incoming->supplier->name : 'Unknown' }}</p>
        </div>
        <div class="page-actions-right d-flex">
            @if($incoming->status == 'draft')
                @can('warehouse.fixed-asset-incoming.process')
                <form method="POST" action="{{ route('warehouse.fixed-assets.incoming.approve', $incoming->id) }}" class="mr-2" onsubmit="return confirm('Approve this receipt?')">
                    @csrf
                    <button type="submit" class="btn btn-sm btn-success"><i class="material-icons mr-1" style="font-size: 16px;">check</i> Approve</button>
                </form>
                @endcan
                @can('warehouse.fixed-asset-incoming.edit')
                <a href="{{ route('warehouse.fixed-assets.incoming.edit', $incoming->id) }}" class="btn btn-sm btn-warning mr-2"><i class="material-icons mr-1" style="font-size: 16px;">edit</i> Edit</a>
                @endcan
            @elseif($incoming->status == 'approved')
                @can('warehouse.fixed-asset-incoming.process')
                <form method="POST" action="{{ route('warehouse.fixed-assets.incoming.complete', $incoming->id) }}" class="mr-2" onsubmit="return confirm('Complete this receipt and create assets?')">
                    @csrf
                    <button type="submit" class="btn btn-sm btn-primary"><i class="material-icons mr-1" style="font-size: 16px;">done_all</i> Complete & Create Assets</button>
                </form>
                @endcan
            @endif
            <a href="{{ route('warehouse.fixed-assets.incoming.print', $incoming->id) }}" class="btn btn-sm btn-outline-info mr-2" target="_blank"><i class="material-icons mr-1" style="font-size: 16px;">print</i> Print</a>
            <a href="{{ route('warehouse.fixed-assets.incoming.index') }}" class="btn btn-sm btn-outline-secondary"><i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back</a>
        </div>
    </div>
@endsection

@section('content')
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show"><span>{{ session('success') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show"><span>{{ session('error') }}</span><button type="button" class="close" data-dismiss="alert"><span>&times;</span></button></div>
    @endif

    <div class="row">
        <div class="col-md-8">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Receipt Details</h6></div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted" width="40%">Receipt #</td><td><code>{{ $incoming->receipt_number }}</code></td></tr>
                                <tr><td class="text-muted">Supplier</td><td>{{ $incoming->supplier ? $incoming->supplier->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Invoice #</td><td>{{ $incoming->invoice_number ?? '-' }}</td></tr>
                                <tr><td class="text-muted">Invoice Date</td><td>{{ $incoming->invoice_date ? $incoming->invoice_date->format('M d, Y') : '-' }}</td></tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted" width="40%">Receipt Date</td><td>{{ $incoming->receipt_date ? $incoming->receipt_date->format('M d, Y') : '-' }}</td></tr>
                                <tr><td class="text-muted">Received By</td><td>{{ $incoming->receiver ? $incoming->receiver->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Approved By</td><td>{{ $incoming->approver ? $incoming->approver->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Status</td><td><span class="badge badge-{{ $incoming->status == 'completed' ? 'success' : ($incoming->status == 'approved' ? 'primary' : 'warning') }}">{{ ucfirst($incoming->status) }}</span></td></tr>
                            </table>
                        </div>
                    </div>
                    @if($incoming->notes)
                        <hr><p class="text-muted mb-0"><strong>Notes:</strong> {{ $incoming->notes }}</p>
                    @endif
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Financial Summary</h6></div>
                <div class="card-body text-center">
                    <h4 class="text-primary mb-1">AED {{ number_format($incoming->total_amount, 2) }}</h4>
                    <small class="text-muted">Total Amount</small>
                    <hr>
                    <div class="row text-center">
                        <div class="col-6">
                            <small class="text-muted d-block">VAT ({{ $incoming->vat_rate ?? 0 }}%)</small>
                            <strong>AED {{ number_format($incoming->vat_amount ?? 0, 2) }}</strong>
                        </div>
                        <div class="col-6">
                            <small class="text-muted d-block">Items</small>
                            <strong>{{ $incoming->items->count() }}</strong>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h6 class="mb-0">Items</h6></div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="thead-light">
                        <tr><th>#</th><th>Asset Name</th><th>Category</th><th>Serial #</th><th>Qty</th><th>Unit Price</th><th>Total</th><th>Condition</th></tr>
                    </thead>
                    <tbody>
                        @foreach($incoming->items as $i => $item)
                        <tr>
                            <td>{{ $i + 1 }}</td>
                            <td>{{ $item->asset_name }}</td>
                            <td>{{ $item->category ? $item->category->category_name : '-' }}</td>
                            <td>{{ $item->serial_number ?? '-' }}</td>
                            <td>{{ $item->quantity }}</td>
                            <td>AED {{ number_format($item->unit_price, 2) }}</td>
                            <td>AED {{ number_format($item->total_price, 2) }}</td>
                            <td><span class="badge badge-{{ $item->condition == 'new' ? 'success' : 'info' }}">{{ ucfirst($item->condition ?? 'new') }}</span></td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
