<?php
session_start();
require '../includes/db.php';

// ১. লগইন চেক
if (!isset($_SESSION['user_id'])) { 
    header("Location: ../login.php"); 
    exit(); 
}

$uid = $_SESSION['user_id'];
$settings = $conn->query("SELECT * FROM settings WHERE id=1")->fetch_assoc();
$primary = $settings['theme_primary'] ?? '#034C44'; // ডাটাবেজ থেকে কালার

// ২. উইথড্র রেকর্ড ফেচ করা
// transactions_fake টেবিল থেকে type='withdraw' গুলো আনা হচ্ছে
$withdraws = $conn->query("SELECT * FROM transactions_fake WHERE user_id=$uid AND type='withdraw' ORDER BY created_at DESC LIMIT 50");
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>উত্তোলনের রেকর্ড</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>
        body { font-family: 'Segoe UI', sans-serif; background-color: #f3f4f6; }
    </style>
</head>
<body class="pb-20">

    <div class="p-4 flex items-center text-white sticky top-0 z-50 shadow-md" style="background-color: <?php echo $primary; ?>;">
        <a href="account.php" class="mr-4 text-xl"><i class="fas fa-chevron-left"></i></a>
        <h1 class="text-lg font-bold">উত্তোলনের রেকর্ড</h1>
    </div>

    <div class="flex justify-around bg-white p-3 shadow-sm mb-2 text-sm text-gray-600 font-medium">
        <button class="border-b-2 border-<?php echo $primary; ?> text-black pb-1">সব</button>
        <button class="hover:text-black">বিচারাধীন</button>
        <button class="hover:text-black">সফল</button>
    </div>

    <div class="bg-white divide-y divide-gray-100">
        <?php if($withdraws->num_rows > 0): ?>
            <?php while($row = $withdraws->fetch_assoc()): ?>
            <div class="p-4 hover:bg-gray-50 transition">
                
                <div class="flex justify-between items-start mb-2">
                    <div class="flex items-center gap-3">
                        <div class="w-10 h-10 rounded-full bg-red-100 text-red-600 flex items-center justify-center text-lg">
                            <i class="fas fa-money-bill-wave"></i>
                        </div>
                        <div>
                            <h3 class="font-bold text-gray-800 text-sm capitalize"><?php echo $row['method']; ?></h3>
                            <p class="text-xs text-gray-400 mt-0.5">
                                <?php echo !empty($row['wallet_number']) ? 'AC: '.$row['wallet_number'] : 'উত্তোলন'; ?>
                            </p>
                        </div>
                    </div>
                    
                    <div class="text-right">
                        <span class="block font-bold text-lg text-gray-800">৳<?php echo number_format($row['amount'], 2); ?></span>
                        
                        <?php if($row['status'] == 'approved'): ?>
                            <span class="text-[10px] bg-green-100 text-green-700 px-2 py-0.5 rounded font-bold">সফল</span>
                        <?php elseif($row['status'] == 'rejected'): ?>
                            <span class="text-[10px] bg-red-100 text-red-700 px-2 py-0.5 rounded font-bold">বাতিল</span>
                        <?php else: ?>
                            <span class="text-[10px] bg-yellow-100 text-yellow-700 px-2 py-0.5 rounded font-bold">পেন্ডিং</span>
                        <?php endif; ?>
                    </div>
                </div>
                
                <div class="flex justify-between items-center text-xs text-gray-400 mt-2 border-t border-dashed pt-2">
                    <span><i class="far fa-clock"></i> <?php echo date('d M, h:i A', strtotime($row['created_at'])); ?></span>
                    
                    <?php if(!empty($row['transaction_id'])): ?>
                    <span class="flex items-center gap-1 bg-gray-100 px-2 py-0.5 rounded select-all">
                        TRX: <?php echo $row['transaction_id']; ?>
                        <i class="far fa-copy cursor-pointer" onclick="alert('কপি করা হয়েছে!')"></i>
                    </span>
                    <?php endif; ?>
                </div>

            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="flex flex-col items-center justify-center py-20 text-gray-400">
                <div class="w-16 h-16 bg-gray-200 rounded-full flex items-center justify-center mb-3">
                    <i class="fas fa-file-invoice-dollar text-2xl text-gray-400"></i>
                </div>
                <p>কোনো উত্তোলনের রেকর্ড পাওয়া যায়নি</p>
            </div>
        <?php endif; ?>
    </div>

</body>
</html>