@extends('layouts.app')

@section('title', 'হাজিরা ড্যাশবোর্ড')

@section('content')
<div class="container-fluid">
    <!-- Statistics Cards -->
    <div class="row">
        <div class="col-lg-4 col-6">
            <div class="small-box bg-info">
                <div class="inner">
                    <h3>{{ $stats['total_students'] ?? 0 }}</h3>
                    <p>মোট ছাত্র/ছাত্রী</p>
                </div>
                <div class="icon">
                    <i class="fa fa-users"></i>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 col-6">
            <div class="small-box bg-success">
                <div class="inner">
                    <h3>{{ $stats['present_today'] ?? 0 }}</h3>
                    <p>আজকে উপস্থিত</p>
                </div>
                <div class="icon">
                    <i class="fa fa-check-circle"></i>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 col-6">
            <div class="small-box bg-warning">
                <div class="inner">
                    <h3>{{ $stats['late_today'] ?? 0 }}</h3>
                    <p>আজকে বিলম্বে</p>
                </div>
                <div class="icon">
                    <i class="fa fa-clock"></i>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 col-6">
            <div class="small-box bg-danger">
                <div class="inner">
                    <h3>{{ $stats['absent_today'] ?? 0 }}</h3>
                    <p>আজকে অনুপস্থিত</p>
                </div>
                <div class="icon">
                    <i class="fa fa-user-slash"></i>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 col-6">
            <div class="small-box bg-primary">
                <div class="inner">
                    <h3>{{ $stats['active_cards'] ?? 0 }}</h3>
                    <p>সক্রিয় কার্ড</p>
                </div>
                <div class="icon">
                    <i class="fa fa-id-card"></i>
                </div>
            </div>
        </div>
        
        <div class="col-lg-4 col-6">
            <div class="small-box bg-secondary">
                <div class="inner">
                    <h3>{{ $stats['online_devices'] ?? 0 }}</h3>
                    <p>অনলাইন ডিভাইস</p>
                </div>
                <div class="icon">
                    <i class="fa fa-server"></i>
                </div>
            </div>
        </div>
    </div>

    <!-- Class Wise Attendance -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fa fa-chart-bar"></i> শ্রেণি ভিত্তিক উপস্থিতি ({{ date('d-m-Y') }})</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="bg-secondary text-white">
                                <tr>
                                    <th>শ্রেণি</th>
                                    <th>শাখা</th>
                                    <th>মোট ছাত্র</th>
                                    <th>উপস্থিত</th>
                                    <th>অনুপস্থিত</th>
                                    <th>উপস্থিতির হার</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($classWiseAttendance as $class)
                                @php
                                    $absent = $class->total - $class->present;
                                    $percentage = $class->total > 0 ? round(($class->present / $class->total) * 100, 2) : 0;
                                @endphp
                                <tr>
                                    <td>{{ $class->class_name_bn ?? 'ক্লাস' }}</td>
                                    <td>{{ $class->section ?? '-' }}</td>
                                    <td class="text-center">{{ $class->total }}</td>
                                    <td class="text-center text-success">{{ $class->present }}</td>
                                    <td class="text-center text-danger">{{ $absent }}</td>
                                    <td class="text-center">
                                        <div class="progress" style="height: 20px;">
                                            <div class="progress-bar bg-success" style="width: {{ $percentage }}%">
                                                {{ $percentage }}%
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="6" class="text-center">কোনো তথ্য নেই</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Attendances -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0"><i class="fa fa-history"></i> সাম্প্রতিক হাজিরা</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead class="bg-secondary text-white">
                                <tr>
                                    <th>সময়</th>
                                    <th>নাম</th>
                                    <th>ধরন</th>
                                    <th>কার্ড</th>
                                    <th>স্ট্যাটাস</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($recentAttendances as $attendance)
                                <tr>
                                    <td>{{ $attendance->check_in_time ? $attendance->check_in_time->format('h:i A') : '-' }}</td>
                                    <td>
                                        @if($attendance->student)
                                            {{ $attendance->student->name_bn }}
                                        @elseif($attendance->teacher)
                                            {{ $attendance->teacher->name_bn }}
                                        @else
                                            অজানা
                                        @endif
                                    </td>
                                    <td>
                                        @if($attendance->student)
                                            <span class="badge badge-primary">ছাত্র</span>
                                        @elseif($attendance->teacher)
                                            <span class="badge badge-success">শিক্ষক</span>
                                        @endif
                                    </td>
                                    <td>{{ $attendance->card->card_uid ?? '-' }}</td>
                                    <td>
                                        @if($attendance->status == 'present')
                                            <span class="badge badge-success">উপস্থিত</span>
                                        @elseif($attendance->status == 'late')
                                            <span class="badge badge-warning">বিলম্বে</span>
                                        @elseif($attendance->status == 'absent')
                                            <span class="badge badge-danger">অনুপস্থিত</span>
                                        @else
                                            <span class="badge badge-secondary">{{ $attendance->status }}</span>
                                        @endif
                                    </td>
                                </tr>
                                @empty
                                <tr>
                                    <td colspan="5" class="text-center">আজকে কোনো হাজিরা নেই</td>
                                </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Devices List -->
    @if($devices->count() > 0)
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-warning">
                    <h5 class="mb-0"><i class="fa fa-microchip"></i> হাজিরা ডিভাইস সমূহ</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered table-sm">
                            <thead class="bg-secondary text-white">
                                <tr>
                                    <th>ডিভাইস নাম</th>
                                    <th>টাইপ</th>
                                    <th>লোকেশন</th>
                                    <th>স্ট্যাটাস</th>
                                    <th>সর্বশেষ অনলাইন</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($devices as $device)
                                <tr>
                                    <td>{{ $device->device_name }}</td>
                                    <td>{{ $device->device_type }}</td>
                                    <td>{{ $device->location }}</td>
                                    <td>
                                        @if($device->status == 'online')
                                            <span class="badge badge-success">অনলাইন</span>
                                        @elseif($device->status == 'offline')
                                            <span class="badge badge-danger">অফলাইন</span>
                                        @else
                                            <span class="badge badge-warning">রক্ষণাবেক্ষণ</span>
                                        @endif
                                    </td>
                                    <td>{{ $device->last_online ? $device->last_online->diffForHumans() : 'নেই' }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @endif

    <!-- Quick Actions -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card">
                <div class="card-header bg-success text-white">
                    <h5 class="mb-0"><i class="fa fa-bolt"></i> দ্রুত অ্যাকশন</h5>
                </div>
                <div class="card-body">
                    <a href="{{ route('attendance.cards') }}" class="btn btn-primary">
                        <i class="fa fa-id-card"></i> কার্ড সমূহ
                    </a>
                    <a href="{{ route('attendance.devices') }}" class="btn btn-info">
                        <i class="fa fa-microchip"></i> ডিভাইস সমূহ
                    </a>
                    <a href="{{ route('attendance.manual') }}" class="btn btn-warning">
                        <i class="fa fa-pen"></i> ম্যানুয়াল হাজিরা
                    </a>
                    <a href="{{ route('attendance.report') }}?from_date={{ date('Y-m-01') }}&to_date={{ date('Y-m-d') }}" class="btn btn-secondary">
                        <i class="fa fa-file-alt"></i> রিপোর্ট
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection