<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    
    <link rel="stylesheet" href="<?= base_url('assets/template/css/bootstrap.min.css') ?>?v=<?= av() ?>">
    <link rel="stylesheet" href="<?= base_url('assets/template/css/bootstrap-icons.css') ?>?v=<?= av() ?>">
    <link rel="stylesheet" href="<?= base_url('assets/template/css/admin-dashboard-custom.css') ?>?v=<?= av() ?>">
    
    <title><?= __('admin.developer_tools') ?></title>
  </head>
<body class="bg-light">
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <nav class="navbar navbar-expand-lg navbar-dark bg-dark shadow-sm mb-4">
                <div class="container-fluid">
                    <a class="navbar-brand fw-bold" href="#">
                        <i class="bi bi-tools me-2"></i><?= __('admin.developer_tools') ?>
                    </a>
                    <button 
                        class="navbar-toggler" 
                        type="button" 
                        data-bs-toggle="collapse" 
                        data-bs-target="#navbarNav" 
                        aria-controls="navbarNav" 
                        aria-expanded="false" 
                        aria-label="Toggle navigation"
                    >
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="collapse navbar-collapse" id="navbarNav">
                        <ul class="navbar-nav me-auto">
                            <li class="nav-item">
                                <a class="nav-link <?= $dtype=="logs" ? "active" : ""; ?>" href="<?= base_url('debug/logs');?>">
                                    <i class="bi bi-file-text me-1"></i><?= __('admin.logs') ?>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?= $dtype=="dbstructure" ? "active" : ""; ?>" href="<?= base_url('debug/dbstructure');?>">
                                    <i class="bi bi-database me-1"></i><?= __('admin.database_structure') ?>
                                </a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link <?= $dtype=="sysupdatereport" ? "active" : ""; ?>" href="<?= base_url('debug/sysupdatereport');?>">
                                    <i class="bi bi-arrow-up-circle me-1"></i><?= __('admin.system_update_report') ?>
                                </a>
                            </li>
                        </ul>
                        <span class="navbar-text">
                            <span class="badge bg-primary"><?= __('admin.version') ?> <?php echo SCRIPT_VERSION; ?></span>
                        </span>
                    </div>
                </div>
            </nav>
            <?php if(isset($error)) { ?>
                <div class="alert alert-danger d-flex align-items-center" role="alert">
                    <i class="bi bi-exclamation-triangle-fill me-2"></i>
                    <div><?= $error ?></div>
                </div>
            <?php } else { ?>
                <?php
                    switch ($dtype) {
                        case 'logs':
                            ?>
                            <div class="row mb-4">
                                <div class="col-md-4">
                                    <div class="card bg-primary text-white border-0 shadow-sm">
                                        <div class="card-body">
                                            <div class="d-flex align-items-center">
                                                <div class="flex-grow-1">
                                                    <h6 class="card-title mb-1"><?= __('admin.total_log_lines') ?></h6>
                                                    <h3 class="mb-0 fw-bold" id="total-lines">0</h3>
                                                </div>
                                                <i class="bi bi-file-earmark-text fs-1 opacity-75"></i>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-danger text-white border-0 shadow-sm">
                                        <div class="card-body">
                                            <div class="d-flex align-items-center">
                                                <div class="flex-grow-1">
                                                    <h6 class="card-title mb-1"><?= __('admin.error_lines') ?></h6>
                                                    <h3 class="mb-0 fw-bold" id="error-lines">0</h3>
                                                </div>
                                                <i class="bi bi-exclamation-triangle fs-1 opacity-75"></i>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="card bg-success text-white border-0 shadow-sm">
                                        <div class="card-body">
                                            <div class="d-flex align-items-center">
                                                <div class="flex-grow-1">
                                                    <h6 class="card-title mb-1"><?= __('admin.log_file_size') ?></h6>
                                                    <h3 class="mb-0 fw-bold" id="file-size">0 KB</h3>
                                                </div>
                                                <i class="bi bi-hdd fs-1 opacity-75"></i>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="card shadow-sm border-0">
                                <div class="card-header bg-primary text-white">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <h5 class="mb-0 fw-bold">
                                            <i class="bi bi-terminal me-2"></i><?= __('admin.log_viewer') ?>
                                        </h5>
                                        <div class="d-flex gap-2">
                                            <div class="dropdown">
                                                <button class="btn btn-outline-light btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                                    <i class="bi bi-funnel me-1"></i><?= __('admin.filter') ?>
                                                </button>
                                                <ul class="dropdown-menu">
                                                    <li><a class="dropdown-item" href="#" onclick="filterLogs('all')"><?= __('admin.all_logs') ?></a></li>
                                                    <li><a class="dropdown-item" href="#" onclick="filterLogs('error')"><?= __('admin.errors_only') ?></a></li>
                                                    <li><a class="dropdown-item" href="#" onclick="filterLogs('warning')"><?= __('admin.warnings_only') ?></a></li>
                                                    <li><a class="dropdown-item" href="#" onclick="filterLogs('info')"><?= __('admin.info_only') ?></a></li>
                                                </ul>
                                            </div>
                                            <button class="btn btn-outline-light btn-sm" onclick="refreshLogs()">
                                                <i class="bi bi-arrow-clockwise me-1"></i><?= __('admin.refresh') ?>
                                            </button>
                                            <button class="btn btn-outline-light btn-sm" onclick="clearLogs()">
                                                <i class="bi bi-trash me-1"></i><?= __('admin.clear') ?>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                                            
                                <div class="card-body p-0">
                                    <div class="debug-log-container bg-dark text-white overflow-auto p-3" style="height: 500px;">
                                        <div id="log-container">
                                            <?php 
                            if (empty(trim($log))) {
                                echo '<div class="text-center text-muted py-5">';
                                echo '<i class="bi bi-info-circle fs-1"></i>';
                                echo '<p class="mt-3 mb-0">' . __('admin.no_logs_available') . '</p>';
                                echo '</div>';
                            } else {
                                $lines = explode("\n", $log);
                                $line_number = 1;
                                foreach ($lines as $line) {
                                    if (trim($line)) {
                                        $line = htmlspecialchars($line);
                                        $type = 'info';
                                        $icon = 'bi-info-circle';
                                        $color = 'text-info';
                                        
                                        if (stripos($line, 'error') !== false || stripos($line, 'fatal') !== false) {
                                            $type = 'error';
                                            $icon = 'bi-x-circle';
                                            $color = 'text-danger';
                                        } elseif (stripos($line, 'warning') !== false || stripos($line, 'warn') !== false) {
                                            $type = 'warning';
                                            $icon = 'bi-exclamation-triangle';
                                            $color = 'text-warning';
                                        } elseif (stripos($line, 'success') !== false || stripos($line, 'ok') !== false) {
                                            $type = 'success';
                                            $icon = 'bi-check-circle';
                                            $color = 'text-success';
                                        }
                                        
                                        echo '<div class="log-line mb-2 p-2 rounded border-start border-3 border-secondary log-' . $type . '" data-type="' . $type . '" data-raw="' . htmlspecialchars($line) . '">';
                                        echo '<div class="d-flex align-items-start">';
                                        echo '<span class="badge bg-secondary me-2 debug-line-number">' . $line_number . '</span>';
                                        echo '<i class="' . $icon . ' ' . $color . ' me-2"></i>';
                                        echo '<code class="text-light flex-grow-1 debug-log-text">' . $line . '</code>';
                                        echo '</div>';
                                        echo '</div>';
                                        $line_number++;
                                    }
                                }
                            }
                            ?>
                        </div>
                    </div>
                </div>
                
                <div class="card-footer bg-light border-0">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center gap-3">
                            <span class="text-muted small">
                                <i class="bi bi-clock me-1"></i><?= __('admin.last_updated') ?>: <span id="last-update"><?= date('H:i:s') ?></span>
                            </span>
                            <div class="form-check form-switch">
                                <input class="form-check-input" type="checkbox" id="auto-scroll-toggle" checked>
                                <label class="form-check-label text-muted small" for="auto-scroll-toggle">
                                    <?= __('admin.auto_scroll') ?>
                                </label>
                            </div>
                        </div>
                        <div class="d-flex gap-2">
                            <button class="btn btn-outline-secondary btn-sm" onclick="clearSearch()">
                                <i class="bi bi-x-circle me-1"></i><?= __('admin.clear_filter') ?>
                            </button>
                            <button class="btn btn-primary btn-sm" onclick="downloadLogs()">
                                <i class="bi bi-download me-1"></i><?= __('admin.download') ?>
                            </button>
                        </div>
                    </div>
                </div>
            </div>

                        <script>
                        let autoScrollEnabled = true;

                        // Count lines and update dashboard
                        document.addEventListener('DOMContentLoaded', function() {
                            const logLines = document.querySelectorAll('.log-line');
                            const errorLines = document.querySelectorAll('.log-error');
                            
                            document.getElementById('total-lines').textContent = logLines.length;
                            document.getElementById('error-lines').textContent = errorLines.length;
                            
                            // Setup auto-scroll toggle first
                            setupAutoScrollToggle();
                            
                            // Initial auto scroll to bottom if enabled
                            if (autoScrollEnabled) {
                                scrollToBottom();
                            }
                        });

                        function scrollToBottom() {
                            const container = document.querySelector('.bg-black');
                            if (container) {
                                container.scrollTop = container.scrollHeight;
                            }
                        }

                        function setupAutoScrollToggle() {
                            const autoScrollToggle = document.getElementById('auto-scroll-toggle');
                            
                            if (autoScrollToggle) {
                                // Load saved preference from localStorage
                                const savedState = localStorage.getItem('logsAutoScroll');
                                if (savedState !== null) {
                                    autoScrollEnabled = savedState === 'true';
                                    autoScrollToggle.checked = autoScrollEnabled;
                                } else {
                                    autoScrollEnabled = autoScrollToggle.checked; // Default to checked
                                }
                                
                                autoScrollToggle.addEventListener('change', function() {
                                    autoScrollEnabled = this.checked;
                                    
                                    // Save preference to localStorage
                                    localStorage.setItem('logsAutoScroll', autoScrollEnabled.toString());
                                    
                                    if (autoScrollEnabled) {
                                        scrollToBottom();
                                        showToast('Auto-scroll enabled');
                                    } else {
                                        showToast('Auto-scroll disabled');
                                    }
                                });
                            }
                        }

                        function showToast(message, type = 'info') {
                            // Create a simple toast notification
                            let alertClass, icon;
                            switch(type) {
                                case 'success':
                                    alertClass = 'alert-success';
                                    icon = 'bi-check-circle';
                                    break;
                                case 'error':
                                    alertClass = 'alert-danger';
                                    icon = 'bi-exclamation-triangle';
                                    break;
                                default:
                                    alertClass = 'alert-info';
                                    icon = 'bi-info-circle';
                            }
                            
                            const toast = document.createElement('div');
                            toast.className = `position-fixed top-0 end-0 m-3 alert ${alertClass} alert-dismissible fade show`;
                            toast.style.zIndex = '9999';
                            toast.innerHTML = `
                                <i class="${icon} me-2"></i>${message}
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            `;
                            document.body.appendChild(toast);
                            
                            // Auto remove after 4 seconds for error messages, 3 for others
                            const timeout = type === 'error' ? 4000 : 3000;
                            setTimeout(() => {
                                if (toast.parentNode) {
                                    toast.remove();
                                }
                            }, timeout);
                        }

                        function filterLogs(type) {
                            const logLines = document.querySelectorAll('.log-line');
                            let visibleCount = 0;
                            
                            logLines.forEach(line => {
                                if (type === 'all' || line.dataset.type === type) {
                                    line.style.display = 'block';
                                    visibleCount++;
                                } else {
                                    line.style.display = 'none';
                                }
                            });
                            
                            // Auto scroll after filtering if enabled
                            if (autoScrollEnabled) {
                                setTimeout(scrollToBottom, 100);
                            }
                        }

                        function clearSearch() {
                            filterLogs('all');
                        }

                        function refreshLogs() {
                            location.reload();
                        }

                        function clearLogs() {
                            // Show confirmation dialog
                            const clearBtn = event.target.closest('button');
                            window.confirmDelete('Are you sure you want to permanently clear all log files? This action cannot be undone!', function() {
                                // Show loading state
                                const originalText = clearBtn.innerHTML;
                                clearBtn.innerHTML = '<i class="bi bi-hourglass-split me-1"></i> Clearing...';
                                clearBtn.disabled = true;
                                
                                // Make AJAX request to clear logs
                                fetch('<?= base_url("admincontrol/clear_logs") ?>', {
                                    method: 'POST',
                                    headers: {
                                        'Content-Type': 'application/json',
                                        'X-Requested-With': 'XMLHttpRequest'
                                    },
                                    body: JSON.stringify({ action: 'clear_logs' })
                                })
                                .then(response => response.json())
                                .then(data => {
                                    // Reset button
                                    clearBtn.innerHTML = originalText;
                                    clearBtn.disabled = false;
                                    
                                    if (data.success) {
                                        // Clear the display
                                        const logContainer = document.getElementById('log-container');
                                        if (logContainer) {
                                            logContainer.innerHTML = `
                                                <div class="text-center text-success py-5">
                                                    <i class="bi bi-check-circle" style="font-size: 3rem;"></i>
                                                    <p class="mt-3 mb-0">Log files cleared successfully!</p>
                                                    <small class="text-muted">All log files have been permanently deleted</small>
                                                </div>
                                            `;
                                        }
                                        
                                        // Update dashboard counters
                                        document.getElementById('total-lines').textContent = '0';
                                        document.getElementById('error-lines').textContent = '0';
                                        
                                        // Show success toast
                                        showToast('Log files cleared successfully!', 'success');
                                    } else {
                                        showToast('Error: ' + (data.message || 'Failed to clear logs'), 'error');
                                    }
                                })
                                .catch(error => {
                                    // Reset button
                                    clearBtn.innerHTML = originalText;
                                    clearBtn.disabled = false;
                                    
                                    console.error('Error:', error);
                                    showToast('Failed to clear logs. Please try again.', 'error');
                                });
                            });
                        }

                        function downloadLogs() {
                            const logLines = document.querySelectorAll('.log-line');
                            let logContent = '';
                            
                            logLines.forEach((line, index) => {
                                if (line.style.display !== 'none') {
                                    const rawData = line.dataset.raw || line.querySelector('code').textContent;
                                    logContent += (index + 1) + ': ' + rawData + '\n';
                                }
                            });
                            
                            if (logContent.trim() === '') {
                                logContent = 'No logs available';
                            }
                            
                            const blob = new Blob([logContent], { type: 'text/plain' });
                            const url = window.URL.createObjectURL(blob);
                            const a = document.createElement('a');
                            a.href = url;
                            a.download = 'system-logs-' + new Date().toISOString().slice(0,19).replace(/:/g, '-') + '.txt';
                            document.body.appendChild(a);
                            a.click();
                            document.body.removeChild(a);
                            window.URL.revokeObjectURL(url);
                        }
                        </script>
                        <?php
                        break;
                    case 'dbstructure':
                        ?>
            <div class="card bg-primary text-white border-0 shadow-sm mb-4">
                <div class="card-body text-center">
                    <div class="mb-3">
                        <i class="bi bi-database-fill fs-1 opacity-75"></i>
                    </div>
                    <h3 class="fw-bold text-white mb-2"><?= __('admin.database_structure') ?></h3>
                    <p class="text-white opacity-75 mb-0">
                        <span class="badge bg-light text-primary px-3 py-2">
                            <?= count($dbstructure); ?> <?= __('admin.tables') ?>
                        </span>
                    </p>
                </div>
            </div>
            
            <div class="accordion shadow-sm" id="accordion-dbstructure">
                    <?php 
                    $table_index = 0;
                    foreach ($dbstructure as $table => $structure) { 
                        $has_primary_key = false;
                        foreach ($structure as $str) {
                            if ($str->primary_key) {
                                $has_primary_key = true;
                                break;
                            }
                        }
                    ?>
                        <div class="accordion-item">
                            <h2 class="accordion-header" id="heading-<?= $table; ?>">
                                <button class="accordion-button bg-primary text-white fw-bold collapsed" 
                                        type="button" 
                                        data-bs-toggle="collapse" 
                                        data-bs-target="#collapse-<?= $table; ?>" 
                                        aria-expanded="false" 
                                        aria-controls="collapse-<?= $table; ?>">
                                    <div class="d-flex align-items-center w-100">
                                        <i class="bi bi-table me-3 text-info"></i>
                                        <span class="me-auto debug-table-name"><?= $table; ?></span>
                                        <div class="d-flex gap-2">
                                            <span class="badge <?= $has_primary_key ? 'bg-success' : 'bg-danger' ?> px-2">
                                                <i class="bi bi-key-fill me-1"></i>
                                                <?= $has_primary_key ? __('admin.has_primary_key') : __('admin.no_primary_key') ?>
                                            </span>
                                            <span class="badge bg-secondary px-2">
                                                <?= count($structure); ?> <?= __('admin.columns') ?>
                                            </span>
                                        </div>
                                    </div>
                                </button>
                            </h2>
                            <div id="collapse-<?= $table; ?>" 
                                 class="accordion-collapse collapse" 
                                 aria-labelledby="heading-<?= $table; ?>" 
                                 data-bs-parent="#accordion-dbstructure">
                                <div class="accordion-body bg-dark text-white p-0">
                                    <div class="table-responsive">
                                        <table class="table table-dark table-hover mb-0 debug-table">
                                            <thead class="table-secondary">
                                                <tr>
                                                    <th class="fw-bold"><i class="bi bi-tag me-2"></i><?= __('admin.name') ?></th>
                                                    <th class="fw-bold"><i class="bi bi-gear me-2"></i><?= __('admin.type') ?></th>
                                                    <th class="fw-bold"><i class="bi bi-rulers me-2"></i><?= __('admin.max_length') ?></th>
                                                    <th class="fw-bold"><i class="bi bi-file-text me-2"></i><?= __('admin.default') ?></th>
                                                    <th class="fw-bold"><i class="bi bi-key me-2"></i><?= __('admin.primary_key') ?></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                                foreach ($structure as $str) {
                                                    $is_primary = $str->primary_key;
                                                    ?>
                                                    <tr class="<?= $is_primary ? 'table-success' : '' ?>">
                                                        <td class="<?= $is_primary ? 'fw-bold' : '' ?>">
                                                            <?= $is_primary ? '<i class="bi bi-key-fill me-2 text-warning"></i>' : '' ?>
                                                            <?= $str->name; ?>
                                                        </td>
                                                        <td>
                                                            <span class="badge bg-info rounded-pill"><?= $str->type; ?></span>
                                                        </td>
                                                        <td class="text-center">
                                                            <?= $str->max_length ?: '<span class="text-muted">-</span>'; ?>
                                                        </td>
                                                        <td>
                                                            <?= $str->default ? '<code class="text-success">' . $str->default . '</code>' : '<span class="text-muted">NULL</span>'; ?>
                                                        </td>
                                                        <td class="text-center">
                                                            <?= $str->primary_key ? 
                                                                '<i class="bi bi-check-circle-fill text-success" title="Primary Key"></i>' : 
                                                                '<i class="bi bi-dash-circle text-muted" title="Not Primary Key"></i>'; ?>
                                                        </td>
                                                    </tr>
                                                    <?php
                                                }
                                            ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>                                    
                    <?php 
                        $table_index++;
                    } ?>
                </div>
            </div>
        </div>
    </div>
</div>
                        <?php
                        break;
                    case 'sysupdatereport':
                        ?>
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header bg-primary text-white">
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex align-items-center">
                            <div class="d-flex me-3">
                                <span class="bg-danger rounded-circle me-1 debug-terminal-dot"></span>
                                <span class="bg-warning rounded-circle me-1 debug-terminal-dot"></span>
                                <span class="bg-success rounded-circle debug-terminal-dot"></span>
                            </div>
                            <span class="fw-medium">
                                <i class="bi bi-terminal me-2"></i><?= __('admin.system_update_console') ?>
                            </span>
                        </div>
                        <div class="d-flex align-items-center">
                            <div class="dropdown me-3">
                                <button class="btn btn-outline-light btn-sm dropdown-toggle" type="button" id="log-dropdown-btn" data-bs-toggle="dropdown" aria-expanded="false">
                                    <?= __('admin.log_files') ?>
                                </button>
                                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="log-dropdown">
                                            <?php 
                                                if (empty($log_files)) {
                                                    echo '<li><span class="dropdown-item text-muted">No log files available</span></li>';
                                                } else {
                                                    foreach($log_files as $file) { 
                                                        $nameExplode = explode('-', str_replace('.json', '', $file));
                                                        $selected = (isset($selected_file) && $selected_file == $file) ? "selected" : "";
                                            ?>
                                                <li>
                                                    <a class="dropdown-item <?= $selected ?>" href="<?= base_url('/debug/sysupdatereport/').str_replace('.json', '', $file) ?>">
                                                        <?= date('d-m-Y H:i:s', (int) $nameExplode[0]) ?> | <?= str_replace('_', '.', $nameExplode[1]) ?>
                                                    </a>
                                                </li>
                                            <?php 
                                                    }
                                                }
                                            ?>
                                        </ul>
                                    </div>
                                    <span class="badge bg-primary px-3 py-2">
                                        v<?php echo SCRIPT_VERSION; ?>
                                    </span>
                                </div>
                            </div>
                        </div>

                        <div class="bg-dark text-white p-4 debug-console-body" style="max-height: 500px; overflow-y: auto;">
                            <?php 
                            $update_attempted = false; // Flag to determine whether an update was attempted
                            
                            if (isset($result) && is_array($result) && !empty($result)) {
                                $update_attempted = true; // An update was attempted if $result is set
                                $is_successfully_updated = true;
                                for ($i=0; $i < sizeof($result); $i++) { 
                                    foreach ($result[$i] as $key => $value) {
                                        if($key == 'error') { 
                                            if($is_successfully_updated == true && str_contains($value, 'already a latest version')) {
                                                $already_latest_version = true;
                                            }
                                            $is_successfully_updated = false;
                                        }
                                        
                                        $color_class = '';
                                        $icon = '';
                                        switch($key) {
                                            case 'success': 
                                                $color_class = 'text-success'; 
                                                $icon = '✓';
                                                break;
                                            case 'error': 
                                                $color_class = 'text-danger'; 
                                                $icon = '✗';
                                                break;
                                            case 'warning': 
                                                $color_class = 'text-warning'; 
                                                $icon = '⚠';
                                                break;
                                            case 'info': 
                                                $color_class = 'text-info'; 
                                                $icon = 'ℹ';
                                                break;
                                            default: 
                                                $color_class = 'text-light';
                                                $icon = '>';
                                        }
                                        
                                        echo '<div class="console mb-2 d-flex align-items-center ' . $color_class . '">';
                                        echo '<span class="text-muted me-2">[' . date('H:i:s') . ']</span>';
                                        echo '<span class="fw-bold me-2">' . $icon . '</span>';
                                        echo htmlspecialchars($value);
                                        echo '</div>';
                                    }
                                }
                            }
                            ?>
                        </div>
                    </div>

                    <!-- Status Message -->
                    <?php if ($update_attempted): ?>
                    <div class="row justify-content-center mb-3">
                        <div class="col-lg-10">
                            <div class="bg-black text-white shadow rounded-3 border border-secondary border-opacity-25" style="font-size: 14px; line-height: 1.6;">
                                <div class="p-3 text-center">
                                    <div class="mb-2">
                                        <?php
                                        if ($is_successfully_updated) {
                                            echo '<i class="bi bi-check-circle-fill text-success" style="font-size: 1.5rem;"></i>';
                                        } elseif (isset($already_latest_version)) {
                                            echo '<i class="bi bi-info-circle-fill text-info" style="font-size: 1.5rem;"></i>';
                                        } else {
                                            echo '<i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 1.5rem;"></i>';
                                        }
                                        ?>
                                    </div>
                                    <div class="fw-bold text-white mb-1" style="font-size: 16px;">
                                        <?php
                                        if ($is_successfully_updated) {
                                            echo 'Update Completed Successfully!';
                                        } elseif (isset($already_latest_version)) {
                                            echo 'System Already Up-to-Date';
                                        } else {
                                            echo 'Update Completed with Issues';
                                        }
                                        ?>
                                    </div>
                                    <div class="text-light opacity-75" style="font-size: 14px;">
                                        <?php 
                                        if ($is_successfully_updated) {
                                            echo 'Your system has been successfully updated to version ' . SCRIPT_VERSION . '.';
                                        } elseif (isset($already_latest_version)) {
                                            echo 'Your system is already running version ' . SCRIPT_VERSION . '.';
                                        } else {
                                            echo 'Please review the console output above for version ' . SCRIPT_VERSION . ' details.';
                                        }
                                        ?>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>
                    
                </div>
            </div>
        </div>
    </div>

                        <?php
                        break;
                    default:
                        ?>
<span class="badge bg-warning w-100 fs-4 d-block mt-3">
Something went wrong, please try again!
</span>
                        <?php
                        break;
                }
            ?>
            <?php } ?>
        </div>
    </div>
