<?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 role = 'Admin'
           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  class="bg2">
  <div class="container  ">
  <header class="">
  <div class="row align-items-center">
    <div class="col-6 text-start">
    <?php 
 
 if( isset($_SESSION['role']) && $_SESSION['role']=="Admin" )  { echo '<a href="admin.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }
else if( isset($_SESSION['role']) && $_SESSION['role']=="User")  { echo  '<a href="tm.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }
 else if( isset($_SESSION['role']) && $_SESSION['role']=="Zbm") { echo '<a href="abm.php"> <img src="images/lpt-left-logo.png" class="img-fluid" alt="Left logo"></a>'; }
else  { echo '<a href="#"> <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" id="loginForm">

          <div class="row mb-3">
            <div class="col-2 col-md-3"></div>
            <div class="col-8 col-md-6"><input type="text" class="form-control" id="username" name="username" placeholder="Admin-id" required></div>
            <div class="col-2 col-md-3"></div>
          </div>
          <div class="row mb-3 ">
            <div class="col-3 col-md-3"></div>
            <div class="col-6 col-md-6"> <center> <input 
    type="image" 
    src="images/login_btn2.png" 
    class="img-fluid" 
    alt="Login" 
  /></center></div>
            <div class="col-3 col-md-3"></div>
          </div>
</form>          

</div>
 
<div class="modal fade" id="confirmModal" tabindex="-1" aria-labelledby="confirmModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="confirmModalLabel">Read Consent Carefully</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        lipsumlipsumlipsumlipsumlipsumlipsumlipsum 
        lipsumlipsumlipsumlipsumlipsumlipsumlipsum 
        lipsumlipsumlipsumlipsumlipsumlipsumlipsum 
        lipsumlipsumlipsumlipsumlipsumlipsumlipsum 
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" id="confirmSubmit">Yes, Continue</button>
      </div>
    </div>
  </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 contact-us 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>
const loginForm = document.getElementById('loginForm');
const confirmModal = new bootstrap.Modal(document.getElementById('confirmModal'));
const confirmSubmitButton = document.getElementById('confirmSubmit');

// When form tries to submit
loginForm.addEventListener('submit', function(event) {
    event.preventDefault(); // Stop immediate form submit
    confirmModal.show();    // Show the Bootstrap modal
});

// When user clicks "Yes, Continue"
confirmSubmitButton.addEventListener('click', function() {
    confirmModal.hide();    // Hide the modal
    loginForm.submit();     // Actually submit the form
});
</script>
</body>
</html>