<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: access");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

session_start();

require __DIR__.'/classes/Database.php';
require __DIR__.'/classes/lib.php';


$db_connection = new Database();
$conn = $db_connection->dbConnection();

$data = json_decode(file_get_contents("php://input"));
//$data = json_decode('{ "qr_id": "m7MdMK" }');

// var_dump( get_defined_vars() );
// echo "xxxxxxx";
// echo $data;
// echo "-------";

$returnData = new CMsg(0);
$fields = ['fields' => ['qr_id']];

// if(true)

    // CHECKING EMPTY FIELDS
if(!isset($data->qr_id) 
|| empty(trim($data->qr_id))
)
{
    $returnData = new CMsg(0,422,'Please Fill in all Required Fields!',$fields);
    
}
// IF THERE ARE NO EMPTY FIELDS THEN-
else 
{
    $qr_id = trim($data->qr_id);

    try
    {
        $fetch_user_qr_id = "SELECT id, qr_id, email, name, phone, qr_code, picture FROM `dogs` WHERE `qr_id`=:qr_id";
        $query_stmt = $conn->prepare($fetch_user_qr_id);
        $query_stmt->bindValue(':qr_id', $qr_id,PDO::PARAM_STR);
        $query_stmt->execute();

        // IF THE dog IS FOUNDED BY qr_id
        if($query_stmt->rowCount())
        {
            $row = $query_stmt->fetch(PDO::FETCH_ASSOC);
            $returnData = new CMsg(1,200,'get dog',$fields,$row);
        }
        else
        {
            $returnData = new CMsg(0,422,'no dog',$fields);
        }
    }
    catch(PDOException $e)
    {
        $returnData = new CMsg(0,500,$e->getMessage(),$fields);
    }
}
echo $returnData->jsonarray();
?>