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

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

    if ($user_type == 'teacher' || $user_type == 'student'){
        $passEnc = new password();
        $pass = $passEnc->password($ac_pass);
    }else{
        $pass = $ac_pass;
    }

// 	$user_type=mysqli_real_escape_string($db,$user_type);
// 	$session=mysqli_real_escape_string($db,$session);
// 	$date=mysqli_real_escape_string($db,$date);	
// 	$login_id=mysqli_real_escape_string($db,$login_id);
// 	$pass=mysqli_real_escape_string($db,$pass);
// 	$device_id=mysqli_real_escape_string($db,$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
	
	$is_sdl_enable = 0;
	
	
	if($user_type=='student'){
	

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

     if (isset($arr['password'])){
         $arr['password'] = $ac_pass;
     }

	 
	 $is_sdl_enable = $db->getval("select is_student_sdl_enable 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);
	
	    }
	
 $stu_sib=$db->getval("select sibling from student where idno='".$arr['idno']."'");
									// print_r($stu_sib);
									$csvString= "$stu_sib";	
									
									$options = explode(",", $csvString);
									
												$options = array_filter($options);
	if(is_array($options) && count($options)>0)
	{ 
//print_r($options);
foreach($options as $val) {
    
	$sib_name=$db->getval("select name from student where idno='".$val."'");
		$mobile=$db->getval("select mobile from student where idno='".$val."'");
			$photo=$db->getval("select photo from student where idno='".$val."'");
			
				$admission=$db->getval("select admissionno from student where idno='".$val."'");
					$class=$db->getval("select class from student where idno='".$val."'");
						$section=$db->getval("select section from student where idno='".$val."'");


    $joson[]=array("name"=>$sib_name,"mobile"=>$mobile,"photo"=>"media/photo/".$photo,"admissionno"=>$admission,"class"=>$class,"section"=>$section);
}

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

    if (isset($arr['password'])){
        $arr['password'] = $ac_pass;
    }
	
	$is_sdl_enable = $db->getval("select is_staff_sdl_enable from tbl_info");
	
	 $infoRow = $db->getRow("select is_staff_login_timebound,staff_login_start_time,staff_login_end_time from tbl_info");
    
    $is_staff_login_timebound = $infoRow['is_staff_login_timebound'];
    $start_time = $infoRow['staff_login_start_time'];
    $end_time   = $infoRow['staff_login_end_time'];
	
	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."'");
        
        
        	$is_sdl_enable = $db->getval("select is_admin_sdl_enable from tbl_info");
        
        
	//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)
	{  
	    
	$allow_login = 1;
	if($is_staff_login_timebound == 1){
	    
	    $login_message = "App can be used between $start_time - $end_time, Please try again in that timeframe";
	    
        $allow_login = isWithInTime($start_time,$end_time);
	}
	
	    if ($_SERVER['HTTP_HOST'] == 'nml.savarna.in'){
            $arr['dob'] = date("Y-m-d", strtotime($arr['dob']));
        }
		echo json_encode(array('status' => 1, 'msg'=>'Login Successfull..!', 'data'=>$arr,'sibling'=>$joson, 'allow_login'=>$allow_login, "login_message"=>$login_message, "is_sdl_enable"=>$is_sdl_enable));    
		//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"];	







function isWithInTime($start,$end) {

$time = date("H:i:s");

            if (($time >= $start )&& ($time <= $end)) {
               // echo 'OK';
                return 1;
            } else {
                //echo 'Not OK';
                return 0;
            }

}










?>