<?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 shop_settings.user_name, 
        shop_settings.shop_id, 
        shop_settings.shop_code, 
        shop_details.shop_logo, 
        shop_details.shop_name 
        FROM shop_details 
        INNER JOIN shop_settings 
        ON shop_details.shop_id = shop_settings.shop_id and 
        shop_details.shop_code = shop_settings.shop_code 
        where shop_settings.mobile_no='$email' and shop_settings.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);
}
}
else
{
    header("location: https://iconxtechnologies.in/404");
    exit();
}

?>