<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Email_activity extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		if(get_cookie('admin_id'))
		{
			$this->session->set_userdata('admin_id',get_cookie('admin_id'));
		}
		if(!$this->session->userdata('admin_id'))
		{
			redirect('admin/login');
		}
	}
	public function index(){
		$content_data = array();
		$data['loginDetails'] = $this->common_model->getAllRow('feu_userdetails',array('id'=>$this->session->userdata('admin_id')),array('usr_users','feu_userdetails.user_id = usr_users.id')) ;
		$activity = array();
		require_once $_SERVER['DOCUMENT_ROOT'].'/Mandrill.php';
		$mandrill = new Mandrill('TveFwCTbtHjorrsBDKwJ8Q');
	    $query = '*';
	    /*$date_from = '2019-02-20';
	    $date_to = '2019-02-20';*/
	    $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);
	    if(count($activity)>0)
	    {
	    	foreach ($activity as $key => $value) {
	    		if(stristr($value['subject'])!='Heisse Neuigkeiten')
	    		{
	    			$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);
		    		}
	    		}
	    		
	    	}
	    }
	    //die;
		$content_data['activity'] = $this->common_model->getAll('usr_email_activity','','','date DESC','','',0,5000);
		//print_r($content_data);die;
		$data['content'] = $this->load->view('admin/log/email_activity', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	function test()
	{
		$content_data = array();
		/*if($this->input->post())
		{
			require $_SERVER['DOCUMENT_ROOT'].'/stripe/Stripe.php';
			$params = array(
					"testmode"   => "on",
					//"private_live_key" => "sk_live_7yHhLLeHGtSdGfc0rJNbLXZl",
					//"public_live_key"  => "pk_live_TyYHG0MtVuby9Ja5c8gkddCx",
					"private_test_key" => "sk_test_S1Eg863dRzyX5S6deJ39KLIh",
					"public_test_key"  => "pk_test_J4rdzwRQrf2RsXG6xafA4y4q",
					//"private_test_key"  => "sk_test_OFGHKhrV3K6yzNg1RtxDaakA",
					//"public_test_key"  => "pk_test_8TMqaTrtjzh44TBsV9uzi0J6",
				);
				
			if ($params['testmode'] == "on") {
				Stripe::setApiKey($params['private_test_key']);
				$pubkey = $params['public_test_key'];
			} else {
				Stripe::setApiKey($params['private_live_key']);
				$pubkey = $params['public_live_key'];
			}
			if($this->input->post('stripeToken'))
			{
				//$amount_cents = str_replace(".","","10.52");  // Chargeble amount
				$description = "Memberhsip for " ;
				
				try {
					
					$customer = Stripe_Customer::create(array(		 
						  "email" => 'abc@gmail.com',
						  "source" => $this->input->post('stripeToken'))
					);

					$cust_id =0;
					if(!empty($customer))
					{
						$cust_id = $customer->id;
					}
					$charge = Stripe_Charge::create(array(		 
						  "amount" => 2000,
						  "currency" => "eur",
						  "customer" => $cust_id)
					);
			 		
					$result = "success";
		 
				} catch(Stripe_CardError $e) {			
			 
				$error = $e->getMessage();
					$result = "declined";
			 
				} catch (Stripe_InvalidRequestError $e) {
					$result = "declined";		  
				} catch (Stripe_AuthenticationError $e) {
					$result = "declined";
				} catch (Stripe_ApiConnectionError $e) {
					$result = "declined";
				} catch (Stripe_Error $e) {
					$result = "declined";
				} catch (Exception $e) {
			 
					if ($e->getMessage() == "zip_check_invalid") {
						$result = "declined";
					} else if ($e->getMessage() == "address_check_invalid") {
						$result = "declined";
					} else if ($e->getMessage() == "cvc_check_invalid") {
						$result = "declined";
					} else {
						$result = "declined";
					}		  
				}
			}
		}*/
		$data['content'] = $this->load->view('admin/log/test', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
}