<?php
/**
 * Surveys List Page
 * Research Survey Management System
 */

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

$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
$filters = [
    'status' => $_GET['status'] ?? '',
    'type' => $_GET['type'] ?? '',
    'search' => $_GET['search'] ?? ''
];

$surveys = getSurveys($page, ITEMS_PER_PAGE, $filters);

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

<div class="dashboard-header">
    <div>
        <h1><i class="fas fa-poll me-2 text-primary"></i>Surveys</h1>
        <p>Manage and monitor your research surveys</p>
    </div>
    <div>
        <a href="<?php echo BASE_URL; ?>/pages/create_survey.php" class="btn btn-primary">
            <i class="fas fa-plus me-1"></i> Create Survey
        </a>
    </div>
</div>

<!-- Filters -->
<div class="card border-0 shadow-sm mb-4">
    <div class="card-body">
        <form method="GET" class="row g-2">
            <div class="col-md-4">
                <div class="input-group">
                    <span class="input-group-text"><i class="fas fa-search"></i></span>
                    <input type="text" class="form-control" name="search" placeholder="Search surveys..." 
                           value="<?php echo htmlspecialchars($filters['search']); ?>">
                </div>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="status">
                    <option value="">All Statuses</option>
                    <option value="draft" <?php echo $filters['status'] === 'draft' ? 'selected' : ''; ?>>Draft</option>
                    <option value="active" <?php echo $filters['status'] === 'active' ? 'selected' : ''; ?>>Active</option>
                    <option value="closed" <?php echo $filters['status'] === 'closed' ? 'selected' : ''; ?>>Closed</option>
                    <option value="archived" <?php echo $filters['status'] === 'archived' ? 'selected' : ''; ?>>Archived</option>
                </select>
            </div>
            <div class="col-md-3">
                <select class="form-select" name="type">
                    <option value="">All Types</option>
                    <option value="quantitative" <?php echo $filters['type'] === 'quantitative' ? 'selected' : ''; ?>>Quantitative</option>
                    <option value="qualitative" <?php echo $filters['type'] === 'qualitative' ? 'selected' : ''; ?>>Qualitative</option>
                    <option value="mixed" <?php echo $filters['type'] === 'mixed' ? 'selected' : ''; ?>>Mixed Methods</option>
                </select>
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100">Filter</button>
            </div>
        </form>
    </div>
</div>

<!-- Surveys Table -->
<div class="card border-0 shadow-sm">
    <div class="card-body p-0">
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>Title</th>
                        <th>Type</th>
                        <th>Status</th>
                        <th>Questions</th>
                        <th>Responses</th>
                        <th>Created By</th>
                        <th>Created</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($surveys['data'])): ?>
                    <tr>
                        <td colspan="8" class="text-center py-5">
                            <i class="fas fa-inbox fa-3x text-muted mb-3 d-block"></i>
                            <p class="text-muted mb-0">No surveys found</p>
                            <a href="<?php echo BASE_URL; ?>/pages/create_survey.php" class="btn btn-primary btn-sm mt-2">
                                Create your first survey
                            </a>
                        </td>
                    </tr>
                    <?php else: ?>
                    <?php foreach ($surveys['data'] as $survey): ?>
                    <tr>
                        <td>
                            <a href="<?php echo BASE_URL; ?>/pages/view_survey.php?id=<?php echo $survey['survey_id']; ?>" 
                               class="text-decoration-none fw-medium">
                                <?php echo htmlspecialchars($survey['title']); ?>
                            </a>
                            <?php if ($survey['description']): ?>
                            <br><small class="text-muted"><?php echo htmlspecialchars(substr($survey['description'], 0, 100)) . (strlen($survey['description']) > 100 ? '...' : ''); ?></small>
                            <?php endif; ?>
                        </td>
                        <td><span class="badge bg-info text-capitalize"><?php echo $survey['survey_type']; ?></span></td>
                        <td><?php echo getStatusBadge($survey['status']); ?></td>
                        <td class="text-center"><?php echo $survey['question_count']; ?></td>
                        <td class="text-center"><?php echo $survey['response_count']; ?></td>
                        <td><?php echo htmlspecialchars($survey['creator_name']); ?></td>
                        <td><small><?php echo formatDate($survey['created_at'], 'M d, Y'); ?></small></td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="<?php echo BASE_URL; ?>/pages/view_survey.php?id=<?php echo $survey['survey_id']; ?>" 
                                   class="btn btn-outline-primary" title="View">
                                    <i class="fas fa-eye"></i>
                                </a>
                                <a href="<?php echo BASE_URL; ?>/pages/edit_survey.php?id=<?php echo $survey['survey_id']; ?>" 
                                   class="btn btn-outline-secondary" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="<?php echo BASE_URL; ?>/pages/analytics.php?survey_id=<?php echo $survey['survey_id']; ?>" 
                                   class="btn btn-outline-success" title="Analytics">
                                    <i class="fas fa-chart-bar"></i>
                                </a>
                                <button type="button" class="btn btn-outline-danger" title="Delete"
                                        onclick="confirmDelete(<?php echo $survey['survey_id']; ?>, '<?php echo addslashes($survey['title']); ?>')">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
    <?php if ($surveys['totalPages'] > 1): ?>
    <div class="card-footer bg-white">
        <nav>
            <ul class="pagination pagination-sm justify-content-center mb-0">
                <?php for ($i = 1; $i <= $surveys['totalPages']; $i++): ?>
                <li class="page-item <?php echo $i === $surveys['page'] ? 'active' : ''; ?>">
                    <a class="page-link" href="?page=<?php echo $i; ?>&status=<?php echo urlencode($filters['status']); ?>&type=<?php echo urlencode($filters['type']); ?>&search=<?php echo urlencode($filters['search']); ?>">
                        <?php echo $i; ?>
                    </a>
                </li>
                <?php endfor; ?>
            </ul>
        </nav>
    </div>
    <?php endif; ?>
</div>

<script>
function confirmDelete(id, title) {
    if (confirm(`Are you sure you want to delete "${title}"? This action cannot be undone.`)) {
        window.location.href = `<?php echo BASE_URL; ?>/api/delete_survey.php?id=${id}`;
    }
}
</script>

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