<?php

session_start();


if(!isset($_SESSION['user_id'])){

    header("Location:login.php");
    exit;

}


require_once "config.php";



$id=$_GET['id'] ?? 0;



if(!$id){

    header("Location:users.php");
    exit;

}




// GET USER

$stmt=$pdo->prepare("
SELECT *
FROM users
WHERE id=?
");


$stmt->execute([$id]);


$user=$stmt->fetch();



if(!$user){

    die("User not found");

}






// UPDATE USER


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


$fullname=$_POST['fullname'];

$username=$_POST['username'];

$email=$_POST['email'];

$role=$_POST['role'];

$status=$_POST['status'];

$password=$_POST['password'];





if($password!=""){


$hashed=password_hash(
$password,
PASSWORD_DEFAULT
);


$stmt=$pdo->prepare("

UPDATE users SET

fullname=?,
username=?,
email=?,
password=?,
role=?,
status=?

WHERE id=?

");


$stmt->execute([

$fullname,
$username,
$email,
$hashed,
$role,
$status,
$id

]);



}else{


$stmt=$pdo->prepare("

UPDATE users SET

fullname=?,
username=?,
email=?,
role=?,
status=?

WHERE id=?

");



$stmt->execute([

$fullname,
$username,
$email,
$role,
$status,
$id

]);


}



$_SESSION['success']="User updated successfully";


header("Location:users.php");

exit;


}



?>



<!DOCTYPE html>

<html>

<head>

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


<title>Edit User</title>


<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap" rel="stylesheet">


<link rel="stylesheet" href="assets/css/sidebar.css">


<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">



<style>


*{
margin:0;
padding:0;
box-sizing:border-box;
font-family:Poppins;
}



body{

background:#f4f7f9;

}



body.dark{

background:#111827;
color:white;

}



.content{

margin-left:260px;
padding:25px;

}



.topbar{

display:flex;
justify-content:space-between;
align-items:center;
margin-bottom:20px;

}



.mobile-btn{

display:none;
font-size:25px;
cursor:pointer;

}


.toggle{

font-size:25px;
cursor:pointer;
color:#059669;

}



.card{

background:white;
padding:25px;
border-radius:18px;
max-width:800px;
box-shadow:0 5px 20px #0002;

}



body.dark .card{

background:#1f2937;

}



.form-grid{

display:grid;
grid-template-columns:1fr 1fr;
gap:15px;

}



.form-group{

margin-bottom:15px;

}



label{

display:block;
font-weight:600;
margin-bottom:5px;

}



input,
select{

width:100%;
padding:12px;
border-radius:10px;
border:1px solid #ddd;

}



button{

background:#059669;
color:white;
border:0;
padding:12px 25px;
border-radius:10px;
cursor:pointer;

}



.back{

background:#64748b;
color:white;
padding:12px 25px;
border-radius:10px;
text-decoration:none;

}



.buttons{

margin-top:20px;
display:flex;
gap:10px;

}



.overlay{

display:none;
position:fixed;
inset:0;
background:#0008;
z-index:999;

}


.overlay.show{

display:block;

}



@media(max-width:768px){


.content{

margin-left:0;
padding:15px;

}


.mobile-btn{

display:block;

}



.sidebar{

left:-260px;

}


.sidebar.active{

left:0;

}



.form-grid{

grid-template-columns:1fr;

}



.buttons{

flex-direction:column;

}



button,
.back{

text-align:center;
width:100%;

}


}


</style>


</head>



<body>



<?php include "sidebar.php"; ?>


<div class="overlay"
id="overlay"
onclick="closeMenu()">
</div>




<div class="content">



<div class="topbar">


<i class="fa fa-bars mobile-btn"
onclick="openMenu()">
</i>



<h2>
<i class="fa fa-user-edit"></i>
Edit User
</h2>



<i class="fa fa-moon toggle"
onclick="theme()">
</i>


</div>





<div class="card">



<form method="POST">



<div class="form-grid">



<div class="form-group">

<label>
Full Name
</label>

<input type="text"
name="fullname"
value="<?=$user['fullname']?>"
required>

</div>





<div class="form-group">

<label>
Username
</label>

<input type="text"
name="username"
value="<?=$user['username']?>"
required>

</div>





<div class="form-group">

<label>
Email
</label>


<input type="email"
name="email"
value="<?=$user['email']?>">

</div>





<div class="form-group">

<label>
New Password
</label>


<input type="password"
name="password"
placeholder="Leave blank to keep old password">

</div>





<div class="form-group">

<label>
Role
</label>


<select name="role">


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


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


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


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


</select>


</div>






<div class="form-group">

<label>
Status
</label>


<select name="status">


<option value="1"
<?=($user['status']==1)?'selected':''?>>

Active

</option>



<option value="0"
<?=($user['status']==0)?'selected':''?>>

Inactive

</option>



</select>



</div>



</div>




<div class="buttons">


<button name="update">

<i class="fa fa-save"></i>

Update User

</button>



<a href="users.php"
class="back">

Cancel

</a>



</div>




</form>



</div>



</div>





<script>


function openMenu(){

sidebar.classList.add("active");
overlay.classList.add("show");

}


function closeMenu(){

sidebar.classList.remove("active");
overlay.classList.remove("show");

}



function theme(){

document.body.classList.toggle("dark");

localStorage.setItem(
"dark",
document.body.classList.contains("dark")
);

}


if(localStorage.getItem("dark")=="true"){

document.body.classList.add("dark");

}


</script>



</body>

</html>