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

require_once '../common/config.php';

// ২. ফাইল ডিলিট করার লজিক
if (isset($_GET['delete'])) {
    $id = intval($_GET['delete']);
    
    $stmt = $conn->prepare("SELECT file_name FROM files WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $res = $stmt->get_result();
    $file_data = $res->fetch_assoc();

    if ($file_data) {
        $file_path = "../uploads/" . $file_data['file_name'];
        if (file_exists($file_path)) {
            unlink($file_path);
        }
        
        $del_stmt = $conn->prepare("DELETE FROM files WHERE id = ?");
        $del_stmt->bind_param("i", $id);
        $del_stmt->execute();
        
        header("Location: index.php?msg=deleted");
        exit();
    }
}

// এডমিন ফোল্ডারের নিজস্ব হেডার ইনক্লুড করা হলো
include 'header.php';

$files = $conn->query("SELECT * FROM files ORDER BY id DESC");
?>

<!-- অ্যাডমিন প্যানেল কন্টেইনার -->
<div class="max-w-7xl mx-auto px-4 py-10 sm:px-6 lg:px-8">

    <!-- ড্যাশবোর্ড হেডার -->
    <div class="flex flex-col xl:flex-row justify-between items-start xl:items-center mb-10 gap-6">
        <div>
            <h2 class="text-2xl md:text-3xl font-bold logo-font tracking-wider uppercase">
                MANAGE <span class="text-cyan-400 glow-text">ASSETS</span>
            </h2>
            <p class="text-gray-500 text-xs font-bold uppercase tracking-[3px] mt-1">Administrative Control Center</p>
        </div>
        
        <!-- অ্যাকশন বাটন গ্রুপ -->
        <div class="flex flex-wrap gap-4 w-full xl:w-auto">
            
            <a href="users.php" class="flex-1 sm:flex-none bg-blue-500/10 border border-blue-500/30 px-6 py-3 rounded-xl flex items-center justify-center space-x-3 text-blue-400 font-bold uppercase tracking-widest text-sm transition-all hover:bg-blue-500 hover:text-black shadow-[0_0_20px_rgba(59,130,246,0.2)]">
                <i class="fa-solid fa-users-gear text-lg"></i>
                <span>Manage Users</span>
            </a>

            <a href="manage-payments.php" class="flex-1 sm:flex-none bg-yellow-500/10 border border-yellow-500/30 px-6 py-3 rounded-xl flex items-center justify-center space-x-3 text-yellow-500 font-bold uppercase tracking-widest text-sm transition-all hover:bg-yellow-500 hover:text-black shadow-[0_0_20px_rgba(234,179,8,0.2)]">
                <i class="fa-solid fa-receipt text-lg"></i>
                <span>Manage Payments</span>
            </a>
            
            <a href="upload.php" class="flex-1 sm:flex-none bg-cyan-600 px-6 py-3 rounded-xl flex items-center justify-center space-x-3 text-black font-bold uppercase tracking-widest text-sm transition-all active:scale-95 shadow-[0_0_20px_rgba(34,211,238,0.3)] hover:bg-white">
                <i class="fa-solid fa-plus-circle text-lg"></i>
                <span>Upload Asset</span>
            </a>
        </div>
    </div>

    <!-- নোটিফিকেশন মেসেজ -->
    <?php if(isset($_GET['msg']) && $_GET['msg'] == 'deleted'): ?>
        <div class="bg-red-500/10 border border-red-500/50 text-red-500 p-4 rounded-xl mb-8 flex items-center shadow-[0_0_15px_rgba(239,68,68,0.1)] animate-pulse">
            <i class="fa-solid fa-trash-can-check mr-3 text-xl"></i>
            <span class="text-sm font-bold uppercase tracking-wider">Protocol Executed: File Deleted Successfully!</span>
        </div>
    <?php endif; ?>

    <!-- ফাইল লিস্ট গ্রিড -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        <?php if($files->num_rows > 0): ?>
            <?php while($row = $files->fetch_assoc()): 
                $expiry = strtotime($row['expiry_time']);
                $is_expired = (time() > $expiry);
                
                $price = isset($row['price']) ? $row['price'] : 0;
                $discount = isset($row['discount']) ? $row['discount'] : 0;
                $final_price = $price - $discount;
            ?>
            
            <div class="cyber-card p-6 flex flex-col justify-between group transition-all hover:-translate-y-1">
                
                <div class="flex justify-between items-start mb-6">
                    <div class="flex items-center space-x-4">
                        <div class="w-14 h-14 <?php echo $is_expired ? 'bg-red-500/10 text-red-500 border-red-500/30' : 'bg-cyan-500/10 text-cyan-400 border-cyan-500/30'; ?> border rounded-2xl flex items-center justify-center shadow-inner">
                            <i class="fa-solid <?php echo $is_expired ? 'fa-file-circle-xmark' : 'fa-file-shield'; ?> text-2xl"></i>
                        </div>
                        <div class="min-w-0">
                            <h3 class="font-bold text-white text-base truncate pr-2 group-hover:text-cyan-400 transition-colors" title="<?php echo $row['file_title']; ?>">
                                <?php echo $row['file_title']; ?>
                            </h3>
                            <p class="text-[10px] text-gray-500 font-black uppercase tracking-widest mt-1 italic">Reference ID: #<?php echo $row['id']; ?></p>
                        </div>
                    </div>
                    
                    <div class="flex space-x-1">
                        <a href="edit.php?id=<?php echo $row['id']; ?>" class="w-9 h-9 flex items-center justify-center bg-blue-500/10 text-blue-400 border border-blue-500/20 rounded-lg hover:bg-blue-500 hover:text-white transition-all shadow-sm">
                            <i class="fa-solid fa-pen-to-square text-xs"></i>
                        </a>
                        <a href="?delete=<?php echo $row['id']; ?>" onclick="return confirm('WARNING: Are you sure you want to permanently delete this asset?')" class="w-9 h-9 flex items-center justify-center bg-red-500/10 text-red-400 border border-red-500/20 rounded-lg hover:bg-red-500 hover:text-white transition-all shadow-sm">
                            <i class="fa-solid fa-trash-can text-xs"></i>
                        </a>
                    </div>
                </div>

                <div class="grid grid-cols-2 gap-y-4 py-4 border-t border-white/5 bg-black/20 rounded-xl px-4">
                    <div class="space-y-1">
                        <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Master Key</span>
                        <div class="flex items-center text-xs font-mono font-bold <?php echo $row['password'] ? 'text-cyan-400' : 'text-gray-600'; ?>">
                            <i class="fa-solid fa-key mr-2 opacity-40"></i>
                            <?php echo $row['password'] ?: 'NO_KEY'; ?>
                        </div>
                    </div>
                    <div class="space-y-1 border-l border-white/5 pl-4">
                        <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Sync Status</span>
                        <div class="flex items-center text-xs font-bold <?php echo $is_expired ? 'text-red-500' : 'text-green-500'; ?>">
                            <i class="fa-solid fa-clock-rotate-left mr-2 opacity-40"></i>
                            <?php echo $is_expired ? 'OFFLINE' : 'ACTIVE'; ?>
                        </div>
                    </div>
                    <div class="space-y-1 border-t border-white/5 pt-3">
                        <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Asset Value</span>
                        <div class="flex items-center text-xs font-bold text-white">
                            <i class="fa-solid fa-bangladeshi-taka-sign mr-2 opacity-40 text-cyan-400"></i>
                            <?php echo $final_price > 0 ? '৳' . number_format($final_price, 0) : 'FREE'; ?>
                        </div>
                    </div>
                    <div class="space-y-1 border-t border-l border-white/5 pt-3 pl-4">
                        <span class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Discount</span>
                        <div class="flex items-center text-xs font-bold text-red-400">
                            <i class="fa-solid fa-percent mr-2 opacity-40"></i>
                            <?php echo $discount > 0 ? '৳' . $discount : '0'; ?>
                        </div>
                    </div>
                </div>

                <div class="mt-4 flex items-center justify-between">
                    <div class="text-[10px] font-bold text-gray-500">
                        EXP: <?php echo date('d M, Y', $expiry); ?>
                    </div>
                </div>

            </div>
            <?php endwhile; ?>
        <?php else: ?>
            <div class="col-span-full text-center py-24 cyber-card border-dashed border-cyan-500/20">
                <i class="fa-solid fa-folder-open text-7xl animate-bounce"></i>
                <p class="text-gray-500 font-bold uppercase tracking-[5px]">No Assets Detected</p>
            </div>
        <?php endif; ?>
    </div>

    <!-- লগআউট বাটন -->
    <div class="mt-16 mb-10 text-center">
        <a href="logout.php" class="inline-flex items-center px-6 py-2 border border-red-500/30 rounded-full text-[10px] font-bold text-gray-500 hover:text-red-500 hover:bg-red-500/5 transition-all uppercase tracking-[4px]">
            <i class="fa-solid fa-power-off mr-2 text-xs"></i> Terminate Admin Session
        </a>
    </div>
</div>

<style>
    .cyber-card { background: rgba(15, 23, 42, 0.6); backdrop-filter: blur(12px); border: 1px solid rgba(255, 255, 255, 0.05); border-radius: 1.5rem; }
</style>

<?php include 'footer.php'; ?>