@extends('layouts.app')

@section('title', 'Machine Details')

@section('content')
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-0 fw-bold">Machine Asset: {{ $machine->name }}</h4>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0">
                    <li class="breadcrumb-item"><a href="{{ url('/') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('machines.index') }}">Machines</a></li>
                    <li class="breadcrumb-item active">Details</li>
                </ol>
            </nav>
        </div>
        <div class="d-flex gap-2">
            <a href="{{ route('machines.edit', $machine->id) }}" class="btn btn-outline-primary">
                <i class="bi bi-pencil me-2"></i> Edit Settings
            </a>
            <button class="btn btn-light" onclick="window.print()">
                <i class="bi bi-printer me-2"></i> Export Report
            </button>
        </div>
    </div>

    <div class="row g-4">
        <!-- Sidebar: Status & Core Info -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-4 overflow-hidden">
                <div class="card-body p-0">
                    <div class="p-4 {{ $machine->status == 'running' ? 'bg-success' : ($machine->status == 'under_maintenance' ? 'bg-danger' : 'bg-warning') }} text-white text-center">
                        <i class="bi bi-cpu display-4 mb-2 d-block"></i>
                        <h5 class="mb-0 text-uppercase fw-bold">{{ str_replace('_', ' ', $machine->status) }}</h5>
                        <small class="opacity-75">Operational Status</small>
                    </div>
                    <div class="p-4">
                        <div class="mb-4">
                            <label class="small text-muted text-uppercase fw-bold d-block mb-1">Asset ID / Code</label>
                            <span class="badge bg-light text-dark border p-2 font-monospace">{{ $machine->code }}</span>
                        </div>
                        <div class="mb-4">
                            <label class="small text-muted text-uppercase fw-bold d-block mb-1">Model Name</label>
                            <div class="fw-bold">{{ $machine->model ?? 'Standard Industrial Series' }}</div>
                        </div>
                        <div class="mb-0">
                            <label class="small text-muted text-uppercase fw-bold d-block mb-1">Serial Number</label>
                            <div class="font-monospace text-muted">{{ $machine->serial_number ?? 'N/A' }}</div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3">
                    <h6 class="mb-0 fw-bold"><i class="bi bi-geo-alt me-2 text-primary"></i>Placement</h6>
                </div>
                <div class="card-body">
                    @if($machine->productionLine)
                        <div class="mb-3">
                            <label class="small text-muted d-block">Production Line</label>
                            <div class="fw-bold">{{ $machine->productionLine->name }}</div>
                        </div>
                        <div class="mb-0">
                            <label class="small text-muted d-block">Facility / Zone</label>
                            <div class="text-muted small">
                                {{ $machine->productionLine->zone->factory->name }} - 
                                {{ $machine->productionLine->zone->name }}
                            </div>
                        </div>
                    @else
                        <div class="text-center py-3 text-muted">
                            <i class="bi bi-exclamation-triangle-fill d-block mb-2 text-warning"></i>
                            Unassigned
                        </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Main Content: Maintenance & Performance -->
        <div class="col-lg-8">
            <div class="row g-4 mb-4">
                <div class="col-md-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center mb-3">
                                <div class="bg-light-primary p-3 rounded-circle me-3">
                                    <i class="bi bi-tools text-primary h4 mb-0"></i>
                                </div>
                                <div>
                                    <div class="text-muted small">Total Logs</div>
                                    <h4 class="mb-0 fw-bold">{{ $machine->maintenanceLogs->count() }}</h4>
                                </div>
                            </div>
                            <div class="small">
                                <span class="text-muted">Last service:</span> 
                                <span class="fw-bold">
                                    {{ $machine->last_maintenance_at ? $machine->last_maintenance_at->format('M d, Y') : 'Never' }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="card border-0 shadow-sm h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center mb-3">
                                <div class="bg-light-info p-3 rounded-circle me-3">
                                    <i class="bi bi-calendar-check text-info h4 mb-0"></i>
                                </div>
                                <div>
                                    <div class="text-muted small">Scheduled Service</div>
                                    <h4 class="mb-0 fw-bold text-info">
                                        {{ $machine->maintenanceSchedules->where('status', 'pending')->count() }}
                                    </h4>
                                </div>
                            </div>
                            <div class="small">
                                <span class="text-muted">Next due:</span>
                                <span class="fw-bold text-primary">
                                    {{ $machine->next_maintenance_at ? $machine->next_maintenance_at->format('M d, Y') : 'Not Scheduled' }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3">
                    <h6 class="mb-0 fw-bold text-primary"><i class="bi bi-clock-history me-2"></i>Historical Maintenance Logs</h6>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">Date</th>
                                    <th>Technician</th>
                                    <th>Duration</th>
                                    <th class="text-end pe-4">Cost</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($machine->maintenanceLogs->sortByDesc('start_time')->take(5) as $log)
                                    <tr>
                                        <td class="ps-4">
                                            <div class="fw-bold">{{ $log->start_time->format('M d, Y') }}</div>
                                            <small class="text-muted">{{ $log->start_time->format('H:i') }}</small>
                                        </td>
                                        <td>{{ $log->technician->name }}</td>
                                        <td>{{ $log->downtime_minutes }} mins</td>
                                        <td class="text-end pe-4 fw-bold">${{ number_format($log->cost, 2) }}</td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="4" class="text-center py-4 text-muted">No maintenance history recorded for this asset.</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
                @if($machine->maintenanceLogs->count() > 5)
                    <div class="card-footer bg-white border-0 text-center pb-3">
                        <a href="#" class="small fw-bold text-decoration-none">View All Recorded Logs</a>
                    </div>
                @endif
            </div>
        </div>
    </div>

    <style>
        .bg-light-primary { background-color: rgba(13, 110, 253, 0.1); }
        .bg-light-info { background-color: rgba(13, 202, 240, 0.1); }
        @media print {
            .btn, .breadcrumb, .card-header i, .card-footer { display: none !important; }
            .card { border: 1px solid #ddd !important; box-shadow: none !important; }
            .bg-success, .bg-danger, .bg-warning { -webkit-print-color-adjust: exact; }
        }
    </style>
@endsection
