<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
use Illuminate\Support\Facades\Log;
use App\Mail\MtnCoiReminder;
class SendReminderMails implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $data;
        /**
         * Create a new job instance.
         *
         * @return void
         */
        public function __construct($data)
        {
          $this->data = $data;
            //
        }

        /**
         * Execute the job.
         *
         * @return void
         */
        public function handle()
        {
    // Log::info($this->data);
    $users = $this->data['email'];
    // return $users;
    // Log::info($users);
    $str = str_replace(' ', ' \\\n ', $this->data['reminder']);
    foreach ($users as $key) {
      Log::info($str);
      if ($key->email !==null) {
        // $data=[
        //   // 'name'=>$key->name,
        //   'reminder'=>$str,
        //   'email'=>$key->email,
        // ];
// $this->sendmail('MTN CONFLICT OF INTEREST',$key->email,$str);
Log::channel('coi')->info('sending reminder mail to : '.$key->email);
        $this->sendmail('SCANCOM PLC (MTN Ghana) Conflict of Interest Policy & Code of Ethics',$key->email,"Y’ello MTNer,\\n Kindly note that you are receiving this message because you either have not started or completed your 2023 Conflict of Interest Declarations.\\n In accordance with the Conflict of Interest Policy, kindly be reminded to complete and submit your declarations by the 9th of September, 2023.\\n\\nThank you");


#
  // Mail::to($key->email)->send(new MtnCoiReminder($data));
      }
    }
  }



        public function sendmail($subj,$mailto,$msg){
          $curl = curl_init();
        $mailurl = env('MAIL_URL');
        $mailauth = env('MAIL_AUTH');
          curl_setopt_array($curl, array(
            CURLOPT_URL => $mailurl,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>'{
            "subject":"'.$subj.'",
            "recipient":"'.$mailto.'",
            "msg":"'.$msg.'"
          }',
            CURLOPT_HTTPHEADER => array(
              $mailauth,
              'Content-Type: application/json'
            ),
          ));

          $response = curl_exec($curl);

          curl_close($curl);
          Log::channel('coi')->info('curl executed and closed.');
          // echo $response;
        Log::channel('coi')->info('curl response: '.$response);
        }
}
