@extends('layouts.app')

@section('title', __('Machines'))

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0">{{ __('Machine Fleet') }}</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">{{ __('Dashboard') }}</a></li>
                    <li class="breadcrumb-item active">{{ __('Machines') }}</li>
                </ol>
            </nav>
        </div>
        <a href="{{ route('machines.create') }}" class="btn btn-primary">
            <i class="bi bi-cpu me-2"></i> {{ __('Register Machine') }}
        </a>
    </div>

    <div class="row g-4">
        @forelse($machines as $machine)
            <div class="col-md-6 col-lg-4">
                <div class="card h-100 border-0 shadow-sm overflow-hidden">
                    <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                        <div>
                            <span class="badge bg-light text-dark border mb-1 small">{{ $machine->code }}</span>
                            <h6 class="mb-0 fw-bold">{{ $machine->name }}</h6>
                        </div>
                        <div class="dropdown">
                            <button class="btn btn-light btn-sm rounded-circle" data-bs-toggle="dropdown">
                                <i class="bi bi-three-dots"></i>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-end shadow border-0">
                                <li><a class="dropdown-item" href="{{ route('machines.edit', $machine->id) }}">{{ __('Edit Settings') }}</a>
                                </li>
                                <li>
                                    <hr class="dropdown-divider">
                                </li>
                                <li>
                                    <form action="{{ route('machines.destroy', $machine->id) }}" method="POST"
                                        onsubmit="return confirm('{{ __('Delete this machine registry?') }}')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="dropdown-item text-danger">{{ __('Decommission') }}</button>
                                    </form>
                                </li>
                            </ul>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="d-flex align-items-center justify-content-between mb-3">
                            <div class="small text-muted">{{ __('Status') }}</div>
                            <span
                                class="badge {{ $machine->status == 'running' ? 'bg-success' : ($machine->status == 'under_maintenance' ? 'bg-danger' : 'bg-warning') }}">
                                {{ __(str_replace('_', ' ', ucfirst($machine->status))) }}
                            </span>
                        </div>

                        <div class="p-2 bg-light rounded-3 mb-3">
                            <div class="d-flex align-items-center gap-2 mb-1">
                                <i class="bi bi-geo-alt-fill text-primary small"></i>
                                <span class="small fw-semibold text-truncate">
                                    {{ $machine->productionLine ? $machine->productionLine->name : __('Unassigned') }}
                                </span>
                            </div>
                        </div>

                        <div class="row g-2 text-center">
                            <div class="col-6">
                                <div class="p-2 border rounded">
                                    <div class="text-muted smallest text-uppercase">{{ __('Last Service') }}</div>
                                    <div class="fw-bold small">
                                        {{ $machine->last_maintenance_at ? $machine->last_maintenance_at->format('M d, Y') : __('Never') }}
                                    </div>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="p-2 border rounded">
                                    <div class="text-muted smallest text-uppercase">{{ __('Next Service') }}</div>
                                    <div class="fw-bold small text-primary">
                                        {{ $machine->next_maintenance_at ? $machine->next_maintenance_at->format('M d, Y') : __('TBD') }}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="card-footer bg-white border-0 pt-0 pb-3">
                        <a href="{{ route('machines.show', $machine->id) }}"
                            class="btn btn-sm btn-outline-primary w-100">{{ __('Performance Metrics') }}</a>
                    </div>
                </div>
            </div>
        @empty
            <div class="col-12">
                <div class="text-center py-5 bg-white rounded shadow-sm">
                    <i class="bi bi-cpu text-muted display-1 mb-3 opacity-25"></i>
                    <h5>{{ __('No machines registered') }}</h5>
                    <p class="text-muted">{{ __('Start by registering your factory assets.') }}</p>
                    <a href="{{ route('machines.create') }}" class="btn btn-primary">{{ __('Add Machine') }}</a>
                </div>
            </div>
        @endforelse
    </div>

    <div class="mt-4">
        {{ $machines->links() }}
    </div>

    <style>
        .smallest {
            font-size: 0.7rem;
        }
    </style>
@endsection