<?php
require 'config.php';
// require_login(); 

// --- অ্যাডমিন নোটিশ ফেচ করার লজিক ---
$notice = null;
if(isset($pdo)) {
    try {
        // সর্বশেষ সচল (status=1) নোটিশটি নেওয়া হচ্ছে
        $stmt = $pdo->query("SELECT title, message FROM notices WHERE status = 1 ORDER BY id DESC LIMIT 1");
        $notice = $stmt->fetch();
    } catch (Exception $e) {
        $notice = null;
    }
}

// Fetch services
$services = [];
if(isset($pdo)) {
    try {
        $checkCol = $pdo->query("SHOW COLUMNS FROM services");
        $allColumns = $checkCol->fetchAll(PDO::FETCH_COLUMN);
        
        $orderBy = "id ASC";
        
        if (in_array('position', $allColumns)) {
            $orderBy = "position ASC";
        } elseif (in_array('serial', $allColumns)) {
            $orderBy = "serial ASC";
        } elseif (in_array('serial_no', $allColumns)) {
            $orderBy = "serial_no ASC";
        } elseif (in_array('sort_order', $allColumns)) {
            $orderBy = "sort_order ASC";
        }

        $sql = "SELECT id, name FROM services WHERE is_active=1 ORDER BY $orderBy";
        $services = $pdo->query($sql)->fetchAll();
    } catch (Exception $e) {
        $services = [];
    }
}
$service_count = count($services);

// আধুনিক আইকন সেট
$icons = ['fa-id-card', 'fa-file-invoice', 'fa-shield-halved', 'fa-bolt', 'fa-fingerprint', 'fa-id-badge'];

require_once 'header.php'; 
?>

<link href="https://fonts.googleapis.com/css2?family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">

