<?php 
require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
use Twilio\Rest\Client;
require_once 'config.php';
//require __DIR__ . '/twilio-php-master/src/Twilio/autoload.php';
$error=false;
$errors=array();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$user_id=$_POST['user_id'];
  $phone=$_POST['phone'];
   $group_id=$_POST['group_id'];
  //echo "SELECT id FROM reffer_log WHERE user_id='".$user_id."' AND group_id = '".$group_id."' AND phone ='".$phone."' AND DATE(created_at)='".date('Y-m-d')."'";die;
   $is_log=mysqli_fetch_array(mysqli_query($con,"SELECT id FROM reffer_log WHERE user_id='".$user_id."' AND group_id = '".$group_id."' AND phone ='".$phone."' AND DATE(created_at)='".date('Y-m-d')."'"));
   
   if($is_log['id']==''){
   $fromuser=mysqli_fetch_array(mysqli_query($con,"SELECT id,point_wallet FROM users WHERE id='".$user_id."'"));
    $msg=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM smsbygroup WHERE group_id='".$group_id."'"));
	

	// Your Account SID and Auth Token from twilio.com/console
	$sid = 'AC805d114ffccc9dad4d6a446f128f60f8';
	$token = '269ac4552238565b441a21bccdbe7b82';
	$client = new Client($sid, $token);
	$no_to='+65'.$phone;
	// Use the client to do fun stuff like send text messages!
	$client->messages->create(
		// the number you'd like to send the message to
		$no_to,
		[
			// A Twilio phone number you purchased at twilio.com/console
			'from' => '+18654138377',
			// the body of the text message you'd like to send
			'body' => $msg['sms']
		]
	);
	$customer=mysqli_fetch_array(mysqli_query($con,"SELECT * FROM settings WHERE id=1"));
   $updatefrompoints=$fromuser['point_wallet']+$customer['refer_commission'];
		
		$createitem= mysqli_query($con,"UPDATE users SET point_wallet=".$updatefrompoints." WHERE id=".$fromuser['id']);
		
		 if($createitem){
			 $t=date('Y-m-d H:i:s');
		//	 echo "INSERT INTO reffer_log (user_id,group_id,phone,point,created_at) VALUES('".$user_id."','".$group_id."','".$phone."','".$customer['refer_commission']."','".$t."')";die;
		mysqli_query($con,"INSERT INTO reffer_log (user_id,group_id,phone,point,created_at) VALUES('".$user_id."','".$group_id."','".$phone."','".$customer['refer_commission']."','".$t."')");
		
	     	$errors['success']=true;
	     	$errors['message']='User Refer Sucessfully';
	 }else{
	     $errors['success']=false;
	     $errors['message']='Somthing went wrong !!';
	 }
   }else{
	     $errors['success']=false;
	     $errors['message']='You have already send on this No for today !!';
	 }
echo json_encode($errors);
}