<?php
// admin/dashboard.php
include __DIR__ . '/../includes/header.php';

if (!isLoggedIn() || !isAdmin()) {
    $_SESSION['alert'] = displayAlert($lang_alerts['ALERT_ADMIN_NO_PERMISSION'], 'danger');
    redirectTo(getCleanUrl('index'));
}

// Get statistics
$stmt = $pdo->prepare("SELECT COUNT(*) as total FROM users");
$stmt->execute();
$total_users = $stmt->fetch()['total'];

$stmt = $pdo->prepare("SELECT COUNT(*) as total FROM tickets");
$stmt->execute();
$total_tickets = $stmt->fetch()['total'];

$stmt = $pdo->prepare("SELECT COUNT(*) as total FROM tickets WHERE status = 'open'");
$stmt->execute();
$open_tickets = $stmt->fetch()['total'];

$total_projects = 0;
if (defined('ENABLE_PROJECTS') && ENABLE_PROJECTS) {
    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM projects");
    $stmt->execute();
    $total_projects = $stmt->fetch()['total'];
}

$total_orders = 0;
$total_quotes = 0;
$total_invoices = 0;
$total_subscriptions = 0;
if (defined('ENABLE_STORE') && ENABLE_STORE) {
    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM orders");
    $stmt->execute();
    $total_orders = $stmt->fetch()['total'];

    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM quotes");
    $stmt->execute();
    $total_quotes = $stmt->fetch()['total'];

    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM invoices");
    $stmt->execute();
    $total_invoices = $stmt->fetch()['total'];

    $stmt = $pdo->prepare("SELECT COUNT(*) as total FROM subscriptions");
    $stmt->execute();
    $total_subscriptions = $stmt->fetch()['total'];
}

// License validation removed for v2.0 as all features are now consolidated.

?>
<p><b><?php echo sprintf($lang_admin['ADMIN_DASHBOARD_WELCOME_MESSAGE'], APP_NAME, '', APP_VERSION); ?></b></p>
<p><?php echo sprintf($lang_admin['ADMIN_DASHBOARD_MADE_BY'], '<a href="https://www.elonovo.com" target="_blank">Elonovo</a>'); ?>
    | <a href="https://supportengine.cloud"
        target="_blank"><?php echo $lang_admin['ADMIN_DASHBOARD_MARKETING_WEBSITE_LINK']; ?></a> | <a
        href="https://supportengine.cloud/help/ticket_create"
        target="_blank"><?php echo $lang_admin['ADMIN_DASHBOARD_TECH_SUPPORT_LINK']; ?></a>
    <?php if (defined('APP_MODE') && APP_MODE === 'Cloud'): ?>
        <br>
        <b><?php echo $lang_admin['ADMIN_DASHBOARD_CLOUD_URL'] ?? 'URL to add to your website or marketing emails:'; ?></b>
        <?php echo BASE_URL; ?><br>
        <?php echo $lang_admin['ADMIN_DASHBOARD_CLOUD_URL_DETAILS'] ?? '(you can also order a custom one with your business name)'; ?>
    <?php endif; ?>
</p>
<?php
if (defined('SETUP_DONE') && !SETUP_DONE) {
    // You can hard-code the message, or reference a string from $lang_alerts
    echo displayAlert(
        $lang_alerts['ALERT_SETUP_NOT_DONE']
        ?? 'Setup is not complete – &nbsp;<a href="assistant">Open the Online Assistant</a>&nbsp;or go to System Settings to complete this task.',
        'warning',   // Bootstrap alert colour
        true,        // we’re passing HTML in the message
        false        // don’t auto-dismiss
    );

}
?>
<div class="row mb-4">
    <div class="col-md-2">
        <div class="card bg-light-primary text-dark h-70">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-uppercase"><?php echo $lang_admin['ADMIN_DASHBOARD_STAT_TOTAL_USERS']; ?></h6>
                        <h2 class="mb-0"><?php echo $total_users; ?></h2>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-light-success text-dark h-70">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="text-uppercase"><?php echo $lang_admin['ADMIN_DASHBOARD_STAT_OPEN_TICKETS']; ?></h6>
                        <h2 class="mb-0"><?php echo $open_tickets; ?></h2>
                    </div>

                </div>
            </div>
        </div>
    </div>
    
    
