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

// Only admins can access this page
if (!isset($_SESSION['user_id']) || $_SESSION['user_role'] !== 'admin') {
    header('Location: ../login.php');
    exit();
}

// Get search parameters
$search_pending = isset($_GET['search_pending']) ? trim($_GET['search_pending']) : '';
$search_verified = isset($_GET['search_verified']) ? trim($_GET['search_verified']) : '';
$search_rejected = isset($_GET['search_rejected']) ? trim($_GET['search_rejected']) : '';

// Handle payment verification
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['verify_payment'])) {
    try {
        // Get payment data from POST
        $transaction_id = (int)($_POST['transaction_id'] ?? 0);
        $action = $_POST['verify_payment']; // 'approve' or 'reject' - get from button value
        $admin_notes = trim($_POST['admin_notes'] ?? '');
        
        if ($transaction_id <= 0) {
            throw new Exception("Invalid transaction data");
        }
        
        // Get transaction details
        $stmt = $pdo->prepare("
            SELECT bt.*, s.first_name, s.last_name, s.student_id as student_code
            FROM bkash_transactions bt
            JOIN students s ON bt.student_id = s.id
            WHERE bt.id = ? AND bt.status = 'pending'
        ");
        $stmt->execute([$transaction_id]);
        $transaction = $stmt->fetch();
        
        if (!$transaction) {
            throw new Exception("Transaction not found or already processed");
        }
        
        // Check if this transaction ID has already been verified for any student
        if ($action === 'approve') {
            $stmt = $pdo->prepare("
                SELECT bt.id, s.first_name, s.last_name, s.student_id as student_code
                FROM bkash_transactions bt
                JOIN students s ON bt.student_id = s.id
                WHERE bt.transaction_id = ? AND bt.status = 'verified'
            ");
            $stmt->execute([$transaction['transaction_id']]);
            $existing_verified = $stmt->fetch();
            
            if ($existing_verified) {
                throw new Exception("This transaction ID has already been verified for student " . 
                    $existing_verified['student_code'] . " (" . 
                    $existing_verified['first_name'] . " " . 
                    $existing_verified['last_name'] . "). " .
                    "The same transaction cannot be accepted multiple times.");
            }
        }
        
        if ($action === 'approve') {
            // Update fee records (distribute payment across due fees)
            $pdo->beginTransaction();
            
            try {
                // Get due fees for this student
                $stmt = $pdo->prepare("
                    SELECT id, amount, paid_amount 
                    FROM fees 
                    WHERE student_id = ? AND status IN ('pending', 'partial', 'overdue') 
                    ORDER BY due_date ASC
                ");
                $stmt->execute([$transaction['student_id']]);
                $due_fees = $stmt->fetchAll();
                
                $remaining_amount = (float)$transaction['amount'];
                $updated_fees = [];
                
                foreach ($due_fees as $fee) {
                    if ($remaining_amount <= 0) break;
                    
                    $due_amount = $fee['amount'] - $fee['paid_amount'];
                    $payment_amount = min($remaining_amount, $due_amount);
                    
                    $new_paid_amount = $fee['paid_amount'] + $payment_amount;
                    $new_status = ($new_paid_amount >= $fee['amount']) ? 'paid' : 'partial';
                    
                    $stmt = $pdo->prepare("
                        UPDATE fees 
                        SET paid_amount = ?, status = ?, paid_date = IF(paid_date IS NULL, CURDATE(), paid_date)
                        WHERE id = ?
                    ");
                    $stmt->execute([$new_paid_amount, $new_status, $fee['id']]);
                    
                    // Insert payment record for this specific fee
                    $ins = $pdo->prepare("
                        INSERT INTO fee_payments (fee_id, amount, payment_date, payment_method, transaction_id, notes, created_by) 
                        VALUES (?, ?, NOW(), ?, ?, ?, ?)
                    ");
                    $ins->execute([
                        $fee['id'], 
                        $payment_amount, 
                        'bkash', 
                        $transaction['transaction_id'], 
                        'bKash payment from ' . $transaction['bkash_number'] . '. Verified by admin. Notes: ' . $admin_notes, 
                        $_SESSION['user_id'] ?? null
                    ]);
                    
                    $remaining_amount -= $payment_amount;
                    $updated_fees[] = [
                        'fee_id' => $fee['id'],
                        'paid_amount' => $payment_amount
                    ];
                }
                
                // Update transaction status
                $stmt = $pdo->prepare("
                    UPDATE bkash_transactions 
                    SET status = 'verified', verified_at = NOW(), verified_by = ?, notes = ?
                    WHERE id = ?
                ");
                $stmt->execute([$_SESSION['user_id'], $admin_notes, $transaction_id]);
                
                $pdo->commit();
                
                $success_message = "Payment verified and applied successfully!";
            } catch (Exception $e) {
                $pdo->rollBack();
                throw new Exception("Failed to update fee records: " . $e->getMessage());
            }
        } else if ($action === 'reject') {
            // Reject the transaction
            $stmt = $pdo->prepare("
                UPDATE bkash_transactions 
                SET status = 'rejected', verified_at = NOW(), verified_by = ?, notes = ?
                WHERE id = ?
            ");
            $stmt->execute([$_SESSION['user_id'], $admin_notes, $transaction_id]);
            
            $success_message = "Payment rejected successfully!";
        }
        
    } catch (Exception $e) {
        $error_message = $e->getMessage();
    }
}

// Handle transaction editing
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_transaction'])) {
    try {
        $transaction_id = (int)($_POST['transaction_id'] ?? 0);
        $transaction_id_value = trim($_POST['edit_transaction_id'] ?? '');
        $bkash_number = trim($_POST['edit_bkash_number'] ?? '');
        $amount = (float)($_POST['edit_amount'] ?? 0);
        $status = trim($_POST['edit_status'] ?? 'pending');
        
        if ($transaction_id <= 0 || empty($transaction_id_value) || empty($bkash_number) || $amount <= 0) {
            throw new Exception("All fields are required");
        }
        
        // Validate bKash number format
        if (!preg_match('/^(01[3-9]\d{8}|8801[3-9]\d{8})$/', $bkash_number)) {
            throw new Exception("Please enter a valid Bangladeshi mobile number for bKash");
        }
        
        // Format bKash number
        if (strpos($bkash_number, '880') === 0) {
            $bkash_number = '0' . substr($bkash_number, 3);
        }
        
        // Validate status
        if (!in_array($status, ['pending', 'verified', 'rejected'])) {
            throw new Exception("Invalid status");
        }
        
        // If changing to verified, check if this transaction ID has already been verified
        if ($status === 'verified') {
            // Get the current transaction to get its transaction_id
            $stmt = $pdo->prepare("SELECT transaction_id FROM bkash_transactions WHERE id = ?");
            $stmt->execute([$transaction_id]);
            $current_transaction = $stmt->fetch();
            
            if ($current_transaction) {
                $stmt = $pdo->prepare("
                    SELECT bt.id, s.first_name, s.last_name, s.student_id as student_code
                    FROM bkash_transactions bt
                    JOIN students s ON bt.student_id = s.id
                    WHERE bt.transaction_id = ? AND bt.status = 'verified' AND bt.id != ?
                ");
                $stmt->execute([$current_transaction['transaction_id'], $transaction_id]);
                $existing_verified = $stmt->fetch();
                
                if ($existing_verified) {
                    throw new Exception("This transaction ID has already been verified for student " . 
                        $existing_verified['student_code'] . " (" . 
                        $existing_verified['first_name'] . " " . 
                        $existing_verified['last_name'] . "). " .
                        "The same transaction cannot be accepted multiple times.");
                }
            }
        }
        
        // Update transaction
        $stmt = $pdo->prepare("
            UPDATE bkash_transactions 
            SET transaction_id = ?, bkash_number = ?, amount = ?, status = ?
            WHERE id = ?
        ");
        $stmt->execute([$transaction_id_value, $bkash_number, $amount, $status, $transaction_id]);
        
        $success_message = "Transaction updated successfully!";
    } catch (Exception $e) {
        $error_message = $e->getMessage();
    }
}

// Get all bKash transactions (pending, verified, and rejected)
$stmt = $pdo->prepare("
    SELECT 
        bt.id as transaction_id,
        bt.amount,
        bt.transaction_id as bkash_transaction_id,
        bt.bkash_number,
        bt.submitted_at,
        bt.verified_at,
        bt.status,
        bt.student_id,
        bt.notes,
        s.student_id as student_code,
        s.first_name,
        s.last_name,
        s.roll_number,
        c.name as class_name,
        c.section,
        u.username as verified_by_name
    FROM bkash_transactions bt
    JOIN students s ON bt.student_id = s.id
    JOIN classes c ON s.class_id = c.id
    LEFT JOIN users u ON bt.verified_by = u.id
    ORDER BY bt.submitted_at DESC
");
$stmt->execute();
$all_transactions = $stmt->fetchAll();

// Group transactions by status and apply search filters
$pending_transactions = [];
$verified_transactions = [];
$rejected_transactions = [];

foreach ($all_transactions as $transaction) {
    switch ($transaction['status']) {
        case 'pending':
            // Apply search filter for pending transactions
            if (empty($search_pending) || 
                stripos($transaction['bkash_transaction_id'], $search_pending) !== false || 
                stripos($transaction['bkash_number'], $search_pending) !== false ||
                stripos($transaction['student_code'], $search_pending) !== false ||
                stripos($transaction['first_name'] . ' ' . $transaction['last_name'], $search_pending) !== false ||
                stripos((string)$transaction['amount'], $search_pending) !== false) {
                $pending_transactions[] = $transaction;
            }
            break;
        case 'verified':
            // Apply search filter for verified transactions
            if (empty($search_verified) || 
                stripos($transaction['bkash_transaction_id'], $search_verified) !== false || 
                stripos($transaction['bkash_number'], $search_verified) !== false ||
                stripos($transaction['student_code'], $search_verified) !== false ||
                stripos($transaction['first_name'] . ' ' . $transaction['last_name'], $search_verified) !== false ||
                stripos((string)$transaction['amount'], $search_verified) !== false) {
                $verified_transactions[] = $transaction;
            }
            break;
        case 'rejected':
            // Apply search filter for rejected transactions
            if (empty($search_rejected) || 
                stripos($transaction['bkash_transaction_id'], $search_rejected) !== false || 
                stripos($transaction['bkash_number'], $search_rejected) !== false ||
                stripos($transaction['student_code'], $search_rejected) !== false ||
                stripos($transaction['first_name'] . ' ' . $transaction['last_name'], $search_rejected) !== false ||
                stripos((string)$transaction['amount'], $search_rejected) !== false) {
                $rejected_transactions[] = $transaction;
            }
            break;
    }
}

?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>bKash পেমেন্ট ভেরিফিকেশন</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="../assets/css/style.css" rel="stylesheet">
    <style>
        /* Remove all animations and transitions to prevent shaking */
        * {
            animation: none !important;
            transition: none !important;
            transform: none !important;
        }
        
        /* Remove blinking cursor effect */
        .form-control, .form-control-plaintext, textarea {
            animation: none !important;
            -webkit-animation: none !important;
            caret-color: #000 !important;
        }
        
        /* Ensure modals don't have blinking effects */
        .modal-content {
            animation: none !important;
            -webkit-animation: none !important;
        }
        
        /* Remove any focus blinking */
        .form-control:focus, textarea:focus {
            box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25) !important;
            animation: none !important;
            -webkit-animation: none !important;
        }
        
        /* Remove any blinking text */
        @keyframes blink {
            from, to { opacity: 1; }
            50% { opacity: 1; }
        }
        
        /* Disable all transitions */
        .btn, .card, .table, .navbar, .form-control, .modal-content {
            transition: none !important;
            animation: none !important;
        }
        
        /* Remove hover effects that might cause jumping */
        .btn:hover, .card:hover, .table tr:hover {
            transform: none !important;
            box-shadow: none !important;
        }
        
        /* Status badges */
        .status-badge {
            padding: 5px 10px;
            border-radius: 15px;
            font-weight: bold;
        }
        .status-pending {
            background-color: #ffc107;
            color: #000;
        }
        .status-verified {
            background-color: #28a745;
            color: #fff;
        }
        .status-rejected {
            background-color: #dc3545;
            color: #fff;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
    <div class="container-fluid">
        <a class="navbar-brand" href="../index.php"><i class="fas fa-graduation-cap"></i> স্কুল ম্যানেজমেন্ট সিস্টেম</a>
        <div class="navbar-nav ms-auto">
            <a class="nav-link" href="manage.php"><i class="fas fa-arrow-left"></i> ফি ম্যানেজ</a>
            <a class="nav-link" href="../logout.php"><i class="fas fa-sign-out-alt"></i> লগআউট</a>
        </div>
    </div>
</nav>

<div class="container-fluid mt-4">
    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header">
                    <h5><i class="fas fa-check-circle"></i> bKash পেমেন্ট ভেরিফিকেশন</h5>
                </div>
                <div class="card-body">
                    <?php if (isset($success_message)): ?>
                        <div class="alert alert-success alert-dismissible fade show">
                            <i class="fas fa-check-circle"></i> <?= htmlspecialchars($success_message) ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <?php if (isset($error_message)): ?>
                        <div class="alert alert-danger alert-dismissible fade show">
                            <i class="fas fa-exclamation-triangle"></i> <?= htmlspecialchars($error_message) ?>
                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                        </div>
                    <?php endif; ?>
                    
                    <!-- Pending Transactions -->
                    <div class="card mb-4">
                        <div class="card-header bg-warning text-dark">
                            <h6><i class="fas fa-clock"></i> অপেক্ষমান ট্রানজেকশন (<?= count($pending_transactions) ?>)</h6>
                        </div>
                        <div class="card-body">
                            <!-- Search Form for Pending Transactions -->
                            <form method="GET" class="mb-3">
                                <div class="row">
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" name="search_pending" placeholder="শিক্ষার্থী আইডি, নাম, ট্রানজেকশন আইডি, bKash নাম্বার বা পরিমাণ অনুসন্ধান করুন..." value="<?= htmlspecialchars($search_pending) ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <div class="btn-group" role="group">
                                            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> অনুসন্ধান</button>
                                            <?php if (!empty($search_pending)): ?>
                                                <a href="verify_bkash_payments.php" class="btn btn-secondary"><i class="fas fa-times"></i> পরিষ্কার</a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            
                            <?php if (empty($pending_transactions)): ?>
                                <p class="text-muted">
                                    <?php if (!empty($search_pending)): ?>
                                        আপনার অনুসন্ধান মানদণ্ডের সাথে মেলে এমন কোন অপেক্ষমান ট্রানজেকশন পাওয়া যায়নি।
                                    <?php else: ?>
                                        কোন অপেক্ষমান ট্রানজেকশন নেই।
                                    <?php endif; ?>
                                </p>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>শিক্ষার্থী আইডি</th>
                                                <th>নাম</th>
                                                <th>ক্লাস</th>
                                                <th>রোল নম্বর</th>
                                                <th>ট্রানজেকশন আইডি</th>
                                                <th>bKash নাম্বার</th>
                                                <th>পরিমাণ</th>
                                                <th>তারিখ</th>
                                                <th>অ্যাকশন</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($pending_transactions as $transaction): ?>
                                                <tr>
                                                    <td><?= htmlspecialchars($transaction['student_code']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['first_name'] . ' ' . $transaction['last_name']) ?></td>
                                                    <td><?= htmlspecialchars(($transaction['class_name'] ?? '') . ' - ' . ($transaction['section'] ?? '')) ?></td>
                                                    <td><?= htmlspecialchars($transaction['roll_number'] ?? '') ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_transaction_id']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_number'] ?? 'N/A') ?></td>
                                                    <td>৳<?= number_format((float)$transaction['amount'], 2) ?></td>
                                                    <td><?= date('d/m/Y H:i', strtotime($transaction['submitted_at'])) ?></td>
                                                    <td>
                                                        <button class="btn btn-sm btn-success" data-bs-toggle="modal" data-bs-target="#verifyModal<?= $transaction['transaction_id'] ?>">
                                                            <i class="fas fa-check"></i> ভেরিফাই
                                                        </button>
                                                    </td>
                                                </tr>
                                                
                                                <!-- Verification Modal -->
                                                <div class="modal fade" id="verifyModal<?= $transaction['transaction_id'] ?>" tabindex="-1">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">bKash পেমেন্ট ভেরিফাই করুন</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                                                            </div>
                                                            <form method="POST">
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="transaction_id" value="<?= $transaction['transaction_id'] ?>">
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label"><strong>শিক্ষার্থী:</strong></label>
                                                                        <p class="form-control-plaintext">
                                                                            <?= htmlspecialchars($transaction['first_name'] . ' ' . $transaction['last_name']) ?> 
                                                                            (<?= htmlspecialchars($transaction['student_code']) ?>)
                                                                        </p>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label"><strong>ট্রানজেকশন আইডি:</strong></label>
                                                                        <p class="form-control-plaintext"><?= htmlspecialchars($transaction['bkash_transaction_id']) ?></p>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label"><strong>bKash নাম্বার:</strong></label>
                                                                        <p class="form-control-plaintext"><?= htmlspecialchars($transaction['bkash_number'] ?? 'N/A') ?></p>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label class="form-label"><strong>পরিমাণ:</strong></label>
                                                                        <p class="form-control-plaintext">৳<?= number_format((float)$transaction['amount'], 2) ?></p>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="admin_notes_<?= $transaction['transaction_id'] ?>" class="form-label">নোট (ঐচ্ছিক)</label>
                                                                        <textarea class="form-control" id="admin_notes_<?= $transaction['transaction_id'] ?>" name="admin_notes" rows="3" placeholder="ভেরিফিকেশন নোট লিখুন..."></textarea>
                                                                    </div>
                                                                    
                                                                    <div class="alert alert-warning">
                                                                        <i class="fas fa-exclamation-circle"></i> 
                                                                        পেমেন্ট ভেরিফাই করার আগে নিশ্চিত করুন যে bKash ট্রানজেকশনটি সঠিক।
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                                                                    <button type="submit" name="verify_payment" value="approve" class="btn btn-success">
                                                                        <i class="fas fa-check"></i> অনুমদন করুন
                                                                    </button>
                                                                    <button type="submit" name="verify_payment" value="reject" class="btn btn-danger">
                                                                        <i class="fas fa-times"></i> প্রত্যাখ্যান করুন
                                                                    </button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Verified Transactions -->
                    <div class="card mb-4">
                        <div class="card-header bg-success text-white">
                            <h6><i class="fas fa-check"></i> যাচাইকৃত ট্রানজেকশন (<?= count($verified_transactions) ?>)</h6>
                        </div>
                        <div class="card-body">
                            <!-- Search Form for Verified Transactions -->
                            <form method="GET" class="mb-3">
                                <div class="row">
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" name="search_verified" placeholder="শিক্ষার্থী আইডি, নাম, ট্রানজেকশন আইডি, bKash নাম্বার বা পরিমাণ অনুসন্ধান করুন..." value="<?= htmlspecialchars($search_verified) ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <div class="btn-group" role="group">
                                            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> অনুসন্ধান</button>
                                            <?php if (!empty($search_verified)): ?>
                                                <a href="verify_bkash_payments.php" class="btn btn-secondary"><i class="fas fa-times"></i> পরিষ্কার</a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            
                            <?php if (empty($verified_transactions)): ?>
                                <p class="text-muted">
                                    <?php if (!empty($search_verified)): ?>
                                        আপনার অনুসন্ধান মানদণ্ডের সাথে মেলে এমন কোন যাচাইকৃত ট্রানজেকশন পাওয়া যায়নি।
                                    <?php else: ?>
                                        কোন যাচাইকৃত ট্রানজেকশন নেই।
                                    <?php endif; ?>
                                </p>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>শিক্ষার্থী আইডি</th>
                                                <th>নাম</th>
                                                <th>ক্লাস</th>
                                                <th>রোল নম্বর</th>
                                                <th>ট্রানজেকশন আইডি</th>
                                                <th>bKash নাম্বার</th>
                                                <th>পরিমাণ</th>
                                                <th>তারিখ</th>
                                                <th>যাচাইকারী</th>
                                                <th>নোট</th>
                                                <th>অ্যাকশন</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($verified_transactions as $transaction): ?>
                                                <tr>
                                                    <td><?= htmlspecialchars($transaction['student_code']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['first_name'] . ' ' . $transaction['last_name']) ?></td>
                                                    <td><?= htmlspecialchars(($transaction['class_name'] ?? '') . ' - ' . ($transaction['section'] ?? '')) ?></td>
                                                    <td><?= htmlspecialchars($transaction['roll_number'] ?? '') ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_transaction_id']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_number'] ?? 'N/A') ?></td>
                                                    <td>৳<?= number_format((float)$transaction['amount'], 2) ?></td>
                                                    <td><?= date('d/m/Y H:i', strtotime($transaction['verified_at'])) ?></td>
                                                    <td><?= htmlspecialchars($transaction['verified_by_name'] ?? 'N/A') ?></td>
                                                    <td><?= htmlspecialchars($transaction['notes'] ?? 'N/A') ?></td>
                                                    <td>
                                                        <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#editModal<?= $transaction['transaction_id'] ?>">
                                                            <i class="fas fa-edit"></i> এডিট
                                                        </button>
                                                    </td>
                                                </tr>
                                                
                                                <!-- Edit Modal -->
                                                <div class="modal fade" id="editModal<?= $transaction['transaction_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">
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="transaction_id" value="<?= $transaction['transaction_id'] ?>">
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_transaction_id_<?= $transaction['transaction_id'] ?>" class="form-label">ট্রানজেকশন আইডি</label>
                                                                        <input type="text" class="form-control" id="edit_transaction_id_<?= $transaction['transaction_id'] ?>" name="edit_transaction_id" value="<?= htmlspecialchars($transaction['bkash_transaction_id']) ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_bkash_number_<?= $transaction['transaction_id'] ?>" class="form-label">bKash নাম্বার</label>
                                                                        <input type="text" class="form-control" id="edit_bkash_number_<?= $transaction['transaction_id'] ?>" name="edit_bkash_number" value="<?= htmlspecialchars($transaction['bkash_number']) ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_amount_<?= $transaction['transaction_id'] ?>" class="form-label">পরিমাণ</label>
                                                                        <input type="number" class="form-control" id="edit_amount_<?= $transaction['transaction_id'] ?>" name="edit_amount" step="0.01" value="<?= $transaction['amount'] ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_status_<?= $transaction['transaction_id'] ?>" class="form-label">স্ট্যাটাস</label>
                                                                        <select class="form-select" id="edit_status_<?= $transaction['transaction_id'] ?>" name="edit_status">
                                                                            <option value="pending" <?= $transaction['status'] === 'pending' ? 'selected' : '' ?>>অপেক্ষমান</option>
                                                                            <option value="verified" <?= $transaction['status'] === 'verified' ? 'selected' : '' ?>>যাচাইকৃত</option>
                                                                            <option value="rejected" <?= $transaction['status'] === 'rejected' ? 'selected' : '' ?>>প্রত্যাখ্যাত</option>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                                                                    <button type="submit" name="edit_transaction" class="btn btn-primary">
                                                                        <i class="fas fa-save"></i> সংরক্ষণ করুন
                                                                    </button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                    
                    <!-- Rejected Transactions -->
                    <div class="card mb-4">
                        <div class="card-header bg-danger text-white">
                            <h6><i class="fas fa-times"></i> প্রত্যাখ্যাত ট্রানজেকশন (<?= count($rejected_transactions) ?>)</h6>
                        </div>
                        <div class="card-body">
                            <!-- Search Form for Rejected Transactions -->
                            <form method="GET" class="mb-3">
                                <div class="row">
                                    <div class="col-md-8">
                                        <input type="text" class="form-control" name="search_rejected" placeholder="শিক্ষার্থী আইডি, নাম, ট্রানজেকশন আইডি, bKash নাম্বার বা পরিমাণ অনুসন্ধান করুন..." value="<?= htmlspecialchars($search_rejected) ?>">
                                    </div>
                                    <div class="col-md-4">
                                        <div class="btn-group" role="group">
                                            <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> অনুসন্ধান</button>
                                            <?php if (!empty($search_rejected)): ?>
                                                <a href="verify_bkash_payments.php" class="btn btn-secondary"><i class="fas fa-times"></i> পরিষ্কার</a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            
                            <?php if (empty($rejected_transactions)): ?>
                                <p class="text-muted">
                                    <?php if (!empty($search_rejected)): ?>
                                        আপনার অনুসন্ধান মানদণ্ডের সাথে মেলে এমন কোন প্রত্যাখ্যাত ট্রানজেকশন পাওয়া যায়নি।
                                    <?php else: ?>
                                        কোন প্রত্যাখ্যাত ট্রানজেকশন নেই।
                                    <?php endif; ?>
                                </p>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-striped">
                                        <thead>
                                            <tr>
                                                <th>শিক্ষার্থী আইডি</th>
                                                <th>নাম</th>
                                                <th>ক্লাস</th>
                                                <th>রোল নম্বর</th>
                                                <th>ট্রানজেকশন আইডি</th>
                                                <th>bKash নাম্বার</th>
                                                <th>পরিমাণ</th>
                                                <th>তারিখ</th>
                                                <th>যাচাইকারী</th>
                                                <th>নোট</th>
                                                <th>অ্যাকশন</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($rejected_transactions as $transaction): ?>
                                                <tr>
                                                    <td><?= htmlspecialchars($transaction['student_code']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['first_name'] . ' ' . $transaction['last_name']) ?></td>
                                                    <td><?= htmlspecialchars(($transaction['class_name'] ?? '') . ' - ' . ($transaction['section'] ?? '')) ?></td>
                                                    <td><?= htmlspecialchars($transaction['roll_number'] ?? '') ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_transaction_id']) ?></td>
                                                    <td><?= htmlspecialchars($transaction['bkash_number'] ?? 'N/A') ?></td>
                                                    <td>৳<?= number_format((float)$transaction['amount'], 2) ?></td>
                                                    <td><?= date('d/m/Y H:i', strtotime($transaction['verified_at'])) ?></td>
                                                    <td><?= htmlspecialchars($transaction['verified_by_name'] ?? 'N/A') ?></td>
                                                    <td><?= htmlspecialchars($transaction['notes'] ?? 'N/A') ?></td>
                                                    <td>
                                                        <button class="btn btn-sm btn-primary" data-bs-toggle="modal" data-bs-target="#editModalRejected<?= $transaction['transaction_id'] ?>">
                                                            <i class="fas fa-edit"></i> এডিট
                                                        </button>
                                                    </td>
                                                </tr>
                                                
                                                <!-- Edit Modal for Rejected Transactions -->
                                                <div class="modal fade" id="editModalRejected<?= $transaction['transaction_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">
                                                                <div class="modal-body">
                                                                    <input type="hidden" name="transaction_id" value="<?= $transaction['transaction_id'] ?>">
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_transaction_id_rejected_<?= $transaction['transaction_id'] ?>" class="form-label">ট্রানজেকশন আইডি</label>
                                                                        <input type="text" class="form-control" id="edit_transaction_id_rejected_<?= $transaction['transaction_id'] ?>" name="edit_transaction_id" value="<?= htmlspecialchars($transaction['bkash_transaction_id']) ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_bkash_number_rejected_<?= $transaction['transaction_id'] ?>" class="form-label">bKash নাম্বার</label>
                                                                        <input type="text" class="form-control" id="edit_bkash_number_rejected_<?= $transaction['transaction_id'] ?>" name="edit_bkash_number" value="<?= htmlspecialchars($transaction['bkash_number']) ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_amount_rejected_<?= $transaction['transaction_id'] ?>" class="form-label">পরিমাণ</label>
                                                                        <input type="number" class="form-control" id="edit_amount_rejected_<?= $transaction['transaction_id'] ?>" name="edit_amount" step="0.01" value="<?= $transaction['amount'] ?>" required>
                                                                    </div>
                                                                    
                                                                    <div class="mb-3">
                                                                        <label for="edit_status_rejected_<?= $transaction['transaction_id'] ?>" class="form-label">স্ট্যাটাস</label>
                                                                        <select class="form-select" id="edit_status_rejected_<?= $transaction['transaction_id'] ?>" name="edit_status">
                                                                            <option value="pending" <?= $transaction['status'] === 'pending' ? 'selected' : '' ?>>অপেক্ষমান</option>
                                                                            <option value="verified" <?= $transaction['status'] === 'verified' ? 'selected' : '' ?>>যাচাইকৃত</option>
                                                                            <option value="rejected" <?= $transaction['status'] === 'rejected' ? 'selected' : '' ?>>প্রত্যাখ্যাত</option>
                                                                        </select>
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">বাতিল</button>
                                                                    <button type="submit" name="edit_transaction" class="btn btn-primary">
                                                                        <i class="fas fa-save"></i> সংরক্ষণ করুন
                                                                    </button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>