<?php
$servername = "localhost";
$username = "user";
$password = "password";
$dbname = "db";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) { die("Ошибка: " . $conn->connect_error); }

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['add'])) {
        $f = $conn->real_escape_string($_POST['fio']);
        $d = $conn->real_escape_string($_POST['department']);
        $conn->query("INSERT INTO employees (fio, department) VALUES ('$f', '$d')");
    } elseif (isset($_POST['update'])) {
        $id = (int)$_POST['id'];
        $f = $conn->real_escape_string($_POST['fio']);
        $d = $conn->real_escape_string($_POST['department']);
        $conn->query("UPDATE employees SET fio='$f', department='$d' WHERE id=$id");
    } elseif (isset($_POST['delete'])) {
        $id = (int)$_POST['id'];
        $conn->query("DELETE FROM employees WHERE id=$id");
    }
    header("Location: index.php"); exit();
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Задание 7 модуль 2</title>
    <style>
        body { font-family: sans-serif; margin: 0; background: #fff; }
        .header { background: #008000; color: white; padding: 20px; display: flex; align-items: center; }
        .header img { height: 60px; margin-right: 20px; background: white; padding: 5px; border-radius: 5px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { padding: 10px; border-bottom: 1px solid #ddd; text-align: left; }
        .btn { background: #008000; color: white; border: none; padding: 5px 10px; cursor: pointer; border-radius: 3px; }
        .add-row { background: #f9f9f9; padding: 20px; }
    </style>
</head>
<body>
    <div class="header">
        <img src="logo.png" alt="Logo">
        <div><h1>Задание 7 модуль 2</h1><h2>База данных сотрудников</h2></div>
    </div>
    <table>
        <tr><th>ФИО</th><th>Отдел</th><th>Действия</th></tr>
        <?php
        $res = @$conn->query("SELECT * FROM employees");
        if ($res) {
            while($row = $res->fetch_assoc()) {
                echo "<tr><form method='POST'>
                    <td>".htmlspecialchars($row['fio'])."</td>
                    <td>".htmlspecialchars($row['department'])."</td>
                    <td>
                        <input type='hidden' name='id' value='".$row['id']."'>
                        <input type='text' name='fio' value='".htmlspecialchars($row['fio'])."'>
                        <input type='text' name='department' value='".htmlspecialchars($row['department'])."'>
                        <button type='submit' name='update' class='btn'>Обновить</button>
                        <button type='submit' name='delete' class='btn'>Удалить</button>
                    </td>
                </form></tr>";
            }
        }
        ?>
    </table>
    <div class="add-row">
        <form method="POST">
            <input type="text" name="fio" placeholder="ФИО" required>
            <input type="text" name="department" placeholder="Отдел" required>
            <button type="submit" name="add" class="btn" style="padding: 10px 20px;">Добавить сотрудника</button>
        </form>
    </div>
</body>
</html>
