<?php
/**
 * packages_list.php - এটি একটি আলাদা কম্পোনেন্ট যা index এবং packages পেজে ব্যবহার করা যাবে।
 */
if (!isset($conn)) {
    include('db_config.php');
}

$limit_sql = isset($limit) ? " LIMIT $limit" : "";
$sql = "SELECT * FROM packages ORDER BY price ASC" . $limit_sql;
$result = mysqli_query($conn, $sql);
?>

<style>
    :root {
        --primary-color: #00aeef;
        --secondary-color: #57a03a;
        --dark-gray: #333;
        --light-bg: #f8f9fa;
        --card-bg: #ffffff;
    }

    .packages-section { padding: 60px 0; background-color: var(--light-bg); text-align: center; }
    .section-header h2 { font-size: 2.2rem; color: var(--dark-gray); text-transform: uppercase; margin-bottom: 30px; font-weight: 800; }
    .package-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 25px; padding: 10px 0; }
    .flip-card { background-color: transparent; width: 100%; height: 400px; perspective: 1000px; cursor: pointer; }
    .flip-card-inner { position: relative; width: 100%; height: 100%; text-align: center; transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1); transform-style: preserve-3d; }
    .flip-card:hover .flip-card-inner { transform: rotateY(180deg); }
    .flip-card-front, .flip-card-back { position: absolute; width: 100%; height: 100%; -webkit-backface-visibility: hidden; backface-visibility: hidden; border-radius: 20px; display: flex; flex-direction: column; justify-content: center; align-items: center; overflow: hidden; box-shadow: 0 8px 20px rgba(0,0,0,0.05); background: var(--card-bg); }
    .flip-card-front { border: 1px solid rgba(0,0,0,0.05); }
    .speed-box { background: var(--primary-color); color: white; width: 110px; height: 110px; border-radius: 20px; display: flex; flex-direction: column; justify-content: center; margin-bottom: 15px; box-shadow: 0 10px 20px rgba(0, 174, 239, 0.2); }
    .speed-box h2 { font-size: 2.8rem; margin: 0; line-height: 1; font-weight: 800; }
    .speed-box span { font-size: 0.9rem; font-weight: bold; text-transform: uppercase; }
    .flip-card-front h3 { font-size: 1.5rem; color: var(--dark-gray); margin-bottom: 8px; font-weight: 700; }
    .price-info { background: #f1f3f5; width: 100%; padding: 10px 0; position: absolute; bottom: 0; border-top: 1px solid #eee; display: flex; flex-direction: column; align-items: center; }
    .price-text { font-size: 1.3rem; font-weight: 800; color: var(--secondary-color); }
    .vat-simple { font-size: 0.75rem; color: #666; font-weight: 600; }
    .flip-card-back { background: linear-gradient(135deg, var(--secondary-color) 0%, #46802e 100%); color: white; transform: rotateY(180deg); padding: 20px; }
    .flip-card-back h3 { font-size: 1.5rem; margin-bottom: 10px; border-bottom: 1px solid rgba(255,255,255,0.2); padding-bottom: 8px; }
    .flip-card-back ul { list-style: none; padding: 0; margin-bottom: 15px; text-align: left; width: 100%; }
    .flip-card-back ul li { margin-bottom: 6px; font-size: 0.9rem; display: flex; align-items: center; }
    .flip-card-back ul li i { margin-right: 10px; }
    .register-btn { background: white; color: var(--primary-color); padding: 8px 20px; border-radius: 50px; text-decoration: none; font-weight: bold; transition: 0.3s; box-shadow: 0 4px 10px rgba(0,0,0,0.1); margin-bottom: 10px; font-size: 0.9rem; }
    .register-btn:hover { background: var(--primary-color); color: white; }
    .vat-apply-text { font-size: 0.7rem; background: rgba(0, 0, 0, 0.15); padding: 3px 10px; border-radius: 4px; }
</style>

<section class="packages-section">
    <div class="container">
        <div class="section-header">
            <h2>আমাদের <span style="color:var(--primary-color)">ইন্টারনেট</span> প্যাকেজসমূহ</h2>
        </div>

        <div class="package-grid">
            <?php 
            if ($result && mysqli_num_rows($result) > 0):
                while($pkg = mysqli_fetch_assoc($result)): 
                    $features = explode(',', $pkg['features']);
                    preg_match('/\d+/', $pkg['speed'], $matches);
                    $speed_val = isset($matches[0]) ? $matches[0] : '0';
            ?>
                <div class="flip-card">
                    <div class="flip-card-inner">
                        <div class="flip-card-front">
                            <div class="speed-box">
                                <h2><?php echo $speed_val; ?></h2>
                                <span>Mbps</span>
                            </div>
                            <h3><?php echo $pkg['name']; ?></h3>
                            <div class="price-info">
                                <div class="price-text">@ <?php echo number_format($pkg['price'], 0); ?> BDT</div>
                                <div class="vat-simple">+ 5% VAT</div>
                            </div>
                        </div>
                        <div class="flip-card-back">
                            <h3><?php echo $pkg['name']; ?></h3>
                            <ul>
                                <li><i class="fas fa-bolt"></i> স্পিড: <?php echo $pkg['speed']; ?></li>
                                <?php foreach($features as $f): if(trim($f)!=""): ?>
                                    <li><i class="fas fa-check-circle"></i> <?php echo trim($f); ?></li>
                                <?php endif; endforeach; ?>
                            </ul>
                            <a href="contact.php?pkg=<?php echo urlencode($pkg['name']); ?>" class="register-btn">অর্ডার করুন</a>
                            <div class="vat-apply-text">Additional 5% VAT will apply</div>
                        </div>
                    </div>
                </div>
            <?php endwhile; endif; ?>
        </div>
    </div>
</section>