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

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

if ($_SESSION['role'] != 'admin') {
    die("Akses ditolak");
}

$id = (int)$_GET['id'];

$userQuery = mysqli_query(
    $conn,
    "SELECT * FROM users WHERE id='$id'"
);

$user = mysqli_fetch_assoc($userQuery);

if (!$user) {
    die("User tidak ditemukan");
}

if (isset($_POST['update'])) {

    $nama     = mysqli_real_escape_string($conn, $_POST['nama']);
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $role     = mysqli_real_escape_string($conn, $_POST['role']);
    $password = trim($_POST['password']);

    if (!empty($password)) {

        $password = md5($password);

        mysqli_query(
            $conn,
            "
            UPDATE users SET
                nama='$nama',
                username='$username',
                password='$password',
                role='$role'
            WHERE id='$id'
            "
        );

    } else {

        mysqli_query(
            $conn,
            "
            UPDATE users SET
                nama='$nama',
                username='$username',
                role='$role'
            WHERE id='$id'
            "
        );
    }

    echo "
    <script>
        alert('User berhasil diupdate');
        window.location='data_user.php';
    </script>
    ";
    exit;
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">

    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">

    <title>Edit User</title>

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <style>

        body{
            background:#f5f7fb;
        }

        .card{
            border:none;
            border-radius:14px;
        }

        .form-control,
        .form-select{
            border-radius:10px;
        }

        .btn{
            border-radius:10px;
        }

        @media (max-width:768px){

            .container{
                padding-left:12px;
                padding-right:12px;
            }

            .card-body{
                padding:18px;
            }

            h4{
                font-size:18px;
            }

            .btn{
                width:100%;
                margin-bottom:10px;
            }

        }

    </style>
</head>

<body>

<div class="container mt-4 mb-4">

    <div class="row justify-content-center">

        <div class="col-lg-6 col-md-8">

            <div class="card shadow">

                <div class="card-header bg-warning">

                    <h4 class="mb-0">
                        Edit User
                    </h4>

                </div>

                <div class="card-body">

                    <form method="post">

                        <div class="mb-3">

                            <label class="form-label">
                                Nama
                            </label>

                            <input
                                type="text"
                                name="nama"
                                class="form-control"
                                value="<?= htmlspecialchars($user['nama']) ?>"
                                required>

                        </div>

                        <div class="mb-3">

                            <label class="form-label">
                                Username
                            </label>

                            <input
                                type="text"
                                name="username"
                                class="form-control"
                                value="<?= htmlspecialchars($user['username']) ?>"
                                required>

                        </div>

                        <div class="mb-3">

                            <label class="form-label">
                                Password Baru
                            </label>

                            <input
                                type="password"
                                name="password"
                                class="form-control"
                                placeholder="Kosongkan jika tidak ingin mengganti password">

                            <small class="text-muted">
                                Password hanya akan berubah jika diisi.
                            </small>

                        </div>

                        <div class="mb-4">

                            <label class="form-label">
                                Role
                            </label>

                            <select
                                name="role"
                                class="form-select">

                                <option
                                    value="admin"
                                    <?= ($user['role'] == 'admin') ? 'selected' : '' ?>>
                                    Admin
                                </option>

                                <option
                                    value="karyawan"
                                    <?= ($user['role'] == 'karyawan') ? 'selected' : '' ?>>
                                    Karyawan
                                </option>

                            </select>

                        </div>

                        <div class="row">

                            <div class="col-md-6">

                                <button
                                    type="submit"
                                    name="update"
                                    class="btn btn-success w-100">

                                    Update User

                                </button>

                            </div>

                            <div class="col-md-6">

                                <a href="data_user.php"
                                   class="btn btn-secondary w-100">

                                    Kembali

                                </a>

                            </div>

                        </div>

                    </form>

                </div>

            </div>

        </div>

    </div>

</div>

</body>
</html>