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

$id = $_GET['id'];
$member = $conn->query("SELECT * FROM team_members WHERE id=$id")->fetch_assoc();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $position = $_POST['position'];
    $description = $_POST['description'];

    // Keep the current photo unless a new one is uploaded
    $photo = $member['photo'];
    if ($_FILES['photo']['name']) {
        $targetDir = "../uploads/team/";
        if (!is_dir($targetDir)) mkdir($targetDir, 0777, true);

        // Generate a new file name for the uploaded photo
        $photo = $targetDir . time() . "_" . basename($_FILES['photo']['name']);
        move_uploaded_file($_FILES['photo']['tmp_name'], $photo);
    }

    // Update the team member in the database
    $stmt = $conn->prepare("UPDATE team_members SET name=?, position=?, photo=?, description=? WHERE id=?");
    $stmt->bind_param("ssssi", $name, $position, $photo, $description, $id);
    $stmt->execute();

    // Redirect to the team page after saving
    header("Location: team.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Member</title>
    <script src="https://cdn.jsdelivr.net/npm/tinymce@6/tinymce.min.js" referrerpolicy="origin"></script>
    
    <script>
    tinymce.init({
        selector: 'textarea[name="description"]',
        height: 400,
        menubar: true,
        plugins: [
            'advlist', 'autolink', 'lists', 'link', 'image', 'charmap',
            'preview', 'anchor', 'searchreplace', 'visualblocks',
            'code', 'fullscreen', 'insertdatetime', 'media', 'table',
            'help', 'wordcount', 'emoticons'
        ],
        toolbar:
            'undo redo | blocks fontfamily fontsize | ' +
            'bold italic underline strikethrough | forecolor backcolor | ' +
            'alignleft aligncenter alignright alignjustify | ' +
            'bullist numlist outdent indent | ' +
            'link image media table | ' +
            'emoticons charmap | fullscreen preview code',
        toolbar_sticky: true,
        image_advtab: true,
        branding: false,
        content_style: `
            body {
                font-family: Arial, sans-serif;
                font-size: 14px;
            }
        `
    });
    </script>

    <style>
    form { max-width: 600px; margin:auto; padding:20px; background:white; border-radius:10px; }
    input, textarea { width:100%; padding:10px; margin-top:10px; }
    button { padding: 10px 20px; background:#004aad; color:white; border:0; border-radius:6px; }
    </style>
</head>
<body>

<h2 style="text-align:center;">Edit Member</h2>

<form method="POST" enctype="multipart/form-data">
    <label>Full Name</label>
    <input name="name" value="<?= htmlspecialchars($member['name']) ?>" required>

    <label>Position</label>
    <input name="position" value="<?= htmlspecialchars($member['position']) ?>" required>

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

    <p>Current Photo:</p>
    <img src="../<?= htmlspecialchars($member['photo']) ?>" width="120">

    <label>New Photo:</label>
    <input type="file" name="photo">

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

</body>
</html>