<style>
    :root {
        --primary: #6C3FAA;
        --primary-dark: #4B2A7A;
        --accent: #FF9F43;
        --bg-light: #F8F5FD;
        --card-bg: #FFFFFF;
        --text-dark: #1E1135;
        --text-muted: #6C6A7A;
        --border: #E2DEF0;
        --success: #10B981;
        --warning: #F59E0B;
        --danger: #EF4444;
        --shadow-sm: 0 4px 15px rgba(108, 63, 170, 0.08);
        --shadow-md: 0 8px 25px rgba(108, 63, 170, 0.12);
        --shadow-lg: 0 12px 40px rgba(108, 63, 170, 0.2);
        --radius: 20px;
        --transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    }

    body {
        background: linear-gradient(135deg, #F5F0FF 0%, #EDE4FB 100%);
        font-family: 'Hind Siliguri', sans-serif;
        color: var(--text-dark);
    }

    .premium-container {
        max-width: 1100px;
        margin: 0 auto;
        padding: 2rem 1.5rem;
    }

    /* Section Header */
    .section-header {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 1.5rem;
    }
    .section-header .icon-circle {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        color: white;
        width: 42px;
        height: 42px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.1rem;
        box-shadow: 0 5px 15px rgba(108,63,170,0.3);
    }
    .section-header h4 {
        font-size: 1.25rem;
        font-weight: 700;
        margin: 0;
    }

    /* Premium Feature Cards */
    .premium-feature-card {
        background: var(--card-bg);
        border-radius: var(--radius);
        padding: 2rem;
        height: 100%;
        border: 1px solid var(--border);
        text-decoration: none;
        transition: var(--transition);
        display: block;
        position: relative;
        overflow: hidden;
        box-shadow: var(--shadow-sm);
    }
    .premium-feature-card:hover {
        transform: translateY(-8px);
        box-shadow: var(--shadow-lg);
        border-color: var(--primary);
    }
    .feature-icon {
        width: 55px;
        height: 55px;
        border-radius: 14px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.5rem;
        margin-bottom: 1.2rem;
    }
    .status-badge {
        font-size: 0.75rem;
        padding: 5px 12px;
        border-radius: 50px;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
        gap: 6px;
    }
    .active-dot {
        width: 7px; height: 7px;
        background: white;
        border-radius: 50%;
        animation: pulse-soft 2s infinite;
    }
    @keyframes pulse-soft {
        0% { box-shadow: 0 0 0 0 rgba(255,255,255,0.4); }
        70% { box-shadow: 0 0 0 8px rgba(255,255,255,0); }
        100% { box-shadow: 0 0 0 0 rgba(255,255,255,0); }
    }

    /* Service List Items */
    .service-list-item {
        background: var(--card-bg);
        border-radius: 16px;
        padding: 1.2rem;
        display: flex;
        align-items: center;
        gap: 15px;
        text-decoration: none;
        border: 1px solid transparent;
        transition: var(--transition);
        box-shadow: var(--shadow-sm);
    }
    .service-list-item:hover {
        border-color: var(--primary);
        transform: translateX(6px);
        box-shadow: var(--shadow-md);
    }
    .service-icon-box {
        width: 45px;
        height: 45px;
        background: linear-gradient(135deg, #F1ECFB, #E6DEF7);
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--primary);
        font-size: 1.1rem;
    }

    /* Quick Access */
    .quick-access-card {
        background: var(--card-bg);
        padding: 1.5rem;
        border-radius: 16px;
        text-align: center;
        text-decoration: none;
        display: block;
        border: 1px solid var(--border);
        transition: var(--transition);
        box-shadow: var(--shadow-sm);
    }
    .quick-access-card:hover {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        color: white;
        transform: translateY(-5px);
        box-shadow: var(--shadow-lg);
    }
    .quick-access-card:hover .quick-icon,
    .quick-access-card:hover .quick-text {
        color: white !important;
    }

    /* Notice Modal */
    .custom-modal-content {
        border: none;
        border-radius: 24px;
        overflow: hidden;
        box-shadow: var(--shadow-lg);
    }
    .modal-header-custom {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        color: white;
        border: none;
        padding: 1.5rem;
    }
    .modal-body-custom {
        padding: 2.5rem;
        text-align: center;
    }
    .btn-close-white {
        filter: brightness(0) invert(1);
    }
</style>

<div class="premium-container">
    
    <!-- অটোমেটিক ডিজিটাল সেবা -->
    <div class="mb-5">
        <div class="section-header">
            <span class="icon-circle"><i class="fas fa-bolt"></i></span>
            <h4>অটোমেটিক ডিজিটাল সেবা</h4>
        </div>

        <div class="row g-4" style="display: flex; flex-wrap: wrap;">
            <!-- Server Copy -->
            <div style="flex: 1 1 280px;">
                <a href="server_copy.php" class="premium-feature-card">
                    <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1rem;">
                        <div class="feature-icon" style="background: linear-gradient(135deg, #F1ECFB, #E6DEF7); color: var(--primary);">
                            <i class="fas fa-server"></i>
                        </div>
                        <span class="status-badge" style="background: var(--primary); color: white;">
                            <span class="active-dot"></span> সচল
                        </span>
                    </div>
                    <h5 class="fw-bold mb-2">সার্ভার কপি</h5>
                    <p class="text-muted small mb-4">অফিসিয়াল সার্ভার থেকে তথ্য ডাউনলোড সিস্টেম।</p>
                    <span class="fw-semibold small" style="color: var(--primary);">আবেদন করুন <i class="fas fa-arrow-right ms-1"></i></span>
                </a>
            </div>

            <!-- NID Make -->
            <div style="flex: 1 1 280px;">
                <a href="sign_old_nid.php" class="premium-feature-card">
                    <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1rem;">
                        <div class="feature-icon" style="background: linear-gradient(135deg, #FDF2F8, #FBCFE8); color: #D946EF;">
                            <i class="fas fa-id-card"></i>
                        </div>
                        <span class="status-badge" style="background: #D946EF; color: white;">অটো জেনারেট</span>
                    </div>
                    <h5 class="fw-bold mb-2">এনআইডি মেক</h5>
                    <p class="text-muted small mb-4">আইডি কার্ড তৈরি করার অটোমেটেড টুলস।</p>
                    <span class="fw-semibold small" style="color: #D946EF;">তৈরি করুন <i class="fas fa-arrow-right ms-1"></i></span>
                </a>
            </div>

            <!-- Smart Card -->
            <div style="flex: 1 1 280px;">
                <a href="smart.php" class="premium-feature-card">
                    <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 1rem;">
                        <div class="feature-icon" style="background: linear-gradient(135deg, #ECFDF5, #D1FAE5); color: #10B981;">
                            <i class="fas fa-address-card"></i>
                        </div>
                        <span class="status-badge" style="background: #10B981; color: white;">স্মার্ট কপি</span>
                    </div>
                    <h5 class="fw-bold mb-2">স্মার্ট কার্ড মেক</h5>
                    <p class="text-muted small mb-4">স্মার্ট কার্ড কপি তৈরি করার অটোমেটেড টুলস।</p>
                    <span class="fw-semibold small" style="color: #10B981;">তৈরি করুন <i class="fas fa-arrow-right ms-1"></i></span>
                </a>
            </div>
        </div>
    </div>

    <!-- অন্যান্য সকল সেবা সমূহ -->
    <div class="mb-5">
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.5rem;">
            <div class="section-header" style="margin-bottom: 0;">
                <span class="icon-circle"><i class="fas fa-layer-group"></i></span>
                <h4>অন্যান্য সকল সেবা সমূহ</h4>
            </div>
            <span style="background: var(--card-bg); color: var(--primary); padding: 8px 16px; border-radius: 50px; font-weight: 700; font-size: 0.9rem; box-shadow: var(--shadow-sm);">
                মোট সেবা: <b><?php echo $service_count; ?></b>
            </span>
        </div>

        <div class="row g-3" style="display: flex; flex-wrap: wrap;">
            <?php if ($service_count > 0): ?>
                <?php foreach ($services as $index => $s): 
                    $current_icon = $icons[$index % count($icons)];
                ?>
                <div style="flex: 1 1 280px;">
                    <a href="subcategories.php?service_id=<?php echo $s['id']; ?>" class="service-list-item">
                        <div class="service-icon-box"><i class="fas <?php echo $current_icon; ?>"></i></div>
                        <div style="flex:1;">
                            <h6 class="mb-0 fw-bold" style="color: var(--text-dark);"><?php echo htmlspecialchars($s['name']); ?></h6>
                            <small style="color: var(--text-muted);">বিস্তারিত ও ফরম দেখুন</small>
                        </div>
                        <i class="fas fa-chevron-right" style="color: var(--border);"></i>
                    </a>
                </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div style="width: 100%; text-align: center; padding: 4rem; background: var(--card-bg); border-radius: var(--radius); box-shadow: var(--shadow-sm);">
                    <i class="fas fa-inbox fa-3x mb-3" style="color: #D1CAE5;"></i>
                    <h5 class="fw-bold" style="color: var(--text-muted);">কোনো সেবা উপলব্ধ নেই</h5>
                    <p style="color: var(--text-muted);">বর্তমানে কোনো সক্রিয় সেবা নেই। পরে আবার চেক করুন।</p>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- কুইক এক্সেস -->
    <div class="mb-5">
        <div class="section-header">
            <span class="icon-circle"><i class="fas fa-rocket"></i></span>
            <h4>কুইক এক্সেস</h4>
        </div>
        <div class="row g-3" style="display: flex; flex-wrap: wrap;">
            <div style="flex: 1 1 120px;">
                <a href="recharge.php" class="quick-access-card">
                    <i class="fas fa-wallet fa-2x mb-2 quick-icon" style="color: var(--primary);"></i>
                    <div class="fw-bold quick-text">রিচার্জ</div>
                </a>
            </div>
            <div style="flex: 1 1 120px;">
                <a href="dashboard.php" class="quick-access-card">
                    <i class="fas fa-history fa-2x mb-2 quick-icon" style="color: #636E72;"></i>
                    <div class="fw-bold quick-text">হিস্টরি</div>
                </a>
            </div>
            <div style="flex: 1 1 120px;">
                <a href="profile.php" class="quick-access-card">
                    <i class="fas fa-user-shield fa-2x mb-2 quick-icon" style="color: var(--primary-dark);"></i>
                    <div class="fw-bold quick-text">প্রোফাইল</div>
                </a>
            </div>
        </div>
    </div>
</div>

<!-- ================= নোটিশ পপ-আপ মোডাল ================= -->
<?php if ($notice): ?>
<div class="modal fade" id="noticeModal" tabindex="-1" aria-labelledby="noticeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content custom-modal-content">
            <div class="modal-header modal-header-custom">
                <h5 class="modal-title fw-bold" id="noticeModalLabel">
                    <i class="fas fa-bullhorn me-2"></i> <?php echo htmlspecialchars($notice['title']); ?>
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body modal-body-custom">
                <div class="mb-4">
                    <i class="fas fa-bell fa-4x" style="color: var(--primary); opacity: 0.15;"></i>
                </div>
                <p style="font-size: 1.1rem; line-height: 1.8; color: var(--text-dark);">
                    <?php echo nl2br(htmlspecialchars($notice['message'])); ?>
                </p>
                <div class="mt-4">
                    <button type="button" class="btn px-5 py-2 fw-bold" data-bs-dismiss="modal" 
                            style="background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: #fff; border-radius: 12px; box-shadow: 0 4px 15px rgba(108,63,170,0.3);">
                        বন্ধ করুন
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    window.onload = function() {
        var myModal = new bootstrap.Modal(document.getElementById('noticeModal'));
        myModal.show();
    };
</script>
<?php endif; ?>

<?php require_once 'footer.php'; ?>