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

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
   
    //$role     = trim($_POST['role']); 

    // Prepare and execute login query with status check
    $stmt = $conn->prepare(
        "SELECT id, userid, role FROM usermaster 
         WHERE userid = ? 
        
           AND Userstatus = 'active'"
    );
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result && $result->num_rows === 1) {
        $user = $result->fetch_assoc();
        // Set session variables
        $_SESSION['id']  = $user['id'];
        $_SESSION['user_id']  = $user['userid'];
        $_SESSION['username'] = $username;
        $_SESSION['role']     = $user['role'];
        
        // Redirect to dashboard
        header('Location: admin-dashboard.php');
        exit;
    } else {
        $error = "Invalid credentials or inactive account.";
    }
    $stmt->close();
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>index</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="css/styles.css" rel="stylesheet">
</head>
<body  >
  <div class="container bg2 ">
  <header class="">
  <div class="row align-items-center">
    <div class="col-6 text-start">
    <a href="admin.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>
    </div>
    <div class="col-6 text-end">
      <img src="images/topmenu-logo.png" class="img-fluid" alt="Topmenu logo">
    </div>
  </div>
</header>
  
<div class="content   mt-3">
<form method="post" action="admin-login.php">

          <div class="row mb-3">
            <div class="col-2 col-md-3"></div>
            <div class="col-8 col-md-6"><div class="box">Data Tracker</div></div>
            <div class="col-2 col-md-3"></div>
          </div>
          <div class="row mb-3 ">
            
            <div class="col-4 col-md-4 "> <a href="#"><div id="quick_count" class="button1">No. of Camps</div></a></div>
             <div class="col-4 col-md-4 "> <a href="#"><div id="totalpatients"  class="button1">No. of patients Screened</div></a></div>
             
            <div class="col-4 col-md-4 "> <a href="export_doctors.php"><div id="detailed_report"  class="button1">Detailed Report</div></a></div>
            
          </div>
</form>          


</div>
 
<div class="row mb-3 ">
<div class="col-3 col-md-12 "></div>
<div class="col-6 col-md-12 "><a href="logout.php"  > <img src="images/logout.png" class="img-fluid "></a></div>
<div class="col-3 col-md-12 "></div>
</div>




<footer>

  <div class="row">
  
    <div class="col-6 col-md-6 padding-zero text-center" style="background:#ECE9F8"><img src="images/disclaimer.png" class="img-fluid"> </div>
    <div class="col-6 col-md-6 padding-zero   text-center" style="background:#54308E"><img src="images/contact-us.png" class="img-fluid"> </div>
  
</div>
</footer>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

<script>
document.querySelector('#quick_count').addEventListener('click', function() {
    const div = this;

    // Fetch count from PHP
    fetch('get_doctors_count.php')
    .then(response => response.text())
    .then(data => {
        let target = parseInt(data);
        let count = 0;
        div.textContent = target;
       
    })
    .catch(error => {
        console.error('Error fetching count:', error);
        div.textContent = "Error!";
    });
});
</script>



<script>
document.querySelector('#totalpatients').addEventListener('click', function() {
    const div = this;

    // Fetch count from PHP
    fetch('get_patients_count.php')
    .then(response => response.text())
    .then(data => {
        let target = parseInt(data);
        let count = 0;
        div.textContent = target;
       
    })
    .catch(error => {
        console.error('Error fetching count:', error);
        div.textContent = "Error!";
    });
});
</script>

<script>
document.querySelector('#detailed_report').addEventListener('click', function() {
   
});
</script>

</body>
</html>