<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
requirePermission('projects.manage');

require_once __DIR__ . '/../config/database.php';

/* =============================
   FILTERS
============================= */

$search = $_GET['search'] ?? '';
$status = $_GET['status'] ?? '';

$sql = "
    SELECT p.*,
           c.company_name
    FROM projects p
    LEFT JOIN companies c ON p.client_company_id = c.id
    WHERE 1=1
";

$params = [];

if ($search) {
    $sql .= " AND (p.project_name LIKE ? OR p.project_code LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

if ($status) {
    $sql .= " AND p.status = ?";
    $params[] = $status;
}

$sql .= " ORDER BY p.created_at DESC";

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$projects = $stmt->fetchAll(PDO::FETCH_ASSOC);

require_once __DIR__ . '/../includes/header.php';
require_once __DIR__ . '/../includes/sidebar.php';
?>

<div class="main-content">

<div class="page-header">
    <h2>Projects</h2>
    <p class="page-subtitle">
        Enterprise project management (Metering + Electrical Scope)
    </p>
</div>

<div class="topbar" style="margin-bottom:20px;">
<?php if (hasPermission('manage_projects')): ?>
    <a href="add_project.php" class="btn-primary">+ Add Project</a>
<?php endif; ?>
</div>

<form method="GET" class="filter-bar">

    <input type="text"
           name="search"
           placeholder="Search project name or code"
           value="<?= htmlspecialchars($search) ?>">

    <select name="status">
        <option value="">All Status</option>
        <option value="PLANNING" <?= $status=='PLANNING'?'selected':'' ?>>Planning</option>
        <option value="ACTIVE" <?= $status=='ACTIVE'?'selected':'' ?>>Active</option>
        <option value="ON_HOLD" <?= $status=='ON_HOLD'?'selected':'' ?>>On Hold</option>
        <option value="COMPLETED" <?= $status=='COMPLETED'?'selected':'' ?>>Completed</option>
        <option value="CANCELLED" <?= $status=='CANCELLED'?'selected':'' ?>>Cancelled</option>
    </select>

    <button type="submit" class="btn-secondary">Filter</button>

</form>

<div class="table-wrap">
<table>
<thead>
<tr>
<th>Code</th>
<th>Project Name</th>
<th>Client</th>
<th>Type</th>
<th>Status</th>
<th>Start</th>
<th>End</th>
<th>Action</th>
</tr>
</thead>
<tbody>

<?php if (empty($projects)): ?>
<tr><td colspan="8">No projects found.</td></tr>
<?php endif; ?>

<?php foreach ($projects as $p): ?>
<tr>
<td><?= htmlspecialchars($p['project_code'] ?? '-') ?></td>
<td><strong><?= htmlspecialchars($p['project_name']) ?></strong></td>
<td><?= htmlspecialchars($p['company_name'] ?? '-') ?></td>
<td><?= htmlspecialchars($p['project_type']) ?></td>

<td>
    <span class="status-badge <?= strtolower($p['status']) ?>">
        <?= htmlspecialchars($p['status']) ?>
    </span>
</td>

<td><?= htmlspecialchars($p['start_date'] ?? '-') ?></td>
<td><?= htmlspecialchars($p['expected_end_date'] ?? '-') ?></td>

<td>
    <a class="action-link"
       href="project_view.php?id=<?= (int)$p['id'] ?>">
       View
    </a>
</td>

</tr>
<?php endforeach; ?>

</tbody>
</table>
</div>

</div>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>