@extends('layouts.admin-simple')

@section('title', 'Fixed Assets - Movement History')

@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.inventory.index') }}">Inventory</a></li>
    <li class="breadcrumb-item active">Movement History</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">Movement History</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">
                @if(isset($asset))
                    {{ $asset->asset_code }} - {{ $asset->asset_name }}
                @else
                    Track asset movements across locations and projects
                @endif
            </p>
        </div>
        <div class="page-actions-right d-flex align-items-center">
            @if(isset($asset))
                <a href="{{ route('warehouse.fixed-assets.catalog.show', $asset->id) }}" class="btn btn-sm btn-outline-info mr-2">
                    <i class="material-icons mr-1" style="font-size: 16px;">visibility</i> View Asset
                </a>
            @endif
            <a href="{{ route('warehouse.fixed-assets.inventory.index') }}" class="btn btn-sm btn-outline-secondary">
                <i class="material-icons mr-1" style="font-size: 16px;">arrow_back</i> Back to Overview
            </a>
        </div>
    </div>
@endsection

@section('content')
    <!-- Filter -->
    <div class="card mb-3">
        <div class="card-body py-2">
            <form method="GET" action="{{ route('warehouse.fixed-assets.inventory.movement-history') }}" class="row align-items-end">
                @if(!isset($asset))
                <div class="col-md-4">
                    <label class="mb-1" style="font-size: 12px;">Select Asset</label>
                    <select name="asset_id" class="form-control form-control-sm select2-dropdown">
                        <option value="">All Assets</option>
                        @foreach($allAssets ?? [] as $a)
                            <option value="{{ $a->id }}" {{ request('asset_id') == $a->id ? 'selected' : '' }}>{{ $a->asset_code }} - {{ $a->asset_name }}</option>
                        @endforeach
                    </select>
                </div>
                @else
                    <input type="hidden" name="asset_id" value="{{ $asset->id }}">
                @endif
                <div class="col-md-3">
                    <label class="mb-1" style="font-size: 12px;">Movement Type</label>
                    <select name="movement_type" class="form-control form-control-sm">
                        <option value="">All Types</option>
                        <option value="incoming" {{ request('movement_type') == 'incoming' ? 'selected' : '' }}>Incoming</option>
                        <option value="allocation" {{ request('movement_type') == 'allocation' ? 'selected' : '' }}>Allocation</option>
                        <option value="return" {{ request('movement_type') == 'return' ? 'selected' : '' }}>Return</option>
                        <option value="transfer" {{ request('movement_type') == 'transfer' ? 'selected' : '' }}>Transfer</option>
                        <option value="maintenance" {{ request('movement_type') == 'maintenance' ? 'selected' : '' }}>Maintenance</option>
                        <option value="disposal" {{ request('movement_type') == 'disposal' ? 'selected' : '' }}>Disposal</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">From Date</label>
                    <input type="date" name="date_from" class="form-control form-control-sm" value="{{ request('date_from') }}">
                </div>
                <div class="col-md-2">
                    <label class="mb-1" style="font-size: 12px;">To Date</label>
                    <input type="date" name="date_to" class="form-control form-control-sm" value="{{ request('date_to') }}">
                </div>
                <div class="col-md-1">
                    <button type="submit" class="btn btn-sm btn-primary btn-block"><i class="material-icons" style="font-size: 14px;">search</i></button>
                </div>
            </form>
        </div>
    </div>

    <!-- Asset Summary (when viewing specific asset) -->
    @if(isset($asset))
    <div class="row mb-3">
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-2">
                    <small class="text-muted d-block">Asset Code</small>
                    <strong><code>{{ $asset->asset_code }}</code></strong>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-2">
                    <small class="text-muted d-block">Current Location</small>
                    <strong>{{ $asset->current_location ?? 'Warehouse' }}</strong>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-2">
                    <small class="text-muted d-block">Current Status</small>
                    <span class="badge badge-primary">{{ ucfirst(str_replace('_', ' ', $asset->lifecycle_status)) }}</span>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card text-center">
                <div class="card-body py-2">
                    <small class="text-muted d-block">Total Movements</small>
                    <strong>{{ $movements->total() }}</strong>
                </div>
            </div>
        </div>
    </div>
    @endif

    <!-- Movements Table -->
    <div class="card">
        <div class="card-header">
            <h6 class="mb-0"><i class="material-icons mr-2 align-middle">timeline</i>Movement History</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>Date</th>
                            @if(!isset($asset))
                            <th>Asset</th>
                            @endif
                            <th>Type</th>
                            <th>From</th>
                            <th>To</th>
                            <th>Notes</th>
                            <th>Performed By</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $typeColors = ['incoming'=>'success','allocation'=>'primary','return'=>'info','transfer'=>'warning','maintenance'=>'secondary','disposal'=>'danger'];
                        @endphp
                        @forelse($movements as $i => $movement)
                        <tr>
                            <td>{{ $movements->firstItem() + $i }}</td>
                            <td>{{ $movement->movement_date ? \Carbon\Carbon::parse($movement->movement_date)->format('M d, Y') : '-' }}</td>
                            @if(!isset($asset))
                            <td>
                                @if($movement->fixedAsset)
                                    <a href="{{ route('warehouse.fixed-assets.catalog.show', $movement->fixedAsset->id) }}">
                                        <code>{{ $movement->fixedAsset->asset_code }}</code>
                                    </a>
                                    <span class="ml-1">{{ $movement->fixedAsset->asset_name }}</span>
                                @else
                                    -
                                @endif
                            </td>
                            @endif
                            <td>
                                <span class="badge badge-{{ $typeColors[$movement->movement_type] ?? 'secondary' }}">
                                    {{ ucfirst(str_replace('_', ' ', $movement->movement_type)) }}
                                </span>
                            </td>
                            <td>
                                {{ $movement->from_location ?? '' }}
                                @if($movement->fromProject)
                                    <br><small class="text-muted">{{ $movement->fromProject->project_name }}</small>
                                @endif
                            </td>
                            <td>
                                {{ $movement->to_location ?? '' }}
                                @if($movement->toProject)
                                    <br><small class="text-muted">{{ $movement->toProject->project_name }}</small>
                                @endif
                            </td>
                            <td>
                                @if($movement->notes)
                                    <span style="font-size: 12px;">{{ Str::limit($movement->notes, 50) }}</span>
                                @else
                                    -
                                @endif
                            </td>
                            <td>{{ $movement->performedBy ? $movement->performedBy->name : '-' }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="{{ isset($asset) ? 7 : 8 }}" class="text-center text-muted py-4">
                                <i class="material-icons" style="font-size: 36px;">timeline</i>
                                <p class="mt-2 mb-0">
                                    @if(isset($asset))
                                        No movement records found for this asset.
                                    @else
                                        No movement records found. Select an asset or adjust filters.
                                    @endif
                                </p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($movements->hasPages())
        <div class="card-footer">
            <div class="d-flex justify-content-between align-items-center">
                <small class="text-muted">Showing {{ $movements->firstItem() }} to {{ $movements->lastItem() }} of {{ $movements->total() }} movements</small>
                {{ $movements->appends(request()->query())->links() }}
            </div>
        </div>
        @endif
    </div>
@endsection

@push('styles')
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
    <style>
        .select2-container { width: 100% !important; }
        .select2-container--default .select2-selection--single { height: 31px; border: 1px solid #ced4da; font-size: 13px; }
        .select2-container--default .select2-selection--single .select2-selection__rendered { line-height: 29px; }
        .select2-container--default .select2-selection--single .select2-selection__arrow { height: 29px; }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.select2-dropdown').select2({
                placeholder: 'Search asset...',
                allowClear: true
            });
        });
    </script>
@endpush
