<?php
/**
 * admin.php - ISP Admin Dashboard with License Status Menu
 */

date_default_timezone_set('Asia/Dhaka');
session_start();

if (!isset($_SESSION['admin_logged_in'])) {
    header('location: login.php');
    exit();
}

include('db_config.php');
mysqli_set_charset($conn, "utf8mb4");

// ১. লগইন করা ইউজারের তথ্য সংগ্রহ (Role & Name)
$display_name = "User";
$display_role = "Staff";
$is_admin = false;

if (isset($_SESSION['user_id'])) {
    $uid = $_SESSION['user_id'];
    $user_query = mysqli_query($conn, "SELECT full_name, role FROM users WHERE id = '$uid' LIMIT 1");
    if ($user_query && mysqli_num_rows($user_query) > 0) {
        $u_info = mysqli_fetch_assoc($user_query);
        $display_name = $u_info['full_name'];
        $display_role = $u_info['role'];
        if (strtolower($display_role) == 'admin') {
            $is_admin = true;
        }
    }
}

// ২. রিপোর্ট এর জন্য ডাটা সংগ্রহ (Stats Queries)
$total_packages = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM packages"))['total'];

// টিকিট স্ট্যাটাস কাউন্ট
$pending_tickets = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM tickets WHERE status='Pending'"))['total'];
$progress_tickets = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM tickets WHERE status='In Progress'"))['total'];
$solved_tickets = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM tickets WHERE status='Resolved'"))['total'];

// ইউজার কাউন্ট
$total_users = 0; 
$user_check = mysqli_query($conn, "SHOW TABLES LIKE 'users'");
if(mysqli_num_rows($user_check) > 0) {
    $total_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM users"))['total'];
}

// নতুন কানেকশন অনুরোধ রিপোর্ট (registrations টেবিল থেকে)
$reg_table_check = mysqli_query($conn, "SHOW TABLES LIKE 'registrations'");
$reg_pending = 0;
$reg_canceled = 0;
$reg_in_progress = 0;
$reg_completed = 0;

if(mysqli_num_rows($reg_table_check) > 0) {
    $reg_pending = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM registrations WHERE status='Pending'"))['total'] ?? 0;
    $reg_canceled = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM registrations WHERE status='Canceled'"))['total'] ?? 0;
    $reg_in_progress = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM registrations WHERE status='In Progress'"))['total'] ?? 0;
    $reg_completed = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM registrations WHERE status='Completed'"))['total'] ?? 0;
}

// ব্লগ ও নোটিশ স্ট্যাটাস কাউন্ট
$blog_table_check = mysqli_query($conn, "SHOW TABLES LIKE 'blogs'");
$total_posts = 0;
$published_posts = 0;
$draft_posts = 0;

if(mysqli_num_rows($blog_table_check) > 0) {
    $total_posts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM blogs"))['total'];
    $published_posts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM blogs WHERE status='Published'"))['total'];
    $draft_posts = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as total FROM blogs WHERE status='Draft'"))['total'];
}

// এডিট বা ডিলিট রিকোয়েস্ট থাকলে অটোমেটিক ভিউ সেট করা - শুধুমাত্র এডমিন অনুমতি পাবে
$view = $_GET['view'] ?? 'dashboard';

// ডিলিট বা এডিট অ্যাকশন প্রোটেকশন
if (isset($_GET['delete_pkg']) || isset($_GET['delete_slider']) || isset($_GET['delete_blog']) || isset($_GET['delete_testi'])) {
    if (!$is_admin) {
        die("<div style='padding:20px; color:red; text-align:center;'>আপনার এই তথ্য মুছে ফেলার অনুমতি নেই। শুধুমাত্র অ্যাডমিন ডিলিট করতে পারেন। <a href='admin.php'>ফিরে যান</a></div>");
    }
}

