@extends('layouts.admin-simple')

@section('title', 'Fixed Asset Incoming')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Fixed Assets</li>
    <li class="breadcrumb-item active">Incoming Receipts</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">Asset Incoming Receipts</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Manage fixed asset receipts from suppliers</p>
        </div>
        @can('warehouse.fixed-asset-incoming.create')
        <div class="page-actions-right">
            <a href="{{ route('warehouse.fixed-assets.incoming.create') }}" class="btn btn-sm btn-primary">
                <i class="material-icons mr-1" style="font-size: 16px;">add</i> New Receipt
            </a>
        </div>
        @endcan
    </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

    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" class="row align-items-center">
                <div class="col-md-3">
                    <input type="text" name="search" class="form-control form-control-sm" placeholder="Search receipts..." value="{{ request('search') }}">
                </div>
                <div class="col-md-2">
                    <select name="status" class="form-control form-control-sm">
                        <option value="">All Status</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>Draft</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>Approved</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-sm btn-primary">Filter</button>
                    <a href="{{ route('warehouse.fixed-assets.incoming.index') }}" class="btn btn-sm btn-outline-secondary">Clear</a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <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>Receipt Number</th>
                            <th>Supplier</th>
                            <th>Invoice #</th>
                            <th>Date</th>
                            <th>Items</th>
                            <th>Total</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($receipts as $i => $receipt)
                        <tr>
                            <td>{{ $receipts->firstItem() + $i }}</td>
                            <td><a href="{{ route('warehouse.fixed-assets.incoming.show', $receipt->id) }}"><code>{{ $receipt->receipt_number }}</code></a></td>
                            <td>{{ $receipt->supplier ? $receipt->supplier->name : '-' }}</td>
                            <td>{{ $receipt->invoice_number ?? '-' }}</td>
                            <td>{{ $receipt->receipt_date ? $receipt->receipt_date->format('M d, Y') : '-' }}</td>
                            <td><span class="badge badge-info">{{ $receipt->items->count() }}</span></td>
                            <td>AED {{ number_format($receipt->total_amount, 2) }}</td>
                            <td>
                                <span class="badge badge-{{ $receipt->status == 'completed' ? 'success' : ($receipt->status == 'approved' ? 'primary' : 'warning') }}">
                                    {{ ucfirst($receipt->status) }}
                                </span>
                            </td>
                            <td>
                                <a href="{{ route('warehouse.fixed-assets.incoming.show', $receipt->id) }}" class="btn btn-sm btn-info" title="View">
                                    <i class="material-icons" style="font-size: 14px;">visibility</i>
                                </a>
                                @if($receipt->status == 'draft')
                                    @can('warehouse.fixed-asset-incoming.edit')
                                    <a href="{{ route('warehouse.fixed-assets.incoming.edit', $receipt->id) }}" class="btn btn-sm btn-warning" title="Edit">
                                        <i class="material-icons" style="font-size: 14px;">edit</i>
                                    </a>
                                    @endcan
                                @endif
                            </td>
                        </tr>
                        @empty
                        <tr><td colspan="9" class="text-center text-muted py-4">No incoming receipts found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($receipts->hasPages())
        <div class="card-footer">{{ $receipts->links() }}</div>
        @endif
    </div>
@endsection
