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

class Weekly_cron extends CI_Controller
{
	public function index()
	{
		$this->load->model('common_model');
		$this->load->library('Utility');
		$weeklyUsers = $this->common_model->getAllMultipleJoin('feu_userdetails',array('visitor_mail'=>2),array(array('usr_users','usr_users.id = feu_userdetails.user_id')));
		
		if(!empty($weeklyUsers))
		{
			foreach ($weeklyUsers as $value) {
				$visitorsData = $this->common_model->getAllMultipleJoin('feu_profilevisits',array('feu_profilevisits.to_user_id'=>$value['user_id'],'feu_profilevisits.created >'=>time()-7*86400),array(array('feu_userdetails','feu_userdetails.user_id = feu_profilevisits.from_user_id'),array('feu_countries','feu_countries.country_code = feu_userdetails.country'),array('sys_files','sys_files.id = feu_userdetails.avatar_file_id')),'feu_profilevisits.created DESC');
				if(!empty($visitorsData))
				{
					$to = $value['email'];
					$sub = 'New visitors in last 7 days';
					$i=0;
					$body='<table cellspacing="0" cellpadding="0" border="0" align="center" bgcolor="#22222c" width="100%" style="max-width: 600px;">
				                    <tr>
				                    <td>
				                       <table cellspacing="0" cellpadding="0" border="0" width="100%">
				                         <tr>';
					foreach ($visitorsData as $visitorData) {
						if($i<6)
						{
							$visitorPreference = $this->common_model->getAll('feu_sexual_preference',array('user_id'=>$this->session->userdata('user_id'))); 	
						 	$genteman_age = (time() - $visitorData['dob_gentleman'])/(365*24*60*60);
			                $lady_age = (time() - $visitorData['dob_lady'])/(365*24*60*60);
								//print_r($visitorData); die;	
								
								$body.='			<td align="center" style="color: #b3975b;font-family: sans-serif;font-size: 15px;line-height: 20px;padding: 40px 20px;text-align: center;">
						                                   <h2 style="text-transform:capitalize; font-size: 23px; padding-bottom: 10px; ">'.$visitorData['username'].'</h2>
														    <span style=" text-transform: capitalize; color: #fff; font-size: 13px;">'.$visitorData['accounttype'].'-'.$visitorData['country_name'].',';
						                                    if($visitorData['dob_lady']>0){ 
						        $body.=							round($lady_age);
						                                    } 
						                                    elseif($visitorData['dob_gentleman']>0){ 
						        $body.=							round($genteman_age);
						                                    }  
						        $body.='                    </span>
						                                </td>';
						                if($i==2)
						                {
						        $body.='</tr>';        	
						                }                

						}
						
		            $i++;
		            }             
				                          if(count($visitorsData)>6)
				                          {
				         $body.='             <tr>
				                             	<td  colspan="3" align="center" style="font-family: sans-serif; font-size: 15px; mso-height-rule: exactly; line-height: 20px; color: #fff;">
				                             		<p style=" margin: 5px 0 0; color: #fff; text-align: left; margin-bottom: 40px; padding: 0 40px;">+'.(count($visitorsData)-6).' more visitors this week</p> 
				                             	</td>
				                             </tr>';                  	
				                          }
				       
				        $body.='             <tr>
				                             	<td  colspan="3" align="center" style="font-family: sans-serif; color: rgb(179, 151, 91); padding: 20px 40px; border-top: 1px solid rgb(79, 79, 79); background: rgb(19, 18, 24) none repeat scroll 0px 0px;">
				                             		Best regards <br>
				                             		<p style="margin:5px 0 0; color:#b3975b">Your Castleevents Team</p> 
				                             	</td>
				                             </tr>
				                          </table>
				                       </td>
				                    </tr>
				                </table>';
				       
					//echo  $body; die;       
					$this->utility->sendCIMail($to,$sub,$body);
				}
			}
		}
	}
}