@extends('layouts.admin-simple')

@section('title', 'View Allocation')

@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.allocations.index') }}">Allocations</a></li>
    <li class="breadcrumb-item active">{{ $allocation->allocation_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">{{ $allocation->allocation_number }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">{{ $allocation->project ? $allocation->project->project_name : '-' }}</p>
        </div>
        <div class="page-actions-right d-flex">
            <a href="{{ route('warehouse.fixed-assets.allocations.print', $allocation->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.allocations.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')
    <div class="row">
        <div class="col-md-8">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Allocation 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%">Allocation #</td><td><code>{{ $allocation->allocation_number }}</code></td></tr>
                                <tr><td class="text-muted">Project</td><td>{{ $allocation->project ? $allocation->project->project_name : '-' }}</td></tr>
                                <tr><td class="text-muted">Division</td><td>{{ $allocation->division ? $allocation->division->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Purpose</td><td>{{ $allocation->purpose ?? '-' }}</td></tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <table class="table table-sm table-borderless">
                                <tr><td class="text-muted" width="40%">Requested By</td><td>{{ $allocation->requester ? $allocation->requester->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Allocated By</td><td>{{ $allocation->allocatedBy ? $allocation->allocatedBy->name : '-' }}</td></tr>
                                <tr><td class="text-muted">Status</td><td>
                                    @php
                                        $colors = ['pending_approval' => 'warning', 'partially_allocated' => 'info', 'allocated' => 'success'];
                                        $labels = ['pending_approval' => 'Pending Allocation', 'partially_allocated' => 'Partially Allocated'];
                                    @endphp
                                    <span class="badge badge-{{ $colors[$allocation->status] ?? 'secondary' }}">{{ $labels[$allocation->status] ?? ucfirst(str_replace('_', ' ', $allocation->status)) }}</span>
                                </td></tr>
                            </table>
                        </div>
                    </div>
                    @if($allocation->rejection_reason)
                        <div class="alert alert-danger mt-2 mb-0"><strong>Rejection Reason:</strong> {{ $allocation->rejection_reason }}</div>
                    @endif
                    @if($allocation->notes)
                        <hr><p class="text-muted mb-0"><strong>Notes:</strong> {{ $allocation->notes }}</p>
                    @endif
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card mb-3">
                <div class="card-header"><h6 class="mb-0">Summary</h6></div>
                <div class="card-body text-center">
                    <h4 class="mb-0">{{ $allocation->items->count() }}</h4>
                    <small class="text-muted">Total Assets</small>
                    <hr>
                    <small class="text-muted">Created {{ $allocation->created_at ? $allocation->created_at->format('M d, Y H:i') : '-' }}</small>
                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-header"><h6 class="mb-0">Allocated Assets</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 Code</th><th>Asset Name</th><th>Category</th><th>Condition</th><th>Allocated</th><th>Action</th></tr>
                    </thead>
                    <tbody>
                        @foreach($allocation->items as $i => $item)
                        <tr>
                            <td>{{ $i + 1 }}</td>
                            <td><code>{{ $item->fixedAsset ? $item->fixedAsset->asset_code : '-' }}</code></td>
                            <td>{{ $item->fixedAsset ? $item->fixedAsset->asset_name : '-' }}</td>
                            <td>{{ $item->fixedAsset && $item->fixedAsset->category ? $item->fixedAsset->category->category_name : '-' }}</td>
                            <td>{{ ucfirst($item->condition_at_allocation ?? '-') }}</td>
                            <td>
                                @if($item->is_allocated)
                                    <span class="badge badge-success">Yes</span>
                                @else
                                    <span class="badge badge-secondary">No</span>
                                @endif
                            </td>
                            <td>
                                @if(!$item->is_allocated && in_array($allocation->status, ['pending_approval', 'partially_allocated', 'approved'], true))
                                    @can('warehouse.fixed-asset-allocations.allocate')
                                    <form method="POST" action="{{ route('warehouse.fixed-assets.allocations.items.allocate', [$allocation->id, $item->id]) }}" class="m-0 allocation-form">
                                        @csrf
                                        <button type="submit" class="btn btn-sm btn-primary">
                                            <i class="material-icons mr-1" style="font-size: 14px;">done_all</i> Allocate
                                        </button>
                                    </form>
                                    @endcan
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

@endsection

@push('styles')
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet" />
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        $(document).on('submit', '.allocation-form', function(e) {
            e.preventDefault();

            const form = this;

            Swal.fire({
                title: 'Allocate asset?',
                text: 'This will move this asset to the selected project and record the movement history.',
                icon: 'question',
                showCancelButton: true,
                confirmButtonText: 'Yes, allocate',
                cancelButtonText: 'Cancel',
                confirmButtonColor: '#1976d2',
                cancelButtonColor: '#6c757d'
            }).then(function(result) {
                if (result.isConfirmed) {
                    form.submit();
                }
            });
        });
    </script>
@endpush
