@extends('layouts.app')

@section('content')
    <x-common.page-breadcrumb pageTitle="{{ $title }}" />

    @if(session('success'))
        <div class="mb-4 rounded-lg bg-success-50 p-4 text-sm text-success-700 dark:bg-success-500/15 dark:text-success-500">
            {{ session('success') }}
        </div>
    @endif

    @if(session('error'))
        <div class="mb-4 rounded-lg bg-error-50 p-4 text-sm text-error-700 dark:bg-error-500/15 dark:text-error-500">
            {{ session('error') }}
        </div>
    @endif

    <div class="mb-4 rounded-2xl border border-brand-200 bg-brand-50 p-4 dark:border-brand-900/40 dark:bg-brand-900/10">
        <div class="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
            <div>
                <h3 class="text-sm font-semibold text-gray-900 dark:text-white/90">Absensi Karyawan</h3>
                <p class="mt-1 text-sm text-gray-600 dark:text-white/70">
                    Absensi hanya bisa diproses jika posisi karyawan berada dalam radius lokasi yang sudah ditentukan.
                </p>
            </div>

            <div class="flex flex-wrap gap-2">
                @if($setting)
                    <span class="inline-flex items-center rounded-lg bg-white px-3 py-1 text-xs font-medium text-gray-700 ring-1 ring-gray-200 dark:bg-white/10 dark:text-white/70 dark:ring-white/10">
                        Radius {{ $setting->radius_meters }} meter
                    </span>

                    @if($setting->work_start_time)
                        <span class="inline-flex items-center rounded-lg bg-white px-3 py-1 text-xs font-medium text-gray-700 ring-1 ring-gray-200 dark:bg-white/10 dark:text-white/70 dark:ring-white/10">
                            Masuk {{ substr($setting->work_start_time, 0, 5) }}
                        </span>
                    @endif

                    @if($setting->work_end_time)
                        <span class="inline-flex items-center rounded-lg bg-white px-3 py-1 text-xs font-medium text-gray-700 ring-1 ring-gray-200 dark:bg-white/10 dark:text-white/70 dark:ring-white/10">
                            Pulang {{ substr($setting->work_end_time, 0, 5) }}
                        </span>
                    @endif
                @else
                    <span class="inline-flex items-center rounded-lg bg-error-50 px-3 py-1 text-xs font-medium text-error-700 dark:bg-error-500/15 dark:text-error-400">
                        Lokasi belum diatur
                    </span>
                @endif

                <a href="{{ route('attendance.recap') }}" class="inline-flex items-center rounded-lg bg-brand-500 px-3 py-1 text-xs font-medium text-white hover:bg-brand-600">
                    Rekap Bulanan
                </a>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 gap-4 lg:grid-cols-3">
        <div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="flex items-start justify-between gap-3">
                <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Absen Hari Ini</h3>

                @if($employee?->is_auto_attendance ?? false)
                    <span class="inline-flex rounded-full bg-brand-50 px-2.5 py-0.5 text-xs font-medium text-brand-700 dark:bg-brand-500/15 dark:text-brand-400">
                        Auto Absen
                    </span>
                @endif
            </div>

            <div class="mt-4 space-y-3 text-sm text-gray-600 dark:text-gray-400">
                <div>
                    <span class="font-medium text-gray-800 dark:text-white/90">Karyawan:</span>
                    {{ $employee->name ?? 'Belum terhubung' }}
                </div>

                <div>
                    <span class="font-medium text-gray-800 dark:text-white/90">Masuk:</span>
                    {{ $attendance?->check_in_at?->format('H:i') ?? '-' }}
                </div>

                <div>
                    <span class="font-medium text-gray-800 dark:text-white/90">Pulang:</span>
                    {{ $attendance?->check_out_at?->format('H:i') ?? '-' }}
                </div>

                <div>
                    <span class="font-medium text-gray-800 dark:text-white/90">Telat:</span>
                    {{ ($attendance?->late_minutes ?? 0) > 0 ? $attendance->late_minutes . ' menit' : '-' }}
                </div>

                <div>
                    <span class="font-medium text-gray-800 dark:text-white/90">Pulang Cepat:</span>
                    {{ ($attendance?->early_leave_minutes ?? 0) > 0 ? $attendance->early_leave_minutes . ' menit' : '-' }}
                </div>
            </div>

            @if($employee?->is_auto_attendance ?? false)
                <div class="mt-5 rounded-2xl bg-brand-50 p-4 text-sm text-brand-700 dark:bg-brand-500/15 dark:text-brand-300">
                    Akun ini menggunakan Auto Absen / Jam Flexible. Data absensi otomatis tercatat per hari dan hanya ditampilkan di Rekap Absensi.
                </div>
            @else
                <form id="checkInForm" action="{{ route('attendance.check-in') }}" method="POST" class="mt-5">
                    @csrf
                    <input type="hidden" name="latitude" id="checkInLatitude">
                    <input type="hidden" name="longitude" id="checkInLongitude">

                    <button type="button"
                        onclick="submitAttendance('checkInForm', 'checkInLatitude', 'checkInLongitude')"
                        class="w-full rounded-lg bg-brand-500 px-4 py-2 text-sm font-medium text-white hover:bg-brand-600">
                        Absen Masuk
                    </button>
                </form>

                <form id="checkOutForm" action="{{ route('attendance.check-out') }}" method="POST" class="mt-3">
                    @csrf
                    <input type="hidden" name="latitude" id="checkOutLatitude">
                    <input type="hidden" name="longitude" id="checkOutLongitude">

                    <button type="button"
                        onclick="submitAttendance('checkOutForm', 'checkOutLatitude', 'checkOutLongitude')"
                        class="w-full rounded-lg bg-gray-800 px-4 py-2 text-sm font-medium text-white hover:bg-gray-900 dark:bg-gray-700 dark:hover:bg-gray-600">
                        Absen Pulang
                    </button>
                </form>

                <p id="locationStatus" class="mt-3 text-xs text-gray-500 dark:text-gray-400"></p>
            @endif
        </div>

        <div class="lg:col-span-2 rounded-2xl border border-gray-200 bg-white dark:border-gray-800 dark:bg-white/[0.03]">
            <div class="border-b border-gray-200 px-5 py-4 dark:border-gray-800">
                <h3 class="text-lg font-semibold text-gray-800 dark:text-white/90">Riwayat Absensi Manual</h3>
                <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                    Data absensi manual terbaru. Akun Auto Absen hanya tampil di Rekap Absensi.
                </p>
            </div>

            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                        <tr class="border-b border-gray-200 dark:border-gray-800">
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Tanggal</th>
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Karyawan</th>
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Masuk</th>
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Pulang</th>
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Telat</th>
                            <th class="px-5 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Pulang Cepat</th>
                        </tr>
                    </thead>

                    <tbody class="divide-y divide-gray-200 dark:divide-gray-800">
                        @forelse($attendances as $item)
                            <tr class="hover:bg-gray-50 dark:hover:bg-white/[0.02]">
                                <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                                    {{ $item->attendance_date->format('d/m/Y') }}
                                </td>

                                <td class="px-5 py-4 text-sm font-medium text-gray-800 dark:text-white/90">
                                    {{ $item->employee->name ?? '-' }}
                                </td>

                                <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                                    {{ $item->check_in_at?->format('H:i') ?? '-' }}
                                </td>

                                <td class="px-5 py-4 text-sm text-gray-500 dark:text-gray-400">
                                    {{ $item->check_out_at?->format('H:i') ?? '-' }}
                                </td>

                                <td class="px-5 py-4 text-sm">
                                    @if(($item->late_minutes ?? 0) > 0)
                                        <span class="inline-flex rounded-full bg-error-50 px-2.5 py-0.5 text-xs font-medium text-error-700 dark:bg-error-500/15 dark:text-error-400">
                                            {{ $item->late_minutes }} menit
                                        </span>
                                    @else
                                        <span class="text-gray-500 dark:text-gray-400">-</span>
                                    @endif
                                </td>

                                <td class="px-5 py-4 text-sm">
                                    @if(($item->early_leave_minutes ?? 0) > 0)
                                        <span class="inline-flex rounded-full bg-warning-50 px-2.5 py-0.5 text-xs font-medium text-warning-700 dark:bg-warning-500/15 dark:text-warning-400">
                                            {{ $item->early_leave_minutes }} menit
                                        </span>
                                    @else
                                        <span class="text-gray-500 dark:text-gray-400">-</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-5 py-8 text-center text-sm text-gray-500 dark:text-gray-400">
                                    Belum ada data absensi manual
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <div class="border-t border-gray-200 px-5 py-4 dark:border-gray-800">
                {{ $attendances->links() }}
            </div>
        </div>
    </div>

    <script>
        function submitAttendance(formId, latitudeInputId, longitudeInputId) {
            const status = document.getElementById('locationStatus');

            if (!navigator.geolocation) {
                status.innerText = 'Browser tidak mendukung GPS / geolocation.';
                return;
            }

            status.innerText = 'Mengambil lokasi kamu...';

            navigator.geolocation.getCurrentPosition(
                function(position) {
                    document.getElementById(latitudeInputId).value = position.coords.latitude;
                    document.getElementById(longitudeInputId).value = position.coords.longitude;

                    status.innerText = 'Lokasi berhasil didapat. Memproses absensi...';

                    document.getElementById(formId).submit();
                },
                function(error) {
                    status.innerText = 'Gagal mengambil lokasi. Pastikan izin lokasi diaktifkan.';
                },
                {
                    enableHighAccuracy: true,
                    timeout: 15000,
                    maximumAge: 0
                }
            );
        }
    </script>
@endsection