@extends('layouts.app')

@section('page_title', 'Work Report Details')

@section('content')
    <div class="row">
        <div class="col-lg-12">
            <div class="mb-4 d-flex justify-content-between align-items-center">
                <a href="{{ url()->previous() }}" class="text-decoration-none text-muted small">
                    <i class="bi bi-arrow-left me-1"></i> Back
                </a>
                <div class="d-flex align-items-center gap-2">
                    @if (auth()->user()->hasRole(['admin', 'super_admin']))
                        <a href="{{ route('admin.exports.report.pdf', $report->id) }}"
                            class="btn btn-sm btn-outline-primary rounded-pill px-3">
                            <i class="bi bi-file-pdf me-1"></i> Download PDF
                        </a>
                    @endif
                    <span
                        class="badge bg-{{ $report->approval_status == 'approved' ? 'success' : ($report->approval_status == 'rejected' ? 'danger' : 'warning') }}-subtle text-{{ $report->approval_status == 'approved' ? 'success' : ($report->approval_status == 'rejected' ? 'danger' : 'warning') }} px-3 rounded-pill">
                        {{ ucfirst($report->approval_status) }}
                    </span>
                </div>
            </div>

            <div class="card border-0 shadow-sm overflow-hidden">
                <div class="card-header bg-white p-4 border-0">
                    <div class="d-flex align-items-center">
                        <div class="icon-box bg-primary-subtle text-primary rounded-3 me-3"
                            style="width: 50px; height: 50px; display: flex; align-items: center; justify-content: center;">
                            <i class="bi bi-file-earmark-text fs-4"></i>
                        </div>
                        <div>
                            <h5 class="fw-bold mb-0">Work Report - {{ $report->created_at->format('d M, Y') }}</h5>
                            <p class="text-muted small mb-0">Submitted by {{ $report->user->name }} at
                                {{ $report->submitted_at->format('h:i A') }}</p>
                        </div>
                    </div>
                </div>
                <div class="card-body p-4 pt-0">
                    <div class="row g-3 mb-4 text-center">
                        <div class="col-md-3">
                            <div class="p-3 bg-light rounded-4 h-100">
                                <span class="d-block small text-muted text-uppercase fw-bold mb-1">Check In</span>
                                <h5 class="fw-bold mb-0">
                                    {{ $report->attendance->check_in ? $report->attendance->check_in->format('h:i A') : '-' }}
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="p-3 bg-light rounded-4 h-100">
                                <span class="d-block small text-muted text-uppercase fw-bold mb-1">Check Out</span>
                                <h5 class="fw-bold mb-0">
                                    {{ $report->attendance->check_out ? $report->attendance->check_out->format('h:i A') : 'Active' }}
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="p-3 bg-primary-subtle rounded-4 h-100">
                                <span class="d-block small text-primary text-uppercase fw-bold mb-1">Total Break</span>
                                <h5 class="fw-bold mb-0 text-primary">
                                    {{ floor($report->attendance->total_break_minutes / 60) }}h
                                    {{ $report->attendance->total_break_minutes % 60 }}m
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="p-3 bg-success-subtle rounded-4 h-100">
                                <span class="d-block small text-success text-uppercase fw-bold mb-1">Net Work Time</span>
                                <h5 class="fw-bold mb-0 text-success">
                                    {{ floor($report->attendance->total_work_minutes / 60) }}h
                                    {{ $report->attendance->total_work_minutes % 60 }}m
                                </h5>
                            </div>
                        </div>
                    </div>

                    <div class="mb-4">
                        <h6 class="fw-bold small text-uppercase text-muted mb-3">Punch Timeline</h6>
                        <div class="card border-0 bg-light rounded-4">
                            <div class="card-body p-4">
                                <div class="timeline-item d-flex mb-4">
                                    <div class="timeline-icon bg-success text-white rounded-circle d-flex align-items-center justify-content-center me-3"
                                        style="width: 40px; height: 40px;">
                                        <i class="bi bi-box-arrow-in-right"></i>
                                    </div>
                                    <div>
                                        <h6 class="fw-bold mb-0">Check In</h6>
                                        <p class="text-muted small mb-0">
                                            {{ $report->attendance->check_in ? $report->attendance->check_in->format('h:i:s A - d M Y') : 'Missing data' }}
                                        </p>
                                    </div>
                                </div>

                                @foreach ($report->attendance->breaks->sortBy('break_start') as $break)
                                    <div class="timeline-item d-flex mb-4 ms-5 border-start ps-4 position-relative">
                                        <div class="position-absolute start-0 top-0 mt-2 ms-n1 bg-warning rounded-circle"
                                            style="width: 8px; height: 8px; left: -4px;"></div>
                                        <div class="timeline-icon bg-warning text-white rounded-circle d-flex align-items-center justify-content-center me-3"
                                            style="width: 32px; height: 32px;">
                                            <i class="bi bi-pause-fill"></i>
                                        </div>
                                        <div>
                                            <h6 class="fw-bold mb-0 small">Break Taken</h6>
                                            <p class="text-muted small mb-0">
                                                {{ $break->break_start ? $break->break_start->format('h:i A') : '?' }} to
                                                {{ $break->break_end ? $break->break_end->format('h:i A') : 'Still on break' }}
                                                <span class="badge bg-light text-dark ms-2">{{ $break->duration_minutes }}
                                                    mins</span>
                                            </p>
                                        </div>
                                    </div>
                                @endforeach

                                @if ($report->attendance->check_out)
                                    <div class="timeline-item d-flex">
                                        <div class="timeline-icon bg-danger text-white rounded-circle d-flex align-items-center justify-content-center me-3"
                                            style="width: 40px; height: 40px;">
                                            <i class="bi bi-box-arrow-left"></i>
                                        </div>
                                        <div>
                                            <h6 class="fw-bold mb-0">Check Out</h6>
                                            <p class="text-muted small mb-0">
                                                {{ $report->attendance->check_out->format('h:i:s A - d M Y') }}</p>
                                        </div>
                                    </div>
                                @endif
                            </div>
                        </div>
                    </div>

                    <div class="mb-4">
                        <h6 class="fw-bold small text-uppercase text-muted mb-3">Break History</h6>
                        <div class="table-responsive">
                            <table class="table table-sm table-hover align-middle border rounded-4 overflow-hidden">
                                <thead class="bg-light">
                                    <tr>
                                        <th class="ps-3 border-0">Break Start</th>
                                        <th class="border-0">Break End</th>
                                        <th class="border-0">Duration</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($report->attendance->breaks as $break)
                                        <tr>
                                            <td class="ps-3">
                                                {{ $break->break_start ? $break->break_start->format('h:i A') : '-' }}</td>
                                            <td>{{ $break->break_end ? $break->break_end->format('h:i A') : 'Active' }}
                                            </td>
                                            <td>{{ $break->duration_minutes }} mins</td>
                                        </tr>
                                    @empty
                                        <tr>
                                            <td colspan="3" class="text-center text-muted py-3 small">No breaks recorded
                                                for this session.</td>
                                        </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>
                    </div>

                    <div class="mb-4">
                        <h6 class="fw-bold small text-uppercase text-muted mb-3">Work Summary</h6>
                        <div class="p-4 bg-light rounded-4 text-secondary" style="line-height: 1.6; white-space: pre-line;">
                            {!! e($report->raw_summary) !!}
                        </div>
                    </div>

                    <div class="mb-4">
                        <h6 class="fw-bold small text-uppercase text-muted mb-3">Executed Tasks
                            ({{ $report->total_tasks }})
                        </h6>
                        <div class="list-group list-group-flush border rounded-4 overflow-hidden">
                            @foreach ($report->tasks as $task)
                                <div
                                    class="list-group-item p-3 d-flex align-items-start border-bottom last-child-no-border">
                                    <span class="badge bg-primary rounded-circle me-3 mt-1"
                                        style="width: 24px; height: 24px; display: flex; align-items: center; justify-content: center;">{{ $loop->iteration }}</span>
                                    <div class="text-secondary fw-medium">{{ $task->task_description }}</div>
                                </div>
                            @endforeach
                        </div>
                    </div>

                    @if (auth()->user()->hasRole(['admin', 'super_admin']) && $report->approval_status == 'pending')
                        <div class="d-flex gap-2 mt-5">
                            <form action="{{ route('admin.reports.approve', $report->id) }}" method="POST"
                                class="flex-grow-1">
                                @csrf
                                <button type="submit" class="btn btn-success w-100 py-3 fw-bold rounded-3">APPROVE
                                    REPORT</button>
                            </form>
                            <form action="{{ route('admin.reports.reject', $report->id) }}" method="POST"
                                class="flex-grow-1">
                                @csrf
                                <button type="submit"
                                    class="btn btn-outline-danger w-100 py-3 fw-bold rounded-3">REJECT</button>
                            </form>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
@endsection

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