@extends('layouts.app')

@section('title', 'মিটিং উপস্থিতি রিপোর্ট - আমবাগান যুব কল্যাণ সমিতি')

@section('styles')
<style>
    .stat-card {
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        color: white;
        text-align: center;
    }
    
    .stat-card h3 {
        font-size: 2.5rem;
        margin-bottom: 10px;
    }
    
    .stat-card p {
        margin-bottom: 0;
        opacity: 0.9;
    }
    
    .bg-meetings {
        background: linear-gradient(135deg, #1a5f7a, #2a9d8f);
    }
    
    .bg-present {
        background: linear-gradient(135deg, #2a9d8f, #57cc99);
    }
    
    .bg-absent {
        background: linear-gradient(135deg, #ff6b6b, #ff8e53);
    }
    
    .bg-rate {
        background: linear-gradient(135deg, #57cc99, #ffd166);
    }
    
    .chart-container {
        background: white;
        border-radius: 10px;
        padding: 20px;
        margin-bottom: 20px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    }
    
    .attendance-rate {
        font-size: 1.5rem;
        font-weight: bold;
    }
    
    .rate-good { color: #28a745; }
    .rate-average { color: #ffc107; }
    .rate-poor { color: #dc3545; }
</style>
@endsection

@section('breadcrumbs')
<nav aria-label="breadcrumb">
    <ol class="breadcrumb bg-light">
        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">ড্যাশবোর্ড</a></li>
        <li class="breadcrumb-item"><a href="{{ route('meetings.index') }}">মিটিং</a></li>
        <li class="breadcrumb-item active">উপস্থিতি রিপোর্ট</li>
    </ol>
</nav>
@endsection

@section('content')
<div class="container-fluid">
    <!-- পরিসংখ্যান কার্ড -->
    <div class="row mb-4">
        <div class="col-md-3">
            <div class="stat-card bg-meetings">
                <h3>{{ $overallStats['total_meetings'] }}</h3>
                <p>মোট মিটিং</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card bg-present">
                <h3>{{ $overallStats['total_present'] }}</h3>
                <p>মোট উপস্থিতি</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card bg-absent">
                <h3>{{ $overallStats['total_absent'] }}</h3>
                <p>মোট অনুপস্থিতি</p>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card bg-rate">
                <h3>{{ $overallStats['avg_attendance_rate'] }}%</h3>
                <p>গড় উপস্থিতি হার</p>
            </div>
        </div>
    </div>
    
    <!-- ফিল্টার ফর্ম -->
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0"><i class="fas fa-filter me-2"></i>ফিল্টার করুন</h5>
        </div>
        <div class="card-body">
            <form method="GET" action="{{ route('meetings.attendance-report') }}">
                <div class="row">
                    <div class="col-md-3 mb-3">
                        <label for="meeting_type" class="form-label">মিটিং টাইপ</label>
                        <select name="meeting_type" id="meeting_type" class="form-select">
                            <option value="">সকল টাইপ</option>
                            @foreach($meetingTypes as $type)
                                <option value="{{ $type }}" {{ request('meeting_type') == $type ? 'selected' : '' }}>
                                    {{ $type }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    
                    <div class="col-md-3 mb-3">
                        <label for="from_date" class="form-label">থেকে তারিখ</label>
                        <input type="date" name="from_date" id="from_date" class="form-control" value="{{ request('from_date') }}">
                    </div>
                    
                    <div class="col-md-3 mb-3">
                        <label for="to_date" class="form-label">পর্যন্ত তারিখ</label>
                        <input type="date" name="to_date" id="to_date" class="form-control" value="{{ request('to_date') }}">
                    </div>
                    
                    <div class="col-md-3 mb-3 d-flex align-items-end">
                        <div class="d-grid gap-2 w-100">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search me-2"></i>খুঁজুন
                            </button>
                            <a href="{{ route('meetings.attendance-report') }}" class="btn btn-secondary">
                                <i class="fas fa-redo me-2"></i>রিসেট
                            </a>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
    
    <!-- মাসিক পরিসংখ্যান -->
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="chart-container">
                <h5 class="mb-3"><i class="fas fa-chart-line me-2"></i>মাসিক উপস্থিতি পরিসংখ্যান</h5>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>মাস-বছর</th>
                                <th>মোট মিটিং</th>
                                <th>সম্পন্ন মিটিং</th>
                                <th>গড় উপস্থিতি</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($monthlyStats as $stat)
                            @php
                                $formattedMonth = \Carbon\Carbon::createFromFormat('Y-m', $stat->month)->format('F Y');
                            @endphp
                            <tr>
                                <td>{{ $formattedMonth }}</td>
                                <td>{{ $stat->total_meetings }}</td>
                                <td>{{ $stat->completed_meetings }}</td>
                                <td>
                                    @php
                                        // গড় উপস্থিতি ক্যালকুলেশন
                                        $attendanceRate = 0;
                                        if ($stat->total_meetings > 0) {
                                            $attendanceRate = round(($stat->completed_meetings / $stat->total_meetings) * 100, 2);
                                        }
                                        
                                        $rateClass = 'rate-good';
                                        if ($attendanceRate < 60) {
                                            $rateClass = 'rate-poor';
                                        } elseif ($attendanceRate < 80) {
                                            $rateClass = 'rate-average';
                                        }
                                    @endphp
                                    <span class="attendance-rate {{ $rateClass }}">{{ $attendanceRate }}%</span>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <!-- সদস্যদের উপস্থিতি র্যাংকিং -->
    <div class="row mb-4">
        <div class="col-md-6">
            <div class="chart-container">
                <h5 class="mb-3"><i class="fas fa-trophy me-2"></i>শীর্ষ উপস্থিতি (প্রথম ১০ জন)</h5>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>সদস্য</th>
                                <th>উপস্থিতি</th>
                                <th>উপস্থিতি হার</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($memberAttendance->take(10) as $index => $attendance)
                            @php
                                $attendanceRate = 0;
                                if ($attendance->total_meetings > 0) {
                                    $attendanceRate = round(($attendance->present_count / $attendance->total_meetings) * 100, 2);
                                }
                            @endphp
                            <tr>
                                <td>{{ $index + 1 }}</td>
                                <td>
                                    @if($attendance->member)
                                        {{ $attendance->member->name }}<br>
                                        <small class="text-muted">{{ $attendance->member->member_id }}</small>
                                    @else
                                        <span class="text-muted">সদস্য নেই</span>
                                    @endif
                                </td>
                                <td>{{ $attendance->present_count }}/{{ $attendance->total_meetings }}</td>
                                <td>
                                    <span class="attendance-rate {{ $attendanceRate >= 80 ? 'rate-good' : ($attendanceRate >= 60 ? 'rate-average' : 'rate-poor') }}">
                                        {{ $attendanceRate }}%
                                    </span>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <div class="col-md-6">
            <div class="chart-container">
                <h5 class="mb-3"><i class="fas fa-exclamation-triangle me-2"></i>সর্বনিম্ন উপস্থিতি</h5>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>সদস্য</th>
                                <th>অনুপস্থিতি</th>
                                <th>উপস্থিতি হার</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($memberAttendance->sortBy('present_count')->take(10) as $index => $attendance)
                            @php
                                $attendanceRate = 0;
                                if ($attendance->total_meetings > 0) {
                                    $attendanceRate = round(($attendance->present_count / $attendance->total_meetings) * 100, 2);
                                }
                            @endphp
                            <tr>
                                <td>{{ $index + 1 }}</td>
                                <td>
                                    @if($attendance->member)
                                        {{ $attendance->member->name }}<br>
                                        <small class="text-muted">{{ $attendance->member->member_id }}</small>
                                    @else
                                        <span class="text-muted">সদস্য নেই</span>
                                    @endif
                                </td>
                                <td>{{ $attendance->absent_count }}/{{ $attendance->total_meetings }}</td>
                                <td>
                                    <span class="attendance-rate {{ $attendanceRate >= 80 ? 'rate-good' : ($attendanceRate >= 60 ? 'rate-average' : 'rate-poor') }}">
                                        {{ $attendanceRate }}%
                                    </span>
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <!-- বিস্তারিত রিপোর্ট -->
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">
                        <i class="fas fa-list me-2"></i>মিটিং অনুযায়ী উপস্থিতি
                    </h5>
                    <div class="text-muted">
                        মোট {{ $meetings->total() }} রেকর্ড
                    </div>
                </div>
                <div class="card-body">
                    @if($meetings->isEmpty())
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>কোনো মিটিং রেকর্ড পাওয়া যায়নি।
                    </div>
                    @else
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>তারিখ</th>
                                    <th>মিটিং টাইপ</th>
                                    <th>বিষয়</th>
                                    <th>মোট সদস্য</th>
                                    <th>উপস্থিত</th>
                                    <th>অনুপস্থিত</th>
                                    <th>উপস্থিতি হার</th>
                                    <th>কার্যকলাপ</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($meetings as $meeting)
                                @php
                                    $attendanceRate = 0;
                                    if ($meeting->total_members > 0) {
                                        $attendanceRate = round(($meeting->present_count / $meeting->total_members) * 100, 2);
                                    }
                                    
                                    $rateClass = 'rate-good';
                                    if ($attendanceRate < 60) {
                                        $rateClass = 'rate-poor';
                                    } elseif ($attendanceRate < 80) {
                                        $rateClass = 'rate-average';
                                    }
                                @endphp
                                <tr>
                                    <td>{{ \Carbon\Carbon::parse($meeting->date)->format('d/m/Y') }}</td>
                                    <td>{{ $meeting->meeting_type }}</td>
                                    <td>{{ Str::limit($meeting->title, 50) }}</td>
                                    <td>{{ $meeting->total_members }}</td>
                                    <td>{{ $meeting->present_count }}</td>
                                    <td>{{ $meeting->absent_count }}</td>
                                    <td>
                                        <span class="attendance-rate {{ $rateClass }}">{{ $attendanceRate }}%</span>
                                    </td>
                                    <td>
                                        <a href="{{ route('meetings.show', $meeting) }}" class="btn btn-sm btn-info" title="দেখুন">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <a href="{{ route('meetings.print-attendance', $meeting) }}" class="btn btn-sm btn-secondary" title="প্রিন্ট">
                                            <i class="fas fa-print"></i>
                                        </a>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    
                    <!-- পেজিনেশন -->
                    <div class="d-flex justify-content-center mt-3">
                        {{ $meetings->withQueryString()->links() }}
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
    
    <!-- প্রিন্ট ও এক্সপোর্ট বাটন -->
    <div class="row mt-4">
        <div class="col-md-12 text-center">
            <button class="btn btn-primary" onclick="window.print()">
                <i class="fas fa-print me-2"></i>প্রিন্ট রিপোর্ট
            </button>
            <button class="btn btn-success" onclick="exportToExcel()">
                <i class="fas fa-file-excel me-2"></i>এক্সেল রপ্তানি
            </button>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
function exportToExcel() {
    const table = document.querySelector('table');
    const html = table.outerHTML;
    const url = 'data:application/vnd.ms-excel,' + escape(html);
    
    const link = document.createElement('a');
    link.href = url;
    link.download = 'attendance-report-' + new Date().toISOString().split('T')[0] + '.xls';
    link.click();
}

// তারিখ ফিল্টার ভ্যালিডেশন
$('#from_date, #to_date').change(function() {
    const fromDate = $('#from_date').val();
    const toDate = $('#to_date').val();
    
    if (fromDate && toDate && fromDate > toDate) {
        alert('শুরুর তারিখ শেষ তারিখের আগে হতে হবে।');
        $(this).val('');
    }
});
</script>
@endsection