<?php
include("../db.php");
header("Access-Control-Allow-Origin: * ");

$user_type = $_REQUEST['user_type'];
$login_id = $_REQUEST['login_id'];
$pass = $_REQUEST['password'];
$session = $_REQUEST['session'];
$date = date("Y-m-d");
$device_id = $_REQUEST['device_id'];

$user_type = mysql_real_escape_string($user_type);
$session = mysql_real_escape_string($session);
$date = mysql_real_escape_string($date);
$login_id = mysql_real_escape_string($login_id);
$pass = mysql_real_escape_string($pass);
$device_id = mysql_real_escape_string($device_id);


// to delete device id start
$select_device_id = $db->getRow("select id from device_token where  device_id='$device_id' ");
//echo  $select_device_id[id]; die;
$arr_delete = $db->delete("device_token", " where id='" . $select_device_id[id] . "'");
// to delete device id end

if ($user_type == 'student') {


    $arr = $db->getRow("select * from student where admissionno='" . $login_id . "' and password='" . $pass . "' and session='" . $session . "'");


    if (count($arr) > 0) {
        $aryData = array(

            "user_type" => $user_type,
            "login_id" => $arr['idno'],
            "password" => $pass,
            "date" => $date,
            "session" => $session,
            "device_id" => $device_id
        );

        //print_r($aryData);
        $insert_complain = $db->insertAry("device_token", $aryData);

    }
}

if ($user_type == 'teacher') {
    $arr = $db->getRow("select * from staff where mobile='" . $login_id . "' and password='" . $pass . "' and session='" . $session . "'");

    $infoRow = $db->getRow("select is_staff_login_timebound,staff_login_start_time,staff_login_end_time from tbl_info");

    if (count($arr) > 0) {
        $aryData = array(

            "user_type" => $user_type,
            "login_id" => $arr['idno'],
            "password" => $pass,
            "date" => $date,
            "session" => $session,
            "device_id" => $device_id
        );

        //print_r($aryData);
        $insert_complain = $db->insertAry("device_token", $aryData);

    }

}


if ($user_type == 'admin') {

    $arr = $db->getRow("select * from admin where login_id='" . $login_id . "' and password='" . $pass . "' and session='" . $session . "'");


    //print_r($arr); die;
    if (count($arr) > 0) {
        $aryData = array(

            "user_type" => $user_type,
            "login_id" => $arr['id'],
            "password" => $pass,
            "date" => $date,
            "session" => $session,
            "device_id" => $device_id
        );

        //print_r($aryData);
        $insert_complain = $db->insertAry("device_token", $aryData);

    }


}


if (is_array($arr) && count($arr) > 0) {

    echo json_encode(array('status' => 1, 'msg' => 'Login Successfull...!', 'data' => $arr));
    //echo "http://mis.tekari.technosofttechnology.com/media/photo/".$arr["photo"];
} else {
    echo json_encode(array('status' => 0, 'msg' => 'Invalid Login ID or Password...!'));
}
//echo $arr["name"];	

?>