<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Customer;

class SmsController extends Controller
{
    public function index(){
        $customers = Customer::orderBy('name','Asc')->get(['id','name','phone']);
        return view('backend.sms.index',compact('customers'));
    }
    public function create(){
        return view('backend.sms.create');
    }
    public function store(Request $request){
        $this->validate($request,[
            'phone'=> 'required',
            'message'=> 'required',
            ]);
        //return $request->all();
        // $this->validate($request,[
        //     'to'=>'required',
        //     'message'=>'required',
        // ]);
        // $username = "azharul787";
        // $hash = "6091d88eaa1c45be12c026dbae8bf810"; 
        // $numbers = $request->to; //"01712144321,01724340952"; //Recipient Phone Number multiple number must be separated by comma
        // $message = $request->message;


        // $params = array('app'=>'ws', 'u'=>$username, 'h'=>$hash, 'op'=>'pv', 'unicode'=>'1','to'=>$numbers, 'msg'=>$message);

        // $ch = curl_init();
        // curl_setopt($ch, CURLOPT_URL, "http://alphasms.biz/index.php?".http_build_query($params, "", "&"));
        // curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json", "Accept:application/json"));
        // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        // curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        // $response = curl_exec($ch);
        // curl_close ($ch);
        // return $response;
       
        $url = "http://api.greenweb.com.bd/api.php?json";
        $data= array(
            'to'=> $request->phone,
            'message'=>$request->message,
            'token'=>"daf0cc0b638862523830815f351a8063"
        ); // Add parameters in key value
        $ch = curl_init(); // Initialize cURL
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $smsresult = curl_exec($ch);

        //Result
        //echo $smsresult;

        //Error Display
        //echo curl_error($ch);
        return back()->with('success',$smsresult);
    }
    
    public function sms_send(Request $request){
        $this->validate($request,[
            'phone'=> 'required',
            'message'=> 'required',
            ]);
            
        $url = "http://api.greenweb.com.bd/api.php?json";
        $data= array(
            'to'=> implode(',',$request->phone),
            'message'=>$request->message,
            'token'=>"daf0cc0b638862523830815f351a8063"
        ); // Add parameters in key value
        $ch = curl_init(); // Initialize cURL
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_ENCODING, '');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $smsresult = curl_exec($ch);

        //Result
        //echo $smsresult;

        //Error Display
        //echo curl_error($ch);
        return back()->with('success',$smsresult);
    }
}
