<?php
require_once __DIR__ . '/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'Ad Calendar';
$table = 'mj_sponsored_ads';
$count = mj_module_count($table);
$recent = [];
try { if (table_exists($table)) { $recent = all_rows("SELECT * FROM `$table` ORDER BY id DESC LIMIT 10"); } } catch(Throwable $e) {}
include __DIR__ . '/_header.php';
?>
<div class="row">
  <div class="col-lg-8">
    <div class="card card-primary card-outline">
      <div class="card-header"><h3 class="card-title"><i class="fas fa-calendar-alt mr-2"></i>Ad Calendar</h3><div class="card-tools"><span class="badge badge-primary"><?= e($count) ?> records</span></div></div>
      <div class="card-body">
        <p class="lead mb-3">View active, scheduled, expiring and expired ad campaigns by date.</p>
        <div class="row"><div class="col-md-4 col-lg-3"><div class="mj-action-card p-3 h-100"><span class="mj-icon-tile mb-2"><i class="fas fa-check"></i></span><h6 class="font-weight-bold">Schedule</h6><p class="text-muted small mb-0">Ready for ad calendar workflow.</p></div></div><div class="col-md-4 col-lg-3"><div class="mj-action-card p-3 h-100"><span class="mj-icon-tile mb-2"><i class="fas fa-check"></i></span><h6 class="font-weight-bold">Avoid overlap</h6><p class="text-muted small mb-0">Ready for ad calendar workflow.</p></div></div><div class="col-md-4 col-lg-3"><div class="mj-action-card p-3 h-100"><span class="mj-icon-tile mb-2"><i class="fas fa-check"></i></span><h6 class="font-weight-bold">Expiring soon</h6><p class="text-muted small mb-0">Ready for ad calendar workflow.</p></div></div><div class="col-md-4 col-lg-3"><div class="mj-action-card p-3 h-100"><span class="mj-icon-tile mb-2"><i class="fas fa-check"></i></span><h6 class="font-weight-bold">Active ads</h6><p class="text-muted small mb-0">Ready for ad calendar workflow.</p></div></div><div class="col-md-4 col-lg-3"><div class="mj-action-card p-3 h-100"><span class="mj-icon-tile mb-2"><i class="fas fa-check"></i></span><h6 class="font-weight-bold">Pause/resume</h6><p class="text-muted small mb-0">Ready for ad calendar workflow.</p></div></div></div>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="small-box bg-gradient-info mj-admin-kpi"><div class="inner"><h3><?= e($count) ?></h3><p>Total Records</p></div><div class="icon"><i class="fas fa-calendar-alt"></i></div></div>
    <div class="card"><div class="card-header"><h3 class="card-title">Quick Actions</h3></div><div class="card-body">
      <a class="btn btn-primary btn-block" href="#"><i class="fas fa-plus mr-1"></i>Create / Add New</a>
      <a class="btn btn-outline-secondary btn-block" href="import-center.php"><i class="fas fa-upload mr-1"></i>Import Data</a>
      <a class="btn btn-outline-info btn-block" href="settings.php"><i class="fas fa-cogs mr-1"></i>Settings</a>
    </div></div>
  </div>
</div>
<div class="card">
  <div class="card-header"><h3 class="card-title">Recent Items</h3></div>
  <div class="card-body table-responsive">
    <?php if(!$recent): ?><div class="mj-empty"><i class="fas fa-inbox fa-2x text-muted mb-2"></i><h5>No records yet</h5><p class="text-muted">Create, import or connect this module to start using it.</p></div><?php else: ?>
    <table class="table table-striped table-hover mj-datatable"><thead><tr><th>ID</th><th>Summary</th><th>Status</th><th>Created</th><th>Action</th></tr></thead><tbody>
      <?php foreach($recent as $r): ?><tr><td><?= e($r['id'] ?? '') ?></td><td><?= e($r['title'] ?? $r['name'] ?? $r['message'] ?? $r['email'] ?? $r['code'] ?? 'Record') ?></td><td><span class="badge badge-light"><?= e($r['status'] ?? $r['priority'] ?? 'active') ?></span></td><td><?= e($r['created_at'] ?? '') ?></td><td><button class="btn btn-sm btn-outline-primary">View</button></td></tr><?php endforeach; ?>
    </tbody></table><?php endif; ?>
  </div>
</div>
<?php include __DIR__ . '/_footer.php'; ?>
