<?php
ob_start();
include("../connect.php");
$response = array();
if(isset($_POST['email']) && isset($_POST['password']) && isset($_POST['imei_no']) && isset($_POST['carrier_name']) && isset($_POST['ip_address']) && isset($_POST['device_id']) && isset($_POST['app_version']) && isset($_POST['phone_model']) && isset($_POST['sdk_phone_version'])) 
{  
   $email=$_POST['email'];
   $pass=$_POST['password'];
   $mac_id=$_POST['imei_no'];
   $carrier_name=$_POST['carrier_name'];
   $ip_address=$_POST['ip_address'];
   $device_id=$_POST['device_id'];
   $app_version=$_POST['app_version'];
   $phone_model=$_POST['phone_model'];
   $sdk_phone_version=$_POST['sdk_phone_version']; 
   $phone_manufacturer=$_POST['phone_manufacturer'];
   
   if($mac_id=="")
   {
		$response['success']=0;
		$response['details']="IMEI No not received. Please contact administrator!!";
   }
   else
   {   
	   $res = mysqli_query($con,"Select * from tbl_distributor_users where (`distributor_email`='$email' || `phone_no`='$email')
	   AND `password`='$pass'");
	   $row=mysqli_fetch_assoc($res);
	   if(mysqli_num_rows($res)>0)	   
	   {
			if($row['status']=="Active")
			{
				 $temp_device_id = $row['imei_no'];
				 if(($temp_device_id=="") || ($temp_device_id==$mac_id))
				 {
				   $dist_id=$row['distributor_id'];
				   
				   $que2=mysqli_query($con,"update tbl_distributor_users set imei_no='$mac_id',ip_address='$ip_address',carrier_name='$carrier_name',device_id='$device_id',app_version='$app_version',phone_model='$phone_model',phone_manufacturer='$phone_manufacturer',sdk_phone_version='$sdk_phone_version',app_login='Yes' where distributor_id='$dist_id'");
				   
				   $response['distributor_id'] = $row['distributor_id'];
				   $response['distributor_name'] = $row['distributor_name'];
				   $response['distributor_email'] = $row['distributor_email'];
				   $response['password'] = $pass;
				   $response['phone_no'] = $row['phone_no'];
				   
				   $response['success']=1;	
				   $response['login_type']='Admin';	
				   $response['details']="You are successfully login!!";
				 }
				 else
				 {
				  $response['success']=0;	
				  $response['details']="You are already logged in. Please logout from another device or contact administrator!!";
				 }
			}
			else
			{
				$response['success']=0;	
				$response['details']="You are no longer an active user. Please contact administrator!!";
			}
	   }  
	   else
	   {
			$res1 = mysqli_query($con,"Select * from tbl_master_customer where (`email_address`='$email' || `customer_phone`='$email')
			AND `password`='$pass'");
			$row1=mysqli_fetch_assoc($res1);
			if(mysqli_num_rows($res1)>0)	   
			{
				if($row1['status']=="Active")
				{
					  $temp_device_id = $row1['imei_no'];
					  if(($temp_device_id=="") || ($temp_device_id==$mac_id))
					  {
						   $cust_id=$row1['customer_id'];
						   $que2=mysqli_query($con,"update tbl_master_customer set imei_no='$mac_id',ip_address='$ip_address',carrier_name='$carrier_name',device_id='$device_id',app_version='$app_version',phone_model='$phone_model',phone_manufacturer='$phone_manufacturer',sdk_phone_version='$sdk_phone_version' where customer_id='$cust_id'");
						   $response['distributor_id'] = $row1['distributor_id'];
						   $response['customer_id'] = $row1['customer_id'];
						   $response['customer_name'] = $row1['customer_name'];
						   $response['email_address'] = $row1['email_address'];
						   $response['customer_phone'] = $row1['customer_phone'];
						   $response['password'] = $pass;
						   $response['success']=1;	
						   $response['login_type']='Customer';	
						   $response['details']="You are successfully login";
					  }
					  else
					  {
						$response['success']=0;	
						$response['details']="You are already logged in. Please logout from another device or contact administrator!!";
					  }
				}
				else
				{
					$response['success']=0;	
					$response['details']="You are no longer an active user. Please contact administrator!!";
				}
			}
			else 
			{
			 $response['success']=0;	
			 $response['details']="You are not a valid user! Please check login details and try again later!!"; 
			}
		}
    }
}	
else
{	
	$response['success']=0;
	$response['details']="Required fields missing";
}
echo json_encode($response);
?>

 