<?php
session_start();
include 'includes/connect.php';

if(isset($_POST['login'])){
    $mobile = mysqli_real_escape_string($conn, $_POST['mobile']);
    $password = $_POST['password']; // raw password (NO escape needed for verify)

    if(empty($mobile) || empty($password)){
        echo "<script>alert('Please enter mobile and password');</script>";
    } else {

        // Only fetch user by mobile
        $query = "SELECT * FROM distributor 
                  WHERE mobile='$mobile' 
                  AND status='Active' 
                  LIMIT 1";

        $result = mysqli_query($conn, $query);

        if(mysqli_num_rows($result) == 1){
            $user = mysqli_fetch_assoc($result);

            // VERIFY PASSWORD
            if(password_verify($password, $user['password'])){

                $_SESSION['distributor_id'] = $user['id'];
                $_SESSION['distributor_name'] = $user['name'];
                $_SESSION['role'] = $user['role'];
                $_SESSION['user_id'] = $user['id'];
                
                header("Location: dashboard.php");
                exit;

            } else {
                echo "<script>alert('Incorrect password');</script>";
            }

        } else {
            echo "<script>alert('Mobile not found or inactive account');</script>";
        }
    }
}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<head>

    <meta charset="utf-8">
    <title>Agri Tech Admin Panel</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <link rel="stylesheet" type="text/css" href="css/animate.min.css">
    <link rel="stylesheet" type="text/css" href="css/animation.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap-select.min.css">
    <link rel="stylesheet" type="text/css" href="css/styles.css">

    <link rel="stylesheet" href="font/fonts.css">
    <link rel="stylesheet" href="icon/style.css">

    <link rel="shortcut icon" href="img/farm_logo.png">
    <link rel="apple-touch-icon-precomposed" href="img/farm_logo.png">

</head>

<body class="body">

<div id="wrapper">

<div id="page">
<div class="login-page">

    <div class="left">
        <img src="img/login.jpg" alt="image">
    </div>

    <div class="right">
        <div class="login-box">

            <form class="form-login flex flex-column gap22 w-full" method="POST">

                <div>
                    <h3>Login to account</h3>
                    <div class="body-text text-white">
                        Enter your mobile number & password to login
                    </div>
                </div>

                <!-- MOBILE -->
                <fieldset class="email">
                    <div class="body-title mb-10 text-white">
                        Mobile Number <span class="tf-color-1">*</span>
                    </div>
                    <input 
                        class="flex-grow" 
                        type="text" 
                        placeholder="Enter your mobile number" 
                        name="mobile" 
                        required
                    >
                </fieldset>

                <!-- PASSWORD -->
                <fieldset class="password">
                    <div class="body-title mb-10 text-white">
                        Password <span class="tf-color-1">*</span>
                    </div>

                    <input 
                        class="password-input" 
                        type="password" 
                        placeholder="Enter your password" 
                        name="password" 
                        required
                    >

                    <span class="show-pass">
                        <i class="icon-eye view"></i>
                        <i class="icon-eye-off hide"></i>
                    </span>
                </fieldset>

                <div class="flex justify-between items-center">
                    <div class="flex gap10 items-center">
                        <input class="tf-check" type="checkbox" id="signed">
                        <label class="body-text text-surface-3" for="signed">
                            Keep me signed in
                        </label>
                    </div>

                    <a href="#" class="body-text tf-color">Forgot password?</a>
                </div>

                <button type="submit" class="tf-button w-full" name="login">
                    Login
                </button>

            </form>

            <div class="bottom body-text text-center text-white w-full">
                Not registered yet?
                <a href="#" class="body-text tf-color">Register Now</a>
            </div>

        </div>
    </div>

</div>
</div>

</div>

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-select.min.js"></script>
<script defer src="js/main.js"></script>

</body>
</html>