<?php
session_start();
include "db.php";

if(isset($_GET['logout'])){
  session_destroy();
  header("Location: admin.php");
  exit;
}

if(!isset($_SESSION['admin'])){
  if(isset($_POST['login'])){
    $u=$_POST['username']; 
    $p=$_POST['password'];
    $res=mysqli_query($conn,"SELECT * FROM admin_users WHERE username='$u' AND password='$p'");
    if(mysqli_num_rows($res)>0){ $_SESSION['admin']=true; }
    else $error="Invalid Login!";
  }
?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Login - Pest Control King</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<style>
body{
  background:#020617;
  display:flex;
  justify-content:center;
  align-items:center;
  height:100vh;
  font-family:'Poppins',sans-serif;
}
.card{
  background:rgba(15,23,42,0.9);
  border:1px solid rgba(148,163,184,0.3);
  border-radius:14px;
  box-shadow:0 10px 30px rgba(0,0,0,0.5);
  color:white;
}
input{
  background:rgba(255,255,255,0.08)!important;
  border:none!important;
  color:white!important;
}
input:focus{
  box-shadow:0 0 0 2px #22c55e!important;
}
.btn-primary{
  background:#22c55e!important;
  border:none!important;
}
</style>
</head>
<body>
<div class="card p-4" style="width:350px;">
<h3 class="mb-3 text-center">Admin Login</h3>
<?php if(isset($error)) echo "<p class='text-danger text-center'>$error</p>"; ?>
<form method="POST">
<input name="username" class="form-control mb-3" placeholder="Username">
<input name="password" type="password" class="form-control mb-3" placeholder="Password">
<button name="login" class="btn btn-primary w-100">Login</button>
</form>
</div>
</body>
</html>

<?php exit; } ?>

<!DOCTYPE html>
<html>
<head>
<title>Admin Dashboard - Pest Control King</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
body{
  font-family:'Poppins',sans-serif;
  background:#0f172a;
  color:white;
}
.sidebar{
  width:240px;
  background:#020617;
  height:100vh;
  position:fixed;
  top:0;left:0;
  padding:1rem;
  border-right:1px solid rgba(148,163,184,0.25);
}
.sidebar a{
  display:block;
  padding:0.7rem 1rem;
  border-radius:12px;
  margin-bottom:10px;
  color:#e2e8f0;
  text-decoration:none;
}
.sidebar a:hover,.active{
  background:#22c55e;
  color:#000!important;
}
.main{
  margin-left:260px;
  padding:2rem;
}
.card-custom{
  background:rgba(15,23,42,0.8);
  border:1px solid rgba(148,163,184,0.25);
  border-radius:18px;
  padding:1.4rem;
}
table{
  width:100%;
  border-radius:12px!important;
  overflow:hidden;
}
table thead{
  background:#22c55e;
  color:black;
  font-weight:600;
}
table td{
  background:rgba(255,255,255,0.05);
  padding:8px;
  border-bottom:1px solid rgba(255,255,255,0.05);
}
.btn-primary{
  background:#22c55e!important;
  border:none!important;
}
textarea,input{
  background:rgba(255,255,255,0.08)!important;
  color:white!important;
}
textarea:focus,input:focus{
  box-shadow:0 0 0 2px #22c55e!important;
}
</style>
</head>
<body>

<!-- SIDEBAR -->
<div class="sidebar">
<h3 style="color:#22c55e;">Admin Panel</h3>
<a href="admin.php?page=enquiries" class="<?=(!isset($_GET['page']) || $_GET['page']=="enquiries")?'active':'';?>">
<i class="fa-solid fa-inbox"></i> Enquiries
</a>
<a href="admin.php?page=seo" class="<?=(isset($_GET['page']) && $_GET['page']=="seo")?'active':'';?>">
<i class="fa-solid fa-magnifying-glass-chart"></i> SEO Settings
</a>
<a href="?logout=1"><i class="fa-solid fa-right-from-bracket"></i> Logout</a>
</div>

<div class="main">

<?php
if(!isset($_GET['page']) || $_GET['page']=="enquiries"){
?>
<h2><i class="fa-solid fa-inbox"></i> Enquiries</h2>
<div class="card-custom mt-3">
<table>
<thead>
<tr><th>ID</th><th>Name</th><th>Phone</th><th>Email</th><th>Service</th><th>Message</th><th>Date</th></tr>
</thead>
<tbody>
<?php
$q=mysqli_query($conn,"SELECT * FROM enquiries ORDER BY id DESC");
while($row=mysqli_fetch_assoc($q)){
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['phone']}</td>
<td>{$row['email']}</td>
<td>{$row['service']}</td>
<td>{$row['message']}</td>
<td>{$row['created_at']}</td>
</tr>";
}
?>
</tbody>
</table>
</div>
<?php } ?>

<?php 
if(isset($_GET['page']) && $_GET['page']=="seo"){

if(isset($_POST['save'])){
mysqli_query($conn,"UPDATE seo_settings SET
title='$_POST[title]',
meta_description='$_POST[description]',
keywords='$_POST[keywords]',
schema_code='$_POST[schema]',
header_code='$_POST[header]',
footer_code='$_POST[footer]'
WHERE id=1");
echo "<p style='color:#22c55e;font-weight:bold;'>SEO Updated Successfully</p>";
}

$seo=mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM seo_settings WHERE id=1"));
?>

<h2><i class="fa-solid fa-magnifying-glass-chart"></i> SEO Manager</h2>
<div class="card-custom mt-3">
<form method="POST">
<label>SEO Title</label>
<input name="title" class="form-control mb-3" value="<?=$seo['title'];?>">

<label>Meta Description</label>
<textarea name="description" class="form-control mb-3" style="height:80px;"><?=$seo['meta_description'];?></textarea>

<label>Keywords</label>
<textarea name="keywords" class="form-control mb-3" style="height:80px;"><?=$seo['keywords'];?></textarea>

<label>Schema JSON-LD</label>
<textarea name="schema" class="form-control mb-3" style="height:120px;"><?=$seo['schema_code'];?></textarea>

<label>Header Code</label>
<textarea name="header" class="form-control mb-3" style="height:120px;"><?=$seo['header_code'];?></textarea>

<label>Footer Code</label>
<textarea name="footer" class="form-control mb-3" style="height:120px;"><?=$seo['footer_code'];?></textarea>

<button class="btn btn-primary">Save SEO Settings</button>
</form>
</div>
<?php } ?>

</div>

</body>
</html>
