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

class Activity extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		$this->load->model('users_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')) ;
		$content_data['couple_lady'] = $this->common_model->getAllMultipleJoin('newsletter_couple_lady',array('avatar_file_id >'=>0),array(array('feu_userdetails','feu_userdetails.user_id = newsletter_couple_lady.couple'),array('sys_files','feu_userdetails.avatar_file_id = sys_files.id')),'newsletter_couple_lady.id DESC','*,newsletter_couple_lady.id');
		
		//print_r($content_data['monthly_users']); 
		//die;
		$data['content'] = $this->load->view('admin/users/activity_list', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function add(){
		$content_data = array();
		$content_data['monday'] = $monday = date( 'Y-m-d', strtotime( 'last friday' ) );
		$content_data['saturday'] = $saturday = date( 'Y-m-d', strtotime( 'thursday this week' ) );
		
		if($this->input->post())
		{//print_r($this->input->post('couple')); die;
			$exist = $this->common_model->getAllRow('newsletter_couple_lady','','','id DESC');
			/*if(!empty($exist))
			{
				$monday = date( 'Y-m-d', strtotime( "+7 day", strtotime($exist['start_date'])) );
				$saturday = date( 'Y-m-d', strtotime( "+7 day", strtotime($exist['end_date']) ) );
			}*/
			foreach ($this->input->post('couple') as $key => $values) {
				if($key>0)
				{
					$monday = date( 'Y-m-d', strtotime( "+7 day", strtotime($monday)) );
					$saturday = date( 'Y-m-d', strtotime( "+7 day", strtotime($saturday) ) );
				}
				$lady = $this->input->post('lady');
				$gentleman = $this->input->post('gentleman');
				$arr['couple'] = $values;
				$arr['lady'] = $lady[$key];
				$arr['gentleman'] = $gentleman[$key];
				$arr['start_date'] = $monday;
				$arr['end_date'] = $saturday;
				
				
				$this->common_model->insertData('newsletter_couple_lady',$arr);
			}
			//echo $this->db->last_query();die;
			$this->utility->setMsg('Activity user  added','SUCCESS');	
			redirect(base_url().'admin/activity');
		}
		$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')) ;
		$weekly_users = $this->common_model->getAllMultipleJoin('notification',array('accounttype'=>'couple','filename !='=>'','LENGTH(about) >'=>5),array(array('feu_userdetails','feu_userdetails.user_id = notification.from_id'),array('sys_files','feu_userdetails.avatar_file_id = sys_files.id'),array('newsletter_couple_lady','newsletter_couple_lady.couple = feu_userdetails.user_id')),'feu_userdetails.created DESC','*,count(from_id) as count,newsletter_couple_lady.id as last_id','from_id',0,200);
		$content_data['weekly_users'] = array();
		if(count($weekly_users)>0)
		{
			foreach ($weekly_users as $key => $value) {
				$membership = $this->users_model->getMembershipDetails($value['user_id']);
				if(count($membership)>0)
				{
					if($membership['id']==5)
					{
						$weekly_users[$key]['membership'] = 'Basic';
					}
					else
					{
						$weekly_users[$key]['membership'] = 'Premium';
					}
				}
				elseif($value['free_member']==1)
				{
					$weekly_users[$key]['membership'] = 'Free';
				}
				else
				{
					$weekly_users[$key]['membership'] = 'No Membership';
				}
				if(count($membership)==0){
					unset($weekly_users[$key]);
				}
			}
		}
		$content_data['weekly_users'] = $weekly_users;
		//pr($content_data['weekly_gentleman']);

		$activeqry = $this->db->query("SELECT newsletter_couple_lady.end_date,newsletter_couple_lady.id as last_id,feu_userdetails.created as feucreated,sys_files.path,sys_files.filename,sys_files.extension,feu_userdetails.username,feu_userdetails.accounttype,feu_userdetails.about,`usr_users`.`id` as `user_id`,`usr_users`.`email` as `email`, `usr_users`.`cc_ref_id` FROM `usr_users` INNER JOIN feu_userdetails ON usr_users.id = feu_userdetails.user_id INNER JOIN sys_files ON feu_userdetails.avatar_file_id = sys_files.id LEFT JOIN newsletter_couple_lady ON feu_userdetails.user_id = newsletter_couple_lady.gentleman WHERE accounttype = 'gentleman' and LENGTH(about) > 5 and path like '%avatar%' and filename != '' Group BY `usr_users`.`id` ORDER BY feucreated DESC limit 150");


		$activegetUserListData = $activeqry->result_array();
		
		if(count($activegetUserListData)>0)
		{
			foreach ($activegetUserListData as $key => $value) {
				$membership = $this->users_model->getMembershipDetails($value['user_id']);
				if(count($membership)>0)
				{
					if($membership['id']==5)
					{
						$activegetUserListData[$key]['membership'] = 'Basic';
						$activegetUserListData[$key]['membership_ids'] = 2;
					}
					else
					{
						$activegetUserListData[$key]['membership'] = 'Premium';
						$activegetUserListData[$key]['membership_ids'] = 3;
					}
				}
				elseif($value['free_member']==1)
				{
					$activegetUserListData[$key]['membership'] = 'Free';
					$activegetUserListData[$key]['membership_ids'] = 1;
				}
				else
				{
					$activegetUserListData[$key]['membership'] = 'No Membership';
					$activegetUserListData[$key]['membership_ids'] = 0;
				}
			}
		}
		$content_data['weekly_gentleman'] = $activegetUserListData;


		foreach ($activegetUserListData as $key => $row)
		{
		    $vc_array_name[$key] = $row['membership_ids'];
		}
		array_multisort($vc_array_name, SORT_DESC, $activegetUserListData);

		$content_data['weekly_gentleman'] = $activegetUserListData;
		//echo '<pre>';print_r($activegetUserListData);die;

		$first_day_this_month = date('Y-01-m');
		$last_day_this_month  = date('Y-t-m');
		$monthly_users = $this->common_model->getAllMultipleJoin('notification',array('accounttype'=>'lady','filename !='=>'','LENGTH(about) >'=>10),array(array('feu_userdetails','feu_userdetails.user_id = notification.from_id'),array('sys_files','feu_userdetails.avatar_file_id = sys_files.id'),array('newsletter_couple_lady','newsletter_couple_lady.lady = feu_userdetails.user_id')),'feu_userdetails.created DESC','*,count(from_id) as count,newsletter_couple_lady.id as last_id','from_id',0,50);
		$content_data['monthly_users'] = array();
		if(count($monthly_users)>0)
		{
			foreach ($monthly_users as $key => $value) {
				$membership = $this->users_model->getMembershipDetails($value['user_id']);
				if(count($membership)>0)
				{
					if($membership['id']==5)
					{
						$monthly_users[$key]['membership'] = 'Basic';
					}
					else
					{
						$monthly_users[$key]['membership'] = 'Premium';
					}
				}
				elseif($value['free_member']==1)
				{
					$monthly_users[$key]['membership'] = 'Free';
				}
				else
				{
					$monthly_users[$key]['membership'] = 'No Membership';
				}
			}
		}
		$content_data['monthly_users'] = $monthly_users;


		$data['content'] = $this->load->view('admin/users/activity', $content_data, TRUE);
		$this->load->view('admin/layout', $data);
	}
	public function delete($advert_id='')
	{
		//echo $cat_id;die;
		if($advert_id!='')
		{
			$advertId=base64_decode($advert_id);
			$this->common_model->deleteData('newsletter_couple_lady',array('id'=>$advertId));
			$this->utility->setMsg('Activity deleted','SUCCESS');	
			redirect(base_url().'admin/activity');
		}
		else
		{
			$this->utility->setMsg('Invalid id','ERROR');	
			redirect(base_url().'admin/activity');
		}
	}
}