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

// ১. আইডি চেক করা এবং ডেটা আনা
if (!isset($_GET['id'])) { header("Location: index.php"); exit(); }
$id = $_GET['id'];

$stmt = $conn->prepare("SELECT * FROM files WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$file = $stmt->get_result()->fetch_assoc();

if (!$file) { echo "File not found!"; exit(); }

// ২. আপডেট লজিক (প্রাইস এবং ডিসকাউন্ট সহ)
if (isset($_POST['update'])) {
    $title = $_POST['title'];
    $password = $_POST['password'];
    $expiry = $_POST['expiry'];
    $yt_url = $_POST['yt_url'];
    $price = $_POST['price']; // নতুন
    $discount = $_POST['discount']; // নতুন

    // কুয়েরি আপডেট করা হয়েছে
    $update_stmt = $conn->prepare("UPDATE files SET file_title=?, password=?, expiry_time=?, youtube_url=?, price=?, discount=? WHERE id=?");
    $update_stmt->bind_param("ssssddi", $title, $password, $expiry, $yt_url, $price, $discount, $id);
    
    if ($update_stmt->execute()) {
        $success = "System Updated: Asset metadata modified successfully!";
        header("Refresh:1; url=index.php");
    } else {
        $error = "System Error: Failed to execute update protocol!";
    }
}

include '../common/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-0 right-0 p-4 opacity-10">
            <i class="fa-solid fa-pen-nib text-8xl text-cyan-500"></i>
        </div>

        <div class="mb-10">
            <h2 class="text-2xl md:text-3xl font-bold logo-font tracking-wider uppercase">
                EDIT <span class="text-cyan-400 glow-text">ASSET DATA</span>
            </h2>
            <p class="text-gray-500 text-[10px] font-bold uppercase tracking-[3px] mt-1">Target ID: #<?php echo $id; ?></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">
                <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">
                <i class="fa-solid fa-triangle-exclamation mr-2"></i> <?php echo $error; ?>
            </div>
        <?php endif; ?>

        <form action="" method="POST" class="space-y-6">
            
            <!-- ফাইল টাইটেল -->
            <div class="space-y-2">
                <label class="block text-[10px] font-black text-gray-500 uppercase tracking-[2px]">Asset Designation (Title)</label>
                <div class="relative">
                    <i class="fa-solid fa-file-signature absolute left-4 top-1/2 -translate-y-1/2 text-cyan-500/50"></i>
                    <input type="text" name="title" value="<?php echo $file['file_title']; ?>" 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 focus:ring-1 focus:ring-cyan-500 outline-none transition-all font-medium">
                </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="<?php echo isset($file['price']) ? $file['price'] : '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="<?php echo isset($file['discount']) ? $file['discount'] : '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 (Password)</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" value="<?php echo $file['password']; ?>" placeholder="Unprotected" 
                               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]">Expiration Protocol (Date/Time)</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>
                        <?php $expiry_val = date('Y-m-d\TH:i', strtotime($file['expiry_time'])); ?>
                        <input type="datetime-local" name="expiry" value="<?php echo $expiry_val; ?>" 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 (YouTube URL)</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" value="<?php echo $file['youtube_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-black/40 border border-cyan-500/10 p-4 rounded-xl flex items-center space-x-4">
                <div class="bg-cyan-500/10 p-3 rounded-lg">
                    <i class="fa-solid fa-hard-drive text-cyan-500"></i>
                </div>
                <div class="min-w-0">
                    <p class="text-[9px] font-bold text-gray-500 uppercase tracking-widest">Linked Storage Object</p>
                    <p class="text-xs font-mono text-cyan-200 truncate"><?php echo $file['file_name']; ?></p>
                </div>
            </div>

            <!-- সেভ বাটন -->
            <button type="submit" name="update" 
                    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-cloud-arrow-up"></i>
                <span>Execute Update</span>
            </button>
        </form>
    </div>
</div>