<?php require_once __DIR__.'/../includes/functions.php'; $admin=require_admin(); $adminTitle='Categories'; verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
    $action=$_POST['action']??'add';
    if($action==='add'){
        $name=trim($_POST['name']??''); if($name){ q('INSERT INTO mj_categories(type,name,slug,icon,is_active,sort_order) VALUES(?,?,?,?,1,?)',[$_POST['type'], $name, slugify($name), trim($_POST['icon']),(int)$_POST['sort_order']]); flash('success','Category added.'); }
    } elseif($action==='toggle') { q('UPDATE mj_categories SET is_active=1-is_active WHERE id=?',[(int)$_POST['id']]); flash('success','Category updated.'); }
    redirect('categories.php');
}
$rows=all_rows('SELECT * FROM mj_categories ORDER BY type,sort_order,name'); include __DIR__.'/_header.php'; ?>
<div class="row"><div class="col-lg-4"><div class="card card-primary card-outline"><div class="card-header"><h3 class="card-title">Add Category</h3></div><form method="post"><div class="card-body"><?=csrf_input()?><input type="hidden" name="action" value="add"><div class="form-group"><label>Type</label><select class="form-control" name="type"><?php foreach(['marketplace','company','service','job','property','vehicle','course','event'] as $t): ?><option value="<?=$t?>"><?=ucfirst($t)?></option><?php endforeach; ?></select></div><div class="form-group"><label>Name</label><input class="form-control" name="name" required></div><div class="form-group"><label>Bootstrap Icon</label><input class="form-control" name="icon" placeholder="bi-phone / bi-cup-hot"></div><div class="form-group"><label>Sort</label><input class="form-control" type="number" name="sort_order" value="0"></div></div><div class="card-footer"><button class="btn btn-primary btn-block">Add Category</button></div></form></div></div><div class="col-lg-8"><div class="card"><div class="card-header"><h3 class="card-title">Category List</h3></div><div class="card-body"><table class="table table-striped mj-data-table"><thead><tr><th>ID</th><th>Type</th><th>Name</th><th>Slug</th><th>Icon</th><th>Active</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td><?=e($r['id'])?></td><td><span class="badge badge-info"><?=e($r['type'])?></span></td><td><b><?=e($r['name'])?></b></td><td><?=e($r['slug'])?></td><td><?=e($r['icon'])?></td><td><?= $r['is_active']?'<span class="badge badge-success">Yes</span>':'<span class="badge badge-secondary">No</span>' ?></td><td><form method="post"><?=csrf_input()?><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?=e($r['id'])?>"><button class="btn btn-sm btn-secondary">Toggle</button></form></td></tr><?php endforeach; ?></tbody></table></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
