<?php
// Define $payment as an empty array
$payment = [];

// Retrieve payment information based on the provided payment_id
if(isset($_GET['payment_id'])){
    $payment_id = $_GET['payment_id'];
    $get_payment_query = "SELECT * FROM payment WHERE payment_id=$payment_id";
    $result = mysqli_query($db, $get_payment_query);
    $payment = mysqli_fetch_assoc($result);
}

if(isset($_POST['editpayment'])){
    $payment_id = mysqli_real_escape_string($db, $_POST['payment_id']);
    $sname =  mysqli_real_escape_string($db, $_POST['sname']);
    $pay = mysqli_real_escape_string($db, $_POST['pay']);
    $approval =  mysqli_real_escape_string($db, $_POST['approval']);
    $note = mysqli_real_escape_string($db, $_POST['note']);
    $reciept = mysqli_real_escape_string($db, $_POST['reciept']);

    // Check if a new file is uploaded
    if(isset($_FILES['paymentfiles']) && $_FILES['paymentfiles']['error'] == UPLOAD_ERR_OK) {
        $paymentfiles = $_FILES['paymentfiles']['name'];
        $tmp_name = $_FILES['paymentfiles']['tmp_name'];
        $upload_dir = 'upload/';
        $destination = $upload_dir . $paymentfiles;
        // Move the uploaded file to the destination directory
        if(move_uploaded_file($tmp_name, $destination)) {
            // Update payment information including the file name
            $update_query = "UPDATE payment SET sname='$sname', paymentfiles='$paymentfiles', pay='$pay', approval='$approval', note='$note' WHERE payment_id = $payment_id";
            if(mysqli_query($db, $update_query)){
                echo "<script>alert('Payment information updated successfully.')</script>";
                header("location:index.php?env=edit_payment&payment_id={$payment_id}");
            }else{
                echo "<script>alert('Error updating payment information: ".mysqli_error($db)."')</script>";
            }
        } else {
            echo "<script>alert('Failed to move uploaded file.')</script>";
        }
    } else {
        // No new file uploaded, only update other fields
        $update_query = "UPDATE payment SET sname='$sname', pay='$pay', approval='$approval', note='$note' WHERE payment_id = $payment_id";
        if(mysqli_query($db, $update_query)){
            echo "<script>alert('Payment information updated successfully.')</script>";
            header("location:index.php?env=edit_payment&payment_id={$payment_id}");
        }else{
            echo "<script>alert('Error updating payment information: ".mysqli_error($db)."')</script>";
        }
    }
}
?>

<!-- Recent Sales Start -->
<div class="container-fluid pt-4 px-4">
    <div class="bg-secondary text-center rounded p-4">
        <div class="d-flex align-items-center justify-content-between mb-4">
            <h6 class="mb-0">Edit Payment Information</h6>
        </div>
        <div class="table-responsive">
            <div class="container">
                <div class="row">
                    <div class="col-md-2"></div>
                    <div class="col-md-8">
                        <!-- Form for editing payment information -->
                        <form method="POST" action="index.php?env=edit_payment" enctype="multipart/form-data">
                            <input type="hidden" name="payment_id" value="<?php echo $payment['payment_id']; ?>">
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-6">
                                        <input type="text" name="sname" value="<?php echo $payment['sname']; ?>" class="form-control" placeholder="Student Name"><br/>
                                    </div>
                                    <div class="col-md-6">
                                        <?php if(!empty($payment['paymentfiles'])): ?>
                                            <a href="upload/<?php echo $payment['paymentfiles']; ?>" target="_blank">View Payment PDF</a><br/>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-6">
                                        <input type="text" name="pay" value="<?php echo $payment['pay']; ?>" class="form-control" placeholder="Payment Amount"><br/>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="text" name="approval" value="<?php echo $payment['approval']; ?>" class="form-control" placeholder="Approval Status"><br/>
                                    </div>
                                </div>  
                            </div>
                            <div class="form-group">
                                <div class="row">
                                    <div class="col-md-6">
                                        <input type="text" name="note" value="<?php echo $payment['note']; ?>" class="form-control" placeholder="Add Note for student"><br/>
                                    </div>
                                    <div class="col-md-6">
                                        <input type="file" name="reciept" value="<?php echo $payment['reciept']; ?>" class="form-control" placeholder="Approval Status"><br/>
                                    </div>
                                </div>  
                            </div>

                            <div class="form-group">
                                <button type="submit" name="editpayment" class="btn btn-outline-primary btn-block">Save Changes</button>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-2"></div>
                </div>            
            </div>
        </div>
    </div>
</div>
<!-- Footer -->
<?php include("footer.php"); ?>