<?php
/**
 * Hostel Fees Management
 * Manage hostel fees and payments for students
 */

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

// Check authentication - redirect to login if not logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: ../../login.php');
    exit;
}

// Check admin role - support both new and legacy session variables
$user_role = $_SESSION['user_role'] ?? $_SESSION['role'] ?? '';
if (!isset($_SESSION['user_id']) || ($user_role !== 'admin')) {
    http_response_code(403);
    die('❌ অ্যাক্সেস denied। আপনার এই পেজ দেখার অনুমতি নেই।<br><br>
    <a href="../../login.php" class="btn btn-primary">লগইন পেজে যান</a>');
}

$success_message = '';
$error_message = '';

// Handle POST requests
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    SecurityHelper::validatePost();
    
    try {
        if (isset($_POST['add_fee'])) {
            $stmt = $pdo->prepare("
                INSERT INTO hostel_fees (allocation_id, fee_type, amount, due_date, academic_year, month, description) 
                VALUES (?, ?, ?, ?, ?, ?, ?)
            ");
            $stmt->execute([
                intval($_POST['allocation_id']),
                sanitize($_POST['fee_type']),
                floatval($_POST['amount']),
                $_POST['due_date'],
                sanitize($_POST['academic_year']),
                sanitize($_POST['month']),
                sanitize($_POST['description'])
            ]);
            $success_message = '✅ ফি সফলভাবে যোগ করা হয়েছে!';
        }
        
        if (isset($_POST['record_payment'])) {
            $stmt = $pdo->prepare("
                INSERT INTO hostel_fee_payments (fee_id, amount, payment_method, transaction_id, payment_date, received_by, remarks) 
                VALUES (?, ?, ?, ?, ?, ?, ?)
            ");
            $stmt->execute([
                intval($_POST['fee_id']),
                floatval($_POST['amount']),
                sanitize($_POST['payment_method']),
                sanitize($_POST['transaction_id']),
                $_POST['payment_date'],
                intval($_SESSION['user_id']),
                sanitize($_POST['remarks'])
            ]);
            
            // Update fee status
            $fee_id = intval($_POST['fee_id']);
            $pdo->prepare("
                UPDATE hostel_fees hf
                SET paid_amount = (SELECT COALESCE(SUM(amount), 0) FROM hostel_fee_payments WHERE fee_id = ?),
                    status = CASE 
                        WHEN (SELECT COALESCE(SUM(amount), 0) FROM hostel_fee_payments WHERE fee_id = ?) >= amount THEN 'paid'
                        WHEN (SELECT COALESCE(SUM(amount), 0) FROM hostel_fee_payments WHERE fee_id = ?) > 0 THEN 'partial'
                        ELSE 'pending'
                    END
                WHERE id = ?
            ")->execute([$fee_id, $fee_id, $fee_id, $fee_id]);
            
            $success_message = '✅ পেমেন্ট সফলভাবে রেকর্ড করা হয়েছে!';
        }
        
        if (isset($_POST['edit_fee'])) {
            $stmt = $pdo->prepare("
                UPDATE hostel_fees 
                SET fee_type = ?, amount = ?, due_date = ?, academic_year = ?, month = ?, description = ?
                WHERE id = ?
            ");
            $stmt->execute([
                sanitize($_POST['fee_type']),
                floatval($_POST['amount']),
                $_POST['due_date'],
                sanitize($_POST['academic_year']),
                sanitize($_POST['month']),
                sanitize($_POST['description']),
                intval($_POST['fee_id'])
            ]);
            $success_message = '✅ ফি সফলভাবে আপডেট করা হয়েছে!';
        }
        
        if (isset($_POST['delete_fee'])) {
            // Check if there are payments
            $check = $pdo->prepare("SELECT COUNT(*) FROM hostel_fee_payments WHERE fee_id = ?");
            $check->execute([intval($_POST['fee_id'])]);
            if ($check->fetchColumn() > 0) {
                $error_message = '❌ এই ফিতে পেমেন্ট রেকর্ড রয়েছে। প্রথমে পেমেন্ট মুছে ফেলুন।';
            } else {
                $stmt = $pdo->prepare("DELETE FROM hostel_fees WHERE id = ?");
                $stmt->execute([intval($_POST['fee_id'])]);
                $success_message = '✅ ফি সফলভাবে মুছে ফেলা হয়েছে!';
            }
        }
    } catch(Exception $e) {
        $error_message = '❌ ত্রুটি: ' . $e->getMessage();
    }
}

// Get active allocations for dropdown
$allocations = $pdo->query("
    SELECT a.*, s.student_id as student_code, s.first_name, s.last_name,
           r.room_number, b.building_name
    FROM hostel_allocations a 
    JOIN students s ON a.student_id = s.id 
    JOIN hostel_rooms r ON a.room_id = r.id 
    JOIN hostel_buildings b ON r.building_id = b.id 
    WHERE a.status='active'
    ORDER BY b.building_name, r.room_number, s.first_name
")->fetchAll();

// Get all hostel fees with student info
$fees = $pdo->query("
    SELECT hf.*, a.student_id, s.first_name, s.last_name, s.student_id as student_code,
           r.room_number, b.building_name,
           (SELECT COALESCE(SUM(amount), 0) FROM hostel_fee_payments WHERE fee_id = hf.id) as paid_amount
    FROM hostel_fees hf
    JOIN hostel_allocations a ON hf.allocation_id = a.id
    JOIN students s ON a.student_id = s.id
    JOIN hostel_rooms r ON a.room_id = r.id
    JOIN hostel_buildings b ON r.building_id = b.id
    ORDER BY hf.due_date DESC, b.building_name, s.first_name
")->fetchAll();

// Get fee payments
$payments = $pdo->query("
    SELECT hfp.*, hf.fee_type, hf.amount as total_amount,
           s.first_name, s.last_name, s.student_id as student_code,
           r.room_number, b.building_name,
           u.username as received_by_name
    FROM hostel_fee_payments hfp
    JOIN hostel_fees hf ON hfp.fee_id = hf.id
    JOIN hostel_allocations a ON hf.allocation_id = a.id
    JOIN students s ON a.student_id = s.id
    JOIN hostel_rooms r ON a.room_id = r.id
    JOIN hostel_buildings b ON r.building_id = b.id
    LEFT JOIN users u ON hfp.received_by = u.id
    ORDER BY hfp.payment_date DESC
    LIMIT 100
")->fetchAll();

// Calculate statistics
$total_fees = array_sum(array_column($fees, 'amount'));
$total_paid = array_sum(array_column($fees, 'paid_amount'));
$total_pending = $total_fees - $total_paid;
$pending_count = count(array_filter($fees, fn($f) => $f['status'] == 'pending'));
$paid_count = count(array_filter($fees, fn($f) => $f['status'] == 'paid'));
$partial_count = count(array_filter($fees, fn($f) => $f['status'] == 'partial'));

// Get current academic year
$current_year = date('Y');
$academic_year = ($current_year - 1) . '-' . $current_year;
?>
<!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">
    <link href="../../assets/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">💰 হোস্টেল ফি ব্যবস্থাপনা</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <a href="../../index.php" class="btn btn-outline-primary btn-sm me-2">
                            <i class="fas fa-home"></i> হোম পেজ
                        </a>
                        <a href="index.php" class="btn btn-outline-secondary btn-sm me-2">
                            <i class="fas fa-arrow-left"></i> হোস্টেল হোম
                        </a>
                        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addFeeModal">
                            <i class="fas fa-plus"></i> নতুন ফি
                        </button>
                    </div>
                </div>
                
                <?php if ($success_message): ?>
                    <div class="alert alert-success"><?= $success_message ?></div>
                <?php endif; ?>
                <?php if ($error_message): ?>
                    <div class="alert alert-danger"><?= $error_message ?></div>
                <?php endif; ?>
                
                <!-- Statistics Cards -->
                <div class="row mb-4">
                    <div class="col-md-3">
                        <div class="card bg-primary text-white">
                            <div class="card-body text-center">
                                <h6>মোট ফি</h6>
                                <h3>৳<?= number_format($total_fees, 2) ?></h3>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card bg-success text-white">
                            <div class="card-body text-center">
                                <h6>পরিশোধিত</h6>
                                <h3>৳<?= number_format($total_paid, 2) ?></h3>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card bg-warning text-dark">
                            <div class="card-body text-center">
                                <h6>বাকি</h6>
                                <h3>৳<?= number_format($total_pending, 2) ?></h3>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card bg-info text-white">
                            <div class="card-body text-center">
                                <h6>ফি সংখ্যা</h6>
                                <h3><?= count($fees) ?></h3>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Status Summary -->
                <div class="row mb-4">
                    <div class="col-md-4">
                        <div class="card">
                            <div class="card-body text-center">
                                <span class="badge bg-danger fs-6">মুলতুবি: <?= $pending_count ?></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card">
                            <div class="card-body text-center">
                                <span class="badge bg-warning fs-6">আংশিক: <?= $partial_count ?></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card">
                            <div class="card-body text-center">
                                <span class="badge bg-success fs-6">পরিশোধিত: <?= $paid_count ?></span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Fees Table -->
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0">📋 ফি তালিকা</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>ছাত্রের নাম</th>
                                        <th>ছাত্র আইডি</th>
                                        <th>ভবন - রুম</th>
                                        <th>ফির ধরন</th>
                                        <th>মোট ফি</th>
                                        <th>পরিশোধিত</th>
                                        <th>বাকি</th>
                                        <th>নির্ধারিত তারিখ</th>
                                        <th>অবস্থা</th>
                                        <th>অ্যাকশন</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($fees as $index => $fee): 
                                        $due = $fee['amount'] - $fee['paid_amount'];
                                    ?>
                                    <tr>
                                        <td><?= $index + 1 ?></td>
                                        <td><?= htmlspecialchars($fee['first_name'] . ' ' . $fee['last_name']) ?></td>
                                        <td><?= htmlspecialchars($fee['student_code']) ?></td>
                                        <td><?= htmlspecialchars($fee['building_name'] . ' - ' . $fee['room_number']) ?></td>
                                        <td><?= htmlspecialchars($fee['fee_type']) ?></td>
                                        <td>৳<?= number_format($fee['amount'], 2) ?></td>
                                        <td>৳<?= number_format($fee['paid_amount'], 2) ?></td>
                                        <td class="<?= $due > 0 ? 'text-danger fw-bold' : '' ?>">৳<?= number_format($due, 2) ?></td>
                                        <td><?= date('d M Y', strtotime($fee['due_date'])) ?></td>
                                        <td>
                                            <span class="badge bg-<?= 
                                                $fee['status'] == 'paid' ? 'success' : 
                                                ($fee['status'] == 'partial' ? 'warning' : 'danger') 
                                            ?>">
                                                <?= 
                                                    $fee['status'] == 'paid' ? 'পরিশোধিত' : 
                                                    ($fee['status'] == 'partial' ? 'আংশিক' : 'মুলতুবি') 
                                                ?>
                                            </span>
                                        </td>
                                        <td>
                                            <?php if ($fee['status'] != 'paid'): ?>
                                            <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#paymentModal<?= $fee['id'] ?>">
                                                <i class="fas fa-money-bill"></i> পেমেন্ট
                                            </button>
                                            <?php endif; ?>
                                            <button class="btn btn-sm btn-info" data-bs-toggle="modal" data-bs-target="#editFeeModal<?= $fee['id'] ?>">
                                                <i class="fas fa-edit"></i>
                                            </button>
                                            <form method="POST" class="d-inline" onsubmit="return confirm('আপনি কি নিশ্চিতভাবে এই ফি মুছে ফেলতে চান?');">
                                                <?= csrf_field() ?>
                                                <input type="hidden" name="fee_id" value="<?= $fee['id'] ?>">
                                                <button type="submit" name="delete_fee" class="btn btn-sm btn-danger">
                                                    <i class="fas fa-trash"></i>
                                                </button>
                                            </form>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                
                <!-- Recent Payments -->
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0">💳 সাম্প্রতিক পেমেন্ট (শেষ ১০০)</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>ছাত্রের নাম</th>
                                        <th>ছাত্র আইডি</th>
                                        <th>ফির ধরন</th>
                                        <th>পরিশোধিত পরিমাণ</th>
                                        <th>পেমেন্ট পদ্ধতি</th>
                                        <th>ট্রানজেকশন আইডি</th>
                                        <th>পেমেন্ট তারিখ</th>
                                        <th>গ্রহণকারী</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($payments as $index => $payment): ?>
                                    <tr>
                                        <td><?= $index + 1 ?></td>
                                        <td><?= htmlspecialchars($payment['first_name'] . ' ' . $payment['last_name']) ?></td>
                                        <td><?= htmlspecialchars($payment['student_code']) ?></td>
                                        <td><?= htmlspecialchars($payment['fee_type']) ?></td>
                                        <td>৳<?= number_format($payment['amount'], 2) ?></td>
                                        <td>
                                            <?php
                                            $method_labels = [
                                                'cash' => 'নগদ',
                                                'bank' => 'ব্যাংক',
                                                'online' => 'অনলাইন',
                                                'cheque' => 'চেক'
                                            ];
                                            echo $method_labels[$payment['payment_method']] ?? $payment['payment_method'];
                                            ?>
                                        </td>
                                        <td><?= htmlspecialchars($payment['transaction_id']) ?: '-' ?></td>
                                        <td><?= date('d M Y', strtotime($payment['payment_date'])) ?></td>
                                        <td><?= htmlspecialchars($payment['received_by_name']) ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <!-- Add Fee Modal -->
    <div class="modal fade" id="addFeeModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">নতুন ফি যোগ করুন</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <?= csrf_field() ?>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">ছাত্র নির্বাচন <span class="text-danger">*</span></label>
                            <select name="allocation_id" class="form-select" required>
                                <option value="">-- ছাত্র নির্বাচন --</option>
                                <?php foreach ($allocations as $a): ?>
                                <option value="<?= $a['id'] ?>">
                                    <?= htmlspecialchars($a['first_name'] . ' ' . $a['last_name'] . ' (' . $a['student_code'] . ') - ' . $a['building_name'] . ' রুম ' . $a['room_number']) ?>
                                </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">ফির ধরন <span class="text-danger">*</span></label>
                            <select name="fee_type" class="form-select" required>
                                <option value="monthly_rent">মাসিক ভাড়া</option>
                                <option value="security_deposit">সিকিউরিটি ডিপোজিট</option>
                                <option value="admission_fee">ভর্তি ফি</option>
                                <option value="maintenance">রক্ষণাবেক্ষণ ফি</option>
                                <option value="other">অন্যান্য</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">পরিমাণ (৳) <span class="text-danger">*</span></label>
                            <input type="number" name="amount" class="form-control" min="0" step="0.01" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">নির্ধারিত তারিখ <span class="text-danger">*</span></label>
                            <input type="date" name="due_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">শিক্ষাবর্ষ</label>
                            <input type="text" name="academic_year" class="form-control" value="<?= $academic_year ?>">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">মাস</label>
                            <select name="month" class="form-select">
                                <option value="">-- মাস নির্বাচন --</option>
                                <option value="January">জানুয়ারি</option>
                                <option value="February">ফেব্রুয়ারি</option>
                                <option value="March">মার্চ</option>
                                <option value="April">এপ্রিল</option>
                                <option value="May">মে</option>
                                <option value="June">জুন</option>
                                <option value="July">জুলাই</option>
                                <option value="August">আগস্ট</option>
                                <option value="September">সেপ্টেম্বর</option>
                                <option value="October">অক্টোবর</option>
                                <option value="November">নভেম্বর</option>
                                <option value="December">ডিসেম্বর</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">বিবরণ</label>
                            <textarea name="description" class="form-control" rows="2"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বন্ধ</button>
                        <button type="submit" name="add_fee" class="btn btn-primary">সংরক্ষণ</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
    <!-- Payment Modals -->
    <?php foreach ($fees as $fee): ?>
    <?php if ($fee['status'] != 'paid'): ?>
    <div class="modal fade" id="paymentModal<?= $fee['id'] ?>" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">পেমেন্ট রেকর্ড করুন</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <?= csrf_field() ?>
                    <input type="hidden" name="fee_id" value="<?= $fee['id'] ?>">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">ছাত্র</label>
                            <input type="text" class="form-control" value="<?= htmlspecialchars($fee['first_name'] . ' ' . $fee['last_name']) ?>" disabled>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">ফির ধরন</label>
                            <input type="text" class="form-control" value="<?= htmlspecialchars($fee['fee_type']) ?>" disabled>
                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">মোট ফি</label>
                                    <input type="text" class="form-control" value="৳<?= number_format($fee['amount'], 2) ?>" disabled>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label class="form-label">পূর্বে পরিশোধিত</label>
                                    <input type="text" class="form-control" value="৳<?= number_format($fee['paid_amount'], 2) ?>" disabled>
                                </div>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">বাকি পরিমাণ</label>
                            <input type="text" class="form-control fw-bold text-danger" value="৳<?= number_format($fee['amount'] - $fee['paid_amount'], 2) ?>" disabled>
                        </div>
                        <hr>
                        <div class="mb-3">
                            <label class="form-label">পেমেন্ট পরিমাণ (৳) <span class="text-danger">*</span></label>
                            <input type="number" name="amount" class="form-control" value="<?= $fee['amount'] - $fee['paid_amount'] ?>" min="1" max="<?= $fee['amount'] - $fee['paid_amount'] ?>" step="0.01" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">পেমেন্ট পদ্ধতি <span class="text-danger">*</span></label>
                            <select name="payment_method" class="form-select" required>
                                <option value="cash">নগদ</option>
                                <option value="bank">ব্যাংক</option>
                                <option value="online">অনলাইন</option>
                                <option value="cheque">চেক</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">ট্রানজেকশন আইডি</label>
                            <input type="text" name="transaction_id" class="form-control">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">পেমেন্ট তারিখ <span class="text-danger">*</span></label>
                            <input type="date" name="payment_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">মন্তব্য</label>
                            <textarea name="remarks" class="form-control" rows="2"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বন্ধ</button>
                        <button type="submit" name="record_payment" class="btn btn-success">পেমেন্ট রেকর্ড করুন</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
    
    <!-- Edit Fee Modal -->
    <div class="modal fade" id="editFeeModal<?= $fee['id'] ?>" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">ফি সম্পাদনা</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <form method="POST">
                    <?= csrf_field() ?>
                    <input type="hidden" name="fee_id" value="<?= $fee['id'] ?>">
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label">ফির ধরন <span class="text-danger">*</span></label>
                            <select name="fee_type" class="form-select" required>
                                <option value="monthly_rent" <?= $fee['fee_type'] == 'monthly_rent' ? 'selected' : '' ?>>মাসিক ভাড়া</option>
                                <option value="security_deposit" <?= $fee['fee_type'] == 'security_deposit' ? 'selected' : '' ?>>সিকিউরিটি ডিপোজিট</option>
                                <option value="admission_fee" <?= $fee['fee_type'] == 'admission_fee' ? 'selected' : '' ?>>ভর্তি ফি</option>
                                <option value="maintenance" <?= $fee['fee_type'] == 'maintenance' ? 'selected' : '' ?>>রক্ষণাবেক্ষণ ফি</option>
                                <option value="other" <?= $fee['fee_type'] == 'other' ? 'selected' : '' ?>>অন্যান্য</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">পরিমাণ (৳) <span class="text-danger">*</span></label>
                            <input type="number" name="amount" class="form-control" value="<?= $fee['amount'] ?>" min="0" step="0.01" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">নির্ধারিত তারিখ <span class="text-danger">*</span></label>
                            <input type="date" name="due_date" class="form-control" value="<?= $fee['due_date'] ?>" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">শিক্ষাবর্ষ</label>
                            <input type="text" name="academic_year" class="form-control" value="<?= htmlspecialchars($fee['academic_year']) ?>">
                        </div>
                        <div class="mb-3">
                            <label class="form-label">মাস</label>
                            <select name="month" class="form-select">
                                <option value="">-- মাস নির্বাচন --</option>
                                <option value="January" <?= $fee['month'] == 'January' ? 'selected' : '' ?>>জানুয়ারি</option>
                                <option value="February" <?= $fee['month'] == 'February' ? 'selected' : '' ?>>ফেব্রুয়ারি</option>
                                <option value="March" <?= $fee['month'] == 'March' ? 'selected' : '' ?>>মার্চ</option>
                                <option value="April" <?= $fee['month'] == 'April' ? 'selected' : '' ?>>এপ্রিল</option>
                                <option value="May" <?= $fee['month'] == 'May' ? 'selected' : '' ?>>মে</option>
                                <option value="June" <?= $fee['month'] == 'June' ? 'selected' : '' ?>>জুন</option>
                                <option value="July" <?= $fee['month'] == 'July' ? 'selected' : '' ?>>জুলাই</option>
                                <option value="August" <?= $fee['month'] == 'August' ? 'selected' : '' ?>>আগস্ট</option>
                                <option value="September" <?= $fee['month'] == 'September' ? 'selected' : '' ?>>সেপ্টেম্বর</option>
                                <option value="October" <?= $fee['month'] == 'October' ? 'selected' : '' ?>>অক্টোবর</option>
                                <option value="November" <?= $fee['month'] == 'November' ? 'selected' : '' ?>>নভেম্বর</option>
                                <option value="December" <?= $fee['month'] == 'December' ? 'selected' : '' ?>>ডিসেম্বর</option>
                            </select>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">বিবরণ</label>
                            <textarea name="description" class="form-control" rows="2"><?= htmlspecialchars($fee['description']) ?></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বন্ধ</button>
                        <button type="submit" name="edit_fee" class="btn btn-primary">আপডেট</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
