<?php
session_start();
include '../database/db.php';

/* Pārbaudām, vai lietotājs ir pieslēdzies */
if (!isset($_SESSION['user_id'])) {
    header("Location: ../users/login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

/* Jaunas komandas izveide */
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['team_name'])) {
    $team_name = $_POST['team_name'];

    /* Saglabājam komandu datubāzē */
    $stmt = $pdo->prepare("INSERT INTO teams (name, created_by) VALUES (?, ?)");
    $stmt->execute([$team_name, $user_id]);

    /* Pārlādējam lapu ar success parametru */
    header("Location: teams.php?created=1");
    exit();
}

/* Iegūstam komandas, kuras izveidojis lietotājs */
$teams = $pdo->prepare("
    SELECT *
    FROM teams
    WHERE created_by = ?
    ORDER BY name ASC
");
$teams->execute([$user_id]);
$owned_teams = $teams->fetchAll(PDO::FETCH_ASSOC);

/* Iegūstam komandas, kurās lietotājs ir dalībnieks */
$joined_teams = $pdo->prepare("
    SELECT t.*, u.name AS creator_name
    FROM teams t
    JOIN team_members tm ON t.id = tm.team_id
    LEFT JOIN users u ON t.created_by = u.id
    WHERE tm.user_id = ? AND t.created_by != ?
    ORDER BY t.name ASC
");
$joined_teams->execute([$user_id, $user_id]);
$member_teams = $joined_teams->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Teams - Work Planner</title>
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/teams.css">
</head>
<body>
<nav>
    <ul>
        <li><a href="../dashboard.php">Dashboard</a></li>
        <li><a href="../calendar/calendar.php">Calendar</a></li>
        <li><a href="../tasks/tasks.php">All Tasks</a></li>
        <li><a href="../teams/teams.php">Teams</a></li>
        <li><a href="../friends/add_friend.php">Friends</a></li>
        <li><a href="../profile/profile.php">Profile</a></li>
        <li><a href="../users/logout.php">Logout</a></li>
    </ul>
</nav>

<main>
    <h2>Teams</h2>
    
    <div class="teams-container">
        <?php if (isset($_GET['created'])): ?>
            <div class="message-box success">Team created successfully!</div>
        <?php endif; ?>
        
        <!-- Izveidot komandas sadaļu -->
        <div class="create-team-section">
            <h3>Create New Team</h3>
            <form method="post" class="team-form">
                <div class="form-group">
                    <label for="team_name">Team Name</label>
                    <input type="text" name="team_name" id="team_name" required placeholder="Enter team name">
                </div>
                <button type="submit" class="btn-create-team">Create Team</button>
            </form>
        </div>

        <!-- Komandu režģis -->
        <div class="teams-grid">
            <!-- Jūsu komandas -->
            <div class="team-section">
                <h3 class="owned">Your Teams</h3>
                <?php if (count($owned_teams) > 0): ?>
                    <div class="teams-list">
                        <?php foreach ($owned_teams as $team): ?>
                            <?php
                            // Iegūt dalībnieku skaitu
                            $count_stmt = $pdo->prepare("SELECT COUNT(*) as count FROM team_members WHERE team_id = ?");
                            $count_stmt->execute([$team['id']]);
                            $member_count = $count_stmt->fetch()['count'];
                            ?>
                            <div class="team-card">
                                <div class="team-info">
                                    <strong><?= htmlspecialchars($team['name']) ?></strong>
                                    <div class="team-meta"><?= $member_count ?> member(s)</div>
                                </div>
                                <a href="team_view.php?team_id=<?= $team['id'] ?>" class="btn-view-team">Manage →</a>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="empty-state">
                        <p>You haven't created any teams yet. Create your first team above!</p>
                    </div>
                <?php endif; ?>
            </div>

            <!-- Komandas, kurās jūs esat pievienoti -->
            <div class="team-section">
                <h3 class="member">Teams You're In</h3>
                <?php if (count($member_teams) > 0): ?>
                    <div class="teams-list">
                        <?php foreach ($member_teams as $team): ?>
                            <?php
                            // Iegūt dalībnieku skaitu
                            $count_stmt = $pdo->prepare("SELECT COUNT(*) as count FROM team_members WHERE team_id = ?");
                            $count_stmt->execute([$team['id']]);
                            $member_count = $count_stmt->fetch()['count'];
                            ?>
                            <div class="team-card">
                                <div class="team-info">
                                    <strong><?= htmlspecialchars($team['name']) ?></strong>
                                    <div class="team-meta">
                                        <?= $member_count ?> member(s) • Created by <?= htmlspecialchars($team['creator_name']) ?>
                                    </div>
                                </div>
                                <a href="team_view.php?team_id=<?= $team['id'] ?>" class="btn-view-team">View →</a>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else: ?>
                    <div class="empty-state">
                        <p>You're not a member of any teams yet.</p>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</main>
</body>
</html>