<?php
include 'layout.php'; // Include the styled layout

$current_version = trim(file_get_contents('version.txt'));
$server_url = 'https://itresources.com.pk/itr/api/get_update_changelog_enhanced.php';

// Optional: fetch only updates newer than current version
// $server_url .= '?current_version=' . urlencode($current_version);

$response = file_get_contents($server_url);
$updates = json_decode($response, true);
?>

<body>
  <div class="main-wrapper">
    <div class="page-wrapper">
      <div class="content">
        <div class="page-header d-flex justify-content-between align-items-center">
          <div class="page-title">
            <h3><i class="fas fa-bolt text-primary me-2"></i>New Features & Updates</h3>
            <p class="text-muted fs-5 mb-0">Here's what's new in your ITR POS system</p>
          </div>
        </div>
        
        <!-- Include enhanced CSS -->
        <link rel="stylesheet" href="assets/css/changelog-enhanced.css">

        <?php if (empty($updates)) : ?>
          <div class="alert alert-info fs-5">
            No new updates available.
          </div>
        <?php else : ?>
          <?php foreach ($updates as $update): ?>
            <div class="card mb-4 shadow-sm border-start border-primary border-3">
              <div class="card-body">
                <h4 class="card-title mb-3">
                  <i class="fas fa-code-branch me-1 text-primary"></i>
                  Version <?= htmlspecialchars($update['version']) ?>
                  <small class="text-muted ms-2 fs-6"><?= date('jS M, Y', strtotime($update['date'])) ?></small>
                </h4>
                <div class="card-text">
                  <?php if (isset($update['changelog_html']) && !empty($update['changelog_html'])): ?>
                    <!-- Enhanced display with rich HTML -->
                    <div class="changelog-enhanced">
                      <?= $update['changelog_html'] ?>
                    </div>
                  <?php else: ?>
                    <!-- Fallback to plain text with enhanced formatting -->
                    <div class="changelog-enhanced">
                      <?= nl2br(htmlspecialchars($update['changelog'])) ?>
                    </div>
                  <?php endif; ?>
                </div>
              </div>
            </div>
          <?php endforeach; ?>
        <?php endif; ?>

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