<?php

// include($_SERVER['DOCUMENT_ROOT'].'/test/web/db.php');
// require_once ($_SERVER['DOCUMENT_ROOT'].'/test/web/jwt/jwt_utils.php');

require '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 = "SELECT id,username,status,email,mobile,imageurl FROM admin where email='$email' or mobile='$email' and password='$pwd' LIMIT 1";
if($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' => "Invalid Login Credentials",'token' => "");
        echo json_encode($rows);
    }
}

else
{
    $rows = array('status'=>"",'message' => 'Sql Error');
    echo json_encode($rows);
}
}

?>