<?php
require_once __DIR__ . '/../lib/core.php';
dc_require_login();
dc_require_csrf();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = isset($_POST['action']) ? $_POST['action'] : 'save';
    $id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
    if ($action === 'delete' && $id > 0) {
        $stmt = dc_db()->prepare('SELECT COUNT(*) FROM door_cert_doors WHERE project_id=?');
        $stmt->execute(array($id));
        if ((int)$stmt->fetchColumn() > 0) {
            dc_flash('error', 'Project is used by door records and cannot be deleted.');
        } else {
            $stmt = dc_db()->prepare('DELETE FROM door_cert_projects WHERE id=?');
            $stmt->execute(array($id));
            dc_flash('success', 'Project deleted.');
        }
        dc_redirect('admin/projects.php');
    }
    $number = strtoupper(trim($_POST['project_number']));
    $client = trim($_POST['client_name']);
    $site = trim($_POST['site_location']);
    if ($id > 0) {
        $stmt = dc_db()->prepare('UPDATE door_cert_projects SET project_number=?, client_name=?, site_location=? WHERE id=?');
        $stmt->execute(array($number, $client, $site, $id));
        dc_flash('success', 'Project updated.');
    } else {
        $stmt = dc_db()->prepare('INSERT INTO door_cert_projects (project_number, client_name, site_location) VALUES (?, ?, ?)');
        $stmt->execute(array($number, $client, $site));
        dc_flash('success', 'Project added.');
    }
    dc_redirect('admin/projects.php');
}

$edit = null;
if (isset($_GET['edit'])) {
    $stmt = dc_db()->prepare('SELECT * FROM door_cert_projects WHERE id=?');
    $stmt->execute(array((int)$_GET['edit']));
    $edit = $stmt->fetch();
}
$projects = dc_db()->query('SELECT * FROM door_cert_projects ORDER BY created_at DESC')->fetchAll();

dc_admin_header('Projects');
?>
<div class="dc-card">
  <h2><?php echo $edit ? 'Edit Project' : 'Add Project'; ?></h2>
  <form method="post">
    <?php echo dc_csrf_field(); ?>
    <input type="hidden" name="id" value="<?php echo dc_h($edit ? $edit['id'] : 0); ?>">
    <div class="dc-grid">
      <div><label>Project Number</label><input name="project_number" value="<?php echo dc_h($edit ? $edit['project_number'] : '001'); ?>" required></div>
      <div><label>Client Name</label><input name="client_name" value="<?php echo dc_h($edit ? $edit['client_name'] : ''); ?>" required></div>
    </div>
    <label>Site Location</label><input name="site_location" value="<?php echo dc_h($edit ? $edit['site_location'] : ''); ?>">
    <p><button type="submit">Save Project</button></p>
  </form>
</div>
<div class="dc-card">
  <h2>Existing Projects</h2>
  <table><tr><th>Project</th><th>Client</th><th>Site</th><th></th></tr>
    <?php foreach ($projects as $project): ?>
      <tr>
        <td><?php echo dc_h($project['project_number']); ?></td>
        <td><?php echo dc_h($project['client_name']); ?></td>
        <td><?php echo dc_h($project['site_location']); ?></td>
        <td class="dc-row-actions">
          <a href="projects.php?edit=<?php echo (int)$project['id']; ?>">Edit</a>
          <form method="post" class="dc-inline-form" onsubmit="return confirm('Delete this project?');">
            <?php echo dc_csrf_field(); ?>
            <input type="hidden" name="action" value="delete">
            <input type="hidden" name="id" value="<?php echo (int)$project['id']; ?>">
            <button type="submit" class="dc-link-button danger">Delete</button>
          </form>
        </td>
      </tr>
    <?php endforeach; ?>
  </table>
</div>
<?php dc_footer(); ?>
