@extends('layouts.admin-simple')

@section('title', 'Upcoming Maintenance')

@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.maintenance.index') }}">Maintenance</a></li>
    <li class="breadcrumb-item active">Upcoming</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">Upcoming Maintenance</h3>
            <p class="text-muted mb-0" style="font-size: 13px;">Scheduled maintenance within the next 30 days</p>
        </div>
        <div class="page-actions-right d-flex">
            <a href="{{ route('warehouse.fixed-assets.maintenance.index') }}" class="btn btn-sm btn-outline-secondary mr-2"><i class="material-icons mr-1" style="font-size: 16px;">list</i> All Records</a>
            @can('warehouse.fixed-asset-maintenance.create')
            <a href="{{ route('warehouse.fixed-assets.maintenance.create') }}" class="btn btn-sm btn-primary"><i class="material-icons mr-1" style="font-size: 16px;">add</i> Schedule New</a>
            @endcan
        </div>
    </div>
@endsection

@section('content')
    <div class="card">
        <div class="card-header bg-info text-white">
            <h6 class="mb-0"><i class="material-icons mr-2 align-middle">schedule</i>Upcoming Scheduled Maintenance</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>Maintenance #</th><th>Asset</th><th>Type</th><th>Scheduled Date</th><th>Days Until</th><th>Priority</th><th>Actions</th></tr>
                    </thead>
                    <tbody>
                        @forelse($records as $i => $rec)
                        @php
                            $daysUntil = $rec->scheduled_date ? now()->diffInDays($rec->scheduled_date, false) : 0;
                        @endphp
                        <tr>
                            <td>{{ $records->firstItem() + $i }}</td>
                            <td><a href="{{ route('warehouse.fixed-assets.maintenance.show', $rec->id) }}"><code>{{ $rec->maintenance_number }}</code></a></td>
                            <td>{{ $rec->fixedAsset ? $rec->fixedAsset->asset_name : '-' }}<br><small class="text-muted">{{ $rec->fixedAsset ? $rec->fixedAsset->asset_code : '' }}</small></td>
                            <td>{{ ucfirst($rec->maintenance_type) }}</td>
                            <td>{{ $rec->scheduled_date ? $rec->scheduled_date->format('M d, Y') : '-' }}</td>
                            <td>
                                @if($daysUntil <= 3)
                                    <span class="badge badge-danger">{{ $daysUntil }} days</span>
                                @elseif($daysUntil <= 7)
                                    <span class="badge badge-warning">{{ $daysUntil }} days</span>
                                @else
                                    <span class="badge badge-info">{{ $daysUntil }} days</span>
                                @endif
                            </td>
                            <td>
                                @php $pColors = ['low'=>'secondary','medium'=>'info','high'=>'warning','critical'=>'danger']; @endphp
                                <span class="badge badge-{{ $pColors[$rec->priority] ?? 'secondary' }}">{{ ucfirst($rec->priority ?? 'medium') }}</span>
                            </td>
                            <td><a href="{{ route('warehouse.fixed-assets.maintenance.show', $rec->id) }}" class="btn btn-sm btn-info"><i class="material-icons" style="font-size: 14px;">visibility</i></a></td>
                        </tr>
                        @empty
                        <tr><td colspan="8" class="text-center text-muted py-4">No upcoming maintenance scheduled.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($records->hasPages())
        <div class="card-footer">{{ $records->links() }}</div>
        @endif
    </div>
@endsection
