<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?= __('admin.system_update_report') ?></title>
    
    <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() ?>">
</head>


<body class="bg-light">
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            
            <!-- Header -->
            <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="<?= base_url('admincontrol/dashboard') ?>">
                        <i class="bi bi-arrow-up-circle me-2"></i><?= __('admin.system_update_report') ?>
                    </a>
                    <span class="navbar-text">
                        <span class="badge bg-primary"><?= __('admin.version') ?> <?php echo SCRIPT_VERSION; ?></span>
                    </span>
                </div>
            </nav>

            <?php 
            $update_attempted = is_array($result) && !empty($result);
            if ($update_attempted):
                $is_successfully_updated = true;
                $already_latest_version = false;
                
                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;
                        }
                    }
                }
            ?>

            <!-- Status Card -->
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-body text-center py-4">
                    <div class="mb-3">
                        <?php if ($is_successfully_updated): ?>
                            <i class="bi bi-check-circle-fill text-success" style="font-size: 3rem;"></i>
                        <?php elseif ($already_latest_version): ?>
                            <i class="bi bi-info-circle-fill text-info" style="font-size: 3rem;"></i>
                        <?php else: ?>
                            <i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 3rem;"></i>
                        <?php endif; ?>
                    </div>
                    <h3 class="fw-bold mb-2">
                        <?php if ($is_successfully_updated): ?>
                            <?= __('admin.update_completed_successfully') ?>
                        <?php elseif ($already_latest_version): ?>
                            <?= __('admin.system_already_updated') ?>
                        <?php else: ?>
                            <?= __('admin.update_completed_with_issues') ?>
                        <?php endif; ?>
                    </h3>
                    <p class="text-muted mb-0">
                        <?php if ($is_successfully_updated): ?>
                            <?= __('admin.system_updated_to_version') ?> <?php echo SCRIPT_VERSION; ?>
                        <?php elseif ($already_latest_version): ?>
                            <?= __('admin.system_running_version') ?> <?php echo SCRIPT_VERSION; ?>
                        <?php else: ?>
                            <?= __('admin.review_console_output') ?> <?php echo SCRIPT_VERSION; ?>
                        <?php endif; ?>
                    </p>
                </div>
            </div>

            <?php if ($update_attempted && $is_successfully_updated && file_exists(FCPATH . 'install/')): ?>
            <!-- Remove Installer Section -->
            <div class="row mb-4">
                <div class="col-lg-10 offset-lg-1">
                    <div class="alert alert-warning border-warning shadow-sm" role="alert" id="installer-alert">
                        <div class="d-flex align-items-start">
                            <div class="flex-shrink-0">
                                <i class="bi bi-shield-exclamation fs-2 text-warning"></i>
                            </div>
                            <div class="flex-grow-1 ms-3">
                                <h5 class="alert-heading mb-2 fw-bold">
                                    <i class="bi bi-shield-lock me-2"></i><?= __('admin.secure_your_installation') ?>
                                </h5>
                                <p class="mb-3"><?= __('admin.remove_installer_recommendation') ?></p>
                                <button type="button" class="btn btn-warning btn-lg w-100" id="remove-installer-btn">
                                    <i class="bi bi-trash me-2"></i><?= __('admin.remove_installer_folder_now') ?>
                                </button>
                                <small class="text-muted d-block mt-2" id="remove-feedback"></small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endif; ?>

            <!-- Terminal Console -->
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-header bg-dark text-white">
                    <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.update_console') ?>
                        </span>
                    </div>
                </div>
                <div class="bg-dark text-white p-4 debug-console-body" style="max-height: 500px; overflow-y: auto;">
                    <?php 
                    for ($i = 0; $i < sizeof($result); $i++) { 
                        foreach ($result[$i] as $key => $value) {
                            $color_class = '';
                            $icon = '';
                            switch($key) {
                                case 'success': 
                                    $color_class = 'text-success'; 
                                    $icon = 'bi-check-circle';
                                    break;
                                case 'error': 
                                    $color_class = 'text-danger'; 
                                    $icon = 'bi-x-circle';
                                    break;
                                case 'warning': 
                                    $color_class = 'text-warning'; 
                                    $icon = 'bi-exclamation-triangle';
                                    break;
                                case 'info': 
                                    $color_class = 'text-info'; 
                                    $icon = 'bi-info-circle';
                                    break;
                                default: 
                                    $color_class = 'text-light';
                                    $icon = 'bi-chevron-right';
                            }
                            
                            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 '<i class="' . $icon . ' me-2"></i>';
                            echo htmlspecialchars($value);
                            echo '</div>';
                        }
                    }
                    ?>
                </div>
            </div>

            <?php else: ?>
            
            <!-- No Results Card -->
            <div class="card shadow-sm border-0 mb-4">
                <div class="card-body text-center py-5">
                    <div class="mb-3">
                        <i class="bi bi-exclamation-circle text-warning" style="font-size: 3rem;"></i>
                    </div>
                    <h4 class="fw-bold mb-2"><?= __('admin.no_update_results') ?></h4>
                    <p class="text-muted mb-0"><?= __('admin.no_update_logs_found') ?></p>
                </div>
            </div>

            <?php endif; ?>

            <!-- Actions -->
            <div class="row">
                <div class="col-12 text-center">
                    <a href="<?= base_url('admincontrol/dashboard') ?>" class="btn btn-primary btn-lg">
                        <i class="bi bi-arrow-left me-2"></i><?= __('admin.back_to_dashboard') ?>
                    </a>
                    <?php if ($update_attempted): ?>
                    <a href="<?= base_url('debug/sysupdatereport') ?>" class="btn btn-outline-secondary btn-lg ms-2" target="_blank">
                        <i class="bi bi-file-text me-2"></i><?= __('admin.view_detailed_logs') ?>
                    </a>
                    <?php endif; ?>
                </div>
            </div>

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

