@extends('layouts.app')

@section('page_title', 'Employee Dashboard')

@section('content')
    <div class="row g-4">
        <!-- Today's Attendance -->
        <div class="col-lg-12">
            <div class="card border-0 shadow-sm overflow-hidden mb-4">
                <div class="card-body p-4">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <div>
                            <h5 class="fw-bold mb-1">Daily Attendance</h5>
                            <p class="text-muted small mb-0">{{ now()->format('l, jS F Y') }}</p>
                        </div>
                        <div id="liveClock" class="fs-4 fw-bold text-primary">00:00:00</div>
                    </div>

                    <div class="row g-3">
                        <div class="col-md-4">
                            <div class="p-3 rounded-4 bg-light border text-center">
                                <span class="text-muted d-block small mb-1 text-uppercase fw-bold tracking-wider">Check
                                    In</span>
                                <h4 class="mb-0 fw-bold">
                                    {{ $attendance && $attendance->check_in ? $attendance->check_in->format('h:i A') : '--:--' }}
                                </h4>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="p-3 rounded-4 bg-primary text-white text-center shadow-sm">
                                <span class="text-white-50 d-block small mb-1 text-uppercase fw-bold tracking-wider">Net
                                    Work Time</span>
                                <h4 class="mb-0 fw-bold" id="workTimer">
                                    @if ($attendance && $attendance->check_in && !$attendance->check_out)
                                        00:00:00
                                    @elseif($attendance && $attendance->check_out)
                                        {{ floor($attendance->total_work_minutes / 60) }}h
                                        {{ $attendance->total_work_minutes % 60 }}m
                                    @else
                                        00:00:00
                                    @endif
                                </h4>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="p-3 rounded-4 bg-light border text-center">
                                <span class="text-muted d-block small mb-1 text-uppercase fw-bold tracking-wider">Check
                                    Out</span>
                                <h4 class="mb-0 fw-bold">
                                    {{ $attendance && $attendance->check_out ? $attendance->check_out->format('h:i A') : '--:--' }}
                                </h4>
                            </div>
                        </div>
                    </div>

                    @if ($activeBreak)
                        <div
                            class="alert alert-warning mt-3 mb-0 py-2 d-flex justify-content-between align-items-center rounded-3 border-0">
                            <div class="small fw-medium">
                                <i class="bi bi-cup-hot-fill me-2"></i> On Break since
                                {{ $activeBreak->break_start->format('h:i A') }}
                            </div>
                            <div class="fw-bold" id="breakTimer">00:00:00</div>
                        </div>
                    @endif

                    <div class="mt-4 d-flex gap-2">
                        @if (!$attendance || !$attendance->check_in)
                            <form action="{{ route('attendance.checkin') }}" method="POST" class="flex-grow-1">
                                @csrf
                                <button type="submit" class="btn btn-primary w-100 py-2 fw-bold fs-5 shadow-sm">
                                    <i class="bi bi-box-arrow-in-right me-2"></i> CHECK IN
                                </button>
                            </form>
                        @elseif(!$attendance->check_out)
                            <form action="{{ route('attendance.checkout') }}" method="POST" class="flex-grow-1">
                                @csrf
                                <button type="submit" class="btn btn-danger w-100 py-2 fw-bold fs-5 shadow-sm">
                                    <i class="bi bi-box-arrow-right me-2"></i> CHECK OUT
                                </button>
                            </form>
                            <form action="{{ route('attendance.break') }}" method="POST">
                                @csrf
                                <button type="submit"
                                    class="btn btn-{{ $activeBreak ? 'warning' : 'outline-warning' }} py-2 fw-bold fs-5 shadow-sm">
                                    <i class="bi bi-{{ $activeBreak ? 'play-fill' : 'cup-hot' }} me-2"></i>
                                    {{ $activeBreak ? 'RESUME' : 'BREAK' }}
                                </button>
                            </form>
                        @else
                            <div class="alert alert-success w-100 mb-0 py-3 text-center fw-bold rounded-3">
                                <i class="bi bi-check-circle-fill me-2"></i> Day Completed!
                            </div>
                        @endif
                    </div>
                </div>
            </div>

            <!-- Recent Attendance Table -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-0 p-4 pb-0">
                    <h6 class="fw-bold mb-0">Recent Attendance</h6>
                </div>
                <div class="card-body p-4">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="border-0 small text-muted text-uppercase">Date</th>
                                    <th class="border-0 small text-muted text-uppercase">Check In</th>
                                    <th class="border-0 small text-muted text-uppercase">Check Out</th>
                                    <th class="border-0 small text-muted text-uppercase">Total Time</th>
                                    <th class="border-0 small text-muted text-uppercase">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($recentAttendance as $item)
                                    <tr>
                                        <td class="fw-medium">{{ $item->date->format('d M, Y') }}</td>
                                        <td>{{ $item->check_in ? $item->check_in->format('h:i A') : '-' }}</td>
                                        <td>{{ $item->check_out ? $item->check_out->format('h:i A') : '-' }}</td>
                                        <td>
                                            @if ($item->total_work_minutes)
                                                {{ floor($item->total_work_minutes / 60) }}h
                                                {{ $item->total_work_minutes % 60 }}m
                                            @else
                                                -
                                            @endif
                                        </td>
                                        <td>
                                            <span
                                                class="badge bg-{{ $item->status == 'present' ? 'success' : ($item->status == 'absent' ? 'danger' : 'warning') }}-subtle text-{{ $item->status == 'present' ? 'success' : ($item->status == 'absent' ? 'danger' : 'warning') }} px-3">
                                                {{ ucfirst($item->status) }}
                                            </span>
                                            @if ($item->is_late)
                                                <span class="badge bg-danger px-1 small ms-1"
                                                    style="font-size: 0.6rem;">LATE</span>
                                            @endif
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="5" class="text-center py-4 text-muted small">No records found</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Right Sidebar Widgets -->
        <div class="col-lg-4">
            <!-- Pending Tasks -->
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-header bg-white border-0 p-4 pb-1 d-flex justify-content-between align-items-center">
                    <h6 class="fw-bold mb-0">My Tasks</h6>
                    <a href="#" class="small text-decoration-none">View All</a>
                </div>
                <div class="card-body p-4 pt-2">
                    @forelse($pendingTasks as $task)
                        <div class="d-flex align-items-center p-3 mb-2 bg-light rounded-3 hover-shadow transition">
                            <div
                                class="task-dot bg-{{ $task->priority == 'high' ? 'danger' : ($task->priority == 'medium' ? 'warning' : 'info') }} me-3">
                            </div>
                            <div class="overflow-hidden">
                                <p class="mb-0 fw-semibold text-truncate small">{{ $task->title }}</p>
                                <span class="text-muted" style="font-size: 0.7rem;">Due:
                                    {{ $task->due_date ? $task->due_date->format('d M') : 'No date' }}</span>
                            </div>
                        </div>
                    @empty
                        <div class="text-center py-3 text-muted small">No pending tasks</div>
                    @endforelse
                </div>
            </div>

            <!-- Announcements -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-0 p-4 pb-1">
                    <h6 class="fw-bold mb-0">Announcements</h6>
                </div>
                <div class="card-body p-4 pt-2">
                    @forelse($announcements as $anc)
                        <div class="mb-3 pb-3 border-bottom last-child-no-border">
                            <div class="d-flex align-items-center mb-1">
                                @if ($anc->pinned)
                                    <i class="bi bi-pin-angle-fill text-danger me-2 small"></i>
                                @endif
                                <h6 class="mb-0 fw-bold small">{{ $anc->title }}</h6>
                            </div>
                            <p class="text-muted mb-1" style="font-size: 0.75rem;">{{ Str::limit($anc->body, 80) }}</p>
                            <span class="text-muted smaller"
                                style="font-size: 0.65rem;">{{ $anc->created_at->diffForHumans() }}</span>
                        </div>
                    @empty
                        <div class="text-center py-3 text-muted small">No announcements</div>
                    @endforelse
                </div>
            </div>
        </div>
    </div>

    @push('scripts')
        <script>
            function updateClock() {
                const now = new Date();
                const timeString = now.toLocaleTimeString([], {
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit',
                    hour12: true
                });
                $('#liveClock').text(timeString);

                // Work Timer
                const checkInStr =
                    "{{ $attendance && $attendance->check_in ? $attendance->check_in->toIso8601String() : '' }}";
                const checkOutStr =
                    "{{ $attendance && $attendance->check_out ? $attendance->check_out->toIso8601String() : '' }}";
                const activeBreakStr = "{{ $activeBreak ? $activeBreak->break_start->toIso8601String() : '' }}";
                const finishedBreakMins = {{ $finishedBreakMinutes ?? 0 }};

                if (checkInStr && !checkOutStr) {
                    const checkIn = new Date(checkInStr);
                    let diffMs = now - checkIn;

                    // Subtract finished breaks
                    diffMs -= (finishedBreakMins * 60 * 1000);

                    // If currently on break, subtract the current break duration so far
                    if (activeBreakStr) {
                        const breakStart = new Date(activeBreakStr);
                        const currentBreakMs = now - breakStart;
                        diffMs -= currentBreakMs;

                        // Also update break timer
                        updateTimer('#breakTimer', currentBreakMs);
                    }

                    updateTimer('#workTimer', diffMs);
                }
            }

            function updateTimer(selector, ms) {
                if (ms < 0) ms = 0;
                const hh = Math.floor(ms / 3600000);
                const mm = Math.floor((ms % 3600000) / 60000);
                const ss = Math.floor((ms % 60000) / 1000);
                $(selector).text(
                    (hh < 10 ? "0" + hh : hh) + ":" +
                    (mm < 10 ? "0" + mm : mm) + ":" +
                    (ss < 10 ? "0" + ss : ss)
                );
            }

            setInterval(updateClock, 1000);
            updateClock();
        </script>
    @endpush

    <style>
        .task-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            flex-shrink: 0;
        }

        .last-child-no-border:last-child {
            border-bottom: 0 !important;
            margin-bottom: 0 !important;
        }
    </style>
@endsection
