<?php

namespace App\Http\Controllers\backend;

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

class SmsController extends Controller
{
    public function index(Request $request){
        $show = $request->show;
        if($show != ''){
            $purchases = SmsPurchase::latest()->paginate($show);
        }else{
            $purchases = SmsPurchase::latest()->paginate(setting()->ddspinp);
        }
        $sms = Sms::latest()->first();
        
        $customers = Customer::orderBy('name','Asc')->get(['id','name','phone']);
        return view('backend.sms.index',compact('customers','sms','purchases'));
    }
    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'=>"7a787dd847f4650b1ba1adfb8771c733"
        ); // 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);
        
        Sms::latest()->first()->decrement('sms_balance',2);
        
        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'=>"7a787dd847f4650b1ba1adfb8771c733"
        ); // 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 edit(){
        $sms = Sms::latest()->first();
        return view('backend.sms.edit',compact('sms'));
    }
    public function update(Request $request,$id){
        //return $request->all();
        $this->validate($request,[
            'sms_balance'=> 'required',
        ]);
        
        $sms = Sms::find($id);
        $sms->sms_balance = $request->sms_balance;
        $sms->expire_date = date('Y-m-d',strtotime($request->expire_date));
        $sms->save();
        
        return redirect()->route('sms.edit')->with('success','Sms Information update success');
    }
}
