<?php
session_start();
include 'layouts/dbconn.php';
include 'layouts/session.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role_code'] !== 'PATIENT') {
    header('Location: login.php');
    exit();
}

$invoice_id = isset($_GET['invoice_id']) ? intval($_GET['invoice_id']) : 0;
$amount = $_GET['amount'] ?? 0;

if ($invoice_id) {
    // Check invoice
    $chk = $conn->query("SELECT * FROM invoices WHERE invoice_id=$invoice_id");
    if ($chk->num_rows > 0) {
        $inv = $chk->fetch_assoc();
        $amount = $inv['balance'];
        $ref = $inv['invoice_number'];
    }
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Make Payment')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Secure Payment</h4>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-6">
                            <div class="card">
                                <div class="card-header bg-success-subtle">
                                    <h5 class="card-title mb-0 text-success"><i class="ri-lock-2-line me-2"></i>Secure
                                        Payment Gateway</h5>
                                </div>
                                <div class="card-body">
                                    <div class="text-center mb-4">
                                        <p class="text-muted">You are paying for:</p>
                                        <h4><?php echo isset($ref) ? "Invoice #$ref" : "Account Balance"; ?></h4>
                                        <h2 class="text-primary mt-3">KES <?php echo number_format($amount, 2); ?></h2>
                                    </div>

                                    <form action="#" method="POST">
                                        <!-- Logic to be integrated with M-Pesa or Stripe -->
                                        <div class="mb-3">
                                            <label class="form-label">Select Payment Method</label>
                                            <div class="d-grid gap-2">
                                                <button type="button" class="btn btn-outline-success btn-lg text-start">
                                                    <i class="ri-smartphone-line me-3"></i> M-Pesa
                                                </button>
                                                <button type="button" class="btn btn-outline-primary btn-lg text-start">
                                                    <i class="ri-bank-card-2-line me-3"></i> Credit / Debit Card
                                                </button>
                                            </div>
                                        </div>

                                        <div class="alert alert-info">
                                            <i class="ri-information-line me-2"></i>
                                            For M-Pesa, a prompt will be sent to your registered phone number.
                                        </div>

                                        <div class="d-grid">
                                            <a href="patient_invoices.php" class="btn btn-light mt-2">Cancel</a>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>