<?php
require_once __DIR__ . '/lib/functions.php';
mdw_require_login();
// Store MDW Admin project data inside devworks-admin/data so the admin owns it.
// If an older /data/projects.json file exists, copy it once as a starting point.
$mdw_projects_file = __DIR__ . '/data/projects.json';
$legacy_projects_file = realpath(__DIR__ . '/../data/projects.json');
if (!file_exists($mdw_projects_file) && $legacy_projects_file && file_exists($legacy_projects_file)) {
    if (!is_dir(dirname($mdw_projects_file))) { @mkdir(dirname($mdw_projects_file), 0755, true); }
    @copy($legacy_projects_file, $mdw_projects_file);
}
$projects = mdw_read_json($mdw_projects_file, []);
$editIndex = isset($_GET['edit']) ? (int)$_GET['edit'] : -1;
$editing = $projects[$editIndex] ?? null;
$statusOptions = ['Completed','In Progress','Planned','Hidden'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';
    if ($action === 'delete') {
        $idx = (int)($_POST['index'] ?? -1);
        if (isset($projects[$idx])) {
            array_splice($projects, $idx, 1);
            if (mdw_write_json($mdw_projects_file, array_values($projects))) {
                mdw_flash('Project deleted.');
            } else {
                mdw_flash('Could not save project data. Check write permissions on devworks-admin/data/projects.json.', 'error');
            }
        }
        header('Location: projects.php'); exit;
    }
    if ($action === 'save') {
        $idx = $_POST['index'] === '' ? -1 : (int)$_POST['index'];
        $entry = [
            'name' => trim($_POST['name'] ?? ''),
            'status' => trim($_POST['status'] ?? 'Planned'),
            'description' => trim($_POST['description'] ?? ''),
            'link' => trim($_POST['link'] ?? ''),
            'sort' => (int)($_POST['sort'] ?? 100),
        ];
        if ($entry['name'] === '') {
            mdw_flash('Project name is required.', 'error');
            header('Location: projects.php'); exit;
        }
        if ($idx >= 0 && isset($projects[$idx])) $projects[$idx] = $entry; else $projects[] = $entry;
        usort($projects, fn($a,$b) => ($a['sort'] ?? 100) <=> ($b['sort'] ?? 100));
        if (mdw_write_json($mdw_projects_file, array_values($projects))) {
            mdw_flash('Project saved.');
        } else {
            mdw_flash('Could not save project data. Check write permissions on devworks-admin/data/projects.json.', 'error');
        }
        header('Location: projects.php'); exit;
    }
}
mdw_render_header('Project Showcase Manager');
?>
<div class="split"><section class="panel"><h2><?php echo $editing ? 'Edit Project' : 'Add Project'; ?></h2><form method="post"><input type="hidden" name="action" value="save"><input type="hidden" name="index" value="<?php echo $editing ? (int)$editIndex : ''; ?>"><label>Project Name</label><input name="name" value="<?php echo mdw_h($editing['name'] ?? ''); ?>" required><label>Status</label><select name="status"><?php foreach($statusOptions as $status): ?><option value="<?php echo mdw_h($status); ?>" <?php echo (($editing['status'] ?? '') === $status) ? 'selected' : ''; ?>><?php echo mdw_h($status); ?></option><?php endforeach; ?></select><label>Description</label><textarea name="description"><?php echo mdw_h($editing['description'] ?? ''); ?></textarea><label>Learn More Link</label><input name="link" value="<?php echo mdw_h($editing['link'] ?? ''); ?>" placeholder="Leave blank to hide button"><label>Sort Order</label><input type="number" name="sort" value="<?php echo (int)($editing['sort'] ?? 100); ?>"><br><br><button class="button" type="submit">Save Project</button> <a class="button ghost" href="projects.php">Clear</a></form></section><section class="panel"><h2>Status Colors</h2><p><span class="status Completed">Completed</span></p><p><span class="status InProgress">In Progress</span></p><p><span class="status Planned">Planned</span></p><p><span class="status Hidden">Hidden</span></p><p class="muted">Hidden entries are saved but not shown publicly by the updated showcase include.</p></section></div><section class="panel" style="margin-top:20px"><h2>Current Projects</h2><table><tr><th>Sort</th><th>Status</th><th>Project</th><th>Link</th><th>Actions</th></tr><?php foreach($projects as $i=>$p): ?><tr><td><?php echo (int)($p['sort'] ?? 100); ?></td><td><span class="status <?php echo str_replace(' ','',mdw_h($p['status'] ?? 'Planned')); ?>"><?php echo mdw_h($p['status'] ?? 'Planned'); ?></span></td><td><strong><?php echo mdw_h($p['name'] ?? ''); ?></strong><br><span class="muted small"><?php echo mdw_h($p['description'] ?? ''); ?></span></td><td><?php echo mdw_h($p['link'] ?? ''); ?></td><td class="row-actions"><a class="button" href="projects.php?edit=<?php echo $i; ?>">Edit</a><form method="post" onsubmit="return confirm('Delete this project?');"><input type="hidden" name="action" value="delete"><input type="hidden" name="index" value="<?php echo $i; ?>"><button class="button danger" type="submit">Delete</button></form></td></tr><?php endforeach; ?></table></section><?php mdw_render_footer(); ?>
