<div class="row resoponsiveRow">
	<?php include APPPATH . "views/backend/dashboard/admin_inc/alert_info.php"; ?>
</div>

<div class="row resoponsiveRow">

	<div class="col-md-9">
		<?php
		$installed_version = isset($settings['version']) ? $settings['version'] : '';
		$latest_version = defined('NEW_VERSION') ? NEW_VERSION : $installed_version;
		$is_update_available = !empty($installed_version) && version_compare($latest_version, $installed_version, '>');
		$versions = isset($whats_new_versions) && is_array($whats_new_versions) ? $whats_new_versions : [];
		$per_page = 5;
		$total_versions = count($versions);
		$total_pages = max(1, (int) ceil($total_versions / $per_page));
		$current_page = (int) ($this->input->get('wn_page', true) ?? 1);
		$current_page = max(1, min($current_page, $total_pages));
		$offset = ($current_page - 1) * $per_page;
		$paged_versions = array_slice($versions, $offset, $per_page);
		$start_version = $total_versions > 0 ? $offset + 1 : 0;
		$end_version = min($offset + $per_page, $total_versions);
		$query = $this->input->get();
		unset($query['wn_page']);
		$build_page_url = function ($page) use ($query) {
			$params = array_merge($query, ['wn_page' => $page]);
			$q = http_build_query($params);
			return current_url() . (!empty($q) ? '?' . $q : '');
		};
		?>

		<div class="card">
			<div class="card-header border-0">
				<h3 class="card-title">What's New</h3>
			</div>
			<div class="card-body">
				<div class="single_app_info border-bottom pb-15 mb-15">
					<div class="d-flex justify-content-between align-items-center mb-10">
						<p class="text-md font-weight-bold m-0">
							<i class="icofont-code-alt fz-20 mr-5 c-primary"></i> Installed Version
						</p>
						<span class="badge bg-primary-soft text-md badge-pill"><?= html_escape($installed_version); ?></span>
					</div>
					<div class="d-flex justify-content-between align-items-center">
						<p class="text-md font-weight-bold m-0">
							<i class="icofont-cloud-refresh fz-20 mr-5 c-primary"></i> Latest Version
						</p>
						<span class="badge bg-light text-md badge-pill"><?= html_escape($latest_version); ?></span>
					</div>
				</div>

				<?php if ($is_update_available) : ?>
					<div class="alert alert-warning">
						New release available. Visit dashboard to start update and review release notes below.
					</div>
					<a href="<?= base_url('admin/dashboard'); ?>" class="btn btn-secondary mb-20">
						<i class="fa fa-cloud-download"></i> Open Update Wizard
					</a>
				<?php else : ?>
					<div class="alert alert-success">You are already using the latest version.</div>
				<?php endif; ?>

				<?php if (!empty($paged_versions)) : ?>
					<div class="d-flex justify-content-between align-items-center mb-10">
						<small class="text-muted">Showing <?= $start_version; ?>-<?= $end_version; ?> of <?= $total_versions; ?> versions</small>
						<small class="text-muted">Page <?= $current_page; ?> / <?= $total_pages; ?></small>
					</div>

					<?php foreach ($paged_versions as $note) : ?>
						<div class="single_app_info border rounded p-15 mb-15">
							<h4 class="mt-0 mb-10">Version <?= html_escape($note['version']); ?></h4>
							<?php if (!empty($note['features'])) : ?>
								<?php foreach ($note['features'] as $feature) : ?>
									<div class="bb_dashed border rounded p-5 mb-10">
										<?php if (!empty($feature['image'])) : ?>
											<div class="mb-10">
												<img src="<?= html_escape($feature['image']); ?>" alt="<?= html_escape($feature['title']); ?>" style="max-width: 100%; height: auto; border-radius: 6px;">
											</div>
										<?php endif; ?>

										<div class="whatsNewContent">
											<?php if (!empty($feature['title'])) : ?>
												<h5 class="mt-0 mb-5">#<?= html_escape($feature['serial'] ?? ''); ?> <?= html_escape($feature['title']); ?></h5>
											<?php endif; ?>
											<?php if (!empty($feature['description'])) : ?>
												<small class="text-muted"><?= html_escape($feature['description']); ?></small>
											<?php endif; ?>
											<?php if (!empty($feature['url'])) : ?>
												<a class="btn btn-sm btn-primary" target="_blank" href="<?= html_escape($feature['url']); ?>">Learn More</a>
											<?php endif; ?>
										</div>
									</div>
								<?php endforeach; ?>
							<?php else : ?>
								<p class="mb-0 text-muted">No features added yet.</p>
							<?php endif; ?>
						</div>
					<?php endforeach; ?>

					<?php if ($total_pages > 1) : ?>
						<div class="d-flex justify-content-between align-items-center mt-20">
							<div>
								<?php if ($current_page > 1) : ?>
									<a class="btn btn-default btn-sm" href="<?= html_escape($build_page_url($current_page - 1)); ?>">
										<i class="fa fa-angle-left"></i> Previous
									</a>
								<?php else : ?>
									<button class="btn btn-default btn-sm" disabled><i class="fa fa-angle-left"></i> Previous</button>
								<?php endif; ?>
							</div>
							<div>
								<?php if ($current_page < $total_pages) : ?>
									<a class="btn btn-default btn-sm" href="<?= html_escape($build_page_url($current_page + 1)); ?>">
										Next <i class="fa fa-angle-right"></i>
									</a>
								<?php else : ?>
									<button class="btn btn-default btn-sm" disabled>Next <i class="fa fa-angle-right"></i></button>
								<?php endif; ?>
							</div>
						</div>
					<?php endif; ?>
				<?php else : ?>
					<div class="alert alert-info mb-0">Release notes are not available yet.</div>
				<?php endif; ?>
			</div>
		</div>
	</div>
</div>
