<?php
require_once 'header.php';
require_permission('projects');

// unos nove grupe / projekta
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_project'])) {
    $name     = trim($_POST['name'] ?? '');
    $location = trim($_POST['location'] ?? '');
    $desc     = trim($_POST['description'] ?? '');
    $status   = $_POST['project_status'] ?? 'open';

    if ($name === '') {
        echo '<div class="alert alert-danger mb-3">Naziv projekta je obavezan.</div>';
    } else {
        $stmt = $mysqli->prepare("INSERT INTO projects (name, location, description, project_status) VALUES (?,?,?,?)");
        $stmt->bind_param('ssss', $name, $location, $desc, $status);
        $stmt->execute();
        $stmt->close();
        echo '<div class="alert alert-success mb-3">Projekt dodan.</div>';
    }
}

// brisanje
if (isset($_GET['delete_id'])) {
    $id = (int)$_GET['delete_id'];
    if ($id > 0) {
        $stmt = $mysqli->prepare("DELETE FROM projects WHERE id = ?");
        try {
            $stmt->bind_param('i', $id);
            $stmt->execute();
            echo '<div class="alert alert-success mb-3">Projekt obrisan.</div>';
        } catch (mysqli_sql_exception $ex) {
            echo '<div class="alert alert-danger mb-3">Projekt nije moguće obrisati (postoje nalozi vezani uz njega).</div>';
        }
        $stmt->close();
    }
}

// pretraga
$search = trim($_GET['q'] ?? '');
$where = '';
if ($search !== '') {
    $esc = $mysqli->real_escape_string($search);
    $where = "WHERE name LIKE '%{$esc}%' OR location LIKE '%{$esc}%'";
}

$sql = "SELECT * FROM projects {$where} ORDER BY name";
$projects = $mysqli->query($sql);
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Projekti</div>
        <div class="text-muted small">Unos i pregled projekata</div>
    </div>
</div>

<div class="row g-3">
    <div class="col-12 col-lg-4">
        <div class="card card-soft">
            <div class="card-header card-soft-header">
                <span class="fw-semibold">Novi projekt</span>
            </div>
            <div class="card-body card-soft-body">
                <form method="post">
                    <input type="hidden" name="create_project" value="1">
                    <div class="mb-2">
                        <label class="form-label">Naziv</label>
                        <input type="text" name="name" class="form-control" required>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Lokacija</label>
                        <input type="text" name="location" class="form-control">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Opis</label>
                        <textarea name="description" class="form-control" rows="3"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Status</label>
                        <select name="project_status" class="form-select">
                            <option value="open">Otvoren</option>
                            <option value="closed">Dovršen</option>
                        </select>
                    </div>
                    <button class="btn btn-primary btn-soft w-100" type="submit">Spremi projekt</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-12 col-lg-8">
        <div class="card card-soft">
            <div class="card-header card-soft-header d-flex justify-content-between align-items-center">
                <span class="fw-semibold">Popis projekata</span>
                <span class="pill-counter d-none d-sm-inline">
                    <?php echo (int)$projects->num_rows; ?> projekata
                </span>
            </div>
            <div class="card-body card-soft-body">
                <div class="mb-3">
                    <form method="get" class="row g-2 align-items-end">
                        <div class="col-12 col-md-6">
                            <label class="form-label">Pretraga projekata</label>
                            <input type="text" name="q"
                                   class="form-control form-control-sm"
                                   placeholder="naziv ili lokacija..."
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <div class="col-auto">
                            <button class="btn btn-outline-primary btn-soft btn-sm" type="submit">
                                Primijeni
                            </button>
                        </div>
                        <?php if ($search !== ''): ?>
                        <div class="col-auto">
                            <a href="projects.php" class="btn btn-outline-secondary btn-soft btn-sm">Reset</a>
                        </div>
                        <?php endif; ?>
                    </form>
                </div>

                <div class="table-responsive d-none d-md-block">
                    <table class="table table-sm align-middle">
                        <thead class="small text-muted">
                        <tr>
                            <th>#</th>
                            <th>Naziv</th>
                            <th>Lokacija</th>
                            <th>Status</th>
                            <th class="text-end">Akcije</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php while ($p = $projects->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo $p['id']; ?></td>
                                <td><?php echo htmlspecialchars($p['name']); ?></td>
                                <td><?php echo htmlspecialchars($p['location']); ?></td>
                                <td>
                                    <?php if ($p['project_status'] === 'closed'): ?>
                                        <span class="badge bg-secondary">Dovršen</span>
                                    <?php else: ?>
                                        <span class="badge bg-success">Otvoren</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-end">
                                    <a href="project_detail.php?project_id=<?php echo $p['id']; ?>"
                                       class="btn btn-sm btn-outline-info btn-soft">Utrošak</a>
                                    <a href="project_edit.php?id=<?php echo $p['id']; ?>"
                                       class="btn btn-sm btn-outline-primary btn-soft">Uredi</a>
                                    <a href="projects.php?delete_id=<?php echo $p['id']; ?>"
                                       class="btn btn-sm btn-outline-danger btn-soft"
                                       onclick="return confirm('Obrisati projekt?');">Obriši</a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        </tbody>
                    </table>
                </div>

                <div class="d-md-none">
                    <?php
                    $projects->data_seek(0);
                    if ($projects->num_rows === 0): ?>
                        <div class="text-muted small">Nema projekata.</div>
                    <?php endif; ?>
                    <?php while ($p = $projects->fetch_assoc()): ?>
                        <div class="list-card">
                            <div class="list-card-title">
                                <?php echo htmlspecialchars($p['name']); ?>
                            </div>
                            <div class="list-card-sub">
                                <?php echo htmlspecialchars($p['location']); ?>
                            </div>
                            <div class="list-card-meta mb-1">
                                Status:
                                <?php if ($p['project_status'] === 'closed'): ?>
                                    <span class="badge bg-secondary">Dovršen</span>
                                <?php else: ?>
                                    <span class="badge bg-success">Otvoren</span>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex justify-content-end gap-1">
                                <a href="project_detail.php?project_id=<?php echo $p['id']; ?>"
                                   class="btn btn-outline-info btn-sm">Utrošak</a>
                                <a href="project_edit.php?id=<?php echo $p['id']; ?>"
                                   class="btn btn-outline-primary btn-sm">Uredi</a>
                                <a href="projects.php?delete_id=<?php echo $p['id']; ?>"
                                   class="btn btn-outline-danger btn-sm"
                                   onclick="return confirm('Obrisati projekt?');">X</a>
                            </div>
                        </div>
                    <?php endwhile; ?>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once 'footer.php'; ?>
