<?php

namespace App;

use Carbon\Carbon;

use Illuminate\Support\Facades\DB;

use Illuminate\Http\Request;

    class Common {



        public static function pt($data){

            return $data;

        }


        
     public static function getCommission($amount, $scheme, $provider_id, $role_id)

     {
 
         $myscheme = DB::table('schemes')->where('id', $scheme)->first();
 
         
 
         if($myscheme && $myscheme->status == "1"){
 
             $comdata = DB::table('scheme_commissions')->where('provider_id', $provider_id)->where('scheme_id', $scheme)->first();
 
             if ($comdata) {
 
                 if($role_id== 6){
 
                     if ($comdata->rt_amount_type == "Commission Percent") {
 
                         $commission = $amount * $comdata->rt_amount_value / 100;
 
                     }else{
 
                         $commission = $comdata->rt_amount_value;
 
                     }
 
                 }else if($role_id== 5){
 
                     if ($comdata->dt_amount_type == "Commission Percent") {
 
                         $commission = $amount * $comdata->dt_amount_value / 100;
 
                     }else{
 
                         $commission = $comdata->dt_amount_value;
 
                     }
 
                 }else if($role_id== 4){
 
                     if ($comdata->md_amount_type == "Commission Percent") {
 
                         $commission = $amount * $comdata->md_amount_value / 100;
 
                     }else{
 
                         $commission = $comdata->md_amount_value;
 
                     }
 
                 }else if($role_id== 2){
 
                     if ($comdata->wt_amount_type == "Commission Percent") {
 
                         $commission = $amount * $comdata->wt_amount_value / 100;
 
                     }else{
 
                         $commission = $comdata->wt_amount_value;
 
                     }
 
                 }else{
 
                     $commission = 0;
 
                 }
 
                 if($commission == null){
 
                     $commission = 0;
 
                 }
 
             }else{
 
                 $commission = 0;
 
             }
 
         }else{
 
             $commission = 0;
 
         }
 
         
 
         return $commission;
 
     }





        public static function sendWhatasappMsg($data){

            //return $data['mobile_number'];

            //user_id,msg_slug,

            $w_api = DB::table('companies')->where('id', 1)->first(['whatsapp_request_url','whatsapp_api_method']);

            $url = $w_api->whatsapp_request_url;

            if($url !=0 || $url !=""){

                $url = str_replace('{MOB}', '' . $data['mobile_number'] . '', $url);

                $url = str_replace('{MSG}', '' . urlencode($data['content']) . '', $url);

                $url = str_replace('{TMP_ID}', '' . $data['template_id'] . '', $url);

                $method = $w_api->whatsapp_api_method;

                $header = [];

                $parameters = "";

                $request_id = "WAS".date("YmdHis").rand(11111, 999999);

                $curl = Common::curl($url, $method, $parameters, $header, "yes", "WHATSAPP_URL", $request_id);

                return 0;

            }else{

                return 1;

            }

        }



        public static function curl($url , $method='GET', $parameters, $header, $log="no", $modal="none", $txnid="none")

    {   

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

        curl_setopt($curl, CURLOPT_URL, $url);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($curl, CURLOPT_MAXREDIRS, 10);

        curl_setopt($curl, CURLOPT_ENCODING, "");

        curl_setopt($curl, CURLOPT_TIMEOUT, 180);

        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

        if($parameters != ""){

            curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);

        }



        if(sizeof($header) > 0){

            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

        }

        

        $response = curl_exec($curl);

        $err = curl_error($curl);

        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        curl_close($curl);

        if($log != "no"){

            DB::table('apilogs')->insert([

                "url" => $url,

                "modal" => $modal,

                "txnid" => $txnid,

                "header" => json_encode($header),

                "request" => json_encode($parameters),

                "response" => $response,

                'created_at' => Carbon::now(),

                'updated_at' => Carbon::now()

            ]);

        }



        return ["response" => $response, "error" => $err, 'code' => $code];

    }

        

    }