<?php

session_start();

if (!isset($_SESSION['user_id'])) {

    echo '<p class="text-danger">Unauthorized access</p>';

    exit;

}



require_once '../config.php';

$conn = getDBConnection();



$payment_id = intval($_GET['payment_id'] ?? 0);



if ($payment_id <= 0) {

    echo '<p class="text-danger">Invalid payment ID</p>';

    exit;

}



// Get payment details

$query = "SELECT p.*, s.id as student_id, s.first_name, s.last_name 

          FROM payments p

          JOIN students s ON p.student_id = s.id

          WHERE p.id = ?";

$stmt = $conn->prepare($query);

$stmt->bind_param("i", $payment_id);

$stmt->execute();

$result = $stmt->get_result();

$payment = $result->fetch_assoc();

$stmt->close();



if (!$payment) {

    echo '<p class="text-danger">Payment not found</p>';

    exit;

}



// Get fee structures for dropdown

$feeStructures = [];

$result = $conn->query("SELECT * FROM fee_structures ORDER BY class_name, fee_type");

if ($result) {

    while ($row = $result->fetch_assoc()) {

        $feeStructures[] = $row;

    }

}



$conn->close();

?>



<input type="hidden" name="payment_id" value="<?php echo $payment['id']; ?>">



<div class="mb-3">

    <label class="form-label">Student</label>

    <input type="text" class="form-control" value="<?php echo htmlspecialchars($payment['first_name'] . ' ' . $payment['last_name']); ?>" readonly>

    <input type="hidden" name="student_id" value="<?php echo $payment['student_id']; ?>">

</div>



<div class="mb-3">

    <label class="form-label">Fee Structure *</label>

    <select class="form-select" name="fee_structure_id" id="editFeeStructureSelect" required>

        <option value="">Select Fee Type</option>

        <?php foreach ($feeStructures as $fee): ?>

            <option value="<?php echo $fee['id']; ?>" 

                data-amount="<?php echo $fee['amount']; ?>"

                <?php echo ($fee['id'] == $payment['fee_structure_id']) ? 'selected' : ''; ?>>

                <?php echo htmlspecialchars($fee['class_name'] . ' - ' . $fee['fee_type'] . ' (' . formatCurrency($fee['amount']) . ')'); ?>

            </option>

        <?php endforeach; ?>

    </select>

</div>



<div class="mb-3">

    <label class="form-label">Amount Paid *</label>

    <input type="number" class="form-control" name="amount_paid" id="editAmountPaid" step="0.01" value="<?php echo $payment['amount_paid']; ?>" required>

</div>



<div class="mb-3">

    <label class="form-label">Payment Date *</label>

    <input type="date" class="form-control" name="payment_date" value="<?php echo $payment['payment_date']; ?>" required>

</div>



<div class="mb-3">

    <label class="form-label">Payment Method *</label>

    <select class="form-select" name="payment_method" required>

        <option value="cash" <?php echo ($payment['payment_method'] == 'cash') ? 'selected' : ''; ?>>Cash</option>

        <option value="card" <?php echo ($payment['payment_method'] == 'card') ? 'selected' : ''; ?>>Card</option>

        <option value="bank_transfer" <?php echo ($payment['payment_method'] == 'bank_transfer') ? 'selected' : ''; ?>>Bank Transfer</option>

        <option value="online" <?php echo ($payment['payment_method'] == 'online') ? 'selected' : ''; ?>>Online</option>

    </select>

</div>



<div class="mb-3">

    <label class="form-label">Transaction ID</label>

    <input type="text" class="form-control" name="transaction_id" value="<?php echo htmlspecialchars($payment['transaction_id']); ?>" placeholder="Enter transaction ID">

</div>



<div class="mb-3">

    <label class="form-label">Notes</label>

    <textarea class="form-control" name="notes" rows="2" placeholder="Any additional notes"><?php echo htmlspecialchars($payment['notes']); ?></textarea>

</div>