<?php
session_start();
include_once '../config.php';
$user_id = $_SESSION['User']['id'];
if($_POST['action']=='Register'){
$Fname = addslashes(trim($_POST['Fname']));
$Lname = addslashes(trim($_POST['Lname']));
$Phone = $_POST['Phone'];
$EmailId = $_POST['EmailId'];
$Password = $_POST['Password'];
$CountryId = addslashes($_POST['CountryId']);
$StateId = addslashes($_POST['StateId']);
$CityId = addslashes($_POST['CityId']);
$Address = addslashes(trim($_POST['Address']));
$Pincode = trim($_POST['Pincode']);
$Type = addslashes(trim($_POST['Type']));
$DeptId = addslashes(trim($_POST['DeptId']));
$CourseId = addslashes(trim($_POST['CourseId']));
$BatchId = addslashes(trim($_POST['BatchId']));

$query = "SELECT Type,Duration,CourseFees FROM tbl_courses WHERE id = '$CourseId'";
$row = getRecord($query);
$CourseFees = $row['CourseFees'];
$Duration = $row['Duration'];

$query2 = "SELECT StartDate,BatchTime,Strength FROM tbl_batches WHERE id = '$BatchId'";
$row2 = getRecord($query2);
$StartDate = $row2['StartDate'];
$BatchTime = $row2['BatchTime'];

$CreatedDate = date('Y-m-d');
$CreatedTime = date('h:i a');

function RandomStringGenerator($n)
{
    $generated_string = "";   
    $domain = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
    $len = strlen($domain);
    for ($i = 0; $i < $n; $i++)
    {
        $index = rand(0, $len - 1);
        $generated_string = $generated_string . $domain[$index];
    }
    return $generated_string;
} 
$n = 12;
$ReferenceNo = RandomStringGenerator($n); 
$sql2 = "SELECT * FROM tbl_users WHERE Phone='$Phone' AND EmailId!='$EmailId' AND Roll=2";
$sql21 = "SELECT * FROM tbl_users WHERE EmailId='$EmailId' AND Phone!='$Phone' AND Roll=2";
$sql3 = "SELECT * FROM tbl_users WHERE Phone='$Phone' AND EmailId='$EmailId' AND Roll=2";

$res2 = $conn->query($sql2);
$row2 = mysqli_num_rows($res2);

$res21 = $conn->query($sql21);
$row21 = mysqli_num_rows($res21);

$res3 = $conn->query($sql3);
$row3 = mysqli_num_rows($res3);

if($row3 > 0){
	echo json_encode(array('msg'=>"Your Email Id & Phone No Already Registered With Us",'status'=>'0'));
}
else if($row21 > 0){
    echo json_encode(array('msg'=>"Your Email Id Already Registered With Us",'status'=>'0'));

} 
else if($row2 > 0){
    echo json_encode(array('msg'=>"Your Phone No Already Registered With Us",'status'=>'0'));
}
else{
$sql = "INSERT INTO tbl_users SET Fname='$Fname',Lname='$Lname',Phone='$Phone',EmailId='$EmailId',Password='$Password',Address='$Address',Pincode='$Pincode',DeptId='$DeptId',CourseId='$CourseId',BatchId='$BatchId',StartDate='$StartDate',BatchTime='$BatchTime',Duration='$Duration',CourseType='$Type',CourseFees='$CourseFees',Status='1',Roll=2,AccName='Student',CreatedDate='$CreatedDate'";
$conn->query($sql);
$CustId = mysqli_insert_id($conn);
$to = $EmailId;
//include("../inc_register_mail.php");
//include("../sendmailsmtp.php");
$CustomerId = "C".$CustId;

$sql3 = "UPDATE tbl_users SET CustomerId='$CustomerId' WHERE id='$CustId'";
$conn->query($sql3);
$query = "SELECT * FROM tbl_users WHERE Phone = '$Phone' AND Password = '$Password'";
    $result = $conn->query($query);
    $rncnt = mysqli_num_rows($result);
    $row = $result->fetch_assoc();
    $_SESSION['User'] = $row;
$uid = $row['id'];
  $user_id = $_SESSION['User']['id'];
     echo json_encode(array('Username'=>$Username,'uid'=>$uid,'msg'=>"Registration Successfull!Redirecting...",'status'=>'1'));
}
}


if($_POST['action']=='Login'){
$Username = addslashes(trim($_POST['Username']));
$Password = addslashes(trim($_POST['Password']));
$query = "SELECT * FROM tbl_users WHERE (Phone = '$Username' OR EmailId='$Username') AND Password = '$Password' AND Status=1 AND Roll=2";
 $rncnt = getRow($query);
 $row = getRecord($query);
 if($rncnt > 0){
 	//$_SESSION['User'] = $row;
 	$uid = $row['id'];
 	$Phone = $row['Phone'];
 	 $otp = rand(1000,9999);
  $_SESSION['otp'] = $otp;
  $user_id = $_SESSION['User']['id'];
  setcookie("member_login",$Username, time() + 30*24*60*60, "/");
  setcookie("member_password",$Password, time() + 30*24*60*60, "/");
  setcookie("user_id",$user_id, time() + 30*24*60*60, "/");
  echo json_encode(array('Username'=>$Phone,'uid'=>$uid,'Status'=>1));
 }
 else{
 	 echo json_encode(array('Status'=>0)); 
 }
}

