@extends('layouts.admin')

@section('title', 'Dashboard')
@section('page-title', 'Dashboard')

@section('content')
<div class="row g-4 mb-4">
    <div class="col-md-4">
        <div class="premium-card p-4">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Total Operators</div>
                    <h2 class="fw-bold mb-0">{{ $totalOperators }}</h2>
                </div>
                <div class="rounded-3 p-3" style="background:linear-gradient(135deg, #667eea, #764ba2);">
                    <i class="bi bi-people-fill text-white fs-4"></i>
                </div>
            </div>
            <div class="mt-3">
                <a href="{{ route('admin.operators.index') }}" class="text-decoration-none small fw-semibold" style="color:#764ba2;">
                    View all <i class="bi bi-arrow-right ms-1"></i>
                </a>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="premium-card p-4">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Total Admins</div>
                    <h2 class="fw-bold mb-0">{{ $totalAdmins }}</h2>
                </div>
                <div class="rounded-3 p-3" style="background:linear-gradient(135deg, #f093fb, #f5576c);">
                    <i class="bi bi-shield-lock-fill text-white fs-4"></i>
                </div>
            </div>
            <div class="mt-3">
                <span class="text-muted small">System administrators</span>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="premium-card p-4">
            <div class="d-flex align-items-center justify-content-between">
                <div>
                    <div class="text-muted small fw-semibold text-uppercase mb-1">Machine Shifts</div>
                    <h2 class="fw-bold mb-0">{{ $sessions->total() }}</h2>
                </div>
                <div class="rounded-3 p-3" style="background:linear-gradient(135deg, #4facfe, #00f2fe);">
                    <i class="bi bi-clock-history text-white fs-4"></i>
                </div>
            </div>
            <div class="mt-3">
                <span class="text-muted small">Total recorded shifts</span>
            </div>
        </div>
    </div>
</div>

{{-- Quick Actions --}}
<div class="premium-card p-4 mb-4">
    <h6 class="fw-bold text-uppercase text-muted small mb-3">Quick Actions</h6>
    <a href="{{ route('admin.operators.index') }}" class="btn btn-sm px-3 py-2 me-2" style="background:#f0f2f5; color:#1a1a2e; font-weight:600; border-radius:10px;">
        <i class="bi bi-people me-1"></i>Manage Operators
    </a>
    <a href="{{ route('admin.operators.create') }}" class="btn btn-sm px-3 py-2" style="background:linear-gradient(135deg, #780045, #F54927); color:#fff; font-weight:600; border-radius:10px;">
        <i class="bi bi-person-plus me-1"></i>Add Operator
    </a>
</div>

{{-- Machine Shifts Table --}}
<div class="premium-card overflow-hidden">
    <div class="p-4 pb-0 d-flex justify-content-between align-items-center">
        <div>
            <h6 class="fw-bold mb-1">Machine Shifts</h6>
            <p class="text-muted small mb-0">All production run/stop records</p>
        </div>
        <span class="badge rounded-pill px-3 py-2" style="background:#f0f2f5; color:#6b7280; font-weight:600;">
            {{ $sessions->total() }} records
        </span>
    </div>
    <div class="table-responsive mt-3">
        <table class="table align-middle mb-0" style="font-size:.9rem;">
            <thead>
                <tr style="background:#f8f9fb;">
                    <th class="text-muted fw-semibold ps-4" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">#</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Product</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Incharge</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Case</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Start</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Stop</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Duration</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Stop Reason</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Operator</th>
                    <th class="text-muted fw-semibold" style="font-size:.75rem; text-transform:uppercase; letter-spacing:.5px;">Status</th>
                </tr>
            </thead>
            <tbody>
                @forelse($sessions as $session)
                    <tr style="border-bottom:1px solid #f0f2f5;">
                        <td class="text-muted ps-4">{{ $session->id }}</td>
                        <td class="fw-semibold">
                            {{ $session->product_name }}
                            @if($session->segments_count > 1)
                                <span class="badge rounded-pill ms-1" style="background:rgba(26,115,232,.1); color:#1a73e8; font-size:.65rem; font-weight:600;">
                                    {{ $session->segments_count }} products
                                </span>
                            @endif
                        </td>
                        <td>{{ $session->incharge_name }}</td>
                        <td>
                            @if($session->case_number_history && str_contains($session->case_number_history, '+'))
                                {{ $session->case_number_history }} = {{ $session->case_number }}
                            @else
                                {{ $session->case_number ?? '—' }}
                            @endif
                        </td>
                        <td>
                            @if($session->start_time)
                                <div>{{ $session->start_time->format('d M Y') }}</div>
                                <small class="text-muted">{{ $session->start_time->format('h:i:s A') }}</small>
                            @else
                                —
                            @endif
                        </td>
                        <td>
                            @if($session->stop_time)
                                <div>{{ $session->stop_time->format('d M Y') }}</div>
                                <small class="text-muted">{{ $session->stop_time->format('h:i:s A') }}</small>
                            @else
                                —
                            @endif
                        </td>
                        <td>
                            @if($session->total_duration > 0)
                                <code style="background:#f0f2f5; padding:4px 10px; border-radius:6px; color:#1a1a2e; font-size:.85rem;">
                                    {{ gmdate('H:i:s', $session->total_duration) }}
                                </code>
                            @else
                                —
                            @endif
                        </td>
                        <td>
                            @if($session->stop_reason)
                                <span title="{{ $session->stop_reason }}">{{ Str::limit($session->stop_reason, 30) }}</span>
                            @else
                                —
                            @endif
                        </td>
                        <td>{{ $session->operator->name ?? '—' }}</td>
                        <td>
                            @if($session->status === 'running')
                                <span class="badge rounded-pill px-3 py-1" style="background:rgba(16,185,129,.1); color:#059669; font-weight:600;">
                                    <i class="bi bi-play-fill me-1"></i>Running
                                </span>
                            @else
                                <span class="badge rounded-pill px-3 py-1" style="background:rgba(239,68,68,.1); color:#dc2626; font-weight:600;">
                                    <i class="bi bi-stop-fill me-1"></i>Stopped
                                </span>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="10" class="text-center text-muted py-5">
                            <i class="bi bi-inbox fs-2 d-block mb-2" style="opacity:.3;"></i>
                            No machine shifts recorded yet.
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($sessions->hasPages())
        <div class="p-3 d-flex justify-content-center border-top">
            {{ $sessions->links() }}
        </div>
    @endif
</div>
@endsection
