@extends('layouts.admin')

@section('page_title', 'Work Reports')

@section('content')
    <div class="row align-items-center mb-5">
        <div class="col">
            <h2 class="fw-extrabold tracking-tight mb-1" style="font-weight: 800; font-size: 1.875rem;">Work Reports</h2>
            <p class="text-secondary opacity-75 mb-0">Review daily progress and task completions from employees.</p>
        </div>
        <div class="col-auto">
            <div class="dropdown">
                <button class="btn btn-light px-4 py-2 fw-bold d-flex align-items-center gap-2 border shadow-sm rounded-3"
                    type="button" data-bs-toggle="dropdown" style="font-size: 0.9375rem; background-color: #ffffff;">
                    <span class="material-symbols-outlined" style="font-size: 1.25rem;">download</span>
                    Export Reports
                </button>
                <ul class="dropdown-menu shadow-lg border-0 rounded-3 mt-2">
                    <li><a class="dropdown-item py-2" href="{{ route('admin.exports.reports.pdf') }}"><i
                                class="bi bi-file-pdf text-danger me-2"></i> PDF Summary</a></li>
                    <li><a class="dropdown-item py-2" href="{{ route('admin.exports.reports.excel') }}"><i
                                class="bi bi-file-earmark-excel text-success me-2"></i> Excel Full Export</a></li>
                </ul>
            </div>
        </div>
    </div>

    <!-- Filters Bar -->
    <div class="card border-0 shadow-sm mb-4"
        style="border-radius: 0.875rem; border: 1px solid rgba(0,0,0,0.05) !important;">
        <div class="card-body p-4">
            <div class="row g-4 align-items-end">
                <div class="col-lg-5">
                    <label class="text-uppercase fw-bold text-secondary mb-2 d-block"
                        style="font-size: 0.65rem; letter-spacing: 0.05rem;">Search Reports</label>
                    <div class="input-group">
                        <span class="input-group-text bg-light border-0 px-3"><span
                                class="material-symbols-outlined text-secondary opacity-50"
                                style="font-size: 1.25rem;">search</span></span>
                        <input type="text" id="reportFilterSearch" class="form-control border-0 bg-light py-2"
                            placeholder="Search by name or content..." style="font-size: 0.875rem;">
                    </div>
                </div>
                <div class="col-lg-3">
                    <label class="text-uppercase fw-bold text-secondary mb-2 d-block"
                        style="font-size: 0.65rem; letter-spacing: 0.05rem;">Approval Status</label>
                    <select id="filterStatus" class="form-select border-0 bg-light py-2" style="font-size: 0.875rem;">
                        <option value="">Any Status</option>
                        <option value="Approved">Approved</option>
                        <option value="Pending">Pending</option>
                        <option value="Rejected">Rejected</option>
                    </select>
                </div>
                <div class="col-lg-3">
                    <label class="text-uppercase fw-bold text-secondary mb-2 d-block"
                        style="font-size: 0.65rem; letter-spacing: 0.05rem;">Date Range</label>
                    <input type="date" id="filterDate" class="form-control border-0 bg-light py-2"
                        style="font-size: 0.875rem;">
                </div>
                <div class="col-lg-1">
                    <button class="btn btn-light w-100 fw-bold py-2" style="font-size: 0.875rem; background: #eaedf2;"
                        onclick="location.reload()">Reset</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Table Card -->
    <div class="card border-0 shadow-sm overflow-hidden"
        style="border-radius: 0.875rem; border: 1px solid rgba(0,0,0,0.05) !important;">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0" id="reportsTable">
                    <thead class="bg-light bg-opacity-50">
                        <tr>
                            <th class="px-4 py-3 text-uppercase text-secondary fw-bold"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem; width: 60px;">#</th>
                            <th class="px-4 py-3 text-uppercase text-secondary fw-bold"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem;">Employee</th>
                            <th class="px-4 py-3 text-uppercase text-secondary fw-bold"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem;">Submission Date</th>
                            <th class="px-4 py-3 text-uppercase text-secondary fw-bold"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem;">Report Summary</th>
                            <th class="px-4 py-3 text-uppercase text-secondary fw-bold"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem; width: 140px;">Status</th>
                            <th class="px-4 py-3 text-end text-uppercase text-secondary fw-bold pe-4"
                                style="font-size: 0.65rem; letter-spacing: 0.05rem; width: 120px;">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y">
                        @foreach ($reports as $report)
                            <tr class="transition-all hover-row">
                                <td class="px-4 py-4 text-secondary fw-medium" style="font-size: 0.8125rem;">
                                    {{ str_pad($loop->iteration, 2, '0', STR_PAD_LEFT) }}</td>
                                <td class="px-4 py-4">
                                    <div class="d-flex align-items-center gap-3">
                                        <div class="size-9 rounded-circle d-flex align-items-center justify-content-center fw-bold text-sm shadow-sm"
                                            style="width: 36px; height: 36px; font-size: 0.8rem; background-color: {{ ['#eef2ff', '#fff1f2', '#ecfdf5', '#fffbeb'][$report->user_id % 4] }}; color: {{ ['#4f46e5', '#e11d48', '#059669', '#d97706'][$report->user_id % 4] }};">
                                            {{ substr($report->user->name, 0, 1) }}{{ substr(strrchr($report->user->name, ' '), 1, 1) ?: '' }}
                                        </div>
                                        <span class="fw-bold" style="font-size: 0.875rem;">{{ $report->user->name }}</span>
                                    </div>
                                </td>
                                <td class="px-4 py-4 text-dark fw-medium" style="font-size: 0.875rem;">
                                    {{ $report->created_at->format('d M, Y') }}</td>
                                <td class="px-4 py-4">
                                    <p class="mb-0 text-secondary text-truncate"
                                        style="font-size: 0.875rem; max-width: 300px;">
                                        {{ Str::limit($report->summary, 80) }}</p>
                                </td>
                                <td class="px-4 py-4">
                                    @php
                                        $statusColor =
                                            $report->approval_status == 'approved'
                                                ? '#059669'
                                                : ($report->approval_status == 'rejected'
                                                    ? '#dc2626'
                                                    : '#d97706');
                                        $statusBg =
                                            $report->approval_status == 'approved'
                                                ? '#ecfdf5'
                                                : ($report->approval_status == 'rejected'
                                                    ? '#fef2f2'
                                                    : '#fffbeb');
                                    @endphp
                                    <span class="px-2.5 py-0.5 rounded-pill text-uppercase fw-bold"
                                        style="font-size: 0.625rem; background-color: {{ $statusBg }}; color: {{ $statusColor }}; border: 1px solid {{ $statusColor }}20;">
                                        {{ $report->approval_status }}
                                    </span>
                                    <span class="d-none">{{ ucfirst($report->approval_status) }}</span>
                                </td>
                                <td class="px-4 py-4 text-end pe-4">
                                    <a href="{{ route('reports.show', $report->id) }}" class="btn btn-icon hover-primary"
                                        title="View Details">
                                        <span class="material-symbols-outlined fs-5">visibility</span>
                                    </a>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            <!-- Pagination Placeholder -->
            <div class="px-4 py-4 bg-light bg-opacity-25 border-top d-flex align-items-center justify-content-between">
                <p class="text-secondary mb-0 fw-medium" style="font-size: 0.8125rem;">Showing <span
                        class="text-dark fw-bold">1-{{ count($reports) }}</span> reports</p>
                <div class="d-flex gap-2">
                    <button class="btn btn-outline-light border text-secondary px-2 py-1 btn-sm"><span
                            class="material-symbols-outlined fs-6 d-block">chevron_left</span></button>
                    <button class="btn btn-primary btn-sm px-2.5 py-1 fw-bold" style="font-size: 0.75rem;">1</button>
                    <button class="btn btn-outline-light border text-secondary px-2 py-1 btn-sm"><span
                            class="material-symbols-outlined fs-6 d-block">chevron_right</span></button>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('styles')
    <style>
        .tracking-tight {
            letter-spacing: -0.015rem;
        }

        .hover-row:hover {
            background-color: rgba(248, 250, 252, 0.5) !important;
        }

        .btn-icon {
            padding: 0.4rem;
            border-radius: 0.5rem;
            color: #94a3b8;
            background: transparent;
            border: 0;
            transition: all 0.2s;
        }

        .btn-icon:hover {
            color: #1a1f2e;
            background: #eaedf2;
        }

        .hover-primary:hover {
            color: var(--primary-color);
            background: rgba(13, 110, 253, 0.1);
        }

        /* DataTable Overrides */
        #reportsTable_wrapper .dataTables_filter,
        #reportsTable_wrapper .dataTables_length,
        #reportsTable_wrapper .dataTables_info,
        #reportsTable_wrapper .dataTables_paginate {
            display: none !important;
        }
    </style>
@endpush

@push('scripts')
    <script>
        $(document).ready(function() {
            const table = $('#reportsTable').DataTable({
                paging: true,
                lengthChange: false,
                pageLength: 25,
                searching: true
            });

            $('#reportFilterSearch').on('keyup', function() {
                table.search(this.value).draw();
            });
            $('#filterStatus').on('change', function() {
                table.column(4).search(this.value).draw();
            });
        });
    </script>
@endpush