</div>

<div class="row">
    <div class="col-12">
        <div class="card mb-4">
            <div class="card-header bg-primary text-white">
                <h5 class="mb-0"><?php echo $lang_admin['ADMIN_DASHBOARD_CARD_TITLE']; ?></h5>
            </div>
            <div class="card-body">
                <p><?php echo $lang_admin['ADMIN_DASHBOARD_CARD_WELCOME_TEXT']; ?></p>
                <div class="row mt-4">

                    <div class="col-md-4 mb-3">
                        <a href="users" class="btn btn-outline-primary btn-lg w-100">
                            <i class="bi bi-people me-2"></i> <?php echo $lang_admin['ADMIN_DASHBOARD_BTN_USERS']; ?>
                        </a>
                    </div>
                    <?php if (isLoggedIn() && defined('ENABLE_FAQS') && ENABLE_FAQS): ?>
                        <div class="col-md-4 mb-3">
                            <a href="../faq" class="btn btn-outline-primary btn-lg w-100">
                                <i class="bi bi-question-circle me-2"></i>
                                <?php echo $lang_navigation['NAV_FAQS'] ?? 'FAQs'; ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    <?php if (isLoggedIn() && defined('ENABLE_NEWS') && ENABLE_NEWS): ?>
                        <div class="col-md-4 mb-3">
                            <a href="../news" class="btn btn-outline-primary btn-lg w-100">
                                <i class="bi bi-megaphone me-2"></i> <?php echo $lang_navigation['NAV_NEWS'] ?? 'News'; ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    <div class="col-md-4 mb-3">
                        <a href="pages" class="btn btn-outline-primary btn-lg w-100">
                            <i class="bi bi-file me-2"></i> <?php echo $lang_navigation['NAV_PAGES'] ?? 'Pages'; ?>
                        </a>
                    </div>

                    <div class="col-12">
                        <hr>
                    </div>

                    <div class="col-md-4 mb-3">
                        <a href="tickets" class="btn btn-outline-primary btn-lg w-100">
                            <i class="bi bi-ticket-detailed me-2"></i>
                            <?php echo $lang_admin['ADMIN_DASHBOARD_BTN_TICKETS']; ?>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="tickets_categories" class="btn btn-outline-primary btn-lg w-100">
                            <i class="bi bi-tags me-2"></i>
                            <?php echo $lang_navigation['NAV_DROPDOWN_TICKETS_CATEGORIES']; ?>
                        </a>
                    </div>
                    <div class="col-md-4 mb-3">
                        <a href="snippets" class="btn btn-outline-primary btn-lg w-100">
                            <i class="bi bi-journals me-2"></i>
                            <?php echo $lang_admin['ADMIN_DASHBOARD_BTN_TICKET_SNIPPETS']; ?>
                        </a>
                    </div>
                    <?php if (isLoggedIn() && defined('ENABLE_PROJECTS') && ENABLE_PROJECTS): ?>
                        <div class="col-md-4 mb-3">
                            <a href="../projects" class="btn btn-outline-primary btn-lg w-100">
                                <i class="bi bi-kanban me-2"></i> <?php echo $lang_navigation['NAV_PROJECTS']; ?>
                            </a>
                        </div>
                    <?php endif; ?>
                    <?php if (isLoggedIn() && defined('ENABLE_PROJECTS') && ENABLE_PROJECTS && defined('ENABLE_FILES') && ENABLE_FILES): ?>
                        <div class="col-md-4 mb-3">
                            <a href="../project_files" class="btn btn-outline-primary btn-lg w-100">
                                <i class="bi bi-folder me-2"></i> <?php echo $lang_navigation['NAV_FILES']; ?>
                            </a>
                        </div>
                    <?php endif; ?>

                    <div class="col-12">
                        <hr>
                    </div>


                    

                </div>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>