<?php
include_once('exp.php'); 
// এরপর নিচে আপনার ওয়েবসাইটের সাধারণ কোড বা হোমপেজ থাকবে...
?>
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

try {
    $db = new PDO('sqlite:trading_platform.db');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // ট্রেড টেবিল (নিশ্চিত করা যে প্রয়োজনীয় কলামগুলো আছে)
    $db->exec("CREATE TABLE IF NOT EXISTS trades (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        user_id INTEGER,
        amount REAL,
        direction TEXT, 
        duration TEXT,  
        status TEXT DEFAULT 'Pending', 
        result_amount REAL DEFAULT 0,
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    )");
} catch (PDOException $e) {
    die("Database Error: " . $e->getMessage());
}

$user_id = $_SESSION['user_id'];
$message = "";

// ইউজারের বর্তমান ব্যালেন্স চেক
$stmt = $db->prepare("SELECT main_balance FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$current_balance = $stmt->fetchColumn();

// ট্রেড প্লেস করার লজিক
if (isset($_POST['place_trade'])) {
    $amount = (float)$_POST['trade_amount'];
    $direction = $_POST['direction']; // 'up' অথবা 'down'
    $duration = $_POST['duration'];

    // সর্বনিম্ন ট্রেড $1.0 চেক
    if ($amount >= 1.0) {
        if ($amount <= $current_balance) {
            // ব্যালেন্স কাটা
            $new_balance = $current_balance - $amount;
            $update = $db->prepare("UPDATE users SET main_balance = ? WHERE id = ?");
            $update->execute([$new_balance, $user_id]);

            // ট্রেড ইনসার্ট করা
            $stmt = $db->prepare("INSERT INTO trades (user_id, amount, direction, duration, status) VALUES (?, ?, ?, ?, 'Pending')");
            $stmt->execute([$user_id, $amount, $direction, $duration]);
            
            $message = "<div class='text-xs bg-green-500/10 text-green-500 p-3 rounded-xl border border-green-500/20 mb-4 text-center font-bold'>Trade Placed: ".strtoupper($direction)."!</div>";
            $current_balance = $new_balance; 
        } else {
            $message = "<div class='text-xs bg-red-500/10 text-red-500 p-3 rounded-xl border border-red-500/20 mb-4 text-center font-bold'>Insufficient Balance!</div>";
        }
    } else {
        $message = "<div class='text-xs bg-yellow-500/10 text-yellow-500 p-3 rounded-xl border border-yellow-500/20 mb-4 text-center font-bold'>Minimum investment is $1.0</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Ziber - Professional Trading</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;600;700;800&display=swap');
        body { background: #0b0e11; color: #ffffff; font-family: 'Plus Jakarta Sans', sans-serif; padding-bottom: 100px; }
        .glass-panel { background: #161a21; border: 1px solid rgba(255, 255, 255, 0.05); }
        .btn-up { background-color: #2fb474; color: black; transition: 0.2s; }
        .btn-up:hover { background-color: #269a63; }
        .btn-down { background-color: #f6465d; color: white; transition: 0.2s; }
        .btn-down:hover { background-color: #d63d50; }
        .trading-input { background: #0b0e11; border: 1px solid #2d3648; transition: 0.3s; color: white; }
        .trading-input:focus { border-color: #2fb474; outline: none; }
        .status-badge { padding: 2px 8px; border-radius: 6px; font-size: 9px; font-weight: 800; text-transform: uppercase; }
        ::-webkit-scrollbar { display: none; }
    </style>
</head>
<body class="p-4">

    <div class="flex justify-between items-center mb-6">
        <div class="flex items-center space-x-2">
            <div class="w-7 h-7 bg-[#2fb474] rounded-lg flex items-center justify-center">
                <i class="fas fa-bolt text-black text-xs"></i>
            </div>
            <span class="text-xl font-extrabold tracking-tighter uppercase">ZIBER</span>
        </div>
        <div class="text-right">
            <p class="text-[9px] text-gray-500 font-bold uppercase tracking-widest">Available Balance</p>
            <p class="text-lg font-black text-[#2fb474]">$<?php echo number_format($current_balance, 2); ?></p>
        </div>
    </div>

    <div class="glass-panel rounded-2xl overflow-hidden h-[320px] mb-6 relative border border-white/5 shadow-2xl">
        <iframe src="https://s.tradingview.com/widgetembed/?symbol=BINANCE:BTCUSDT&interval=1&theme=dark" class="w-full h-full border-0"></iframe>
    </div>

    <div class="glass-panel p-5 rounded-3xl mb-8">
        <?php echo $message; ?>
        
        <form method="POST" class="grid grid-cols-2 gap-4">
            <div class="col-span-2">
                <label class="text-[10px] text-gray-500 uppercase font-black tracking-widest mb-2 block">Investment Amount</label>
                <input type="number" name="trade_amount" min="1" step="0.01" value="1.00" required 
                       class="trading-input w-full p-4 rounded-xl text-xl font-bold">
            </div>

            <div>
                <label class="text-[10px] text-gray-500 uppercase font-black tracking-widest mb-2 block">Time Frame</label>
                <select name="duration" class="trading-input w-full p-4 rounded-xl text-xs font-bold">
                    <option value="1m">1 MINUTE</option>
                    <option value="2m">2 MINUTES</option>
                    <option value="5m">5 MINUTES</option>
                </select>
            </div>

            <div class="flex flex-col justify-end">
                <div class="bg-black/40 p-3 rounded-xl border border-white/5 text-center">
                    <p class="text-[8px] text-gray-400 uppercase font-bold">Profit Rate</p>
                    <p class="text-sm font-black text-green-500">+92%</p>
                </div>
            </div>

            <div class="col-span-2 grid grid-cols-2 gap-4 pt-2">
                <button type="submit" name="direction" value="up" class="btn-up py-4 rounded-2xl font-black text-sm uppercase tracking-widest active:scale-95 shadow-lg">
                    <i class="fas fa-caret-up mr-2 text-lg"></i> UP
                </button>
                <button type="submit" name="direction" value="down" class="btn-down py-4 rounded-2xl font-black text-sm uppercase tracking-widest active:scale-95 shadow-lg">
                    <i class="fas fa-caret-down mr-2 text-lg"></i> DOWN
                </button>
                <input type="hidden" name="place_trade" value="1">
            </div>
        </form>
    </div>

    <div class="mb-6">
        <div class="flex justify-between items-center mb-4">
            <h3 class="text-[10px] text-gray-500 uppercase font-black tracking-widest">Active & Recent Trades</h3>
            <span class="text-[9px] bg-white/5 px-2 py-1 rounded text-gray-400">Live Updates</span>
        </div>
        
        <div class="space-y-3">
            <?php
            $stmt = $db->prepare("SELECT * FROM trades WHERE user_id = ? ORDER BY id DESC LIMIT 6");
            $stmt->execute([$user_id]);
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)):
                $is_up = ($row['direction'] == 'up');
                $status = $row['status'];
                
                // ডাইনামিক স্টাইল
                $bg_color = $is_up ? 'bg-green-500/10' : 'bg-red-500/10';
                $icon_color = $is_up ? 'text-green-500' : 'text-red-500';
                $status_class = ($status == 'Pending') ? 'bg-yellow-500/20 text-yellow-500' : 
                               (($status == 'Win') ? 'bg-green-500/20 text-green-500' : 'bg-red-500/20 text-red-500');
            ?>
            <div class="glass-panel p-4 rounded-2xl flex justify-between items-center border-l-4 <?php echo $is_up ? 'border-l-green-500' : 'border-l-red-500'; ?>">
                <div class="flex items-center space-x-3">
                    <div class="w-10 h-10 rounded-xl flex items-center justify-center <?php echo $bg_color . ' ' . $icon_color; ?>">
                        <i class="fas <?php echo $is_up ? 'fa-arrow-up' : 'fa-arrow-down'; ?> text-sm"></i>
                    </div>
                    <div>
                        <p class="text-[11px] font-extrabold uppercase leading-none mb-1">
                            <?php echo $is_up ? 'Buy / Call' : 'Sell / Put'; ?>
                        </p>
                        <p class="text-[9px] text-gray-500 font-bold"><?php echo date('H:i:s', strtotime($row['created_at'])); ?> • <?php echo $row['duration']; ?></p>
                    </div>
                </div>
                <div class="text-right">
                    <p class="text-sm font-black mb-1">$<?php echo number_format($row['amount'], 2); ?></p>
                    <span class="status-badge <?php echo $status_class; ?>"><?php echo $status; ?></span>
                </div>
            </div>
            <?php endwhile; ?>
            
            <?php if ($stmt->rowCount() == 0): ?>
                <div class="text-center py-8 opacity-30">
                    <i class="fas fa-folder-open text-3xl mb-2"></i>
                    <p class="text-[10px] font-bold uppercase">No trades found</p>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <?php if(file_exists('navbar.php')) include 'navbar.php'; ?>

</body>
</html>