<?php
require_once __DIR__.'/../includes/functions.php';
$admin = require_admin();
verify_csrf();
if (function_exists('mj_v290_ensure_schema')) { mj_v290_ensure_schema(); }
$adminTitle = 'Admin Task Center';
$adminSubtitle = 'Internal work queue for payments, listings, company reviews, support and follow-ups.';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = (string)($_POST['action'] ?? 'add');
    if ($action === 'add') {
        q('INSERT INTO mj_admin_task_notes(admin_id,task_title,task_status,task_priority,due_at,note,created_at) VALUES(?,?,?,?,?,?,NOW())', [(int)($admin['id']??0), trim((string)($_POST['task_title']??'Task')), 'open', (string)($_POST['task_priority']??'normal'), trim((string)($_POST['due_at']??'')) ?: null, trim((string)($_POST['note']??''))]);
        flash('success','Task added.');
    } elseif ($action === 'status') {
        q('UPDATE mj_admin_task_notes SET task_status=?, updated_at=NOW() WHERE id=?', [(string)($_POST['task_status']??'done'), (int)($_POST['id']??0)]);
        flash('success','Task updated.');
    }
    redirect('admin-tasks.php');
}
$counts = function_exists('mj_v290_admin_task_counts') ? mj_v290_admin_task_counts() : [];
$rows = table_exists('mj_admin_task_notes') ? mj_v290_simple_rows('SELECT t.*, a.name admin_name FROM mj_admin_task_notes t LEFT JOIN mj_admin_users a ON a.id=t.admin_id ORDER BY FIELD(t.task_status,"open","pending","in_progress","done","cancelled"), COALESCE(t.due_at,t.created_at) ASC LIMIT 200') : [];
include __DIR__.'/_header.php';
?>
<div class="row mj-admin-stat-row"><?php foreach([['Open',$counts['open']??0,'primary'],['Urgent',$counts['urgent']??0,'danger'],['Due Today',$counts['due_today']??0,'warning'],['Done',$counts['done']??0,'success']] as $c): ?><div class="col-md-3"><div class="small-box bg-<?=e($c[2])?>"><div class="inner"><h3><?=e($c[1])?></h3><p><?=e($c[0])?></p></div><div class="icon"><i class="fas fa-tasks"></i></div></div></div><?php endforeach; ?></div>
<div class="row"><div class="col-lg-4"><div class="card card-primary card-outline"><form method="post"><?=csrf_input()?><input type="hidden" name="action" value="add"><div class="card-header"><h3 class="card-title">Add Task</h3></div><div class="card-body"><label>Task Title</label><input class="form-control mb-2" name="task_title" required><label>Priority</label><select class="form-control mb-2" name="task_priority"><option>normal</option><option>high</option><option>urgent</option><option>low</option></select><label>Due Date</label><input class="form-control mb-2" type="datetime-local" name="due_at"><label>Note</label><textarea class="form-control" name="note" rows="4"></textarea></div><div class="card-footer"><button class="btn btn-primary">Save Task</button></div></form></div></div><div class="col-lg-8"><div class="card"><div class="card-header"><h3 class="card-title">Task Queue</h3></div><div class="card-body table-responsive"><table class="table table-hover"><thead><tr><th>Task</th><th>Priority</th><th>Due</th><th>Status</th><th>Action</th></tr></thead><tbody><?php foreach($rows as $r): ?><tr><td><b><?=e($r['task_title'])?></b><br><small><?=e($r['note']??'')?></small></td><td><?=e($r['task_priority']??'normal')?></td><td><?=e($r['due_at']??'')?></td><td><?=mj_v289_render_status_badge($r['task_status']??'open')?></td><td><form method="post" class="d-inline"><?=csrf_input()?><input type="hidden" name="action" value="status"><input type="hidden" name="id" value="<?=e($r['id'])?>"><select name="task_status" class="form-control form-control-sm d-inline" style="width:130px"><option>open</option><option>in_progress</option><option>done</option><option>cancelled</option></select><button class="btn btn-sm btn-outline-primary">Update</button></form></td></tr><?php endforeach; ?><?php if(!$rows): ?><tr><td colspan="5" class="text-center text-muted p-4">No tasks yet.</td></tr><?php endif; ?></tbody></table></div></div></div></div>
<?php include __DIR__.'/_footer.php'; ?>
