<?php
session_start();
error_reporting(0);
include("includes/config.php");

if (isset($_POST['submit'])) {
  $ret = mysqli_query($bd, "SELECT * FROM users WHERE userEmail='" . $_POST['username'] . "' and password='" . md5($_POST['password']) . "'");
  $num = mysqli_fetch_array($ret);
  if ($num > 0) {
    $extra = "dashboard.php";
    $_SESSION['login'] = $_POST['username'];
    $_SESSION['id'] = $num['id'];
    $host = $_SERVER['HTTP_HOST'];
    $uip = $_SERVER['REMOTE_ADDR'];
    $status = 1;
    $log = mysqli_query($bd, "INSERT INTO userlog(uid,username,userip,status) VALUES('" . $_SESSION['id'] . "','" . $_SESSION['login'] . "','$uip','$status')");
    $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
    header("location:http://$host$uri/$extra");
    exit();
  } else {
    $_SESSION['login'] = $_POST['username'];
    $uip = $_SERVER['REMOTE_ADDR'];
    $status = 0;
    mysqli_query($bd, "INSERT INTO userlog(username,userip,status) VALUES('" . $_SESSION['login'] . "','$uip','$status')");
    $errormsg = "Invalid username or password";
  }
}

if (isset($_POST['change'])) {
  $email = $_POST['email'];
  $contact = $_POST['contact'];
  $password = md5($_POST['password']);
  $query = mysqli_query($bd, "SELECT * FROM users WHERE userEmail='$email' and contactNo='$contact'");
  $num = mysqli_fetch_array($query);
  if ($num > 0) {
    mysqli_query($bd, "UPDATE users SET password='$password' WHERE userEmail='$email' and contactNo='$contact'");
    $msg = "Password Changed Successfully";
  } else {
    $errormsg = "Invalid email id or Contact no";
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>GIPS | User Login</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />

  <!-- Bootstrap -->
  <link href="assets/css/bootstrap.css" rel="stylesheet">
  <link href="assets/font-awesome/css/font-awesome.css" rel="stylesheet" />
  <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap" rel="stylesheet">

  <style>
    body, html {
      margin: 0;
      padding: 0;
      height: 100%;
      font-family: 'Montserrat', sans-serif;
    }

    #bg-video {
      position: fixed;
      top: 0;
      left: 0;
      min-width: 100%;
      min-height: 100%;
      object-fit: cover;
      z-index: -2;
    }

    .video-overlay {
      position: fixed;
      top: 0;
      left: 0;
      height: 100%;
      width: 100%;
      backdrop-filter: blur(5px);
      background-color: rgba(0, 0, 0, 0.3);
      z-index: -1;
    }

    .login-container {
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .login-form {
      background-color: rgba(255, 255, 255, 0.95);
      padding: 40px 30px;
      border-radius: 12px;
      width: 100%;
      max-width: 400px;
      box-shadow: 0px 12px 25px rgba(0, 0, 0, 0.15);
    }

    .login-form h2 {
      text-align: center;
      font-weight: 700;
      margin-bottom: 30px;
    }

    .form-control {
      margin-bottom: 20px;
      height: 45px;
      font-size: 16px;
    }

    .btn-theme {
      background-color: #ff7d19;
      border: none;
      font-weight: bold;
      font-size: 16px;
      padding: 12px;
      border-radius: 8px;
      width: 100%;
      color: white;
    }

    .btn-theme:hover {
      background-color: #e66f13;
    }

    .text-center a {
      color: #ff7d19;
      text-decoration: none;
    }

    .error-msg {
      color: red;
      text-align: center;
      margin-bottom: 10px;
    }

    .success-msg {
      color: green;
      text-align: center;
      margin-bottom: 10px;
    }

    .footer {
      position: fixed;
      bottom: 10px;
      text-align: center;
      width: 100%;
      color: white;
      font-size: 14px;
      text-shadow: 1px 1px 2px #000;
    }

    .modal-content {
      border-radius: 10px;
    }
  </style>
</head>
<body>

<!-- 🎥 Background Video -->
<video autoplay muted loop id="bg-video">
  <source src="https://www.gniotgroup.edu.in/img/video/Website%20Video.mp4" type="video/mp4">
</video>
<div class="video-overlay"></div>

<!-- 🔐 Login Form -->
<div class="login-container">
  <form class="login-form" method="post" name="login">
    <h2>Grievance Management System</h2>

    <?php if ($errormsg): ?>
      <div class="error-msg"><?php echo htmlentities($errormsg); ?></div>
    <?php endif; ?>
    <?php if ($msg): ?>
      <div class="success-msg"><?php echo htmlentities($msg); ?></div>
    <?php endif; ?>

    <input type="text" name="username" class="form-control" placeholder="Email" required autofocus>
    <input type="password" name="password" class="form-control" placeholder="Password" required>

    <button class="btn btn-theme" name="submit" type="submit">
      <i class="fa fa-lock"></i> SIGN IN
    </button>

    <div class="text-center mt-3">
      <a data-toggle="modal" href="#myModal">Forgot Password?</a><br><br>
      Don't have an account? <a href="registration.php">Register</a>
    </div>
  </form>
</div>

<!-- Forgot Password Modal -->
<form name="forgot" method="post">
  <div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">

        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h4 class="modal-title">Forgot Password?</h4>
        </div>

        <div class="modal-body">
          <p>Enter your details below to reset your password:</p>
          <input type="email" name="email" placeholder="Email" class="form-control" required><br>
          <input type="text" name="contact" placeholder="Contact No" class="form-control" required><br>
          <input type="password" name="password" placeholder="New Password" id="password" class="form-control" required><br>
          <input type="password" name="confirmpassword" placeholder="Confirm Password" id="confirmpassword" class="form-control" required>
        </div>

        <div class="modal-footer">
          <button class="btn btn-default" type="button" data-dismiss="modal">Cancel</button>
          <button class="btn btn-theme" type="submit" name="change" onclick="return valid();">Submit</button>
        </div>

      </div>
    </div>
  </div>
</form>

<div class="footer">&copy; 2025 GIPS. All rights reserved.</div>

<!-- Scripts -->
<script src="assets/js/jquery.js"></script>
<script src="assets/js/bootstrap.min.js"></script>

<script>
  function valid() {
    if (document.forgot.password.value !== document.forgot.confirmpassword.value) {
      alert("Password and Confirm Password do not match!");
      document.forgot.confirmpassword.focus();
      return false;
    }
    return true;
  }
</script>
</body>
</html>
