<?php
// ১. বাংলাদেশের টাইমজোন সেট করা হলো (ভেরি ইম্পর্ট্যান্ট)
date_default_timezone_set('Asia/Dhaka');

// ডাটাবেজ কানেকশন কনফিগারেশন
$servername = "localhost";
$username = "rangdha1_alom"; 
$password = "Akash789@";     
$dbname = "rangdha1_alom";   

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("<div style='color:red; text-align:center; margin-top:20px;'>Connection failed: " . $conn->connect_error . "</div>");
}

// ২. অটো টেবিল ক্রিয়েশন লজিক
$create_accounts_table = "CREATE TABLE IF NOT EXISTS accounts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    balance DECIMAL(10,2) NOT NULL DEFAULT 0.00
)";
$conn->query($create_accounts_table);

$check_account = $conn->query("SELECT id FROM accounts WHERE id = 1");
if ($check_account->num_rows == 0) {
    $conn->query("INSERT INTO accounts (id, balance) VALUES (1, 0.00)");
}

$create_reseller_table = "CREATE TABLE IF NOT EXISTS reseller (
    id INT AUTO_INCREMENT PRIMARY KEY,
    trx_id VARCHAR(50) NOT NULL UNIQUE,
    amount DECIMAL(10,2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
$conn->query($create_reseller_table);

$message = "";

// ৩. টাকা অ্যাড করার প্রফেশনাল লজিক
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['add_money'])) {
    $amount = floatval($_POST['amount']);
    
    if ($amount > 0) {
        // বাংলাদেশ টাইম অনুযায়ী Trx ID তৈরি (যেমন: TRX260625...)
        $trx_id = "TRX" . date("ymd") . strtoupper(substr(md5(uniqid(mt_rand(), true)), 0, 6));

        $conn->begin_transaction();

        try {
            // ১. ব্যালেন্স আপডেট
            $update_sql = "UPDATE accounts SET balance = balance + ? WHERE id = 1";
            $stmt1 = $conn->prepare($update_sql);
            $stmt1->bind_param("d", $amount);
            $stmt1->execute();

            // ২. reseller টেবিলে ডাটা ইনসার্ট (বাংলাদেশ কারেন্ট টাইমসহ)
            $current_time = date("Y-m-d H:i:s");
            $reseller_sql = "INSERT INTO reseller (trx_id, amount, created_at) VALUES (?, ?, ?)";
            $stmt2 = $conn->prepare($reseller_sql);
            $stmt2->bind_param("sds", $trx_id, $amount, $current_time);
            $stmt2->execute();

            $conn->commit();
            
            $message = "<div class='alert success'>
                            <strong>ধন্যবাদ!</strong> ৳" . number_format($amount, 2) . " সফলভাবে যোগ হয়েছে।<br>
                            <span class='trx-badge'>Trx ID: $trx_id</span>
                        </div>";
        } catch (Exception $e) {
            $conn->rollback();
            $message = "<div class='alert danger'>ব্যালেন্স আপডেট করতে সমস্যা হয়েছে। আবার চেষ্টা করুন।</div>";
        }
    } else {
        $message = "<div class='alert danger'>অনুগ্রহ করে সঠিক পরিমাণের টাকা লিখুন।</div>";
    }
}

// ৪. বর্তমান ব্যালেন্স তুলে আনা
$balance_query = "SELECT balance FROM accounts WHERE id = 1";
$balance_result = $conn->query($balance_query);
$current_balance = 0.00;

if ($balance_result && $balance_result->num_rows > 0) {
    $row = $balance_result->fetch_assoc();
    $current_balance = $row['balance'];
}

