<?php
header("Access-Control-Allow-Origin: *");
include_once 'includes/variables.php';

DEFINE('DB_HOST', $host);
DEFINE('DB_USER', $user);
DEFINE('DB_PASSWORD', $pass);
DEFINE('DB_NAME', $database);

$mysqli = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to MySQL');
@mysql_select_db(DB_NAME) or die('Could not select the database');

//$response = array("error" => FALSE);
//mysql_query("SET NAMES 'utf8'");

/**
 * Get user by Mobile and password
 */
try
{
    $json = file_get_contents('php://input');
    //converting json object to php associative array
    $loginData = json_decode($json, true);

    if (isset($loginData['mobileNo'])) {

        // receiving the post params
        $mob = $loginData['mobileNo'];
        $fcm_token = $loginData['fcm_token'];
    
		
        $query = "SELECT * FROM user_master WHERE mobile = '" . $mob . "' AND status=1";
        $resouter = mysql_query($query);

        $set = array();
        $total_records = mysql_num_rows($resouter);
        if ($total_records >= 1) {
            $query1 = "UPDATE user_master SET firbase_token='" . $fcm_token . "' WHERE mobile = '" . $mob . "'";
            $resouter1 = mysql_query($query1);

            while ($results = mysql_fetch_array($resouter, MYSQL_ASSOC)) {

                $set['TimepassApp'][] = $results;
                $response["status"] = "success";
                $response["new_user"] = false;
                $response["message"] = "Signin Successfully";

                $response["user"]["userID"] = $results["slno"];
                $response["user"]["userName"] = $results["nick_name"];
                $response["user"]["mobileNo"] = $results["mobile"];
                $response["user"]["emailID"] = $results["email"];
                $response["user"]["profileImage"] = $results["profile_image"];
                echo json_encode($response);
            }
        } else {
            //$results[]={};
            $insquery = "INSERT INTO user_master (mobile,firbase_token, status) VALUES('" . $mob . "','" . $fcm_token . "','1')";
            $resinner = mysql_query($insquery);

            $query = "SELECT * FROM user_master WHERE mobile = '" . $mob . "' AND status=1";
            $resouter = mysql_query($query);

            $set = array();
            $total_records = mysql_num_rows($resouter);
            if ($total_records >= 1) {
                $query1 = "UPDATE user_master SET firbase_token='" . $fcm_token . "' WHERE mobile = '" . $mob . "'";
                $resouter1 = mysql_query($query1);

                while ($results = mysql_fetch_array($resouter, MYSQL_ASSOC)) {

                    $set['TimepassApp'][] = $results;
                    $response["status"] = "success";
                    $response["new_user"] = true;
                    $response["message"] = "New User Registered Successfully";

                    $response["user"]["userID"] = $results["slno"];
                    $response["user"]["userName"] = $results["nick_name"];
                    $response["user"]["firstName"] = $results["firstname"];
                    $response["user"]["lastName"] = $results["lastname"];
                    $response["user"]["mobileNo"] = $results["mobile"];
                    $response["user"]["emailID"] = $results["email"];
                    $response["user"]["profileImage"] = $results["profile_image"];
                    echo json_encode($response);
                }
            }

        }

    }
} catch (Exception $e) {
    $response["status"] = "failed";
    $response["message"] = $e->getMessage();
    echo json_encode($response);
}

// echo $val= str_replace('\\/', '/', json_encode($set));
