<?php
session_start();
require_once 'config.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$userId = $_SESSION['user_id'];
global $pdo;

// Auto-add columns if missing (Safety check)
try { $pdo->exec("ALTER TABLE `deposit_requests` ADD `channel_id` INT(11) NOT NULL DEFAULT '0'"); } catch (Exception $e) {}
try { $pdo->exec("ALTER TABLE `deposit_requests` ADD `actual_pay_amount` DECIMAL(10,2) NOT NULL DEFAULT '0.00'"); } catch (Exception $e) {}
try { $pdo->exec("ALTER TABLE `deposit_requests` ADD `order_id` VARCHAR(50) NULL AFTER `id`"); } catch(Exception $e) {}

// Fetch History
$history = [];
try {
    $stmt = $pdo->prepare("
        SELECT d.*, c.channel_name, m.name as method_name 
        FROM deposit_requests d 
        LEFT JOIN payment_channels c ON d.channel_id = c.id
        LEFT JOIN payment_methods m ON c.method_id = m.id
        WHERE d.user_id = ? 
        ORDER BY d.created_at DESC
    ");
    $stmt->execute([$userId]);
    $history = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    $error = $e->getMessage();
}
?>
<!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>Deposit History</title>
    <style>
        @font-face { font-family: 'Bahnschrift'; src: url('https://91appt.com/assets/woff2/bahnschrift-610d57ac.woff2') format('woff2'); font-display: swap;}
        :root { --bg-color: #f7f8fa; --main-red: #f3595b; }
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: -apple-system, sans-serif; -webkit-tap-highlight-color: transparent;}
        body { background: var(--bg-color); color: #333; max-width: 480px; margin: 0 auto; padding-top: 100px; }
        .bahnschrift { font-family: 'Bahnschrift', sans-serif !important; }

        /* HEADER */
        .header-wrap { position: fixed; top: 0; left: 50%; transform: translateX(-50%); width: 100%; max-width: 480px; z-index: 99; background:#fff;}
        .header { height: 46px; display: flex; align-items: center; justify-content: center; box-shadow: 0 1px 3px rgba(0,0,0,0.02);}
        .back-icon { position: absolute; left: 15px; font-size: 20px; color: #666; cursor: pointer; text-decoration: none;}
        .title { font-size: 16px; font-weight: 500; }

        /* FILTER TABS */
        .filter-bar { display: flex; gap: 10px; padding: 10px 15px; overflow-x: auto; border-bottom: 1px solid #f5f5f5;}
        .filter-bar::-webkit-scrollbar { display: none; }
        .f-tab { background: #fff; border: 1px solid #eee; color: #666; padding: 6px 15px; border-radius: 6px; font-size: 12px; white-space: nowrap; display:flex; align-items:center; gap:5px; box-shadow: 0 2px 4px rgba(0,0,0,0.02);}
        .f-tab.active { background: #ffeded; color: var(--main-red); border-color: #ffcdcd; font-weight:bold;}
        .f-tab.active svg { fill: var(--main-red); }
        
        .date-filter { display: flex; justify-content: space-between; padding: 10px 15px; font-size: 13px; color: #666; background: #fff; box-shadow: 0 2px 5px rgba(0,0,0,0.02);}

        /* LIST */
        .history-list { padding: 15px; }
        .card { background: #fff; border-radius: 10px; padding: 15px; margin-bottom: 15px; box-shadow: 0 2px 8px rgba(0,0,0,0.02); }
        
        .card-top { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px dashed #eee; padding-bottom: 10px; margin-bottom: 10px; }
        .tag-deposit { background: #3AC967; color: white; padding: 3px 10px; border-radius: 4px; font-size: 11px; font-weight: bold;}
        
        .status { font-size: 13px; font-weight: bold; }
        .status.pending { color: #ed6a0c; }
        .status.success { color: #07c160; }
        .status.failed { color: #ee0a24; display:flex; align-items:center; gap:4px;}

        .detail-row { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 12px; color: #999; }
        .detail-val { color: #333; font-weight: 500; }
        .amt-val { font-size: 15px; color: var(--main-red); font-weight: bold; }

        .btn-review { width: 100%; background: #fffbe8; color: #ed6a0c; border: 1px solid #ffd01e; padding: 10px; border-radius: 20px; margin-top: 10px; font-weight: bold; font-size: 13px; text-align: center; }

        .empty-box { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 50px 0; color: #999; font-size: 13px; }
    </style>
</head>
<body>

    <div class="header-wrap">
        <div class="header">
            <a href="deposit.php" class="back-icon">❮</a>
            <div class="title">Deposit history</div>
        </div>
        <div class="filter-bar">
            <div class="f-tab active">
                <svg width="14" height="14" viewBox="0 0 24 24"><path d="M3 3h8v8H3V3zm0 10h8v8H3v-8zm10-10h8v8h-8V3zm0 10h8v8h-8v-8z"/></svg> All
            </div>
            <div class="f-tab">ArUpi Pay</div>
            <div class="f-tab">Innate UPI-QR</div>
            <div class="f-tab">USDT</div>
        </div>
        <div class="date-filter">
            <span>All</span>
            <span style="display:flex; align-items:center; gap:5px;">Choose a date <svg width="12" height="12" fill="#999" viewBox="0 0 24 24"><path d="M7 10l5 5 5-5z"/></svg></span>
        </div>
    </div>

    <div class="history-list">
        <?php if(empty($history)): ?>
            <div class="empty-box">
                <svg width="100" height="80" viewBox="0 0 100 80" fill="#ebedf0"><path d="M20 20h60v40H20z"/><path d="M10 30h80v10H10z" fill="#dcdee0"/><path d="M30 45h40v4H30zm0 10h20v4H30z" fill="#c8c9cc"/></svg>
                No data
            </div>
        <?php else: ?>
            <?php foreach($history as $row): 
                // Determine Status Logic
                $statusClass = ''; $statusText = '';
                if ($row['status'] == 'pending' || $row['status'] == 0) {
                    $statusClass = 'pending'; $statusText = 'Pending Review'; // Changed from 'To Be Paid'
                } elseif ($row['status'] == 'approved' || $row['status'] == 1) {
                    $statusClass = 'success'; $statusText = 'Success';
                } else {
                    $statusClass = 'failed'; $statusText = 'Failed';
                }
                
                // Fallback order number if missing
                $orderNum = !empty($row['order_id']) ? $row['order_id'] : "RC" . date('YmdHis', strtotime($row['created_at'])) . str_pad($row['id'], 4, '0', STR_PAD_LEFT);
            ?>
            <div class="card">
                <div class="card-top">
                    <span class="tag-deposit">Deposit</span>
                    <span class="status <?= $statusClass ?>">
                        <?= $statusText ?>
                        <?php if($statusClass == 'failed'): ?>
                            <svg width="16" height="16" fill="#ee0a24" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm3.5-9c.83 0 1.5-.67 1.5-1.5S16.33 8 15.5 8 14 8.67 14 9.5s.67 1.5 1.5 1.5zm-7 0c.83 0 1.5-.67 1.5-1.5S9.33 8 8.5 8 7 8.67 7 9.5 7.67 11 8.5 11zm3.5 6.5c2.33 0 4.31-1.46 5.11-3.5H6.89c.8 2.04 2.78 3.5 5.11 3.5z"/></svg>
                        <?php endif; ?>
                    </span>
                </div>
                <div class="detail-row">
                    <span>Balance</span>
                    <span class="detail-val amt-val bahnschrift">₹<?= number_format($row['amount'], 2) ?></span>
                </div>
                <div class="detail-row">
                    <span>Type</span>
                    <span class="detail-val"><?= htmlspecialchars($row['channel_name'] ?? 'Gateway') ?></span>
                </div>
                <div class="detail-row">
                    <span>Time</span>
                    <span class="detail-val bahnschrift"><?= date('Y-m-d H:i:s', strtotime($row['created_at'])) ?></span>
                </div>
                <div class="detail-row">
                    <span>Order number</span>
                    <span class="detail-val bahnschrift" style="font-size: 10px; letter-spacing: 0.5px;"><?= $orderNum ?></span>
                </div>
                <div class="detail-row">
                    <span>UTR / Ref No.</span>
                    <span class="detail-val bahnschrift" style="font-size: 12px; color: #1989fa;"><?= htmlspecialchars($row['utr_number']) ?></span>
                </div>

                <?php if($statusClass == 'pending'): ?>
                    <div class="btn-review">Order is under review by Admin</div>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

</body>
</html>