<?php
// STEP 2F-2 ADMIN ASSIGNMENTS ADMIN-LABEL DYNAMIC

require_once __DIR__ . '/auth.php';

if (!function_exists('app_setting')) {
    function app_setting(PDO $pdo, string $key, string $default = ''): string {
        try {
            $stmt = $pdo->prepare("SELECT setting_value FROM app_settings WHERE setting_key = ? LIMIT 1");
            $stmt->execute([$key]);
            $value = $stmt->fetchColumn();
            return ($value !== false && $value !== null && $value !== '') ? (string)$value : $default;
        } catch (Throwable $e) {
            return $default;
        }
    }
}

$adminAssignmentsTitle = app_setting($pdo, 'admin_label_assignments', 'Manage Assignments');
 require_admin(); $pageTitle = $adminAssignmentsTitle;
function get_assignment(PDO $pdo,int $id):?array{ $s=$pdo->prepare("SELECT * FROM assignments WHERE id=? LIMIT 1"); $s->execute([$id]); $r=$s->fetch(); return $r?:null; }
$error=''; $notice='';
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{ $action=$_POST['action']??''; $id=(int)($_POST['id']??0);
  if($action==='save'){
   $title=trim($_POST['title']??''); $description=trim($_POST['description']??''); $video=trim($_POST['video_url']??''); $audio=trim($_POST['audio_url']??''); $pdf=trim($_POST['pdf_url']??''); $sort=(int)($_POST['sort_order']??0); $active=isset($_POST['active'])?1:0;
   if(!empty($_POST['remove_video']))$video=''; if(!empty($_POST['remove_audio']))$audio=''; if(!empty($_POST['remove_pdf']))$pdf='';
   if($title===''){$error='Title is required.';} elseif($id>0){$s=$pdo->prepare("UPDATE assignments SET title=?,description=?,content=?,video_url=?,audio_url=?,pdf_url=?,sort_order=?,active=? WHERE id=?"); $s->execute([$title,$description,$description,$video,$audio,$pdf,$sort,$active,$id]); header('Location: admin_assignments.php?notice=updated'); exit;} else {$s=$pdo->prepare("INSERT INTO assignments (title,description,content,video_url,audio_url,pdf_url,sort_order,active) VALUES (?,?,?,?,?,?,?,?)"); $s->execute([$title,$description,$description,$video,$audio,$pdf,$sort,$active]); header('Location: admin_assignments.php?notice=added'); exit;}
  }
  if($action==='delete'&&$id>0){$s=$pdo->prepare("DELETE FROM assignments WHERE id=?");$s->execute([$id]);header('Location: admin_assignments.php?notice=deleted');exit;}
  if($action==='toggle'&&$id>0){$a=get_assignment($pdo,$id); if($a){$new=empty($a['active'])?1:0;$s=$pdo->prepare("UPDATE assignments SET active=? WHERE id=?");$s->execute([$new,$id]);} header('Location: admin_assignments.php?notice=updated');exit;}
 }catch(Throwable $e){$error=$e->getMessage();}
}
if(!empty($_GET['notice'])){$notice=['added'=>'Assignment added.','updated'=>'Assignment updated.','deleted'=>'Assignment deleted.'][$_GET['notice']]??'';}
$editing=!empty($_GET['edit'])?get_assignment($pdo,(int)$_GET['edit']):null; $items=$pdo->query("SELECT * FROM assignments ORDER BY sort_order ASC,id DESC")->fetchAll(); require_once __DIR__ . '/includes/header.php';
$t=$editing['title']??'';$d=$editing['description']??($editing['content']??'');$v=$editing['video_url']??'';$a=$editing['audio_url']??'';$p=$editing['pdf_url']??'';$sort=$editing['sort_order']??0;$active=$editing?!empty($editing['active']):true;
?>
<main><section class="section"><div class="container"><div class="panel"><h1><?php echo h($adminAssignmentsTitle); ?></h1><p class="muted"><?php echo h(app_setting($pdo,'admin_assignments_help','Tap Edit to load current assignment content into the editor.')); ?></p><?php if($error): ?><div class="error"><?php echo h($error); ?></div><?php endif; ?><?php if($notice): ?><div class="notice"><?php echo h($notice); ?></div><?php endif; ?>
<div class="panel <?php echo $editing ? 'edit-banner' : ''; ?>"><h2><?php echo $editing?'Edit Assignment':'Add New Assignment'; ?></h2><?php if($editing): ?><div class="notice">Editing: <strong><?php echo h($t); ?></strong></div><?php endif; ?><form method="post"><input type="hidden" name="action" value="save"><input type="hidden" name="id" value="<?php echo h($editing['id']??0); ?>"><label><?php echo h(app_setting($pdo,'assignment_label_title','Title')); ?></label><input name="title" value="<?php echo h($t); ?>" required><label><?php echo h(app_setting($pdo,'assignment_label_description','Description / Instructions')); ?></label><textarea name="description"><?php echo h($d); ?></textarea><hr><h3><?php echo h(app_setting($pdo,'media_video_label','Video')); ?></h3><?php if($v): ?><p class="muted" style="word-break:break-word;">Current: <?php echo h($v); ?></p><label><input type="checkbox" name="remove_video" style="width:auto;"> Remove current video</label><?php endif; ?><label><?php echo h(app_setting($pdo,'assignment_label_video_url','Video URL')); ?></label><input name="video_url" value="<?php echo h($v); ?>"><h3><?php echo h(app_setting($pdo,'media_audio_label','Audio')); ?></h3><?php if($a): ?><p class="muted" style="word-break:break-word;">Current: <?php echo h($a); ?></p><label><input type="checkbox" name="remove_audio" style="width:auto;"> Remove current audio</label><?php endif; ?><label><?php echo h(app_setting($pdo,'assignment_label_audio_url','Audio URL')); ?></label><input name="audio_url" value="<?php echo h($a); ?>"><h3><?php echo h(app_setting($pdo,'media_pdf_label','PDF')); ?></h3><?php if($p): ?><p class="muted" style="word-break:break-word;">Current: <?php echo h($p); ?></p><label><input type="checkbox" name="remove_pdf" style="width:auto;"> Remove current PDF</label><?php endif; ?><label><?php echo h(app_setting($pdo,'assignment_label_pdf_url','PDF URL')); ?></label><input name="pdf_url" value="<?php echo h($p); ?>"><label>Sort Order</label><input name="sort_order" type="number" value="<?php echo h($sort); ?>"><label><input type="checkbox" name="active" <?php echo $active?'checked':''; ?> style="width:auto;"> Active / visible</label><div class="btn-row"><button class="btn btn-primary" type="submit"><?php echo $editing?'Save Changes':'Add Assignment'; ?></button><?php if($editing): ?><a class="btn btn-secondary" href="admin_assignments.php">Cancel Edit</a><?php endif; ?></div></form></div>
<div class="panel" style="margin-top:1rem;"><h2>Existing Assignments</h2><p class="muted">Tap Edit to load the assignment into the editor above.</p><table class="module-table"><tr><th>Title</th><th>Status</th><th>Actions</th></tr><?php foreach($items as $item): ?><tr><td><strong><?php echo h($item['title']); ?></strong><br><span class="muted">Sort: <?php echo h($item['sort_order']??0); ?></span></td><td><?php echo !empty($item['active'])?'Active':'Hidden'; ?></td><td><div class="actions"><a class="btn btn-secondary btn-small" href="admin_assignments.php?edit=<?php echo h($item['id']); ?>">Edit</a><form method="post" style="display:inline;"><input type="hidden" name="action" value="toggle"><input type="hidden" name="id" value="<?php echo h($item['id']); ?>"><button class="btn btn-secondary btn-small"><?php echo !empty($item['active'])?'Hide':'Show'; ?></button></form><form method="post" style="display:inline;" onsubmit="return confirm('Delete?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="id" value="<?php echo h($item['id']); ?>"><button class="btn btn-danger btn-small">Delete</button></form></div></td></tr><?php endforeach; ?></table></div></div></div></section></main><?php require_once __DIR__ . '/includes/footer.php'; ?>
