@extends('layouts.admin-simple')

@section('title', 'Material Issue Details')

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.inventory.material-issued') }}">Material Issued</a></li>
    <li class="breadcrumb-item active">{{ $displayNumber }}</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">Material Issue #{{ $displayNumber }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Direct and indirect goods issue details</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            <a href="{{ route('warehouse.inventory.material-issued') }}" 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="container-fluid">
    <div class="card mb-3">
        <div class="card-header">
            <h6 class="card-title mb-0 d-flex align-items-center">
                <i class="material-icons mr-2">assignment</i>
                Request Summary
            </h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <table class="table table-sm table-borderless mb-0">
                        <tr>
                            <td width="35%"><strong>Request Number:</strong></td>
                            <td>{{ $displayNumber }}</td>
                        </tr>
                        <tr>
                            <td><strong>Request Date:</strong></td>
                            <td>{{ $requestDate ? $requestDate->format('d M, Y') : '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Status:</strong></td>
                            <td><span class="badge badge-{{ $status->class }}">{{ $status->label }}</span></td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table class="table table-sm table-borderless mb-0">
                        <tr>
                            <td width="35%"><strong>Project:</strong></td>
                            <td>{{ $project->project_name ?? 'N/A' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Division:</strong></td>
                            <td>{{ $division->division_name ?? 'N/A' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Requested By:</strong></td>
                            <td>{{ $requester->name ?? 'N/A' }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    @if($directRequest)
        <div class="card mb-3">
            <div class="card-header">
                <h6 class="card-title mb-0 d-flex align-items-center">
                    <i class="material-icons mr-2">inventory</i>
                    Direct Goods ({{ $directRequest->items->count() }})
                </h6>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th>Item Code</th>
                                <th>Description</th>
                                <th class="text-right">Requested</th>
                                <th class="text-right">Issued</th>
                                <th class="text-right">Balance</th>
                                <th class="text-right">Unit Price</th>
                                <th class="text-right">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($directRequest->items as $item)
                                <tr>
                                    <td>{{ $item->item->item_code ?? 'N/A' }}</td>
                                    <td>{{ $item->item->item_description ?? 'Unknown Item' }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_requested, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_released, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_balance, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->total_price, 2) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot class="bg-light">
                            <tr>
                                <th colspan="6" class="text-right">Direct Goods Total</th>
                                <th class="text-right">{{ number_format($directRequest->items->sum('total_price'), 2) }}</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    @endif

    @if($indirectRequest)
        <div class="card mb-3">
            <div class="card-header">
                <h6 class="card-title mb-0 d-flex align-items-center">
                    <i class="material-icons mr-2">category</i>
                    Indirect Goods ({{ $indirectRequest->items->count() }})
                </h6>
            </div>
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-sm mb-0">
                        <thead class="bg-light">
                            <tr>
                                <th>Item Code</th>
                                <th>Description</th>
                                <th>Invoice</th>
                                <th class="text-right">Requested</th>
                                <th class="text-right">Issued</th>
                                <th class="text-right">Balance</th>
                                <th class="text-right">Unit Price</th>
                                <th class="text-right">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($indirectRequest->items as $item)
                                <tr>
                                    <td>{{ $item->indirectGood->ig_code ?? 'N/A' }}</td>
                                    <td>{{ $item->indirectGood->ig_description ?? 'Unknown Item' }}</td>
                                    <td>{{ $item->invoice_number ?: '-' }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_requested, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_released, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->quantity_balance, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                                    <td class="text-right">{{ number_format($item->total_price, 2) }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                        <tfoot class="bg-light">
                            <tr>
                                <th colspan="7" class="text-right">Indirect Goods Total</th>
                                <th class="text-right">{{ number_format($indirectRequest->items->sum('total_price'), 2) }}</th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    @endif
</div>
@endsection

@push('styles')
    <style>
        .card-title {
            font-size: 16px;
            font-weight: 600;
            color: #495057;
        }

        .table th {
            border-top: none;
            font-weight: 600;
            color: #495057;
            font-size: 13px;
        }

        .table td {
            vertical-align: middle;
            font-size: 13px;
        }

        .table-borderless td {
            border: none;
            padding: 0.25rem 0.5rem;
        }
    </style>
@endpush
