<?php
session_start();
require_once '../inc/functions.php';

// Проверка авторизации и прав администратора
if (!isLoggedIn()) {
    header('Location: ../index.php');
    exit;
}
$user = getUserById($_SESSION['user_id']);
if ($user['role'] != 'admin') {
    header('Location: ../index.php');
    exit;
}

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$boss = null;
if ($id) {
    $boss = getBossById($id);
    if (!$boss) {
        header('Location: bosses.php');
        exit;
    }
}

$message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = [
        'name' => $_POST['name'],
        'description' => $_POST['description'],
        'level_required' => (int)$_POST['level_required'],
        'health' => (int)$_POST['health'],
        'attack' => (int)$_POST['attack'],
        'defense' => (int)$_POST['defense'],
        'accuracy' => (int)$_POST['accuracy'],
        'reward_exp' => (int)$_POST['reward_exp'],
        'reward_bucks' => (int)$_POST['reward_bucks'],
        'cooldown_minutes' => (int)$_POST['cooldown_minutes'],
    ];

    if ($id) {
        $result = updateBoss($id, $data, $_FILES['image']);
    } else {
        $result = createBoss($data, $_FILES['image']);
    }

    if ($result['success']) {
        header('Location: bosses.php');
        exit;
    } else {
        $message = $result['message'];
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title><?= $id ? 'Редактировать' : 'Создать' ?> босса</title>
    <meta charset="utf-8">
    <style>
        body { font-family: sans-serif; background: #111; color: #eee; padding: 20px; }
        a { color: #0af; }
        .form-group { margin-bottom: 15px; }
        label { display: block; margin-bottom: 3px; color: #aaa; }
        input[type=text], input[type=number], textarea, select { width: 300px; padding: 5px; background: #333; border: 1px solid #555; color: #fff; }
        textarea { height: 80px; }
        .btn { display: inline-block; padding: 8px 15px; background: #2a2; color: #fff; border: none; border-radius: 3px; cursor: pointer; }
        .message { background: #a22; padding: 10px; margin-bottom: 10px; }
        img.preview { max-width: 100px; max-height: 100px; margin-top: 5px; }
    </style>
</head>
<body>
    <h1><?= $id ? 'Редактировать' : 'Создать' ?> босса</h1>
    <?php if ($message): ?><div class="message"><?= htmlspecialchars($message) ?></div><?php endif; ?>
    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>Название *</label>
            <input type="text" name="name" required value="<?= htmlspecialchars($boss['name'] ?? '') ?>">
        </div>
        <div class="form-group">
            <label>Описание</label>
            <textarea name="description"><?= htmlspecialchars($boss['description'] ?? '') ?></textarea>
        </div>
        <div class="form-group">
            <label>Требуемый уровень *</label>
            <input type="number" name="level_required" required value="<?= $boss['level_required'] ?? 1 ?>">
        </div>
        <div class="form-group">
            <label>Здоровье босса *</label>
            <input type="number" name="health" required value="<?= $boss['health'] ?? 100 ?>">
        </div>
        <div class="form-group">
            <label>Атака босса *</label>
            <input type="number" name="attack" required value="<?= $boss['attack'] ?? 20 ?>">
        </div>
        <div class="form-group">
            <label>Защита босса *</label>
            <input type="number" name="defense" required value="<?= $boss['defense'] ?? 10 ?>">
        </div>
        <div class="form-group">
            <label>Точность босса (шанс крита) *</label>
            <input type="number" name="accuracy" required value="<?= $boss['accuracy'] ?? 5 ?>">
        </div>
        <div class="form-group">
            <label>Награда опыт *</label>
            <input type="number" name="reward_exp" required value="<?= $boss['reward_exp'] ?? 0 ?>">
        </div>
        <div class="form-group">
            <label>Награда баксы *</label>
            <input type="number" name="reward_bucks" required value="<?= $boss['reward_bucks'] ?? 0 ?>">
        </div>
        <div class="form-group">
            <label>Кулдаун (минут) *</label>
            <input type="number" name="cooldown_minutes" required value="<?= $boss['cooldown_minutes'] ?? 60 ?>">
        </div>
        <div class="form-group">
            <label>Изображение</label>
            <input type="file" name="image" accept="image/*">
            <?php if ($id && $boss['image']): ?>
                <br><img src="../<?= $boss['image'] ?>" class="preview" alt="">
            <?php endif; ?>
        </div>
        <button type="submit" class="btn">Сохранить</button>
        <a href="bosses.php" class="btn" style="background:#555;">Отмена</a>
    </form>
</body>
</html>