<?php
require 'config_db.php';
require 'jwt_utils.php';

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
if(isset($postdata) && !empty($postdata))
{

$pwd = mysqli_real_escape_string($con, trim($request->password));
$email = mysqli_real_escape_string($con, trim($request->username));

$sql_check = "SELECT * from app_users where mobile ='$email' and password='$pwd'";
$sql = "SELECT * from app_users where mobile ='$email' and password='$pwd' and status = '1'";
$cc = $con->query($sql_check)->num_rows;
if($cc != 0)
{
    $result = mysqli_query($con,$sql);
    $row = mysqli_fetch_assoc($result);
    if($row != null)
    {
        $headers = array('alg'=>'HS256','typ'=>'JWT');
        $payload = array('user_details'=>$row,'exp'=>(time() + 60000)); // 60 is equals 1 minute
        $jwt = generate_jwt($headers, $payload);
        $rows = array('status'=>"Success",'message' => "Login Success",'token' => $jwt);
        echo json_encode($rows);  
    }
    else
    {
        $rows = array('status'=>"Failure",'message' => 'Your Account Deactivated','token' => "");
        echo json_encode($rows);
    }
}
else
{
    $rows = array('status'=>"Failure",'message' => "Invalid Login Credentials",'token' => "");
    echo json_encode($rows);
}
}


?>