<?php
/**
 * admin.php - ISP প্যাকেজ ম্যানেজমেন্ট অ্যাডমিন প্যানেল
 */

include('db_config.php');

// ১. প্যাকেজ ডিলিট করার লজিক
if (isset($_GET['delete'])) {
    $id = $_GET['delete'];
    mysqli_query($conn, "DELETE FROM packages WHERE id=$id");
    header('location: admin.php');
}

// ২. প্যাকেজ যোগ বা আপডেট করার লজিক
if (isset($_POST['save'])) {
    $name = $_POST['name'];
    $speed = $_POST['speed'];
    $price = $_POST['price'];
    $featured = isset($_POST['featured']) ? 1 : 0;
    $features = $_POST['features'];

    if (isset($_POST['id']) && $_POST['id'] != '') {
        // আপডেট
        $id = $_POST['id'];
        $query = "UPDATE packages SET name='$name', speed='$speed', price='$price', featured=$featured, features='$features' WHERE id=$id";
    } else {
        // নতুন ইনসার্ট
        $query = "INSERT INTO packages (name, speed, price, featured, features) VALUES ('$name', '$speed', '$price', $featured, '$features')";
    }
    
    mysqli_query($conn, $query);
    header('location: admin.php');
}

// এডিট করার জন্য ডেটা আনা
$edit_data = null;
if (isset($_GET['edit'])) {
    $id = $_GET['edit'];
    $res = mysqli_query($conn, "SELECT * FROM packages WHERE id=$id");
    $edit_data = mysqli_fetch_assoc($res);
}

// সকল প্যাকেজ লিস্ট আনা
$packages = mysqli_query($conn, "SELECT * FROM packages ORDER BY id DESC");
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ISP Admin Panel</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        :root { --red: #ED1C24; --dark: #1a1a1a; }
        body { font-family: 'Segoe UI', sans-serif; background: #f4f7f6; margin: 0; padding: 20px; }
        .admin-container { max-width: 1000px; margin: auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 5px 20px rgba(0,0,0,0.1); }
        h2 { border-bottom: 2px solid var(--red); padding-bottom: 10px; color: var(--dark); }
        
        /* Form Styles */
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 5px; font-weight: bold; }
        input[type="text"], input[type="number"], textarea { width: 100%; padding: 10px; border: 1px solid #ddd; border-radius: 5px; }
        .btn-save { background: var(--red); color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; font-weight: bold; }
        
        /* Table Styles */
        table { width: 100%; border-collapse: collapse; margin-top: 30px; }
        th, td { padding: 12px; border: 1px solid #eee; text-align: left; }
        th { background: var(--dark); color: white; }
        tr:hover { background: #f9f9f9; }
        .btn-edit { color: #2ecc71; margin-right: 10px; text-decoration: none; }
        .btn-delete { color: var(--red); text-decoration: none; }
        .badge { background: var(--red); color: white; padding: 2px 8px; border-radius: 10px; font-size: 12px; }
    </style>
</head>
<body>

<div class="admin-container">
    <h2><i class="fas fa-tasks"></i> প্যাকেজ ম্যানেজমেন্ট প্যানেল</h2>
    
    <!-- ইনপুট ফর্ম -->
    <form action="admin.php" method="POST" style="margin-top: 20px; background: #fafafa; padding: 20px; border-radius: 8px;">
        <input type="hidden" name="id" value="<?php echo $edit_data ? $edit_data['id'] : ''; ?>">
        
        <div style="display: flex; gap: 15px;">
            <div class="form-group" style="flex: 1;">
                <label>প্যাকেজের নাম</label>
                <input type="text" name="name" value="<?php echo $edit_data ? $edit_data['name'] : ''; ?>" required placeholder="উদা: স্টার্টার">
            </div>
            <div class="form-group" style="flex: 1;">
                <label>ইন্টারনেট স্পিড</label>
                <input type="text" name="speed" value="<?php echo $edit_data ? $edit_data['speed'] : ''; ?>" required placeholder="উদা: 20 Mbps">
            </div>
        </div>

        <div style="display: flex; gap: 15px;">
            <div class="form-group" style="flex: 1;">
                <label>মাসিক মূল্য (টাকা)</label>
                <input type="number" name="price" value="<?php echo $edit_data ? $edit_data['price'] : ''; ?>" required>
            </div>
            <div class="form-group" style="flex: 1; padding-top: 30px;">
                <label><input type="checkbox" name="featured" <?php echo ($edit_data && $edit_data['featured']) ? 'checked' : ''; ?>> জনপ্রিয় প্যাকেজ হিসেবে দেখান</label>
            </div>
        </div>

        <div class="form-group">
            <label>বৈশিষ্ট্যসমূহ (কমা দিয়ে আলাদা করুন)</label>
            <textarea name="features" rows="3" placeholder="আনলিমিটেড ডেটা, ২৪/৭ সাপোর্ট, রিয়েল আইপি"><?php echo $edit_data ? $edit_data['features'] : ''; ?></textarea>
        </div>

        <button type="submit" name="save" class="btn-save">
            <?php echo $edit_data ? 'আপডেট করুন' : 'নতুন প্যাকেজ যোগ করুন'; ?>
        </button>
        <?php if($edit_data): ?>
            <a href="admin.php" style="margin-left: 10px; color: #666;">বাতিল করুন</a>
        <?php endif; ?>
    </form>

    <!-- প্যাকেজ লিস্ট টেবিল -->
    <table>
        <thead>
            <tr>
                <th>নাম</th>
                <th>স্পিড</th>
                <th>মূল্য</th>
                <th>ফিচার</th>
                <th>অ্যাকশন</th>
            </tr>
        </thead>
        <tbody>
            <?php while($row = mysqli_fetch_assoc($packages)): ?>
            <tr>
                <td>
                    <?php echo $row['name']; ?> 
                    <?php if($row['featured']): ?> <span class="badge">Popular</span> <?php endif; ?>
                </td>
                <td><?php echo $row['speed']; ?></td>
                <td>৳<?php echo $row['price']; ?></td>
                <td style="font-size: 13px; color: #666;"><?php echo $row['features']; ?></td>
                <td>
                    <a href="admin.php?edit=<?php echo $row['id']; ?>" class="btn-edit"><i class="fas fa-edit"></i></a>
                    <a href="admin.php?delete=<?php echo $row['id']; ?>" class="btn-delete" onclick="return confirm('আপনি কি নিশ্চিত?')"><i class="fas fa-trash"></i></a>
                </td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>

    <div style="margin-top: 20px; text-align: right;">
        <a href="index.php" target="_blank" style="text-decoration: none; color: var(--red); font-weight: bold;">
            <i class="fas fa-external-link-alt"></i> ওয়েবসাইট দেখুন
        </a>
    </div>
</div>

</body>
</html>