@extends('layouts.admin-simple')

@section('title', $good->ig_description)

@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.catalog.index') }}">Catalog</a></li>
    <li class="breadcrumb-item active">{{ $good->ig_code }}</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">{{ $good->ig_description }}</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">IG Code: {{ $good->ig_code }}</p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            <a href="{{ route('warehouse.indirect-goods.catalog.index') }}" class="btn btn-sm btn-outline-secondary mr-2">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i>
                Back to List
            </a>
            @can('warehouse.indirect-goods.edit')
            <a href="{{ route('warehouse.indirect-goods.catalog.edit', $good->id) }}" class="btn btn-sm btn-warning">
                <i class="material-icons mr-1" style="font-size: 16px;">edit</i>
                Edit
            </a>
            @endcan
        </div>
    </div>
@endsection

@section('content')
    <!-- Item Information -->
    <div class="card shadow mb-4">
        <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Indirect Good Information</h6>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <h6 class="text-primary font-weight-bold mb-3">Basic Information</h6>
                    <dl class="row">
                        <dt class="col-sm-4 text-muted">IG Code</dt>
                        <dd class="col-sm-8"><code>{{ $good->ig_code }}</code></dd>

                        <dt class="col-sm-4 text-muted">Description</dt>
                        <dd class="col-sm-8">{{ $good->ig_description }}</dd>

                        <dt class="col-sm-4 text-muted">Category</dt>
                        <dd class="col-sm-8">
                            @if($good->category)
                                {{ $good->category->category_name }}
                            @else
                                <span class="text-muted">No category assigned</span>
                            @endif
                        </dd>

                        <dt class="col-sm-4 text-muted">Unit of Measure</dt>
                        <dd class="col-sm-8">{{ $good->unit ? $good->unit->display_name : $good->unit_of_measure }}</dd>

                        <dt class="col-sm-4 text-muted">Status</dt>
                        <dd class="col-sm-8">
                            @if($good->status === 'active')
                                <span class="badge badge-success">Active</span>
                            @else
                                <span class="badge badge-secondary">Inactive</span>
                            @endif
                        </dd>

                        <dt class="col-sm-4 text-muted">Created Date</dt>
                        <dd class="col-sm-8">{{ $good->created_at ? $good->created_at->format('M d, Y H:i') : 'N/A' }}</dd>

                        <dt class="col-sm-4 text-muted">Last Updated</dt>
                        <dd class="col-sm-8">{{ $good->updated_at ? $good->updated_at->format('M d, Y H:i') : 'N/A' }}</dd>
                    </dl>
                </div>

                <div class="col-md-6">
                    <h6 class="text-primary font-weight-bold mb-3">Stock Information</h6>
                    <dl class="row">
                        <dt class="col-sm-4 text-muted">Min Stock Level</dt>
                        <dd class="col-sm-8">{{ $good->min_stock_level ? number_format($good->min_stock_level, 2) : 'Not set' }}</dd>

                        <dt class="col-sm-4 text-muted">Max Stock Level</dt>
                        <dd class="col-sm-8">{{ $good->max_stock_level ? number_format($good->max_stock_level, 2) : 'Not set' }}</dd>

                        <dt class="col-sm-4 text-muted">Reorder Point</dt>
                        <dd class="col-sm-8">{{ $good->reorder_point ? number_format($good->reorder_point, 2) : 'Not set' }}</dd>

                        <dt class="col-sm-4 text-muted">Current Stock</dt>
                        <dd class="col-sm-8">
                            @php
                                $totalStock = $good->total_quantity;
                            @endphp
                            @if($good->is_out_of_stock)
                                <span class="badge badge-danger">{{ number_format($totalStock, 2) }} {{ $good->unit ? $good->unit->unit_symbol : $good->unit_of_measure }}</span>
                                <small class="text-danger ml-1">Out of Stock</small>
                            @elseif($good->below_reorder_point)
                                <span class="badge badge-warning">{{ number_format($totalStock, 2) }} {{ $good->unit ? $good->unit->unit_symbol : $good->unit_of_measure }}</span>
                                <small class="text-warning ml-1">Low Stock</small>
                            @else
                                <span class="badge badge-info">{{ number_format($totalStock, 2) }} {{ $good->unit ? $good->unit->unit_symbol : $good->unit_of_measure }}</span>
                            @endif
                        </dd>

                        <dt class="col-sm-4 text-muted">Avg Unit Price</dt>
                        <dd class="col-sm-8">
                            <span class="badge badge-primary">AED {{ number_format($good->average_unit_price, 2) }}</span>
                        </dd>

                        <dt class="col-sm-4 text-muted">Inventory Batches</dt>
                        <dd class="col-sm-8">
                            <span class="badge badge-primary">{{ $good->inventories->count() }} batch(es)</span>
                        </dd>
                    </dl>

                    @if($good->notes)
                        <h6 class="text-primary font-weight-bold mb-3 mt-4">Notes</h6>
                        <p class="text-muted">{{ $good->notes }}</p>
                    @endif
                </div>
            </div>
        </div>
    </div>

    @if($good->inventories->count() > 0)
    <!-- Inventory Batches -->
    <div class="card shadow mb-4">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <h6 class="m-0 font-weight-bold text-primary">Inventory Batches</h6>
            <small class="text-muted">{{ $good->inventories->count() }} batch(es)</small>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-light">
                        <tr>
                            <th>#</th>
                            <th>Division</th>
                            <th>Supplier</th>
                            <th>Batch / Invoice</th>
                            <th>Qty Available</th>
                            <th>Unit Price</th>
                            <th>Total Value</th>
                            <th>Location</th>
                            <th>Expiry Date</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($good->inventories as $index => $inv)
                        <tr>
                            <td>{{ $index + 1 }}</td>
                            <td>{{ $inv->division ? $inv->division->division_name : 'N/A' }}</td>
                            <td>{{ $inv->supplier ? $inv->supplier->supplier_name : 'N/A' }}</td>
                            <td>
                                @if($inv->batch_number)
                                    <code>{{ $inv->batch_number }}</code><br>
                                @endif
                                @if($inv->invoice_number)
                                    <small class="text-muted">Inv: {{ $inv->invoice_number }}</small>
                                @endif
                            </td>
                            <td>
                                @if($inv->quantity_available <= 0)
                                    <span class="text-danger font-weight-bold">{{ number_format($inv->quantity_available, 2) }}</span>
                                @else
                                    <span class="font-weight-bold">{{ number_format($inv->quantity_available, 2) }}</span>
                                @endif
                            </td>
                            <td>AED {{ number_format($inv->unit_price, 2) }}</td>
                            <td>AED {{ number_format($inv->total_value, 2) }}</td>
                            <td>{{ $inv->location ?? 'N/A' }}</td>
                            <td>
                                @if($inv->expiry_date)
                                    @if($inv->expiry_date->isPast())
                                        <span class="text-danger">{{ $inv->expiry_date->format('M d, Y') }}</span>
                                        <br><small class="text-danger">Expired</small>
                                    @elseif($inv->expiry_date->diffInDays(now()) <= 30)
                                        <span class="text-warning">{{ $inv->expiry_date->format('M d, Y') }}</span>
                                        <br><small class="text-warning">Expiring soon</small>
                                    @else
                                        {{ $inv->expiry_date->format('M d, Y') }}
                                    @endif
                                @else
                                    N/A
                                @endif
                            </td>
                            <td>
                                @if($inv->status === 'in_stock')
                                    <span class="badge badge-success">In Stock</span>
                                @elseif($inv->status === 'low_stock')
                                    <span class="badge badge-warning">Low Stock</span>
                                @elseif($inv->status === 'out_of_stock')
                                    <span class="badge badge-danger">Out of Stock</span>
                                @else
                                    <span class="badge badge-secondary">{{ ucfirst(str_replace('_', ' ', $inv->status ?? 'unknown')) }}</span>
                                @endif
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="bg-light font-weight-bold">
                        <tr>
                            <td colspan="4" class="text-right">Totals:</td>
                            <td>{{ number_format($good->inventories->sum('quantity_available'), 2) }}</td>
                            <td>-</td>
                            <td>AED {{ number_format($good->inventories->sum('total_value'), 2) }}</td>
                            <td colspan="3"></td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    @endif

    @if($stockMovements->count() > 0)
    <!-- Recent Stock Movements -->
    <div class="card shadow">
        <div class="card-header py-3 d-flex justify-content-between align-items-center">
            <h6 class="m-0 font-weight-bold text-primary">Recent Stock Movements</h6>
            <small class="text-muted">Last 20 movements</small>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-light">
                        <tr>
                            <th>Date</th>
                            <th>Type</th>
                            <th>Quantity</th>
                            <th>Before</th>
                            <th>After</th>
                            <th>Division</th>
                            <th>Invoice #</th>
                            <th>User</th>
                            <th>Notes</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($stockMovements as $movement)
                        @php
                            $badgeClass = match($movement->movement_type) {
                                'in' => 'badge-success',
                                'out' => 'badge-danger',
                                'return' => 'badge-info',
                                'adjustment' => 'badge-warning',
                                'transfer' => 'badge-primary',
                                default => 'badge-secondary'
                            };
                            $badgeText = match($movement->movement_type) {
                                'in' => 'Stock In',
                                'out' => 'Stock Out',
                                'return' => 'Return',
                                'adjustment' => 'Adjustment',
                                'transfer' => 'Transfer',
                                default => ucfirst($movement->movement_type)
                            };
                            $isIncrease = in_array($movement->movement_type, ['in', 'return', 'transfer_in'])
                                || ($movement->movement_type === 'adjustment' && $movement->quantity_after > $movement->quantity_before);
                        @endphp
                        <tr>
                            <td>{{ $movement->created_at->format('M d, Y H:i') }}</td>
                            <td><span class="badge {{ $badgeClass }}">{{ $badgeText }}</span></td>
                            <td>
                                @if($isIncrease)
                                    <span class="text-success font-weight-bold">+{{ number_format($movement->quantity_moved, 2) }}</span>
                                @else
                                    <span class="text-danger font-weight-bold">-{{ number_format($movement->quantity_moved, 2) }}</span>
                                @endif
                                {{ $good->unit ? $good->unit->unit_symbol : $good->unit_of_measure }}
                            </td>
                            <td>{{ number_format($movement->quantity_before, 2) }}</td>
                            <td>{{ number_format($movement->quantity_after, 2) }}</td>
                            <td>{{ $movement->division ? $movement->division->division_name : 'N/A' }}</td>
                            <td>
                                @if($movement->invoice_number)
                                    <code>{{ $movement->invoice_number }}</code>
                                @else
                                    -
                                @endif
                            </td>
                            <td>{{ $movement->user ? $movement->user->name : 'System' }}</td>
                            <td>{{ $movement->notes ?? '-' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    @endif
@endsection

@push('styles')
    <style>
        .card-header {
            background-color: #fff;
        }

        .badge {
            font-size: 12px;
        }

        dt {
            font-weight: 500;
        }

        dd {
            margin-bottom: 0.75rem;
        }

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

        .table td {
            vertical-align: middle;
            font-size: 13px;
        }
    </style>
@endpush
