<?php
session_start();
include 'config.php';
include 'routeros_api.class.php';
date_default_timezone_set('Asia/Dhaka');

if (!isset($_GET['cid'])) { header("Location: index.php"); exit; }

$cid = mysqli_real_escape_string($conn, $_GET['cid']);
$cust_res = mysqli_query($conn, "SELECT * FROM customers WHERE customer_id = '$cid'");
$cust = mysqli_fetch_assoc($cust_res);

// রিচার্জ লজিক
if (isset($_POST['activate_customer'])) {
    $amount = mysqli_real_escape_string($conn, $_POST['amount']);
    $method = mysqli_real_escape_string($conn, $_POST['payment_method']);
    $trx_id = mysqli_real_escape_string($conn, $_POST['trx_id']);
    $today_time = date('Y-m-d H:i:s');

    $m_ip = "202.125.68.177"; $m_user = "rang@php"; $m_pass = "php@rang"; $m_port = 8729; 
    $api = new RouterosAPI();

    if ($api->connect($m_ip, $m_user, $m_pass, $m_port)) {
        $api->comm("/ppp/secret/set", array("numbers" => $cid, "disabled" => "no"));
        mysqli_query($conn, "INSERT INTO transactions (customer_id, amount, payment_method, trx_id, payment_date) VALUES ('$cid', '$amount', '$method', '$trx_id', '$today_time')");
        mysqli_query($conn, "UPDATE customers SET status='active', expiry_date = DATE_ADD(expiry_date, INTERVAL 1 MONTH) WHERE customer_id = '$cid'");
        
        // SMS Code (আপনার মূল কোড অনুযায়ী)
        $new_expiry = date("d-M-Y", strtotime("+1 month", strtotime($cust['expiry_date'])));
        $msg = "Dear $cust[name],\nYour Bill $amount Tk Payment Done\nYour Line Activated\nNext Deu $new_expiry\nThanks for using\nRANGDHANU.NET

Any Problem Call
01624445842";
        $sms_url = "http://bulksmsbd.net/api/smsapi?api_key=bqyPSfigjl5k9Fn1cZW4&type=text&number=" . $cust['phone'] . "&senderid=8809648908043&message=" . urlencode($msg);
        @file_get_contents($sms_url);

        $api->disconnect();
        echo "<script>alert('রিচার্জ সফল হয়েছে!'); window.location.href='index.php';</script>";
    }
}

// ট্রানজেকশন হিস্ট্রি সংগ্রহ
$history = mysqli_query($conn, "SELECT * FROM transactions WHERE customer_id = '$cid' ORDER BY payment_date DESC");
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Recharge - <?php echo $cust['name']; ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Inter', sans-serif; background: #f3f4f6; margin: 0; padding: 20px; }
        .container { max-width: 600px; margin: 0 auto; }
        .card { background: white; padding: 25px; border-radius: 15px; box-shadow: 0 4px 6px rgba(0,0,0,0.05); margin-bottom: 20px; }
        h2 { margin-top: 0; font-size: 20px; color: #1f2937; border-bottom: 2px solid #f3f4f6; padding-bottom: 10px; }
        .form-group { margin-bottom: 15px; }
        label { display: block; font-size: 13px; color: #6b7280; font-weight: 600; margin-bottom: 5px; }
        input, select { width: 100%; padding: 12px; border: 1px solid #e5e7eb; border-radius: 8px; box-sizing: border-box; }
        .btn-save { background: #10b981; color: white; border: none; width: 100%; padding: 14px; border-radius: 8px; font-weight: 600; cursor: pointer; font-size: 16px; }
        .history-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 13px; }
        .history-table th, .history-table td { padding: 12px; text-align: left; border-bottom: 1px solid #f3f4f6; }
        .history-table th { color: #6b7280; font-weight: 600; }
        .back-link { display: inline-block; margin-bottom: 15px; text-decoration: none; color: #4f46e5; font-weight: 600; }
    </style>
</head>
<body>

<div class="container">
    <a href="index.php" class="back-link">← ফিরে যান</a>

    <!-- রিচার্জ ফর্ম -->
    <div class="card">
        <h2>বিল সংগ্রহ: <?php echo $cust['name']; ?> (<?php echo $cust['customer_id']; ?>)</h2>
        <form method="POST">
            <div class="form-group">
                <label>বিল এর পরিমাণ (TK)</label>
                <input type="number" name="amount" value="<?php echo $cust['bill_amount']; ?>" required>
            </div>
            <div class="form-group">
                <label>পেমেন্ট মাধ্যম</label>
                <select name="payment_method" required>
                    <option value="Cash">Cash (নগদ)</option>
                    <option value="bKash">bKash</option>
                    <option value="Nagad">Nagad</option>
                </select>
            </div>
            <div class="form-group">
                <label>ট্রানজেকশন আইডি (TrxID)</label>
                <input type="text" name="trx_id" placeholder="ঐচ্ছিক">
            </div>
            <button type="submit" name="activate_customer" class="btn-save">পেমেন্ট নিশ্চিত ও একটিভ করুন</button>
        </form>
    </div>

    <!-- রিচার্জ হিস্ট্রি টেবিল -->
    <div class="card">
        <h2>পূর্বের রিচার্জ হিস্ট্রি</h2>
        <table class="history-table">
            <thead>
                <tr>
                    <th>তারিখ</th>
                    <th>পরিমাণ</th>
                    <th>মাধ্যম</th>
                    <th>TrxID</th>
                </tr>
            </thead>
            <tbody>
                <?php if(mysqli_num_rows($history) > 0): ?>
                    <?php while($h = mysqli_fetch_assoc($history)): ?>
                    <tr>
                        <td><?php echo date("d M Y", strtotime($h['payment_date'])); ?></td>
                        <td><?php echo $h['amount']; ?> TK</td>
                        <td><?php echo $h['payment_method']; ?></td>
                        <td><?php echo $h['trx_id'] ?: '-'; ?></td>
                    </tr>
                    <?php endwhile; ?>
                <?php else: ?>
                    <tr><td colspan="4" style="text-align:center; color:#9ca3af;">কোনো হিস্ট্রি পাওয়া যায়নি</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</body>
</html>