if (isset($_GET['edit_pkg']) || isset($_GET['delete_pkg'])) {
    $view = 'packages';
}
if (isset($_GET['edit_slider']) || isset($_GET['delete_slider'])) {
    $view = 'sliders';
}
if (isset($_GET['edit_blog']) || isset($_GET['delete_blog'])) {
    $view = 'blogs';
}
if (isset($_GET['edit_testi']) || isset($_GET['delete_testi'])) {
    $view = 'testimonials';
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NetPro ISP Admin - Unified Reports</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
    <style>
        :root { 
            --primary: #00aeef; --secondary: #57a03a; --dark: #1a1a1a; --light: #f8f9fa; 
            --red: #ed1c24; --orange: #f39c12; --indigo: #6610f2; --teal: #20c997;
            --bg-body: #f0f2f5; --bg-card: white; --text-main: #333; --text-sub: #666; --border: #eee;
            --nav-bg: white; --sidebar-bg: white; --sidebar-text: #444;
        }

        [data-theme="dark"] {
            --bg-body: #121212; --bg-card: #1e1e1e; --text-main: #e0e0e0; --text-sub: #b0b0b0; --border: #333;
            --nav-bg: #1e1e1e; --light: #2d2d2d; --sidebar-bg: #1e1e1e; --sidebar-text: #adb5bd;
        }

        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: var(--bg-body); color: var(--text-main); margin: 0; display: flex; transition: 0.3s; }
        
        /* Sidebar Styles - White Design */
        .sidebar { width: 260px; background: var(--sidebar-bg); color: var(--sidebar-text); height: 100vh; position: fixed; border-right: 1px solid var(--border); transition: 0.3s; z-index: 100; overflow-y: auto; }
        .sidebar-header { text-align: center; padding: 23px 0; border-bottom: 1px solid var(--border); }
        .sidebar-header h2 { font-size: 18px; color: var(--red); margin: 0; font-weight: 800; }
        .sidebar-menu { padding: 10px; }
        .sidebar a { display: flex; align-items: center; color: var(--sidebar-text); padding: 10px 15px; text-decoration: none; transition: 0.3s; font-size: 14px; font-weight: 500; border-radius: 8px; margin-bottom: 5px; }
        .sidebar a i { width: 25px; font-size: 18px; margin-right: 10px; }
        .sidebar a:hover { background: var(--light); color: var(--primary); }
        .sidebar a.active { background: #e0f4ff; color: var(--primary); font-weight: 600; }
        
        /* Main Content */
        .main-content { margin-left: 260px; width: calc(100% - 260px); min-height: 100vh; }
        .top-nav { background: var(--nav-bg); padding: 15px 40px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 2px 5px rgba(0,0,0,0.05); position: sticky; top: 0; z-index: 90; }
        .content-body { padding: 20px 30px; }

        .user-profile { display: flex; align-items: center; gap: 12px; }
        .user-details { text-align: right; }
        .user-details h4 { margin: 0; font-size: 14px; color: var(--text-main); }
        .user-details span { font-size: 11px; color: var(--primary); background: #e0f4ff; padding: 2px 8px; border-radius: 10px; font-weight: bold; text-transform: uppercase; }

        /* Report Cards */
        .stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 30px; }
        .stat-card { background: var(--bg-card); padding: 20px; border-radius: 12px; display: flex; align-items: center; box-shadow: 0 5px 15px rgba(0,0,0,0.05); transition: transform 0.2s; position: relative; overflow: hidden; border: 1px solid var(--border); }
        .stat-card:hover { transform: translateY(-5px); }
        .stat-card i { font-size: 35px; margin-right: 15px; opacity: 0.2; position: absolute; right: 15px; top: 20px; }
        .stat-info h3 { margin: 0; font-size: 26px; color: var(--text-main); font-weight: 800; }
        .stat-info p { margin: 2px 0 0; color: var(--text-sub); font-size: 14px; font-weight: 600; }
        
        /* Sections */
        .section-card { background: var(--bg-card); padding: 25px; border-radius: 12px; box-shadow: 0 5px 15px rgba(0,0,0,0.05); border: 1px solid var(--border); margin-bottom: 20px; }
        table { width: 100%; border-collapse: collapse; margin-top: 15px; color: var(--text-main); }
        th, td { padding: 12px 15px; text-align: left; border-bottom: 1px solid var(--border); }
        th { background: var(--light); font-size: 13px; color: var(--text-sub); text-transform: uppercase; }
        
        /* Badges */
        .badge { padding: 4px 10px; border-radius: 20px; font-size: 11px; font-weight: bold; text-transform: uppercase; }
        .bg-pending { background: #ffebee; color: #c62828; } 
        .bg-in-progress { background: #fff3e0; color: #ef6c00; } 
        .bg-resolved { background: #e8f5e9; color: #2e7d32; } 
        .btn { cursor: pointer; border: none; transition: 0.3s; }

        /* Theme Toggle */
        .theme-toggle { cursor: pointer; padding: 8px 15px; border-radius: 20px; background: var(--light); border: 1px solid var(--border); color: var(--text-main); font-weight: bold; font-size: 13px; }
        
        /* Charts Container */
        .charts-container { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 30px; }
        @media (max-width: 992px) { .charts-container { grid-template-columns: 1fr; } .sidebar { width: 70px; } .sidebar-header h2, .sidebar a span { display: none; } .main-content { margin-left: 70px; width: calc(100% - 70px); } }
    </style>
</head>
<body data-theme="light">

<div class="sidebar">
    <div class="sidebar-header">
        <h2>NetPro ISP CONTROL</h2>
    </div>
    <div class="sidebar-menu">
        <a href="admin.php?view=dashboard" class="<?php echo $view == 'dashboard' ? 'active' : ''; ?>">
            <i class="fas fa-th-large"></i> <span>ড্যাশবোর্ড</span>
        </a>
        <a href="admin.php?view=packages" class="<?php echo $view == 'packages' ? 'active' : ''; ?>">
            <i class="fas fa-box"></i> <span>প্যাকেজ ম্যানেজমেন্ট</span>
        </a>
        <a href="admin.php?view=sliders" class="<?php echo $view == 'sliders' ? 'active' : ''; ?>">
            <i class="fas fa-images"></i> <span>স্লাইডার সেটআপ</span>
        </a>
        <a href="admin.php?view=tickets" class="<?php echo $view == 'tickets' ? 'active' : ''; ?>">
            <i class="fas fa-ticket-alt"></i> <span>সাপোর্ট টিকিট</span>
        </a>
        <a href="admin.php?view=connection_requests" class="<?php echo $view == 'connection_requests' ? 'active' : ''; ?>">
            <i class="fas fa-plug"></i> <span>নতুন কানেকশন</span>
        </a>
        <a href="admin.php?view=blogs" class="<?php echo $view == 'blogs' ? 'active' : ''; ?>">
            <i class="fas fa-newspaper"></i> <span>ব্লগ ও পোস্ট</span>
        </a>
        <a href="admin.php?view=media" class="<?php echo $view == 'media' ? 'active' : ''; ?>">
            <i class="fas fa-photo-video"></i> <span>মিডিয়া লাইব্রেরি</span>
        </a>
        <a href="admin.php?view=gallery" class="<?php echo $view == 'gallery' ? 'active' : ''; ?>">
            <i class="fas fa-photo-video"></i> <span>এলবাম গ্যালারি</span>
        </a>
        <a href="admin.php?view=testimonials" class="<?php echo $view == 'testimonials' ? 'active' : ''; ?>">
            <i class="fas fa-comment-dots"></i> <span>টেস্টিনিমোনিয়াল</span>
        </a>
        <a href="admin.php?view=partners" class="<?php echo $view == 'partners' ? 'active' : ''; ?>">
            <i class="fas fa-handshake"></i> <span>পার্টনার সেটআপ</span>
        </a>
        <a href="admin.php?view=users" class="<?php echo $view == 'users' ? 'active' : ''; ?>">
            <i class="fas fa-user-plus"></i> <span>ইউজার ম্যানেজমেন্ট</span>
        </a>
        <a href="admin.php?view=staff" class="<?php echo $view == 'staff' ? 'active' : ''; ?>">
            <i class="fas fa-users-cog"></i> <span>স্টাফ ম্যানেজমেন্ট</span>
        </a>
        <a href="admin.php?view=footer" class="<?php echo $view == 'footer' ? 'active' : ''; ?>">
            <i class="fas fa-th-list"></i> <span>ফুটার ম্যানেজমেন্ট</span>
        </a>

        <!-- লাইসেন্স স্ট্যাটাস মেনু (নতুন যুক্ত করা হয়েছে) -->
        <?php if(isset($is_theme_active) && $is_theme_active): ?>
        <div style="margin-top: 5px; border-top: 1px solid var(--border); padding-top: 5px;">
            <a href="#" style="color: var(--secondary); cursor: default;">
                <i class="fas fa-check-shield"></i> <span>লাইসেন্স একটিভ আছে</span>
            </a>
        </div>
        <?php endif; ?>

        <div style="margin-top: 5px; border-top: 1px solid var(--border); padding-top: 5px;">
            <a href="logout.php" style="color: var(--red);">
                <i class="fas fa-sign-out-alt"></i> <span>লগআউট</span>
            </a>
        </div>
    </div>
</div>

<div class="main-content">
    <div class="top-nav">
        <div class="breadcrumb">অ্যাডমিন / <strong><?php echo ucfirst($view); ?></strong></div>
        <div style="display: flex; align-items: center; gap: 25px;">
            <button class="theme-toggle" onclick="toggleTheme()">
                <i class="fas fa-moon"></i> <span id="theme-text">ডার্ক মোড</span>
            </button>
            <div class="user-profile">
                <div class="user-details">
                    <h4><?php echo $display_name; ?></h4>
                    <span><?php echo $display_role; ?></span>
                </div>
                <i class="fas fa-user-circle" style="font-size: 35px; color: #ddd;"></i>
            </div>
        </div>
    </div>

    <div class="content-body">
        
        <?php if($view == 'dashboard'): ?>
            <h4 style="margin: 0 0 15px 0; color: var(--text-sub);"><i class="fas fa-chart-pie"></i> সাধারণ পরিসংখ্যান</h4>
            <div class="stats-grid">
                <div class="stat-card" style="border-left: 5px solid var(--primary);">
                    <div class="stat-info"><h3><?php echo $total_packages; ?></h3><p>মোট প্যাকেজ</p></div>
                    <i class="fas fa-box" style="color: var(--primary);"></i>
                </div>
                <div class="stat-card" style="border-left: 5px solid var(--red);">
                    <div class="stat-info"><h3><?php echo $pending_tickets; ?></h3><p>পেন্ডিং টিকিট</p></div>
                    <i class="fas fa-exclamation-circle" style="color: var(--red);"></i>
                </div>
                <div class="stat-card" style="border-left: 5px solid var(--secondary);">
                    <div class="stat-info"><h3><?php echo $solved_tickets; ?></h3><p>সমাধানকৃত টিকিট</p></div>
                    <i class="fas fa-check-double" style="color: var(--secondary);"></i>
                </div>
                <div class="stat-card" style="border-left: 5px solid #6c757d;">
                    <div class="stat-info"><h3><?php echo $total_users; ?></h3><p>মোট ইউজার</p></div>
                    <i class="fas fa-users" style="color: #6c757d;"></i>
                </div>
            </div>

            <!-- চার্ট সেকশন -->
            <div class="charts-container">
                <div class="section-card">
                    <h4 style="margin-top:0;"><i class="fas fa-chart-bar"></i> কানেকশন স্ট্যাটাস</h4>
                    <div id="connectionChart"></div>
                </div>
                <div class="section-card">
                    <h4 style="margin-top:0;"><i class="fas fa-chart-line"></i> টিকিট এনালাইটিক্স</h4>
                    <div id="ticketChart"></div>
                </div>
            </div>

        <?php elseif($view == 'packages'): ?>
            <?php include('package_logic.php'); ?>
        <?php elseif($view == 'sliders'): ?>
            <?php include('slider_logic.php'); ?>
        <?php elseif($view == 'tickets'): ?>
            <?php include('ticket_logic.php'); ?>
        <?php elseif($view == 'connection_requests'): ?>
            <?php include('view_connection_requests.php'); ?>
        <?php elseif($view == 'blogs'): ?>
            <?php include('blog_logic.php'); ?>
        <?php elseif($view == 'media'): ?>
            <?php include('media_logic.php'); ?>
        <?php elseif($view == 'gallery'): ?>
            <?php include('gallery_logic.php'); ?>
        <?php elseif($view == 'testimonials'): ?>
            <?php include('testimonial_logic.php'); ?>
        <?php elseif($view == 'partners'): ?>
            <?php include('partner_logic.php'); ?>
        <?php elseif($view == 'users'): ?>
            <?php include('user_mgmt.php'); ?>
        <?php elseif($view == 'staff'): ?>
            <?php include('staff_mgmt.php'); ?>
        <?php elseif($view == 'footer'): ?>
            <?php include('admin_footer_settings.php'); ?>
        <?php endif; ?>

    </div>
</div>

<script>
    function toggleTheme() {
        const body = document.body;
        const currentTheme = body.getAttribute('data-theme');
        const newTheme = currentTheme === 'light' ? 'dark' : 'light';
        body.setAttribute('data-theme', newTheme);
        localStorage.setItem('theme', newTheme);
        updateThemeUI(newTheme);
    }

    function updateThemeUI(theme) {
        const themeText = document.getElementById('theme-text');
        const themeIcon = document.querySelector('.theme-toggle i');
        if (theme === 'dark') {
            themeText.innerText = 'লাইট মোড';
            themeIcon.className = 'fas fa-sun';
        } else {
            themeText.innerText = 'ডার্ক মোড';
            themeIcon.className = 'fas fa-moon';
        }
    }

    window.onload = () => {
        const savedTheme = localStorage.getItem('theme') || 'light';
        document.body.setAttribute('data-theme', savedTheme);
        updateThemeUI(savedTheme);
        renderCharts();
    };

    function renderCharts() {
        var connectionOptions = {
            series: [{
                name: 'অনুরোধ সংখ্যা',
                data: [<?php echo "$reg_pending, $reg_in_progress, $reg_completed, $reg_canceled"; ?>]
            }],
            chart: { type: 'bar', height: 250, toolbar: {show: false} },
            plotOptions: { bar: { borderRadius: 4, horizontal: false, columnWidth: '45%', distributed: true } },
            colors: ['#f39c12', '#6610f2', '#20c997', '#ed1c24'],
            dataLabels: { enabled: false },
            xaxis: { categories: ['পেন্ডিং', 'চলমান', 'সম্পন্ন', 'বাতিল'] }
        };
        new ApexCharts(document.querySelector("#connectionChart"), connectionOptions).render();

        var ticketOptions = {
            series: [<?php echo "$pending_tickets, $progress_tickets, $solved_tickets"; ?>],
            chart: { type: 'donut', height: 250 },
            labels: ['পেন্ডিং', 'চলমান', 'সমাধানকৃত'],
            colors: ['#ed1c24', '#f39c12', '#57a03a'],
            legend: { position: 'bottom' },
            dataLabels: { enabled: true }
        };
        new ApexCharts(document.querySelector("#ticketChart"), ticketOptions).render();
    }
</script>

</body>
</html>