<?php
require_once 'config.php';

if (!isset($_SESSION['opd_patient'])) {
    header('Location: index.php');
    exit;
}

$patient_id = $_SESSION['opd_patient'];
$bill_id = (int)($_GET['bill_id'] ?? 0);

// Get bill details
$bill = $conn->query("SELECT * FROM opd_bills WHERE id = $bill_id AND patient_id = $patient_id")->fetch_assoc();

if (!$bill) {
    die("<div style='padding:50px;text-align:center;font-family:sans-serif;'><h3>❌ বিল পাওয়া যায়নি!</h3><a href='dashboard.php'>ড্যাশবোর্ডে ফিরুন</a></div>");
}

$success = '';
$error = '';

if (isset($_POST['submit_payment'])) {
    $txn_id = $conn->real_escape_string($_POST['transaction_id']);
    
    if (empty($txn_id)) {
        $error = "ট্রানজ্যাকশন আইডি প্রয়োজন!";
    } else {
        $sql = "UPDATE opd_bills SET payment_status='paid', transaction_id='$txn_id' WHERE id=$bill_id";
        if ($conn->query($sql)) {
            $success = "✅ পেমেন্ট সফল!";
            $bill['payment_status'] = 'paid';
            $bill['transaction_id'] = $txn_id;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <title>Bill Payment</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-4">
<div class="container" style="max-width:500px">
    <a href="dashboard.php#bills" class="btn btn-secondary mb-3">← ফিরুন</a>
    
    <?php if($success): ?>
        <div class="alert alert-success"><?=$success?></div>
    <?php endif; ?>
    <?php if($error): ?>
        <div class="alert alert-danger"><?=$error?></div>
    <?php endif; ?>
    
    <div class="card">
        <div class="card-header bg-success text-white"><h5>💰 বিল পেমেন্ট</h5></div>
        <div class="card-body">
            <p><strong>পরিমাণ:</strong> ৳<?=number_format($bill['amount'])?></p>
            <p><strong>বিবরণ:</strong> <?=$bill['description']?></p>
            <p><strong>স্ট্যাটাস:</strong> <?=$bill['payment_status']=='paid'?'✅ পেইড':'❌ আনপেইড'?></p>
            
            <?php if($bill['payment_status']=='unpaid'): ?>
            <hr>
            <p class="text-muted">bKash/Nagad/Rocket এ পেমেন্ট করে ট্রানজ্যাকশন আইডি দিন:</p>
            <form method="POST">
                <input type="text" name="transaction_id" class="form-control mb-3" placeholder="TrxID" required>
                <button type="submit" name="submit_payment" class="btn btn-success w-100">Pay Now</button>
            </form>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>