<?php
session_start();
require_once '../config/database.php';

// Check if user is logged in and has permission
if (!isset($_SESSION['user_id']) || ($_SESSION['user_role'] != 'admin' && $_SESSION['user_role'] != 'teacher')) {
    header('Location: ../login.php');
    exit();
}

// Get payment information from session
$student_id = $_SESSION['payment_student_id'] ?? null;
$payment_amount = $_SESSION['payment_amount'] ?? 0;
$payment_method = $_SESSION['payment_method'] ?? 'Cash';
$transaction_id = $_SESSION['payment_tran_id'] ?? '';

// Clear session variables
unset($_SESSION['payment_student_id']);
unset($_SESSION['payment_amount']);
unset($_SESSION['payment_method']);
unset($_SESSION['payment_tran_id']);

// Get student information
$student = null;
if ($student_id) {
    $stmt = $pdo->prepare("
        SELECT s.*, c.name as class_name, c.section as class_section 
        FROM students s 
        JOIN classes c ON s.class_id = c.id 
        WHERE s.id = ?
    ");
    $stmt->execute([$student_id]);
    $student = $stmt->fetch();
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ফি পেমেন্ট সফল - স্কুল ম্যানেজমেন্ট সিস্টেম</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { 
            font-family: 'Hind Siliguri', sans-serif; 
            background-color: #f8f9fa; 
        }
        .success-container {
            max-width: 800px;
            margin: 2rem auto;
        }
        .success-card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 0 30px rgba(0,0,0,0.1);
        }
        .success-header {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
            color: white;
            border-radius: 15px 15px 0 0 !important;
            padding: 2rem;
            text-align: center;
        }
        .payment-details {
            background-color: #f8f9fa;
            border-radius: 10px;
            padding: 1.5rem;
            margin: 1.5rem 0;
        }
        .detail-row {
            display: flex;
            justify-content: space-between;
            padding: 0.5rem 0;
            border-bottom: 1px solid #eee;
        }
        .detail-row:last-child {
            border-bottom: none;
        }
        .btn-print {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 25px;
            padding: 0.75rem 2rem;
            font-weight: 600;
        }
        .step-instructions {
            background-color: #e8f4f8;
            border-left: 4px solid #17a2b8;
            padding: 1rem;
            border-radius: 0 8px 8px 0;
            margin-top: 1.5rem;
        }
        .step-number {
            display: inline-block;
            width: 30px;
            height: 30px;
            background: #17a2b8;
            color: white;
            border-radius: 50%;
            text-align: center;
            line-height: 30px;
            font-weight: bold;
            margin-right: 10px;
        }
    </style>
</head>
<body>
    <div class="success-container">
        <div class="card success-card">
            <div class="success-header">
                <i class="fas fa-check-circle fa-3x mb-3"></i>
                <h2>ফি পেমেন্ট সফল!</h2>
                <p class="lead">আপনার পেমেন্ট সফলভাবে সম্পন্ন হয়েছে</p>
            </div>
            
            <div class="card-body p-4">
                <?php if ($student): ?>
                    <div class="payment-details">
                        <h4 class="text-center mb-4"><i class="fas fa-receipt"></i> পেমেন্ট বিবরণ</h4>
                        
                        <div class="detail-row">
                            <strong>শিক্ষার্থীর নাম:</strong>
                            <span><?php echo htmlspecialchars($student['first_name'] . ' ' . $student['last_name']); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>রোল নম্বর:</strong>
                            <span><?php echo htmlspecialchars($student['roll_number']); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>ক্লাস:</strong>
                            <span><?php echo htmlspecialchars($student['class_name'] . ' - ' . $student['class_section']); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>পেমেন্ট পদ্ধতি:</strong>
                            <span><?php echo htmlspecialchars($payment_method); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>ট্রানজেকশন আইডি:</strong>
                            <span><?php echo htmlspecialchars($transaction_id); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>পেমেন্টের পরিমাণ:</strong>
                            <span class="text-success fw-bold">৳<?php echo number_format($payment_amount, 2); ?></span>
                        </div>
                        
                        <div class="detail-row">
                            <strong>পেমেন্টের তারিখ:</strong>
                            <span><?php echo date('d/m/Y H:i:s'); ?></span>
                        </div>
                    </div>
                    
                    <div class="text-center mt-4">
                        <button class="btn btn-print" onclick="window.print()">
                            <i class="fas fa-print"></i> রসিদ প্রিন্ট করুন
                        </button>
                        <a href="fee_payment_dashboard.php" class="btn btn-primary ms-2">
                            <i class="fas fa-arrow-left"></i> ফি ড্যাশবোর্ডে ফিরে যান
                        </a>
                    </div>
                <?php else: ?>
                    <div class="text-center py-5">
                        <i class="fas fa-exclamation-triangle fa-3x text-warning mb-3"></i>
                        <h4>পেমেন্ট তথ্য পাওয়া যায়নি</h4>
                        <p class="text-muted">দয়া করে আবার চেষ্টা করুন</p>
                        <a href="fee_payment_dashboard.php" class="btn btn-primary">
                            <i class="fas fa-arrow-left"></i> ফি ড্যাশবোর্ডে ফিরে যান
                        </a>
                    </div>
                <?php endif; ?>
                
                <!-- Step-by-Step Instructions -->
                <div class="step-instructions mt-4">
                    <h5><i class="fas fa-info-circle"></i> পরবর্তী ধাপসমূহ:</h5>
                    <div class="mt-2">
                        <p><span class="step-number">1</span> রসিদ প্রিন্ট করুন এবং শিক্ষার্থীকে প্রদান করুন</p>
                        <p><span class="step-number">2</span> প্রয়োজনে আরও ফি সংগ্রহ করতে "ফি ড্যাশবোর্ডে ফিরে যান" বাটনে ক্লিক করুন</p>
                        <p><span class="step-number">3</span> রিপোর্ট দেখতে ফি রিপোর্ট মডিউলে যান</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Auto print on page load
        window.addEventListener('load', function() {
            // Uncomment the line below if you want to auto-print
            // window.print();
        });
    </script>
</body>
</html>