<?php 
// ১. ডাটাবেস কানেকশন
$conn = new mysqli('localhost', 'tamimsho_download', 'tamimsho_download', 'tamimsho_download');
$conn->set_charset("utf8mb4");

// ২. ডিলিট লজিক (অপ্রয়োজনীয় রেজোলিউশন মোছার জন্য)
if(isset($_GET['del_id'])){
    $del_id = intval($_GET['del_id']);
    $conn->query("DELETE FROM quality_settings WHERE id=$del_id");
    header("Location: admin.php?status=deleted&type=" . $_GET['type']);
    exit();
}

// ৩. সেটিংস আপডেট লজিক
if(isset($_POST['update_settings'])){
    if(!empty($_POST['quality'])){
        foreach($_POST['quality'] as $id => $val){
            $is_premium = isset($val['premium']) ? 1 : 0;
            $is_hidden = isset($val['hidden']) ? 1 : 0;
            $price = mysqli_real_escape_string($conn, $val['price']);
            $conn->query("UPDATE quality_settings SET is_premium=$is_premium, is_hidden=$is_hidden, price='$price' WHERE id=$id");
        }
    }
    header("Location: admin.php?status=success&type=" . $_POST['current_type']);
    exit();
}

// ৪. ট্যাব লজিক
$active_tab = isset($_GET['type']) ? $_GET['type'] : 'video';
?>
<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel - DOWNLOAD 1280</title>
    <style>
        body { background: #000; color: #fff; font-family: 'Segoe UI', sans-serif; padding: 0; margin: 0; }
        .header-bar { background: #00FFFF; color: #000; padding: 20px; text-align: center; font-weight: bold; font-size: 22px; text-transform: uppercase; letter-spacing: 2px; border-bottom: 4px solid #FF007F; }
        
        /* ট্যাব ডিজাইন */
        .tab-container { display: flex; justify-content: center; background: #080808; border-bottom: 1px solid #222; }
        .tab { padding: 15px 40px; cursor: pointer; color: #555; font-weight: bold; text-decoration: none; transition: 0.3s; text-transform: uppercase; border-bottom: 3px solid transparent; }
        .tab.active { color: #00FFFF; border-bottom: 3px solid #00FFFF; background: rgba(0,255,255,0.05); }
        .tab:hover { color: #00FFFF; }

        .container { max-width: 1100px; margin: 30px auto; padding: 0 15px; }
        
        .top-nav { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
        .btn-view { color: #00FFFF; text-decoration: none; font-size: 14px; font-weight: bold; border: 1px solid #00FFFF; padding: 5px 15px; border-radius: 5px; }
        .btn-view:hover { background: #00FFFF; color: #000; }

        table { width: 100%; border-collapse: collapse; background: #080808; border-radius: 10px; overflow: hidden; }
        th { background: #00FFFF; color: #000; padding: 15px; border: 1px solid #000; text-transform: uppercase; font-size: 12px; }
        td { padding: 12px; border: 1px solid #1a1a1a; text-align: center; color: #fff; font-weight: bold; }
        
        tr:hover { background: #111; }
        
        input[type="number"] { width: 90px; background: #000; color: #00FFFF; border: 1px solid #333; padding: 8px; text-align: center; border-radius: 5px; outline: none; font-weight: bold; }
        input[type="number"]:focus { border-color: #00FFFF; }
        input[type="checkbox"] { transform: scale(1.6); cursor: pointer; accent-color: #00FFFF; }
        
        .save-btn { display: block; width: 300px; margin: 40px auto; padding: 15px; background: #00FFFF; color: #000; border: none; cursor: pointer; font-weight: bold; border-radius: 50px; font-size: 16px; text-transform: uppercase; transition: 0.3s; box-shadow: 0 0 15px rgba(0,255,255,0.3); }
        .save-btn:hover { background: #fff; box-shadow: 0 0 25px #00FFFF; transform: translateY(-2px); }
        
        .status-msg { text-align: center; color: #00ff00; background: rgba(0,255,0,0.1); padding: 10px; border-radius: 5px; margin-bottom: 20px; }
        .btn-del { color: #ff4d4d; text-decoration: none; font-size: 18px; }
    </style>
</head>
<body>
    <div class="header-bar">QUALITY CONTROL PANEL</div>

    <div class="tab-container">
        <a href="admin.php?type=video" class="tab <?php echo ($active_tab == 'video') ? 'active' : ''; ?>">Video Settings</a>
        <a href="admin.php?type=audio" class="tab <?php echo ($active_tab == 'audio') ? 'active' : ''; ?>">Audio Settings</a>
    </div>
    
    <div class="container">
        <div class="top-nav">
            <span style="color: #666;">ম্যানেজ করুন আপনার ডাউনলোড কোয়ালিটি</span>
            <a href="index.php" class="btn-view" target="_blank">← লাইভ সাইট দেখুন</a>
        </div>

        <?php if(isset($_GET['status']) && $_GET['status'] == 'success') echo '<p class="status-msg">✅ '.ucfirst($active_tab).' সেটিংস সফলভাবে সেভ করা হয়েছে!</p>'; ?>
        
        <form method="POST">
            <input type="hidden" name="current_type" value="<?php echo $active_tab; ?>">
            <table>
                <thead>
                    <tr>
                        <th>Quality Name</th>
                        <th>Premium?</th>
                        <th>Price ($)</th>
                        <th>Hide/Lock?</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                <?php
                $res = $conn->query("SELECT * FROM quality_settings WHERE type='$active_tab' ORDER BY quality_name ASC");
                if ($res && $res->num_rows > 0) {
                    while($row = $res->fetch_assoc()){
                        echo "<tr>
                            <td style='text-align:left; padding-left:30px; color: #00FFFF; letter-spacing: 1px;'>".strtoupper($row['quality_name'])."</td>
                            <td><input type='checkbox' name='quality[{$row['id']}][premium]' ".($row['is_premium'] ? 'checked' : '')."></td>
                            <td><input type='number' step='0.01' name='quality[{$row['id']}][price]' value='{$row['price']}'></td>
                            <td><input type='checkbox' name='quality[{$row['id']}][hidden]' ".($row['is_hidden'] ? 'checked' : '')."></td>
                            <td><a href='admin.php?del_id={$row['id']}&type=$active_tab' class='btn-del' title='Delete' onclick='return confirm(\"আপনি কি এটি ডিলিট করতে চান?\")'>&times;</a></td>
                        </tr>";
                    }
                } else {
                    echo "<tr><td colspan='5' style='padding: 60px; color: #444; font-size: 18px;'>এখনো কোনো $active_tab ডাটা নেই।<br><small>প্রথমে একটি লিঙ্ক প্রসেস করুন।</small></td></tr>";
                }
                ?>
                </tbody>
            </table>
            <button type="submit" name="update_settings" class="save-btn">SAVE <?php echo strtoupper($active_tab); ?> CHANGES</button>
        </form>
    </div>
</body>
</html>