<?php

error_reporting(E_ALL);
ini_set('display_errors',1);

session_start();
include "../config/db.php";

/* PROJECT BASE PATH */
$admin_base = "/digital_panchayat/admin/";
$operator_base = "/digital_panchayat/operator/";

if(isset($_POST['login']))
{

$email = mysqli_real_escape_string($conn,$_POST['email']);
$password = mysqli_real_escape_string($conn,$_POST['password']);

$query = mysqli_query($conn,"SELECT * FROM users WHERE email='$email' AND status=1");

if(mysqli_num_rows($query)==1)
{

$user = mysqli_fetch_assoc($query);

if($password == $user['password'])
{

$_SESSION['user_id']  = $user['id'];
$_SESSION['role_id']  = $user['role_id'];
$_SESSION['name']     = $user['name'];
$_SESSION['district_id'] = $user['district_id'];
$_SESSION['block_id']    = $user['block_id'];
$_SESSION['village_id']  = $user['village_id'];

/* =========================
   ROLE BASED REDIRECTION
========================= */

if($user['role_id']==6)   // Operator (Mobile Panel)
{
header("Location: ".$operator_base."operator_dashboard.php");
exit;
}
else   // All other roles (Admin Panel)
{
header("Location: ".$admin_base."dashboard.php");
exit;
}

}
else
{
$error="Wrong Password";
}

}
else
{
$error="User Not Found";
}

}

?>

<!DOCTYPE html>
<html>

<head>

<title>Digital Panchayat Login</title>

<style>

body{
font-family:Arial;
background:#f4f6f9;
}

.login-box{
width:350px;
margin:120px auto;
padding:30px;
background:white;
border-radius:6px;
box-shadow:0 0 10px rgba(0,0,0,0.1);
}

input{
width:100%;
padding:10px;
margin-top:8px;
}

button{
width:100%;
padding:10px;
background:#2c3e50;
color:white;
border:none;
margin-top:10px;
cursor:pointer;
}

button:hover{
background:#1a252f;
}

.error{
color:red;
margin-bottom:10px;
}

</style>

</head>

<body>

<div class="login-box">

<h2>Digital Panchayat Login</h2>

<?php
if(isset($error))
{
echo "<p class='error'>$error</p>";
}
?>

<form method="POST">

<label>Email</label>
<input type="email" name="email" required>

<br><br>

<label>Password</label>
<input type="password" name="password" required>

<br><br>

<button type="submit" name="login">Login</button>

</form>

</div>

</body>

</html>