<?php
/**
 * Projects Page
 * Research Survey Management System
 */

define('PAGE_TITLE', 'Projects');
require_once __DIR__ . '/../includes/functions.php';
requireAuth();

$pdo = getDBConnection();

// Handle project creation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
    if ($_POST['action'] === 'create') {
        $stmt = $pdo->prepare("INSERT INTO research_projects (project_name, description, created_by, status, start_date, end_date, methodology_notes) 
                               VALUES (:name, :desc, :user, :status, :start, :end, :notes)");
        $stmt->execute([
            'name' => sanitize($_POST['project_name']),
            'desc' => sanitize($_POST['description'] ?? ''),
            'user' => $_SESSION['user_id'],
            'status' => $_POST['status'] ?? 'draft',
            'start' => $_POST['start_date'] ?: null,
            'end' => $_POST['end_date'] ?: null,
            'notes' => sanitize($_POST['methodology_notes'] ?? '')
        ]);
        
        logAudit('create', 'project', $pdo->lastInsertId());
        
        $_SESSION['flash_message'] = 'Project created successfully!';
        $_SESSION['flash_type'] = 'success';
        header('Location: ' . BASE_URL . '/pages/projects.php');
        exit;
    }
}

$projects = getProjects();

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

<div class="dashboard-header">
    <div>
        <h1><i class="fas fa-folder-open me-2 text-primary"></i>Research Projects</h1>
        <p>Organize surveys into research projects with methodological frameworks</p>
    </div>
    <div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createProjectModal">
            <i class="fas fa-plus me-1"></i> New Project
        </button>
    </div>
</div>

<?php if (empty($projects)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-body text-center py-5">
        <i class="fas fa-folder-open fa-5x text-muted mb-3"></i>
        <h4>No Projects Yet</h4>
        <p class="text-muted">Create your first research project to organize surveys.</p>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createProjectModal">
            <i class="fas fa-plus me-1"></i> Create Project
        </button>
    </div>
</div>
<?php else: ?>
<div class="row g-3">
    <?php foreach ($projects as $project): ?>
    <div class="col-md-6 col-lg-4">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-2">
                    <h5 class="card-title mb-0">
                        <a href="<?php echo BASE_URL; ?>/pages/project_detail.php?id=<?php echo $project['project_id']; ?>" 
                           class="text-decoration-none">
                            <?php echo htmlspecialchars($project['project_name']); ?>
                        </a>
                    </h5>
                    <?php echo getStatusBadge($project['status']); ?>
                </div>
                <?php if ($project['description']): ?>
                <p class="card-text small text-muted"><?php echo htmlspecialchars(substr($project['description'], 0, 150)) . (strlen($project['description']) > 150 ? '...' : ''); ?></p>
                <?php endif; ?>
                
                <div class="d-flex justify-content-between align-items-center mt-3">
                    <div class="small text-muted">
                        <i class="fas fa-poll me-1"></i><?php echo $project['survey_count']; ?> surveys
                        <br>
                        <i class="fas fa-user me-1"></i><?php echo htmlspecialchars($project['creator_name']); ?>
                    </div>
                    <div class="small text-muted text-end">
                        <?php if ($project['start_date']): ?>
                        <i class="fas fa-calendar me-1"></i><?php echo formatDate($project['start_date'], 'M d'); ?>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <div class="card-footer bg-white border-top-0">
                <div class="btn-group btn-group-sm w-100">
                    <a href="<?php echo BASE_URL; ?>/pages/project_detail.php?id=<?php echo $project['project_id']; ?>" 
                       class="btn btn-outline-primary">
                        <i class="fas fa-eye me-1"></i> View
                    </a>
                    <a href="<?php echo BASE_URL; ?>/pages/surveys.php?project_id=<?php echo $project['project_id']; ?>" 
                       class="btn btn-outline-success">
                        <i class="fas fa-poll me-1"></i> Surveys
                    </a>
                </div>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<!-- Create Project Modal -->
<div class="modal fade" id="createProjectModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <form method="POST">
                <input type="hidden" name="action" value="create">
                <div class="modal-header">
                    <h5 class="modal-title"><i class="fas fa-plus-circle me-2 text-primary"></i>Create Research Project</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="row g-3">
                        <div class="col-12">
                            <label class="form-label">Project Name <span class="text-danger">*</span></label>
                            <input type="text" class="form-control form-control-lg" name="project_name" required
                                   placeholder="e.g., Customer Experience Research 2026">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Description</label>
                            <textarea class="form-control" name="description" rows="3" 
                                      placeholder="Describe the research project objectives"></textarea>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Status</label>
                            <select class="form-select" name="status">
                                <option value="draft">Draft</option>
                                <option value="active">Active</option>
                            </select>
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">Start Date</label>
                            <input type="date" class="form-control" name="start_date">
                        </div>
                        <div class="col-md-4">
                            <label class="form-label">End Date</label>
                            <input type="date" class="form-control" name="end_date">
                        </div>
                        <div class="col-12">
                            <label class="form-label">Methodology Notes</label>
                            <textarea class="form-control" name="methodology_notes" rows="2"
                                      placeholder="Describe the research methodology (e.g., Mixed methods, Cross-sectional, Longitudinal)"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save me-1"></i> Create Project
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

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