<?php
include 'layout.php'; // Use your existing layout

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

$response = file_get_contents($server_url . '?current_version=' . urlencode($current_version));
$data = 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">
            <h4><i class="fas fa-sync-alt text-success me-2"></i>System Update Check</h4>
            <p class="text-muted mb-0">Review the latest available ITR POS version and features</p>
          </div>
        </div>

        <?php if (!empty($data['update_available']) && !empty($data['updates'])): ?>
          <div class="alert alert-warning">
            <strong>🚀 New Update Available!</strong> Your current version is <strong>v<?= htmlspecialchars($current_version) ?></strong>.
          </div>

          <?php foreach ($data['updates'] as $update): ?>
            <div class="card mb-3 border-start border-success border-3 shadow-sm">
              <div class="card-body">
                <h5 class="card-title mb-2">
                  <i class="fas fa-code-branch me-1 text-success"></i>
                  Version <?= htmlspecialchars($update['version']) ?>
                  <?php if (!empty($update['is_critical'])): ?>
                    <span class="badge bg-danger ms-2">Critical</span>
                  <?php elseif (!empty($update['is_force'])): ?>
                    <span class="badge bg-warning text-dark ms-2">Required</span>
                  <?php endif; ?>
                </h5>

                <div class="mb-2">
                  <label class="text-muted fw-semibold">Changelog:</label>
                  <pre class="bg-light p-3 rounded" style="white-space:pre-wrap; font-family:inherit;">
<?= htmlspecialchars($update['changelog']) ?>
                  </pre>
                </div>
              </div>
            </div>
          <?php endforeach; ?>

        <?php else: ?>
          <div class="alert alert-success">
            ✅ You're up to date! Current version: <strong>v<?= htmlspecialchars($current_version) ?></strong>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</body>
</html>
