<?php
session_start();
include 'dbconnection.php';

if (!isset($_SESSION['emp_type']) || $_SESSION['emp_type'] !== 'admin') {
    die("Unauthorized access");
}

$id = intval($_GET['id']);  // Always sanitize GET inputs

$query = "SELECT * FROM student WHERE s_id = $id";
$result = $con->query($query);
$data = $result->fetch_assoc();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $con->real_escape_string($_POST['s_name']);
    $fname = $con->real_escape_string($_POST['s_fname']);
    $phone = $con->real_escape_string($_POST['s_phone']);
    $tazkera = $con->real_escape_string($_POST['s_school']);
    $note = $con->real_escape_string($_POST['s_note']);

    $update = "UPDATE student SET 
                s_name = '$name', 
                s_fname = '$fname', 
                s_phone = '$phone', 
                s_school = '$tazkera',
                s_note = '$note'  
               WHERE s_id = $id";
    $con->query($update);
    header("Location: view_student.php");
    exit;
}
?>

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="shortcut icon" type="image/png" href="images/favicon.png">
    <title>ویرایش محصل</title>
    <style>
        * {
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f9f9f9;
            margin: 0;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 480px;
            margin: 30px auto;
            background: white;
            padding: 25px 30px;
            border-radius: 8px;
            box-shadow: 0 8px 20px rgb(0 0 0 / 0.1);
        }

        h2 {
            text-align: center;
            margin-bottom: 24px;
            color: #414465;
        }

        form label {
            display: block;
            margin-bottom: 6px;
            font-weight: 600;
            color: #414465;
        }

        form input[type="text"] {
            width: 100%;
            padding: 10px 12px;
            margin-bottom: 20px;
            border: 1.8px solid #DEBD6B;
            border-radius: 6px;
            font-size: 16px;
            direction: rtl;
            transition: border-color 0.3s ease;
        }

        form input[type="text"]:focus {
            outline: none;
            border-color: #414465;
        }

        button {
            width: 100%;
            padding: 12px;
            background: #414465;
            border: none;
            border-radius: 6px;
            color: white;
            font-size: 18px;
            cursor: pointer;
            font-weight: 700;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background: #5a5f7d;
        }

        .back-button {
            margin-top: 20px;
            text-align: center;
        }

        .back-button a {
            display: inline-block;
            padding: 10px 20px;
            background: #DEBD6B;
            color: #fff;
            text-decoration: none;
            border-radius: 6px;
            font-weight: bold;
        }

        @media (max-width: 520px) {
            .container {
                margin: 20px 15px;
                padding: 20px;
            }

            form input[type="text"], button {
                font-size: 16px;
                padding: 10px;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h2>ویرایش معلومات محصل</h2>
    <form method="POST" action="" onsubmit="return confirm('آیا مطمئن هستید که می‌خواهید تغییرات را ذخیره کنید؟');">
        <label for="s_name">نام:</label>
        <input type="text" id="s_name" name="s_name" value="<?php echo htmlspecialchars($data['s_name']); ?>" required>

        <label for="s_fname">اسم پدر:</label>
        <input type="text" id="s_fname" name="s_fname" value="<?php echo htmlspecialchars($data['s_fname']); ?>" required>

        <label for="s_phone">شماره تماس:</label>
        <input type="text" id="s_phone" name="s_phone" value="<?php echo htmlspecialchars($data['s_phone']); ?>" required>

        <label for="s_tazkera">اسم مکتب:</label>
        <input type="text" id="s_tazkera" name="s_tazkera" value="<?php echo htmlspecialchars($data['s_school']); ?>" required>

        <label for="s_tazkera">نوت:</label>
        <input type="text" id="s_note" name="s_note" value="<?php echo htmlspecialchars($data['s_note']); ?>" required>

        <button type="submit">ذخیره تغییرات</button>
    </form>

    <div class="back-button">
        <a href="view_student.php">← بازگشت</a>
    </div>
</div>

</body>
</html>
