<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
$adminTitle = 'Modules / Add-ons';
$adminSubtitle = 'Enable or disable marketplace modules globally. User-specific access remains controlled from Admin → Users.';
if (function_exists('mj_v289_ensure_schema')) { mj_v289_ensure_schema(); }
verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
  if (table_exists('mj_site_modules')) {
    foreach($_POST['modules']??[] as $id=>$val){
      try { q('UPDATE mj_site_modules SET is_enabled=? WHERE id=?',[(int)$val,(int)$id]); } catch(Throwable $e) {}
    }
  }
  admin_log('save_modules','mj_site_modules',0,['count'=>count($_POST['modules']??[])]);
  flash('success','Modules saved.'); redirect('modules.php');
}
$groups = function_exists('mj_v289_module_rows_grouped') ? mj_v289_module_rows_grouped() : [];
include __DIR__.'/_header.php';
?>

<div class="card card-primary card-outline">
  <div class="card-header d-flex align-items-center">
    <h3 class="card-title"><i class="fas fa-puzzle-piece mr-2"></i>Frontend & Marketplace Modules</h3>
    <div class="ml-auto"><a href="module-center.php" class="btn btn-sm btn-outline-primary"><i class="fas fa-th-large mr-1"></i>Module Center</a></div>
  </div>
  <form method="post">
    <div class="card-body">
      <?= csrf_input() ?>
      <?php if(!$groups): ?>
        <div class="alert alert-warning">No module records found. Run the latest migration or open this page once after database connection is ready.</div>
      <?php endif; ?>
      <?php foreach($groups as $groupName=>$rows): ?>
        <div class="mj-module-group-block mb-4">
          <div class="mj-module-group-title"><h5><?= e($groupName) ?></h5><small><?= e(count($rows)) ?> modules</small></div>
          <div class="row">
            <?php foreach($rows as $r): ?>
              <div class="col-md-6 col-xl-4 mb-3">
                <div class="card h-100 mj-module-toggle-card">
                  <div class="card-body">
                    <div class="d-flex justify-content-between align-items-start">
                      <div><i class="<?= e($r['admin_icon'] ?? 'fas fa-puzzle-piece') ?> text-primary mr-1"></i><h5 class="font-weight-bold mb-1 d-inline"><?= e($r['module_name'] ?? $r['module_code'] ?? 'Module') ?></h5></div>
                      <span class="badge badge-<?= !empty($r['is_enabled'])?'success':'secondary' ?>"><?= !empty($r['is_enabled'])?'Enabled':'Off' ?></span>
                    </div>
                    <p class="text-muted small my-3"><?= e($r['description'] ?? '') ?></p>
                    <code><?= e($r['module_code'] ?? '') ?></code>
                    <select class="form-control mt-3" name="modules[<?= e($r['id'] ?? 0) ?>]">
                      <option value="1" <?= !empty($r['is_enabled'])?'selected':'' ?>>Enabled</option>
                      <option value="0" <?= empty($r['is_enabled'])?'selected':'' ?>>Disabled</option>
                    </select>
                  </div>
                </div>
              </div>
            <?php endforeach; ?>
          </div>
        </div>
      <?php endforeach; ?>
    </div>
    <div class="card-footer d-flex align-items-center">
      <button class="btn btn-primary"><i class="fas fa-save mr-1"></i>Save Modules</button>
      <a href="module-center.php" class="btn btn-outline-secondary ml-2">Back to Module Center</a>
      <small class="text-muted ml-auto">Disabled modules hide from menus and can show a clean disabled-message page.</small>
    </div>
  </form>
</div>
<?php include __DIR__.'/_footer.php'; ?>
