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

if (!isset($_SESSION['user_id'])) {
    header("Location: ../users/login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// Pārbaudām vai ir norādīts task_id
if (!isset($_GET['task_id'])) {
    header("Location: ../calendar/calendar.php");
    exit();
}

$task_id = $_GET['task_id'];

// Iegūstam uzdevumu
$stmt = $pdo->prepare("SELECT * FROM tasks WHERE id = ? AND user_id = ?");
$stmt->execute([$task_id, $user_id]);
$task = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$task) {
    echo "<p>Task not found or access denied.</p>";
    exit();
}

$owner_check = $pdo->prepare("SELECT id FROM calendars WHERE id = ? AND user_id = ?");
$owner_check->execute([$task['calendar_id'], $user_id]);
$is_owner = $owner_check->fetch();

if (!$is_owner) {
    $access_check = $pdo->prepare("
        SELECT can_edit FROM calendar_access 
        WHERE calendar_id = ? AND user_id = ? AND can_edit = 1
    ");
    $access_check->execute([$task['calendar_id'], $user_id]);
    if (!$access_check->fetch()) {
        header("Location: tasks.php?error=no_permission");
        exit();
    }
}

// Ja forma iesniegta — atjaunojam
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title']);
    $description = trim($_POST['description']);
    $start_datetime = $_POST['start_datetime'];
    $end_datetime = $_POST['end_datetime'];
    $color = $_POST['color'];

    if ($title && $start_datetime && $end_datetime) {
        $update_stmt = $pdo->prepare("
            UPDATE tasks 
            SET title = ?, description = ?, start_datetime = ?, end_datetime = ?, color = ?
            WHERE id = ? AND user_id = ?
        ");
        $update_stmt->execute([$title, $description, $start_datetime, $end_datetime, $color, $task_id, $user_id]);

        header("Location: ../calendar/calendar.php?view=day&year=" . date('Y', strtotime($start_datetime)) . "&month=" . date('m', strtotime($start_datetime)) . "&day=" . date('d', strtotime($start_datetime)) . "&calendar_id=" . $task['calendar_id']);
        exit();
    } else {
        $error = "Please fill in all required fields!";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Edit Task</title>
<link rel="stylesheet" href="../css/calendar.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>

<div class="edit-container">
    <h2>Edit Task</h2>

    <?php if (isset($error)): ?>
        <p class="error"><?= htmlspecialchars($error) ?></p>
    <?php endif; ?>

    <form method="POST">
        <label for="title">Title*</label>
        <input type="text" id="title" name="title" value="<?= htmlspecialchars($task['title']) ?>" required>

        <label for="description">Description</label>
        <textarea id="description" name="description"><?= htmlspecialchars($task['description']) ?></textarea>

        <label for="start_datetime">Start Date/Time*</label>
        <input type="datetime-local" id="start_datetime" name="start_datetime"
               value="<?= date('Y-m-d\TH:i', strtotime($task['start_datetime'])) ?>" required>

        <label for="end_datetime">End Date/Time*</label>
        <input type="datetime-local" id="end_datetime" name="end_datetime"
               value="<?= date('Y-m-d\TH:i', strtotime($task['end_datetime'])) ?>" required>

        <label for="color">Color</label>
        <input type="color" id="color" name="color" value="<?= htmlspecialchars($task['color']) ?>">

        <button type="submit">Save Changes</button>
    </form>

    <a class="cancel-link" href="../calendar/calendar.php?view=day&year=<?= date('Y', strtotime($task['start_datetime'])) ?>&month=<?= date('m', strtotime($task['start_datetime'])) ?>&day=<?= date('d', strtotime($task['start_datetime'])) ?>&calendar_id=<?= $task['calendar_id'] ?>">Cancel</a>
</div>

</body>
</html>