<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Email_activity_cron extends CI_Controller
{
	public function index()
	{
		$this->load->model('common_model');
		$activity = array();
		/*require_once $_SERVER['DOCUMENT_ROOT'].'/Mandrill.php';
		$mandrill = new Mandrill('TveFwCTbtHjorrsBDKwJ8Q');
	    $query = '*';
	    $date_from = date('Y-m-d');
	    $date_to = date('Y-m-d');
	    $tags = array();
	    $senders = array();
	    $api_keys = array('TveFwCTbtHjorrsBDKwJ8Q');
	    $limit = 1000;
	    $activity = $mandrill->messages->search($query, $date_from, $date_to, $tags, $senders, $api_keys, $limit);*/
	    $curl = curl_init();

		curl_setopt_array($curl, array(
		  CURLOPT_URL => "https://api.sendgrid.com/v3/messages?limit=10",
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "GET",
		  CURLOPT_POSTFIELDS => "{}",
		  CURLOPT_HTTPHEADER => array(
		    "authorization: Bearer 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"
		  ),
		));

		$response = curl_exec($curl);
		$err = curl_error($curl);

		curl_close($curl);

		if ($err) {
		  echo "cURL Error #:" . $err;
		} else {
		  echo '<pre>'; print_r($response);die;
		}
	    if(count($activity)>0)
	    {
	    	foreach ($activity as $key => $value) {
	    		$arr = array(
	    					'email'=>$value['email'],
	    					'subject'=>$value['subject'],
	    					'opens'=>$value['opens'],
	    					'clicks'=>$value['clicks'],
	    					'state'=>$value['state'],
	    					'date'=>date('Y-m-d',$value['ts']),
	    					);
	    		$exist = $this->common_model->getAllRow('usr_email_activity',$arr);
	    		if(count($exist)==0)
	    		{
	    			$this->common_model->insertData('usr_email_activity',$arr);
	    		}
	    	}
	    }
	}
}