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

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // 1️⃣ Insert new member
    $name = $_POST['name'];
    $position = $_POST['position'];
    $description = $_POST['description'];

    $photo = '';
    if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
        $targetDir = "../uploads/team/";
        if (!is_dir($targetDir)) mkdir($targetDir, 0777, true);

        $fileName = time() . "_" . basename($_FILES['photo']['name']);
        $targetFile = $targetDir . $fileName;

        // Validate image type
        $allowed = ['jpg','jpeg','png','gif','webp'];
        $fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
        if (!in_array($fileExt, $allowed)) {
            die("Only JPG, PNG, GIF, WEBP files are allowed.");
        }

        if (move_uploaded_file($_FILES['photo']['tmp_name'], $targetFile)) {
            // Save web path for DB
            $photo = './uploads/team/' . $fileName;
        } else {
            die("Failed to upload photo. Check folder permissions.");
        }
    }

    // Insert into DB
    $stmt = $conn->prepare("INSERT INTO team_members (name, position, photo, description) VALUES (?,?,?,?)");
    $stmt->bind_param("ssss", $name, $position, $photo, $description);
    $stmt->execute();

    // 2️⃣ Update existing photo paths
    $update_sql = "UPDATE team_members 
                   SET photo = REPLACE(photo, '../uploads/team/', './uploads/team/')";
    $conn->query($update_sql);

    $message = "New member added and existing photo paths updated successfully!";
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <?php include 'header.php'; ?>
<meta charset="UTF-8">
<title>Add & Update Team Members</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>
body { font-family: Arial, sans-serif; background: #f0f2f5; }
.container { max-width: 700px; margin: 50px auto; padding: 30px; background: #fff; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.1); }
h2 { text-align: center; color: #333; }
input, textarea { width: 100%; padding: 12px 15px; margin-top: 10px; border: 1px solid #ccc; border-radius: 8px; box-sizing: border-box; }
button { padding: 12px 25px; background: #004aad; color: #fff; border: none; border-radius: 8px; cursor: pointer; font-size: 16px; display: block; margin: 20px auto; }
button:hover { background: #003a8c; }
.message { text-align: center; color: green; font-weight: bold; margin-top: 20px; }
label { font-weight: bold; margin-top: 15px; display: block; }
</style>
</head>
<body>

<div class="container">
<h2>Add New Team Member & Update Photo Paths</h2>

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

<form method="POST" enctype="multipart/form-data">
    <label>Full Name</label>
    <input type="text" name="name" placeholder="Full Name" required>

    <label>Position</label>
    <input type="text" name="position" placeholder="Position" required>

    <label>Description</label>
    <textarea name="description" placeholder="Description"></textarea>

    <label>Photo</label>
    <input type="file" name="photo" accept="image/*">

    <button type="submit">Add Member & Update Paths</button>
</form>
</div>

</body>
</html>
