@extends('layouts.admin-simple')

@section('title', 'Incoming Receipt - ' . $incoming->receipt_number)

@section('breadcrumb')
    <li class="breadcrumb-item">Warehouse</li>
    <li class="breadcrumb-item">Indirect Goods</li>
    <li class="breadcrumb-item"><a href="{{ route('warehouse.indirect-goods.incoming.index') }}">Incoming Receipts</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;">
                Incoming Receipt -
                @switch($incoming->status)
                    @case('draft')
                        <span class="badge badge-secondary">Draft</span>
                        @break
                    @case('approved')
                        <span class="badge badge-info">Approved</span>
                        @break
                    @case('completed')
                        <span class="badge badge-success">Completed</span>
                        @break
                @endswitch
            </p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            @if($incoming->status === 'draft')
                @can('warehouse.indirect-incoming.edit')
                    <a href="{{ route('warehouse.indirect-goods.incoming.edit', $incoming->id) }}" class="btn btn-sm btn-primary mr-2">
                        <i class="material-icons mr-1" style="font-size: 16px;">edit</i>
                        Edit
                    </a>
                @endcan

                @can('warehouse.indirect-incoming.process')
                    <form action="{{ route('warehouse.indirect-goods.incoming.approve', $incoming->id) }}" method="POST" class="d-inline mr-2">
                        @csrf
                        <button type="submit" class="btn btn-sm btn-info"
                                onclick="return confirm('Are you sure you want to approve this receipt?');">
                            <i class="material-icons mr-1" style="font-size: 16px;">check</i>
                            Approve
                        </button>
                    </form>

                    <form action="{{ route('warehouse.indirect-goods.incoming.destroy', $incoming->id) }}" method="POST" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-danger"
                                onclick="return confirm('Are you sure you want to delete this receipt? This action cannot be undone.');">
                            <i class="material-icons mr-1" style="font-size: 16px;">delete</i>
                            Delete
                        </button>
                    </form>
                @endcan
            @elseif($incoming->status === 'approved')
                @can('warehouse.indirect-incoming.process')
                    <form action="{{ route('warehouse.indirect-goods.incoming.complete', $incoming->id) }}" method="POST" class="d-inline mr-2">
                        @csrf
                        <button type="submit" class="btn btn-sm btn-success"
                                onclick="return confirm('Complete this receipt and add items to inventory? This action cannot be reversed.');">
                            <i class="material-icons mr-1" style="font-size: 16px;">done_all</i>
                            Complete & Process to Inventory
                        </button>
                    </form>
                @endcan
            @elseif($incoming->status === 'completed')
                <a href="{{ route('warehouse.indirect-goods.incoming.print', $incoming->id) }}" target="_blank" class="btn btn-sm btn-outline-secondary mr-2">
                    <i class="material-icons mr-1" style="font-size: 16px;">print</i>
                    Print
                </a>
            @endif

            <a href="{{ route('warehouse.indirect-goods.incoming.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i>
                Back to List
            </a>
        </div>
    </div>
