<?php 
include 'includes/header.php'; 
include 'includes/sidebar.php';

// GET CURRENT WALLET (latest)
$current = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM payment_details ORDER BY id DESC LIMIT 1"));

$success = false;

if($_POST){
    $image = $_FILES['image']['name'];
    move_uploaded_file($_FILES['image']['tmp_name'],"../uploads/".$image);

    mysqli_query($conn,"INSERT INTO payment_details (payment_text,payment_image) VALUES ('{$_POST['text']}','$image')");

    $success = true;

    // Refresh current wallet
    $current = [
        'payment_text' => $_POST['text'],
        'payment_image' => $image
    ];
}
?>

<div class="container-fluid py-5">

    <!-- Success Alert -->
    <?php if($success){ ?>
        <div class="alert alert-success alert-dismissible fade show mx-auto" style="max-width:600px;" role="alert">
            ✅ Payment details saved successfully!
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php } ?>

    <div class="row">
        <div class="col-lg-5 mx-auto">

            <!-- CURRENT WALLET -->
            <?php if($current){ ?>
            <div class="card p-4 mb-4">

                <h5 class="text-primary mb-3">💼 Current Wallet: <?php echo nl2br($current['payment_text']); ?></h5>

                <div class="text-center mb-2">
                    <img src="../uploads/<?php echo $current['payment_image']; ?>" 
                         class="img-fluid rounded" 
                         style="max-height:100px;">
                </div>

              

            </div>
            <?php } ?>

            <!-- FORM -->
            <div class="card p-4">

                <h4 class="mb-4 text-center text-primary">
                   💳 Payment Setup
                </h4>

                <form method="POST" enctype="multipart/form-data" id="paymentForm">

                    <div class="mb-4">
                        <label class="form-label">Payment Instructions (Crypto Wallet)</label>
                        <textarea 
                            name="text" 
                            class="form-control"
                            rows="5"
                            placeholder="Enter payment instructions..."
                            required
                        ></textarea>
                    </div>

                    <div class="mb-4">
                        <label class="form-label">Upload Crypto Wallet QR Code Image</label>
                        <input 
                            type="file" 
                            name="image" 
                            class="form-control"
                            accept="image/*"
                            required
                        >
                    </div>

                    <div class="d-grid">
                        <button id="submitBtn" class="btn btn-primary py-2 fw-semibold">
                            💾 Save Payment Details
                        </button>
                    </div>

                </form>

            </div>

        </div>
    </div>

</div>

<!-- JS -->
<script>
document.getElementById('paymentForm').addEventListener('submit', function(){
    const btn = document.getElementById('submitBtn');
    btn.innerHTML = '⏳ Saving...';
    btn.disabled = true;
});
</script>

<?php include 'includes/footer.php'; ?>