// ৫. শেষ ৫টি রিসেলার ট্রানজেকশন হিস্টোরি দেখা
$history_query = "SELECT * FROM reseller ORDER BY id DESC LIMIT 5";
$history_result = $conn->query($history_query);
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>রিসেলার ওয়ালেট ড্যাশবোর্ড</title>
    <link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        * { box-sizing: border-box; font-family: 'Hind Siliguri', sans-serif; margin: 0; padding: 0; }
        body { background: #f8fafc; color: #1e293b; padding: 20px; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
        
        .container { width: 100%; max-width: 480px; background: #ffffff; padding: 30px; border-radius: 24px; box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.05), 0 8px 10px -6px rgba(0, 0, 0, 0.05); }
        h2 { text-align: center; margin-bottom: 25px; color: #0f172a; font-size: 24px; font-weight: 700; }
        
        .balance-card { background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%); color: white; padding: 25px; border-radius: 18px; text-align: center; margin-bottom: 25px; box-shadow: 0 10px 20px -5px rgba(59, 130, 246, 0.4); }
        .balance-card p { font-size: 14px; opacity: 0.9; text-transform: uppercase; letter-spacing: 1px; font-weight: 600; }
        .balance-card h1 { font-size: 36px; margin-top: 8px; font-weight: 700; }

        .form-group { margin-bottom: 20px; }
        label { display: block; margin-bottom: 8px; font-weight: 600; color: #334155; font-size: 15px; }
        .input-wrapper { position: relative; display: flex; align-items: center; }
        .currency-symbol { position: absolute; left: 15px; color: #64748b; font-weight: bold; font-size: 18px; }
        input[type="number"] { width: 100%; padding: 14px 14px 14px 40px; border: 2px solid #e2e8f0; border-radius: 12px; font-size: 18px; outline: none; transition: all 0.3s ease; font-weight: 600; color: #0f172a; }
        input[type="number"]:focus { border-color: #3b82f6; box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.15); }
        
        button { width: 100%; background: #0f172a; color: white; border: none; padding: 14px; font-size: 16px; border-radius: 12px; cursor: pointer; font-weight: 600; transition: all 0.2s ease; box-shadow: 0 4px 6px -1px rgba(15, 23, 42, 0.1); }
        button:hover { background: #1e293b; transform: translateY(-1px); }
        button:active { transform: translateY(1px); }

        .alert { padding: 15px; border-radius: 12px; margin-bottom: 25px; font-size: 15px; text-align: center; line-height: 1.6; }
        .success { background-color: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }
        .danger { background-color: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
        .trx-badge { display: inline-block; background: #e2e8f0; color: #334155; padding: 2px 8px; border-radius: 6px; margin-top: 5px; font-family: monospace; font-size: 13px; font-weight: bold; }

        .history-section { margin-top: 30px; }
        .history-section h3 { font-size: 16px; color: #334155; margin-bottom: 15px; border-bottom: 2px solid #f1f5f9; padding-bottom: 8px; font-weight: 600; }
        .trx-item { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid #f1f5f9; }
        .trx-item:last-child { border-bottom: none; }
        .trx-id { color: #475569; font-family: monospace; font-size: 14px; font-weight: 600; }
        .trx-date { color: #94a3b8; font-size: 12px; margin-top: 3px; }
        .trx-amount { color: #16a34a; font-weight: 700; font-size: 16px; }
    </style>
</head>
<body>

<div class="container">
    <h2>রিসেলার ওয়ালেট</h2>
    
    <div class="balance-card">
        <p>বর্তমান ব্যালেন্স</p>
        <h1>৳ <?php echo number_format($current_balance, 2); ?></h1>
    </div>

    <?php if(!empty($message)) echo $message; ?>

    <form method="POST" action="">
        <div class="form-group">
            <label for="amount">টাকার পরিমাণ বসান</label>
            <div class="input-wrapper">
                <span class="currency-symbol">৳</span>
                <input type="number" id="amount" name="amount" placeholder="যেমন: 500" min="1" step="0.01" required>
            </div>
        </div>
        <button type="submit" name="add_money">টাকা অ্যাড করুন</button>
    </form>

    <div class="history-section">
        <h3>সাম্প্রতিক রিসেলার ট্রানজেকশন</h3>
        <?php
        if ($history_result && $history_result->num_rows > 0) {
            while($row = $history_result->fetch_assoc()) {
                // ডাটাবেজের টাইমস্ট্যাম্পটিকে বাংলাদেশি ফর্মেটে রূপান্তর
                $formatted_date = date("d M Y, h:i A", strtotime($row['created_at']));
                
                echo "<div class='trx-item'>";
                echo "<div>";
                echo "<div class='trx-id'>" . htmlspecialchars($row['trx_id']) . "</div>";
                echo "<div class='trx-date'>" . $formatted_date . "</div>";
                echo "</div>";
                echo "<div class='trx-amount'>+ ৳" . number_format($row['amount'], 2) . "</div>";
                echo "</div>";
            }
        } else {
            echo "<p style='color:#94a3b8; font-size:14px; text-align:center; padding:20px 0;'>কোনো রিসেলার ডাটা পাওয়া যায়নি।</p>";
        }
        ?>
    </div>
</div>

</body>
</html>
<?php 
$conn->close(); 
?>
