<?php
session_start();
require_once "../config/db.php";

/* ==========================
   CEK LOGIN ADMIN
========================== */
if (!isset($_SESSION['admin_id'])) {
    header("Location: ../login.php");
    exit;
}

$message = "";

/* ==========================
   TAMBAH ADMIN
========================== */
if(isset($_POST['tambah'])){

$username = trim($_POST['username']);
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$role = $_POST['role'];

$stmt = $conn->prepare("INSERT INTO admin (username,password,role,status) VALUES (?,?,?,'aktif')");
$stmt->bind_param("sss",$username,$password,$role);

if($stmt->execute()){
$message="Admin berhasil ditambahkan";
}else{
$message="Gagal menambahkan admin";
}

$stmt->close();
}

/* ==========================
   HAPUS ADMIN
========================== */
if(isset($_GET['hapus'])){

$id=$_GET['hapus'];

$cek = $conn->query("SELECT role FROM admin WHERE id='$id'")->fetch_assoc();

if($cek['role']!='owner'){
$conn->query("DELETE FROM admin WHERE id='$id'");
}

header("Location: admin.php");
exit;
}

/* ==========================
   AMBIL DATA ADMIN
========================== */
$admins = $conn->query("SELECT * FROM admin ORDER BY id DESC");
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../assets/style.css">
<title>Kelola Admin - LuminaCare</title>

<style>

body{
margin:0;
font-family:Arial, sans-serif;
background:#f4f6f9;
}

.header{
background:#1e3a8a;
color:white;
padding:20px;
display:flex;
justify-content:space-between;
align-items:center;
}

.header-title{
font-size:20px;
font-weight:bold;
}

.nav a{
color:white;
text-decoration:none;
margin-left:20px;
font-size:14px;
}

.nav a:hover{
text-decoration:underline;
}

.container{
padding:30px;
}

.section{
background:white;
padding:20px;
border-radius:8px;
margin-bottom:20px;
box-shadow:0 4px 10px rgba(0,0,0,0.05);
}

input,select{
padding:8px;
margin:5px;
border:1px solid #ccc;
border-radius:4px;
}

button{
padding:8px 15px;
background:#1e40af;
color:white;
border:none;
border-radius:5px;
cursor:pointer;
}

button:hover{
background:#1e3a8a;
}

table{
width:100%;
border-collapse:collapse;
margin-top:15px;
}

table th, table td{
border:1px solid #ddd;
padding:10px;
text-align:left;
}

table th{
background:#f1f5f9;
}

.link-btn{
display:inline-block;
padding:6px 10px;
background:#dc2626;
color:white;
text-decoration:none;
border-radius:4px;
font-size:13px;
}

.link-btn:hover{
background:#b91c1c;
}

.success{
color:green;
margin-bottom:10px;
}

.table-wrapper{
overflow-x:auto;
}

</style>
</head>

<body>

<?php include "../inc/navbar_admin.php"; ?>

<div class="container">

<div class="section">

<h3>Tambah Admin</h3>

<?php if($message){ ?>
<p class="success"><?= $message ?></p>
<?php } ?>

<form method="POST">

<input type="text" name="username" placeholder="Username" required>

<input type="password" name="password" placeholder="Password" required>

<select name="role">
<option value="admin">Admin</option>
<option value="owner">Owner</option>
</select>

<button name="tambah">Tambah Admin</button>

</form>

</div>

<div class="section">

<h3>Daftar Admin</h3>

<div class="table-wrapper">

<table>

<tr>
<th>ID</th>
<th>Username</th>
<th>Role</th>
<th>Status</th>
<th>Aksi</th>
</tr>

<?php while($row=$admins->fetch_assoc()){ ?>

<tr>
<td><?= $row['id'] ?></td>
<td><?= $row['username'] ?></td>
<td><?= $row['role'] ?></td>
<td><?= $row['status'] ?></td>
<td>

<?php if($row['role']!='owner'){ ?>

<a class="link-btn" href="?hapus=<?= $row['id'] ?>" 
onclick="return confirm('Hapus admin ini?')">
Hapus
</a>

<?php } else { ?>

Owner

<?php } ?>

</td>
</tr>

<?php } ?>

</table>

</div>

</div>

</div>

<?php include "../inc/footer.php"; ?>

</body>
</html>