if($_POST['action']=='VerifyOtp'){
$Phone = addslashes(trim($_POST['Phone']));
$YourOtp = addslashes(trim($_POST['YourOtp']));
$GetOtp = addslashes(trim($_POST['GetOtp']));
if($YourOtp == $GetOtp){
$query = "SELECT * FROM tbl_users WHERE Phone = '$Phone' AND Status=1";
$rncnt = getRow($query);
if($rncnt > 0){
$row = getRecord($query);
$_SESSION['User'] = $row;
$uid = $row['id'];
unset($_SESSION['otp']);
echo json_encode(array('status'=>1,'Username'=>$Phone,'uid'=>$uid));
}
}
else{
echo json_encode(array('status'=>0));   
}
}


if($_POST['action']=='Edit'){
$id = $_POST['id'];
$Fname = addslashes(trim($_POST['Fname']));
$Mname = addslashes(trim($_POST['Mname']));
$Lname = addslashes(trim($_POST['Lname']));
$Phone = $_POST['Phone'];
$EmailId = $_POST['EmailId'];
$Phone2 = $_POST['Phone2'];
$Password = $_POST['Password'];
$Gender = addslashes($_POST['Gender']);
$Day = addslashes($_POST['Day']);
$Month = addslashes($_POST['Month']);
$Year = addslashes($_POST['Year']);
$CountryId = addslashes($_POST['CountryId']);
$StateId = addslashes($_POST['StateId']);
$CityId = addslashes($_POST['CityId']);
$Address = addslashes(trim($_POST['Address']));
$Pincode = trim($_POST['Pincode']);
$CreatedDate = date('Y-m-d');

$randno = rand(1,100);
$src = $_FILES['Photo']['tmp_name'];
$fnm = substr($_FILES["Photo"]["name"], 0,strrpos($_FILES["Photo"]["name"],'.')); 
$fnm = str_replace(" ","_",$fnm);
$ext = substr($_FILES["Photo"]["name"],strpos($_FILES["Photo"]["name"],"."));
$dest = $UploadUrl.'/uploads/'. $randno . "_".$fnm . $ext;
$imagepath =  $randno . "_".$fnm . $ext;
if(move_uploaded_file($src, $dest))
{
$Photo = $imagepath ;
} 
else{
    $Photo = $_POST['OldPhoto'];
}

$sql2 = "SELECT * FROM tbl_users WHERE (Phone='$Phone' OR EmailId='$EmailId') AND id!='$id'";   
$res2 = $conn->query($sql2);
$row2 = mysqli_num_rows($res2);
if($row2 > 0){
    echo 0;
}
else{
$sql = "UPDATE tbl_users SET Fname='$Fname',Lname='$Lname',Phone='$Phone',EmailId='$EmailId',Phone2='$Phone2',Address='$Address',Pincode='$Pincode',ModifiedDate='$CreatedDate',Photo='$Photo' WHERE id='$id'";
$conn->query($sql);
echo 1;
}
 }   

 if($_POST['action']=='ChangePassword'){
$id = $_POST['id'];
$OldPassword = addslashes(trim($_POST['OldPassword']));
$NewPassword = addslashes(trim($_POST['NewPassword']));
$CnfPassword = addslashes(trim($_POST['CnfPassword']));
if($NewPassword == $CnfPassword){
   $sql2 = "SELECT * FROM tbl_users WHERE id='$id'";   
$res2 = $conn->query($sql2);
$row2 = $res2->fetch_assoc();
$old_Pass = $row2['Password'];
if($old_Pass == $OldPassword){
$sql = "UPDATE tbl_users SET Password='$NewPassword' WHERE id='$id'";
$conn->query($sql);
echo 1;
  
}
else{
    //old not match
  echo 2;
} 
}
else{
    //cnf not match
    echo 3;
 }
}




 if($_POST['action']=='ForgotPass'){
$Username = $_POST['Username'];
$sql = "SELECT * FROM tbl_customers WHERE (Phone = '$Username' OR EmailId='$Username')";
$rncnt = getRow($sql);
if($rncnt > 0){
    $sql2 = "SELECT * FROM tbl_customers WHERE (Phone = '$Username' OR EmailId='$Username')";
$row = getRecord($sql2);
$EmailId = $row['EmailId'];
$Phone = $row['Phone'];
$Password = $row['Password'];
$Fname = $row['Fname'];
    $to = $EmailId;
//include("../incforgotmail.php");
//include("../sendmailsmtp.php");

$username = $sms_username;
$password = $sms_password;
$type ="TEXT";
$sender=$sms_sender;
$mobile = $Phone;
$message = urlencode("Your Login Details Are");
$message.= urlencode("\n");
$message.= urlencode("Username:");
$message.= urlencode($EmailId." / ".$Phone);
$message.= urlencode("\n");
$message.= urlencode("Password:");
$message.= urlencode($Password);
$message.= urlencode("\n");
$message.= urlencode("Regards,");
$message.= urlencode("\n");
$message.= urlencode("Multifix");
$baseurl ="https://app.indiasms.com/sendsms/bulksms";
$url =$baseurl."?username=".$username."&password=".$password."&type=".$type."&sender=".$sender."&mobile=".$mobile."&message=".$message;
$return = file($url);
echo 1;
  } 
  else{
    echo 0;
  } 
}

?>