<?php
session_start();

if (!isset($_SESSION['user'])) {
    // Redirect to login page if not logged in
    header('Location: ./login');
    exit();
}

$user = $_SESSION['user'];
$email = $user['email_address'];


?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Account</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script id="chatway" async="true" src="https://cdn.chatway.app/widget.js?id=1Pv8KHGhVszV"></script>
</head>
<body>
    <div id="depositModal" class="modal">
    <div class="modal-content">
        <span class="close">&times;</span>
        <h2>DEPOSIT</h2>
        <img src="../images/btc_deposit_code.jpg" alt="Square Image" class="square-image">
        <div class="copy-section">
        <p id="textToCopy">bc1qx9qvk8ngc5rg07lk6tvy8lw8awg9rjnkr5r6an</p>
        <button id="copyButton">Copy</button>
        </div>
        <button id="closeModal">Sent</button>
    </div>
    </div>
    <div class="container">
        
        <main class="main-content">
            <header class="header">
                <div class="logo_img">
                    <a href="https://your-link.com">
                    <img src="../images/logo_white.png" alt="Trader Viewer Logo">
                    </a>
                </div>
                <div class="user-actions">
                    <p>Welcome <?php echo htmlspecialchars($user['status']); ?></p>
                    <i class="user-<?php echo htmlspecialchars($user['status']); ?>"></i>
                </div>

                <div class="user-actions">
                    <p>Welcome <?php echo htmlspecialchars($user['first_name']); ?> - </p>
                    <a href="./login/logout.php"><p> (Logout) </p> </a>
                    <i class="bell-icon"></i>
                </div>
            </header>
            <section class="dashboard">
                <div class="cards">
                    <div class="card">
                        <div class="card-header">
                            <span>Profit</span>
                            <span>+<?php echo htmlspecialchars($user['profit_percentage']); ?>%</span>
                        </div>
                        <div class="card-body">$<?php echo htmlspecialchars($user['profit']); ?></div>
                    </div>
                    <div class="card">
                        <div class="card-header">
                            <span>Running Trade</span>
                        </div>
                        <div class="card-body">$<?php echo htmlspecialchars($user['running_trade']); ?></div>
                    </div>
                    <div class="card">
                        <div class="card-header">
                            <span>Bonus</span>
                        </div>
                        <div class="card-body">$<?php echo htmlspecialchars($user['bonus']); ?></div>
                    </div>

                    <div class="card">
                        <div class="card-header">
                            <span>Total Deposit</span>
                        </div>
                        <div class="card-body">$<?php echo htmlspecialchars($user['total_deposit']); ?></div>
                    </div>
                </div>
                
                <div class="chart">
                    <h2>Live Chart</h2>
                    <!-- TradingView Widget BEGIN -->
                    <script type="text/javascript" src="https://d33t3vvu2t2yu5.cloudfront.net/tv.js"></script>
                    <script type="text/javascript">
                    new TradingView.widget({
                      "autosize": true,
                      "symbol": "COINBASE:BTCUSD",
                      "interval": "D",
                      "timezone": "Etc/UTC",
                      "theme": "Black",
                      "style": "2",
                      "locale": "en",
                      "toolbar_bg": "rgba(0, 0, 0, 1)",
                      "hide_top_toolbar": true,
                      "save_image": false,
                      "hideideas": true
                    });
                    </script>
                    <!-- TradingView Widget END -->
                    </div>
                    <div class="exchange-total">
                        
                        <div class="exchange">
                            <h2>Deposit</h2>
                            <form>
                                <div class="form-group">
                                    <label for="currency">Currency</label>
                                    <select id="currency">
                                        <option value="btc">Bitcoin</option>
                                    </select>
                                </div>
                    
                                <div class="form-group">
                                    <label for="Network">Network</label>
                                    <select id="Network">
                                        <option value="BTC">BTC</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label for="withdraw-amount">Amount</label>
                                    <input type="text" id="withdraw-amount" placeholder="Enter deposit Amount">
                                </div>
                                <button type="submit">Deposit</button>
                            </form>
                    </div>
                    <div class="exchange">
                        <h2>Withdraw</h2>
                        <form>
                            <div class="form-group">
                                <label for="currency">Currency</label>
                                <select id="currency">
                                    <option value="btc">Bitcoin</option>
                                </select>
                            </div>
                
                            <div class="form-group">
                                <label for="amount">Enter Your Wallet Address</label>
                                <input type="text" id="amount" placeholder="Enter Wallet Address">
                            </div>
                            <div class="form-group">
                                <label for="withdraw-amount">Amount</label>
                                <input type="text" id="withdraw-amount" placeholder="Enter Withdrawal Amount">
                            </div>
                            <button type="submit">Withdraw</button>
                        </form>
                    </div>
                </div>

                <div class="transactions">

                
                    <h2>Transactions</h2>

                    <div class="container">
                
                <table id="dataTable" class="table table-bordered">
                    <!-- Table Headings -->
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>From/To</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Amount</th>
                            
                        </tr>
                    </thead>
                    <!-- Table Body -->
                    <tbody>
                        <?php
                            // Connect to MySQL
                            include_once '../secure/dbconn.php';
                                    $sql = "SELECT * FROM `transactions` WHERE user_email = '$email' ORDER BY id DESC";
                            
                            // Fetch data with pagination and filter
                            $result = mysqli_query($conn, $sql);

                            if (mysqli_num_rows($result) > 0) {
                                while($row = mysqli_fetch_assoc($result)) {
                                    echo "<tr>";
                                    // Add checkbox column
                                    echo "<td>" . $row['name'] . "</td>";
                                    echo "<td>" . $row['from'] . "</td>";
                                    echo "<td>" . $row['date'] . "</td>";
                                    echo "<td>" . $row['status'] . "</td>";
                                    echo "<td>" . $row['amount'] . "</td>";
                                    echo "</tr>";
                                }
                            } else {
                                echo "<tr><td colspan='18'>No data found</td></tr>";
                            }
                            // Close connection
                            mysqli_close($conn);
                        ?>
                    </tbody>
                </table>
            </div>
            
            </div>
                    
                </div>
            </section>
        </main>
    </div>
    <script src="script.js"></script>
</body>
</html>