<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>

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
    const consoleContainer = document.querySelector('.debug-console-body');
    if (consoleContainer) {
        consoleContainer.scrollTop = consoleContainer.scrollHeight;
    }

    const removeBtn = document.getElementById('remove-installer-btn');
    if (removeBtn) {
        removeBtn.addEventListener('click', function() {
            if (!confirm('<?= __('admin.confirm_remove_installer') ?>')) {
                return;
            }

            const feedback = document.getElementById('remove-feedback');
            const alertBox = document.getElementById('installer-alert');
            
            removeBtn.disabled = true;
            removeBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-2"></span><?= __('admin.removing') ?>...';
            
            if (feedback) {
                feedback.classList.remove('text-success', 'text-danger');
                feedback.textContent = '<?= __('admin.removing_installer_directory') ?>...';
            }

            const licenseKey = '<?= isset($license_key) ? $license_key : "" ?>';
            
            if (!licenseKey || licenseKey === '') {
                if (feedback) {
                    feedback.classList.add('text-danger', 'fw-semibold');
                    feedback.textContent = '<?= __('admin.unable_to_remove_installer') ?> - License key not found.';
                }
                removeBtn.disabled = false;
                removeBtn.innerHTML = '<i class="bi bi-trash me-2"></i><?= __('admin.remove_installer_folder_now') ?>';
                return;
            }
            
            fetch('<?= base_url("installversion/remove_installer_dir") ?>', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'X-Requested-With': 'XMLHttpRequest'
                },
                body: 'license=' + encodeURIComponent(licenseKey)
            })
            .then(response => response.json())
            .then(json => {
                if (json.success) {
                    if (alertBox) {
                        alertBox.classList.remove('alert-warning', 'border-warning');
                        alertBox.classList.add('alert-success', 'border-success');
                        alertBox.innerHTML = `
                            <div class="d-flex align-items-start">
                                <div class="flex-shrink-0">
                                    <i class="bi bi-check-circle-fill fs-3 text-success"></i>
                                </div>
                                <div class="flex-grow-1 ms-3">
                                    <h5 class="alert-heading mb-2 fw-bold">
                                        <i class="bi bi-shield-check me-2"></i><?= __('admin.installation_secured') ?>
                                    </h5>
                                    <p class="mb-0">${json.message || '<?= __('admin.installer_removed_successfully') ?>'}</p>
                                </div>
                            </div>
                        `;
                    }
                } else {
                    removeBtn.disabled = false;
                    removeBtn.innerHTML = '<i class="bi bi-trash me-2"></i><?= __('admin.remove_installer_folder_now') ?>';
                    if (feedback) {
                        feedback.classList.add('text-danger', 'fw-semibold');
                        feedback.textContent = json.message || '<?= __('admin.unable_to_remove_installer') ?>';
                    }
                }
            })
            .catch(err => {
                removeBtn.disabled = false;
                removeBtn.innerHTML = '<i class="bi bi-trash me-2"></i><?= __('admin.remove_installer_folder_now') ?>';
                if (feedback) {
                    feedback.classList.add('text-danger', 'fw-semibold');
                    feedback.textContent = '<?= __('admin.request_failed') ?>: ' + err.message;
                }
            });
        });
    }
});
</script>
</html>