<?php
/**
 * Parent Portal - Main Dashboard (Enhanced)
 */

// Enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/../config/database.php';

// Start session if not already started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Check if user is logged in as parent
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'parent') {
    header('Location: ../parent_login.php');
    exit;
}

// Get parent information
$stmt = $pdo->prepare("SELECT * FROM parents WHERE user_id = ?");
$stmt->execute([$_SESSION['user_id']]);
$parent = $stmt->fetch();

if (!$parent) {
    header('Location: ../parent_register.php');
    exit;
}

// Get associated students (simplified query)
try {
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name
        FROM students s
        LEFT JOIN classes c ON s.class_id = c.id
        WHERE s.id IN (
            SELECT student_id FROM parent_students WHERE parent_id = ?
        ) AND s.status = 'active'
    ");
    $stmt->execute([$parent['id']]);
    $students = $stmt->fetchAll();
} catch (Exception $e) {
    $students = [];
}

// Simple statistics calculation
$stats = [];
foreach ($students as $student) {
    $student_id = $student['id'];
    
    // Simple attendance calculation (last 10 records)
    try {
        $stmt = $pdo->prepare("
            SELECT 
                COUNT(*) as total,
                SUM(CASE WHEN status = 'present' THEN 1 ELSE 0 END) as present
            FROM attendance 
            WHERE student_id = ? 
            ORDER BY date DESC 
            LIMIT 10
        ");
        $stmt->execute([$student_id]);
        $attendance = $stmt->fetch();
        $attendance_pct = $attendance['total'] > 0 ? round(($attendance['present'] / $attendance['total']) * 100, 1) : 0;
    } catch (Exception $e) {
        $attendance_pct = 0;
    }
    
    // Simple pending fees calculation
    try {
        $stmt = $pdo->prepare("
            SELECT SUM(amount - paid_amount) as pending 
            FROM fees 
            WHERE student_id = ? AND status IN ('pending', 'overdue')
        ");
        $stmt->execute([$student_id]);
        $pending_fees = $stmt->fetchColumn() ?: 0;
    } catch (Exception $e) {
        $pending_fees = 0;
    }
    
    $stats[$student_id] = [
        'attendance' => $attendance_pct,
        'pending_fees' => $pending_fees
    ];
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>অভিভাবক পোর্টাল - <?= APP_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        .parent-sidebar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: white;
        }
        .parent-sidebar .nav-link {
            color: rgba(255,255,255,0.8);
            padding: 15px 20px;
            border-radius: 8px;
            margin: 5px 0;
            text-decoration: none;
            display: block;
        }
        .parent-sidebar .nav-link:hover, .parent-sidebar .nav-link.active {
            background: rgba(255,255,255,0.2);
            color: white;
        }
        .student-card {
            border-left: 4px solid #667eea;
        }
        .stat-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .welcome-banner {
            background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
            border-left: 4px solid #2196f3;
        }
        .feature-card {
            transition: transform 0.2s;
            border: 1px solid #eee;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 15px rgba(0,0,0,0.1);
        }
    </style>
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Parent Sidebar -->
            <nav class="col-md-2 d-none d-md-block parent-sidebar p-3">
                <div class="text-center mb-4">
                    <div class="bg-white rounded-circle d-inline-flex align-items-center justify-content-center mb-3" style="width:80px;height:80px;">
                        <i class="fas fa-user-tie fa-2x text-primary"></i>
                    </div>
                    <h6 class="mb-0"><?= htmlspecialchars($parent['father_name']) ?></h6>
                    <small>অভিভাবক</small>
                </div>
                
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link active" href="index.php">
                            <i class="fas fa-home me-2"></i> ড্যাশবোর্ড
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="results.php">
                            <i class="fas fa-chart-line me-2"></i> ফলাফল
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="subjects.php">
                            <i class="fas fa-book me-2"></i> বিষয়সমূহ
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="fees.php">
                            <i class="fas fa-money-bill-wave me-2"></i> ফি পেমেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="events.php">
                            <i class="fas fa-calendar-alt me-2"></i> ইভেন্ট
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="attendance.php">
                            <i class="fas fa-clipboard-check me-2"></i> উপস্থিতি
                        </a>
                    </li>
                    <li class="nav-item mt-4">
                        <a class="nav-link text-warning" href="../parent_logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i> লগআউট
                        </a>
                    </li>
                </ul>
            </nav>
            
            <!-- Main Content -->
            <main class="col-md-10 ms-sm-auto px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3">👋 স্বাগতম, <?= htmlspecialchars(explode(' ', $parent['father_name'])[0]) ?>!</h2>
                    <div class="text-muted">
                        <?= date('l, d F Y', strtotime('now')) ?>
                    </div>
                </div>
                
                <!-- Welcome Banner -->
                <div class="alert welcome-banner mb-4" role="alert">
                    <h5 class="alert-heading"><i class="fas fa-info-circle"></i> স্বাগতম অভিভাবক পোর্টালে</h5>
                    <p class="mb-0">
                        আপনার সন্তানের সকল তথ্য এখানে পাবেন। আমাদের সিস্টেমে আপনাকে স্বাগতম!
                    </p>
                </div>
                
                <!-- Quick Feature Cards -->
                <div class="row mb-4">
                    <div class="col-md-4 mb-3">
                        <a href="results.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-chart-line fa-2x text-success mb-2"></i>
                                    <h5>ফলাফল</h5>
                                    <p class="text-muted small">শিক্ষার্থীদের পরীক্ষার ফলাফল দেখুন</p>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="fees.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-money-bill-wave fa-2x text-info mb-2"></i>
                                    <h5>ফি পেমেন্ট</h5>
                                    <p class="text-muted small">বকেয়া ফি পেমেন্ট করুন</p>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="attendance.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-clipboard-check fa-2x text-warning mb-2"></i>
                                    <h5>উপস্থিতি</h5>
                                    <p class="text-muted small">দৈনিক উপস্থিতির হিসাব</p>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                
                <div class="row mb-4">
                    <div class="col-md-4 mb-3">
                        <a href="subjects.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-book fa-2x text-primary mb-2"></i>
                                    <h5>বিষয়সমূহ</h5>
                                    <p class="text-muted small">কোর্স/সাবজেক্ট তথ্য</p>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="events.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-calendar-alt fa-2x text-danger mb-2"></i>
                                    <h5>ইভেন্ট</h5>
                                    <p class="text-muted small">স্কুল ইভেন্ট ও নোটিশ</p>
                                </div>
                            </div>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="../parent_logout.php" class="text-decoration-none">
                            <div class="card feature-card text-center">
                                <div class="card-body">
                                    <i class="fas fa-sign-out-alt fa-2x text-secondary mb-2"></i>
                                    <h5>লগআউট</h5>
                                    <p class="text-muted small">সিস্টেম থেকে বের হন</p>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
                
                <!-- Students Overview -->
                <h4 class="mb-3"><i class="fas fa-child text-primary"></i> আপনার সন্তানদের তথ্য</h4>
                
                <?php if (count($students) > 0): ?>
                <div class="row mb-4">
                    <?php foreach ($students as $student): 
                        $stat = $stats[$student['id']] ?? ['attendance' => 0, 'pending_fees' => 0];
                    ?>
                    <div class="col-md-6 mb-3">
                        <div class="card student-card">
                            <div class="card-body">
                                <div class="d-flex align-items-center mb-3">
                                    <div class="bg-light rounded-circle d-flex align-items-center justify-content-center me-3" style="width:80px;height:80px;">
                                        <i class="fas fa-user-graduate fa-2x text-primary"></i>
                                    </div>
                                    <div>
                                        <h5 class="mb-1"><?= htmlspecialchars($student['first_name'] . ' ' . $student['last_name']) ?></h5>
                                        <p class="mb-0 text-muted">
                                            ক্লাস: <?= htmlspecialchars($student['class_name'] ?? 'N/A') ?> | 
                                            রোল: <?= $student['roll_number'] ?? 'N/A' ?>
                                        </p>
                                    </div>
                                </div>
                                
                                <div class="row">
                                    <div class="col-6 text-center">
                                        <div class="stat-card bg-success text-white p-2">
                                            <h6 class="mb-0"><?= $stat['attendance'] ?>%</h6>
                                            <small>উপস্থিতি</small>
                                        </div>
                                    </div>
                                    <div class="col-6 text-center">
                                        <div class="stat-card <?= $stat['pending_fees'] > 0 ? 'bg-danger' : 'bg-success' ?> text-white p-2">
                                            <h6 class="mb-0">৳<?= number_format($stat['pending_fees'], 0) ?></h6>
                                            <small>বকেয়া</small>
                                        </div>
                                    </div>
                                </div>
                                
                                <div class="mt-3 text-center">
                                    <div class="btn-group" role="group">
                                        <a href="results.php?student_id=<?= $student['id'] ?>" class="btn btn-sm btn-outline-primary">
                                            <i class="fas fa-chart-line"></i> ফলাফল
                                        </a>
                                        <a href="fees.php?student_id=<?= $student['id'] ?>" class="btn btn-sm btn-outline-info">
                                            <i class="fas fa-money-bill-wave"></i> ফি
                                        </a>
                                        <a href="attendance.php?student_id=<?= $student['id'] ?>" class="btn btn-sm btn-outline-warning">
                                            <i class="fas fa-clipboard-check"></i> উপস্থিতি
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                </div>
                <?php else: ?>
                <div class="alert alert-info">
                    <h5><i class="fas fa-info-circle"></i> কোনো সন্তান খুঁজে পাওয়া যায়নি</h5>
                    <p>আপনার সন্তানদের সাথে সম্পর্ক স্থাপন করতে স্কুল অফিসে যোগাযোগ করুন।</p>
                </div>
                <?php endif; ?>
                
                <!-- Quick Information -->
                <h4 class="mb-3"><i class="fas fa-lightbulb text-warning"></i> গুরুত্বপূর্ণ তথ্য</h4>
                <div class="row">
                    <div class="col-md-4 mb-3">
                        <div class="card">
                            <div class="card-body text-center">
                                <i class="fas fa-phone fa-2x text-primary mb-2"></i>
                                <h6>যোগাযোগ</h6>
                                <p class="text-muted small">স্কুলের সাথে যোগাযোগ রাখুন</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <div class="card">
                            <div class="card-body text-center">
                                <i class="fas fa-calendar-alt fa-2x text-success mb-2"></i>
                                <h6>ইভেন্ট</h6>
                                <p class="text-muted small">স্কুল ইভেন্ট ও পরীক্ষার তারিখ</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4 mb-3">
                        <div class="card">
                            <div class="card-body text-center">
                                <i class="fas fa-file-invoice fa-2x text-info mb-2"></i>
                                <h6>ডকুমেন্ট</h6>
                                <p class="text-muted small">প্রয়োজনীয় ডকুমেন্ট ডাউনলোড</p>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>