@endsection

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

    @if(session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
    @endif

    <!-- Receipt Details Card -->
    <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">info</i>
                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 width="30%"><strong>Receipt Number:</strong></td>
                            <td>{{ $incoming->receipt_number }}</td>
                        </tr>
                        <tr>
                            <td><strong>Status:</strong></td>
                            <td>
                                @switch($incoming->status)
                                    @case('draft')
                                        <span class="badge badge-secondary">Draft</span>
                                        @break
                                    @case('approved')
                                        <span class="badge badge-info">Approved</span>
                                        @break
                                    @case('completed')
                                        <span class="badge badge-success">Completed</span>
                                        @break
                                @endswitch
                            </td>
                        </tr>
                        <tr>
                            <td><strong>Supplier:</strong></td>
                            <td>
                                @if($incoming->supplier)
                                    {{ $incoming->supplier->name }}
                                    @if($incoming->supplier->supplier_code)
                                        <small class="text-muted">({{ $incoming->supplier->supplier_code }})</small>
                                    @endif
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong>Invoice Number:</strong></td>
                            <td>{{ $incoming->invoice_number ?? '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Invoice Date:</strong></td>
                            <td>{{ $incoming->invoice_date ? $incoming->invoice_date->format('d M, Y') : '-' }}</td>
                        </tr>
                        <tr>
                            <td><strong>Receipt Date:</strong></td>
                            <td>{{ $incoming->receipt_date ? $incoming->receipt_date->format('d M, Y') : $incoming->created_at->format('d M, Y') }}</td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table class="table table-sm table-borderless">
                        <tr>
                            <td width="30%"><strong>Project:</strong></td>
                            <td>
                                @if($incoming->project)
                                    <strong>{{ $incoming->project->project_name }}</strong>
                                    @if($incoming->project->project_number)
                                        <br><small class="text-muted">{{ $incoming->project->project_number }}</small>
                                    @endif
                                @else
                                    <span class="text-muted">No Project (Warehouse Stock)</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong>Division:</strong></td>
                            <td>
                                @if($incoming->division)
                                    <span class="badge badge-outline-primary">{{ $incoming->division->division_name }}</span>
                                    @if($incoming->division->division_code)
                                        <small class="text-muted ml-1">({{ $incoming->division->division_code }})</small>
                                    @endif
                                @else
                                    <span class="text-muted">-</span>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td><strong>Received By:</strong></td>
                            <td>{{ $incoming->receiver->name ?? '-' }}</td>
                        </tr>
                        @if($incoming->approved_by)
                        <tr>
                            <td><strong>Approved By:</strong></td>
                            <td>
                                {{ $incoming->approver->name ?? '-' }}
                                @if($incoming->approved_at)
                                    <br><small class="text-muted">{{ $incoming->approved_at->format('d M, Y H:i') }}</small>
                                @endif
                            </td>
                        </tr>
                        @endif
                        <tr>
                            <td><strong>Created At:</strong></td>
                            <td>{{ $incoming->created_at->format('d M, Y H:i') }}</td>
                        </tr>
                    </table>
                </div>
            </div>

            @if($incoming->notes)
            <div class="row mt-2">
                <div class="col-12">
                    <strong>Notes:</strong>
                    <p class="text-muted mb-0 mt-1">{{ $incoming->notes }}</p>
                </div>
            </div>
            @endif
        </div>
    </div>

    <!-- Items Card -->
    <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>
                Items ({{ $incoming->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 style="width: 4%;">#</th>
                            <th style="width: 22%;">Indirect Good</th>
                            <th style="width: 9%;">Qty Ordered</th>
                            <th style="width: 9%;">Qty Delivered</th>
                            <th style="width: 9%;">Qty Accepted</th>
                            <th style="width: 10%;">Unit Price</th>
                            <th style="width: 10%;">Total Price</th>
                            <th style="width: 9%;">Batch</th>
                            <th style="width: 9%;">Expiry</th>
                            <th style="width: 9%;">Notes</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($incoming->items as $index => $item)
                            <tr>
                                <td>{{ $index + 1 }}</td>
                                <td>
                                    @if($item->indirectGood)
                                        <strong>{{ $item->indirectGood->ig_description }}</strong>
                                        <br><small class="text-muted">{{ $item->indirectGood->ig_code }}</small>
                                    @else
                                        <span class="text-muted">Unknown Item</span>
                                    @endif
                                </td>
                                <td class="text-center">{{ $item->quantity_ordered > 0 ? number_format($item->quantity_ordered, 2) : '-' }}</td>
                                <td class="text-center">{{ number_format($item->quantity_delivered, 2) }} {{ $item->unit_of_measure }}</td>
                                <td class="text-center">{{ number_format($item->quantity_accepted, 2) }} {{ $item->unit_of_measure }}</td>
                                <td class="text-right">{{ number_format($item->unit_price, 2) }}</td>
                                <td class="text-right font-weight-bold">{{ number_format($item->total_price, 2) }}</td>
                                <td>{{ $item->batch_number ?: '-' }}</td>
                                <td>{{ $item->expiry_date ? $item->expiry_date->format('d M, Y') : '-' }}</td>
                                <td><small>{{ $item->notes ?: '-' }}</small></td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="10" class="text-center text-muted py-3">No items found</td>
                            </tr>
                        @endforelse
                    </tbody>
                    @if($incoming->items->count() > 0)
                    <tfoot class="bg-light">
                        <tr>
                            <td colspan="6" class="text-right font-weight-bold">Subtotal:</td>
                            <td class="text-right font-weight-bold">
                                {{ number_format($incoming->items->sum('total_price'), 2) }}
                            </td>
                            <td colspan="3"></td>
                        </tr>
                        @if($incoming->vat_amount > 0)
                        <tr>
                            <td colspan="6" class="text-right font-weight-bold">VAT ({{ number_format($incoming->vat_rate, 2) }}%):</td>
                            <td class="text-right font-weight-bold">
                                {{ number_format($incoming->vat_amount, 2) }}
                            </td>
                            <td colspan="3"></td>
                        </tr>
                        @endif
                        <tr>
                            <td colspan="6" class="text-right font-weight-bold text-success" style="font-size: 14px;">Grand Total:</td>
                            <td class="text-right font-weight-bold text-success" style="font-size: 14px;">
                                {{ number_format($incoming->total_amount, 2) }}
                            </td>
                            <td colspan="3"></td>
                        </tr>
                    </tfoot>
                    @endif
                </table>
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <style>
        .table th {
            border-top: none;
            font-weight: 600;
            color: #495057;
            font-size: 12px;
            text-transform: uppercase;
            letter-spacing: 0.3px;
        }
        .table td {
            vertical-align: middle;
            font-size: 13px;
        }
        .card-title {
            font-size: 16px;
            font-weight: 600;
            color: #495057;
        }
        .badge-outline-primary {
            color: #007bff;
            border: 1px solid #007bff;
            background: transparent;
        }
    </style>
@endpush
