<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Twilio\Rest\Client;
use App\User;
use DB;

class otpController extends Controller
{
    function send_otp(Request $request){

    	$phone=$request->get('phone');
    	
    	$otp=mt_rand(00000,99999);

    	$account_sid = getenv("TWILIO_SID");
        $auth_token = getenv("TWILIO_AUTH_TOKEN");
        $twilio_number = "+15406666583";
        $client = new Client($account_sid, $auth_token);
        $msg=$client->messages->create($phone, 
                ['from' => $twilio_number, 'body' => $otp." is your otp for iflexapps account. Dont share this with anyone else."] );
        $otp=($otp*7);
        return $otp;
    }
    
    
    
    
    function checknsend(Request $request){

    	$iflex_id=$request->get('iflex_id');
    	
    	$user=User::where([
    		['iflex_ID','=',$iflex_id]
    	    	])->orWhere([['email','=',$iflex_id]]);
    	
    	
    	 if($user->count()==1){
    	 
    	$user=$user->first();
    	$uid=$user->ifex_ID;
        $phone=$user->phone;
        if($phone!=0){
    	$otp=mt_rand(00000,99999);

    	$account_sid = getenv("TWILIO_SID");
        $auth_token = getenv("TWILIO_AUTH_TOKEN");
        $twilio_number = "+15406666583";
        $client = new Client($account_sid, $auth_token);
        $msg=$client->messages->create($phone, 
                ['from' => $twilio_number, 'body' => $otp." is your otp for iflexapps account. Dont share this with anyone else."] );
                
        
        
        $otp=($otp*7);
        $phint=substr($phone, -3);
        $data = ["otp" => $otp, "phone" => $phint];
        return $data;
        }else{
            $data = ["otp" => -1, "phone" => -1];
            return $data;
        }
        
    	 }else{
    	     $data = ["otp" => 0, "phone" => 0];
             return $data;
    	 }
    	 
    }
    
    
}