</div>

    <!--bootstrap 5 js files-->
     <script src="<?= base_url('assets/template/js/jquery-3.6.0.min.js'); ?>"></script>
     <script src="<?= base_url('assets/template/js/bootstrap.bundle.min.js'); ?>"></script>
    <!--bootstrap 5 js files-->

    <!-- Global Delete Confirmation Modal (standalone debug page - no admin footer) -->
    <div class="modal fade" id="globalDeleteConfirm" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-sm">
            <div class="modal-content border-0 shadow">
                <div class="modal-header bg-danger text-white border-0">
                    <h6 class="modal-title"><i class="bi bi-exclamation-triangle me-2"></i><?= __('admin.confirm_delete') ?? 'Confirm Delete' ?></h6>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body text-center py-4">
                    <p class="mb-0" id="globalDeleteMessage"><?= __('admin.are_you_sure_delete') ?? 'Are you sure you want to delete this item?' ?></p>
                </div>
                <div class="modal-footer border-0 justify-content-center">
                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal"><?= __('admin.cancel') ?? 'Cancel' ?></button>
                    <button type="button" class="btn btn-danger btn-sm" id="globalDeleteConfirmBtn"><i class="bi bi-trash me-1"></i><?= __('admin.delete') ?? 'Delete' ?></button>
                </div>
            </div>
        </div>
    </div>
    <script>
    window.confirmDelete = function(message, callback) {
        if (message) $('#globalDeleteMessage').text(message);
        var modal = new bootstrap.Modal(document.getElementById('globalDeleteConfirm'));
        $('#globalDeleteConfirmBtn').off('click').on('click', function() {
            modal.hide();
            if (typeof callback === 'function') callback();
        });
        modal.show();
    };
    </script>

    <?php if($dtype=="sysupdatereport") { ?>

<script type="text/javascript"> 
    var $container = $('.bg-black'),
    $scrollTo = $('.console:last-child');
    $container.animate({
        scrollTop: $scrollTo.offset().top - $container.offset().top + $container.scrollTop()
    });

    function redirectTOLog(that) {
        window.location.href = "<?= base_url('/debug/sysupdatereport/') ?>"+that.value;
    }
</script>

<script>
    document.addEventListener("DOMContentLoaded", function() {
    // Check if the page has been automatically reloaded
    if (sessionStorage.getItem('autoSelectedLastItem') !== 'true') {
        const dropdownMenu = document.querySelector('.dropdown-menu');
        const lastItem = dropdownMenu.lastElementChild;
        
        if (lastItem) {
            const lastItemLink = lastItem.querySelector('a');
            const lastItemURL = lastItemLink.getAttribute('href');
            const dropdownButton = document.getElementById('log-dropdown-btn');

            // Update button label
            if (dropdownButton) {
                dropdownButton.textContent = lastItemLink.textContent;
            }

            // Set a flag in sessionStorage
            sessionStorage.setItem('autoSelectedLastItem', 'true');

            // Redirect to the URL of the last item
            if (lastItemURL) {
                window.location.href = lastItemURL;
            }
        }
    } else {
        // Reset the flag so manual selection still works
        sessionStorage.setItem('autoSelectedLastItem', 'false');
    }
    });
</script>
    <?php } ?>
  </body>
</html>