<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) { header("Location: login.php"); exit(); }
require_once '../common/config.php';

if (isset($_POST['upload'])) {
    $title = $_POST['title'];
    $password = $_POST['password'];
    $expiry = $_POST['expiry'];
    $yt_url = $_POST['yt_url'];
    $price = $_POST['price']; // নতুন
    $discount = $_POST['discount']; // নতুন

    // ফাইল হ্যান্ডলিং
    $target_dir = "../uploads/";
    if (!file_exists($target_dir)) { mkdir($target_dir, 0777, true); }
    
    $file_name = time() . "_" . basename($_FILES["file"]["name"]);
    $target_file = $target_dir . $file_name;

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        // SQL কোড আপডেট করা হয়েছে (price এবং discount কলাম সহ)
        $stmt = $conn->prepare("INSERT INTO files (file_title, file_name, password, expiry_time, youtube_url, price, discount) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("sssssdd", $title, $file_name, $password, $expiry, $yt_url, $price, $discount);
        $stmt->execute();
        $success = "Protocol Successful: Asset deployed to storage!";
    } else {
        $error = "Critical Error: Data transmission failed!";
    }
}

include '../admin/header.php';
?>

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

    <!-- ব্যাক বাটন -->
    <div class="mb-8">
        <a href="index.php" class="inline-flex items-center text-cyan-400 font-bold uppercase tracking-[2px] text-xs hover:text-white transition-colors group">
            <i class="fa-solid fa-arrow-left mr-2 group-hover:-translate-x-1 transition-transform"></i> 
            Return to Command Center
        </a>
    </div>

    <!-- মেইন আপলোড কার্ড -->
    <div class="cyber-card p-6 md:p-10 relative overflow-hidden">
        
        <div class="absolute -top-4 -right-4 p-4 opacity-5">
            <i class="fa-solid fa-cloud-arrow-up text-9xl text-cyan-400"></i>
        </div>

        <div class="mb-10">
            <h2 class="text-2xl md:text-3xl font-bold logo-font tracking-wider uppercase">
                INITIATE <span class="text-cyan-400 glow-text">UPLOADING</span>
            </h2>
            <p class="text-gray-500 text-[10px] font-bold uppercase tracking-[3px] mt-1">Status: System Online / Authorized Access</p>
        </div>

        <?php if(isset($success)): ?>
            <div class="bg-cyan-500/10 border border-cyan-500/50 text-cyan-400 p-4 rounded-xl mb-6 text-sm font-bold uppercase tracking-wider animate-pulse text-center">
                <i class="fa-solid fa-circle-check mr-2"></i> <?php echo $success; ?>
            </div>
        <?php endif; ?>

        <?php if(isset($error)): ?>
            <div class="bg-red-500/10 border border-red-500/50 text-red-500 p-4 rounded-xl mb-6 text-sm font-bold uppercase tracking-wider text-center">
                <i class="fa-solid fa-triangle-exclamation mr-2"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form action="" method="POST" enctype="multipart/form-data" class="space-y-6">
            
            <div class="space-y-2">
                <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Asset Name / Title *</label>
                <div class="relative">
                    <i class="fa-solid fa-signature absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/50"></i>
                    <input type="text" name="title" required placeholder="Enter designation..." 
                           class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 focus:ring-1 focus:ring-cyan-500 outline-none transition-all font-medium">
                </div>
            </div>

            <div class="space-y-2">
                <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Core Object Selection *</label>
                <div class="relative group">
                    <input type="file" name="file" required 
                           class="w-full text-sm text-gray-400 file:mr-4 file:py-3 file:px-6 file:rounded-xl file:border-0 file:text-[10px] file:font-black file:uppercase file:tracking-widest file:bg-cyan-500 file:text-black hover:file:bg-cyan-400 transition-all cursor-pointer bg-slate-950/50 border border-white/10 rounded-xl pr-4">
                </div>
            </div>

            <!-- প্রাইস এবং ডিসকাউন্ট গ্রিড (নতুন) -->
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="space-y-2">
                    <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Market Price (BDT) *</label>
                    <div class="relative">
                        <i class="fa-solid fa-tags absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/50"></i>
                        <input type="number" step="0.01" name="price" value="0.00" required 
                               class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all font-mono">
                    </div>
                </div>

                <div class="space-y-2">
                    <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">System Discount (BDT)</label>
                    <div class="relative">
                        <i class="fa-solid fa-percent absolute left-4 top-1/2 -translate-y-1/2 text-red-500/50"></i>
                        <input type="number" step="0.01" name="discount" value="0.00" required 
                               class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all font-mono">
                    </div>
                </div>
            </div>

            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="space-y-2">
                    <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Security Key (Optional)</label>
                    <div class="relative">
                        <i class="fa-solid fa-key absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/50"></i>
                        <input type="text" name="password" placeholder="Lock asset..." 
                               class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all font-mono">
                    </div>
                </div>

                <div class="space-y-2">
                    <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Time Limit Protocol *</label>
                    <div class="relative">
                        <i class="fa-solid fa-clock absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/50"></i>
                        <input type="datetime-local" name="expiry" required 
                               class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all">
                    </div>
                </div>
            </div>

            <div class="space-y-2">
                <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Visual tutorial Link (Optional)</label>
                <div class="relative">
                    <i class="fa-brands fa-youtube absolute left-4 top-1/2 -translate-y-1/2 text-red-500/70"></i>
                    <input type="url" name="yt_url" placeholder="https://youtube.com/..." 
                           class="w-full bg-slate-950/50 border border-white/10 rounded-xl py-3 pl-12 pr-4 text-white focus:border-cyan-500 outline-none transition-all">
                </div>
            </div>

            <div class="bg-cyan-500/5 border border-cyan-500/10 p-4 rounded-xl text-center">
                <p class="text-[9px] text-gray-500 uppercase font-black tracking-[2px] leading-relaxed">
                    <i class="fa-solid fa-shield-halved mr-1 text-cyan-500/50"></i> 
                    Encryption will be applied automatically. Set price to 0 for free access.
                </p>
            </div>

            <button type="submit" name="upload" 
                    class="cyber-button w-full text-white font-bold py-4 rounded-xl shadow-lg transition-all active:scale-[0.98] flex items-center justify-center space-x-3 uppercase tracking-[3px] text-sm">
                <i class="fa-solid fa-bolt-lightning"></i>
                <span>Execute Publication</span>
            </button>
        </form>
    </div>
</div>

<style>
    input[type="datetime-local"]::-webkit-calendar-picker-indicator { filter: invert(1); cursor: pointer; }
    .cyber-card { background: rgba(15, 23, 42, 0.7); backdrop-filter: blur(15px); border: 1px solid rgba(255, 255, 255, 0.05); border-radius: 2rem; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); }
    .cyber-button { background: linear-gradient(90deg, #06b6d4 0%, #3b82f6 100%); border: none; }
    .cyber-button:hover { box-shadow: 0 0 30px rgba(6, 182, 212, 0.5); transform: translateY(-2px); }
</style>

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