<?php
defined('BASEPATH') or exit('No direct script access allowed');
require_once "vendor/autoload.php";

use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;

class Home extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
	}

	public function index()
	{
		$data['page'] = 'home';
		$data['app_setting'] 				= $this->setting_model->get_all_app_setting();
		$data['slider'] 					= $this->Front_model->get_slider_by_id(1);
		$data['short_about_us_image'] 		= $this->Front_model->get_our_service_image();
		$data['all_short_about_us_content'] = $this->Front_model->get_all_our_service();
		$data['banner_heading'] 			= $this->Front_model->get_all_banner_heading();
		$data['all_course_category'] 		= $this->course_model->get_all_course_category();
		$data['all_home_course'] 			= $this->course_model->get_all_home_page_course();
		$data['all_blog'] 					= $this->blog_model->get_all_blogs_for_home_page_limit_rand();
		$data['slider_block']				= $this->Front_model->get_all_slider_block();
		$data['all_testimonial'] 			= $this->Front_model->get_all_testimonial();
		$data['all_logo']					= $this->Front_model->get_all_placement_logo();
		$data['content_6_i'] 				= $this->Front_model->get_all_about_us_content_6_i();
		$data['content_6_ii'] 				= $this->Front_model->get_all_about_us_content_6_ii();
		$data['ebook'] 						= $this->Front_model->get_ebook_data();
		$this->load->view('front/index', $data);
	}

	public function about()
	{
		$data['page'] = 'about';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['title'] = $data['app_setting']->app_meta_title;
		$data['content_1'] = $this->Front_model->get_all_about_us_content_1();
		$data['content_2'] = $this->Front_model->get_all_about_us_content_2_i();
		$data['content_2_i'] = $this->Front_model->get_all_about_us_content_2_ii();
		$data['content_3'] = $this->Front_model->get_all_about_us_content_3();
		$data['content_4_i'] = $this->Front_model->get_all_about_us_content_4_i();
		$data['content_4_ii'] = $this->Front_model->get_all_about_us_content_4_ii();
		$data['content_5'] = $this->Front_model->get_all_about_us_content_5();
		$data['content_6_i'] = $this->Front_model->get_all_about_us_content_6_i();
		$data['content_6_ii'] = $this->Front_model->get_all_about_us_content_6_ii();
		$data['all_timeline'] = $this->Front_model->get_all_about_us_content_timeline();
		$data['brain'] = $this->Front_model->get_brain_behiend();
		$data['get_all_award'] = $this->Front_model->get_all_about_us_content_award();
		$this->load->view('front/index', $data);
	}

	public function contact()
	{
		$data['page'] = 'contact';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	public function all_course_category($slug)
	{
		$this->db->where('category_slug', $slug);
		$c_cat = $this->db->get('course_category')->row();
		if ($c_cat) {
			$this->db->select('slug, thumbnail, duration, level, title, price, short_description, id');
			$this->db->where('category_id', $c_cat->category_id);
			$data['all_course'] = $this->db->get('course')->result();

			$data['page'] = 'all_course_category';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['title'] = $data['app_setting']->app_meta_title;
			$data['category'] = $c_cat;
			$this->load->view('front/index', $data);
		} else {
			redirect('/');
		}
	}

	public function course_details($slug)
	{
		$this->db->select('a.*, b.category_slug, b.category_name');
		$this->db->from('course a');
		$this->db->join('course_category b', 'b.category_id = a.category_id');
		$this->db->where('a.slug', $slug);
		$course = $this->db->get()->row();
		if ($course) {
			$data['page'] = 'course_details';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['title'] = $data['app_setting']->app_meta_title;
			$data['course'] = $course;
			$this->load->view('front/index', $data);
		} else {
			redirect('/');
		}
	}

	public function submit_booking_form()
	{
		if ($this->input->post()) {
			$data = array(
				'lead_name' => $this->input->post('lead_name') !== null ? $this->input->post('lead_name') : '',
				'lead_email' => $this->input->post('lead_email') !== null ? $this->input->post('lead_email') : '',
				'lead_mobile' => $this->input->post('lead_mobile') !== null ? $this->input->post('lead_mobile') : '',
				'lead_source' => $this->input->post('lead_source') !== null ? $this->input->post('lead_source') : '',
				'lead_course' => $this->input->post('lead_course') !== null ? $this->input->post('lead_course') : '',
				'lead_class' => $this->input->post('lead_class') !== null ? $this->input->post('lead_class') : '',
				'lead_message' => $this->input->post('lead_message') !== null ? $this->input->post('lead_message') : '',
				'lead_date' => date('Y-m-d'),
				'lead_status' => 0
			);
			if ($this->db->insert('lead_management', $data)) {
				$rand = rand(100000000000, 999999999999);
				$this->session->set_userdata('rand_id', $rand);
				echo $rand;
			} else {
				echo 0;
			}
		} else {
			redirect('/');
		}
	}

	public function ebook_booking_form()
	{
		if ($this->input->post()) {
			$data = array(
				'lead_name' => $this->input->post('lead_name') !== null ? $this->input->post('lead_name') : '',
				'lead_email' => $this->input->post('lead_email') !== null ? $this->input->post('lead_email') : '',
				'lead_mobile' => $this->input->post('lead_mobile') !== null ? $this->input->post('lead_mobile') : '',
				'lead_source' => $this->input->post('lead_source') !== null ? $this->input->post('lead_source') : '',
				'lead_course' => $this->input->post('lead_course') !== null ? $this->input->post('lead_course') : '',
				'lead_class' => $this->input->post('lead_class') !== null ? $this->input->post('lead_class') : '',
				'lead_message' => $this->input->post('lead_message') !== null ? $this->input->post('lead_message') : '',
				'lead_date' => date('Y-m-d'),
				'lead_status' => 0
			);
			if ($this->db->insert('lead_management', $data)) {
				$rand = rand(100000000000, 999999999999);
				$this->session->set_userdata('rand_id', $rand);
				echo $rand;
			} else {
				echo 0;
			}
		} else {
			redirect('/');
		}
	}

	public function thank_you($no)
	{
		if ($this->session->userdata('rand_id') == $no) {
			$data['page'] = 'thank_you';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['title'] = $data['app_setting']->app_meta_title;
			$this->load->view('front/index', $data);
		} else {
			redirect('/');
		}
	}

	public function ebook_thank_you($no)
	{
		if ($this->session->userdata('rand_id') == $no) {
			$data['page'] = 'ebook_thank_you';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['ebook'] 	     = $this->Front_model->get_ebook_data();
			$data['title'] 		 = $data['app_setting']->app_meta_title;
			$this->load->view('front/index', $data);
		} else {
			redirect('/');
		}
	}

	public function all_blog()
	{
		$data['page'] = 'all_blog';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['title'] = $data['app_setting']->app_meta_title;
		$data['get_all_blog'] = $this->blog_model->get_all_blogs_for_home_page();
		$this->load->view('front/index', $data);
	}

	public function blog_details($slug)
	{
		$data['page'] = 'blog_details';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['blog'] = $this->blog_model->get_blog_by_slug($slug);
		$data['title'] = $data['blog']->blog_title;
		$this->load->view('front/index', $data);
	}

	public function image_gallery()
	{
		$data['page'] = 'image_gallery';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['all_image'] = $this->Front_model->get_gallery_by_mode(1);
		$data['all_category'] = $this->Front_model->get_all_gallery_category();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	public function video_gallery()
	{
		$data['page'] = 'video_gallery';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['all_video'] = $this->Front_model->get_gallery_by_mode(2);
		$data['all_category'] = $this->Front_model->get_all_gallery_category();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	public function page_details($slug)
	{
		$data['page'] = 'page_details';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['details'] = $this->Front_model->get_page_by_slug($slug);
		if ($data['details']->meta_title) {
			$data['meta_title'] = $data['details']->meta_title;
		} else {
			$data['meta_title'] = $data['details']->name;
		}

		if ($data['details']->meta_descrition) {
			$data['meta_descrition'] = $data['details']->meta_descrition;
		}

		if ($data['details']->meta_keyword) {
			$data['meta_keyword'] = $data['details']->meta_keyword;
		}
		if (file_exists($data['details']->image)) {
			$data['og_image'] = $data['details']->image;
		}

		$this->load->view('front/index', $data);
	}

	public function check_mobile_no_for_free_quiz()
	{
		$mobile = $this->input->post('mobile');

		$this->db->where('lead_source', "Free Quiz");
		$this->db->where('lead_mobile', $mobile);
		$data = $this->db->get('lead_management')->num_rows();
		if ($data) {
			$arrayName = array(
				'msg' => '<span class="badge badge-danger">Mobile No Already Exits.</span>',
				'status' => 6,
			);
			echo json_encode($arrayName);
		} else {
			$arrayName = array(
				'status' => 5,
			);
			echo json_encode($arrayName);
		}
	}

	public function check_email_id_for_free_quiz()
	{

		$email = $this->input->post('email');

		$this->db->where('lead_source', "Free Quiz");
		$this->db->where('lead_email', $email);
		$data = $this->db->get('lead_management')->num_rows();
		if ($data) {
			$arrayName = array(
				'msg' => '<span class="badge badge-danger">Email ID Already Exits.</span>',
				'status' => 6,
			);
			echo json_encode($arrayName);
		} else {
			$arrayName = array(
				'status' => 5,
			);
			echo json_encode($arrayName);
		}
	}

	// public function submit_free_quiz_form()
	// {
	// 	if($this->input->post())
	// 	{
	// 		$data = array(
	// 		    'lead_name' => $this->input->post('lead_name') !== null ? $this->input->post('lead_name') : '',
	// 		    'lead_email' => $this->input->post('lead_email') !== null ? $this->input->post('lead_email') : '',
	// 		    'lead_mobile' => $this->input->post('lead_mobile') !== null ? $this->input->post('lead_mobile') : '',
	// 		    'lead_source' => $this->input->post('lead_source') !== null ? $this->input->post('lead_source') : '',
	// 		    'lead_course' => $this->input->post('lead_course') !== null ? $this->input->post('lead_course') : '',
	// 		    'lead_class' => $this->input->post('lead_class') !== null ? $this->input->post('lead_class') : '',
	// 		    'lead_message' => $this->input->post('lead_message') !== null ? $this->input->post('lead_message') : '',
	// 		    'lead_date' => date('Y-m-d'),
	// 		    'lead_status' => 0
	// 		);
	// 		if($this->db->insert('lead_management', $data))
	// 		{
	// 			$rand = $this->generate_random_string(50);
	// 			$timeDifference = 5 * 60 * 60 + 30 * 60;
	// 			$localExpirationTime = time() + $timeDifference;
	// 			$cookie = array(
	// 			    'name'   => 'free_quiz',
	// 			    'value'  => 'value_'.$rand,
	// 			    'expire' => $localExpirationTime, // 1 hour
	// 			    'secure' => false // Not using HTTPS
	// 			);
	// 			set_cookie($cookie);
	// 			$this->session->set_userdata('rand_id', $rand);
	// 			if($data['lead_source'] == 'Free Quiz')
	// 			{
	// 				redirect(base_url().'free_quiz/'.$rand);
	// 			}
	// 			else
	// 			{
	// 				redirect(base_url().'thank_you/'.$rand);
	// 			}

	// 		}
	// 		else
	// 		{
	// 			$this->session->set_userdata('error', "Form Submit Failed. Please Try Again");
	//         	redirect($this->agent->referrer());
	// 		}
	// 	}
	// 	else
	// 	{
	// 		$this->session->set_userdata('error', "Form Submit Failed. Please Try Again");
	// 		redirect($this->agent->referrer());
	// 	}
	// }

	public function submit_free_quiz_form()
	{
		if ($this->input->post()) {
			// Verify CAPTCHA
			$captchaResponse = $this->input->post('g-recaptcha-response');
			$secretKey = 'YOUR_SECRET_KEY'; // Replace with your reCAPTCHA secret key
			$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$captchaResponse");
			$responseKeys = json_decode($response, true);

			// Check if CAPTCHA validation passed
			if (!$responseKeys['success']) {
				$this->session->set_userdata('error', 'CAPTCHA validation failed. Please try again.');
				redirect($this->agent->referrer());
				return;
			}

			// Prepare form data
			$data = array(
				'lead_name' => $this->input->post('lead_name') ?: '',
				'lead_email' => $this->input->post('lead_email') ?: '',
				'lead_mobile' => $this->input->post('lead_mobile') ?: '',
				'lead_source' => $this->input->post('lead_source') ?: '',
				'lead_course' => $this->input->post('lead_course') ?: '',
				'lead_class' => $this->input->post('lead_class') ?: '',
				'lead_message' => $this->input->post('lead_message') ?: '',
				'lead_date' => date('Y-m-d'),
				'lead_status' => 0
			);

			if ($this->db->insert('lead_management', $data)) {
				$rand = $this->generate_random_string(50);
				$timeDifference = 5 * 60 * 60 + 30 * 60;
				$localExpirationTime = time() + $timeDifference;

				$cookie = array(
					'name' => 'free_quiz',
					'value' => 'value_' . $rand,
					'expire' => $localExpirationTime,
					'secure' => false
				);

				set_cookie($cookie);
				$this->session->set_userdata('rand_id', $rand);

				if ($data['lead_source'] == 'Free Quiz') {
					redirect(base_url() . 'free_quiz/' . $rand);
				} else {
					redirect(base_url() . 'thank_you/' . $rand);
				}
			} else {
				$this->session->set_userdata('error', 'Form submission failed. Please try again.');
				redirect($this->agent->referrer());
			}
		} else {
			$this->session->set_userdata('error', 'Form submission failed. Please try again.');
			redirect($this->agent->referrer());
		}
	}


	public function generate_random_string($length = 10)
	{
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$randomString = '';

		for ($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, strlen($characters) - 1)];
		}

		return $randomString;
	}

	public function free_quiz($no)
	{
		$cookieValue = get_cookie('free_quiz');
		if ($cookieValue == 'value_' . $no) {
			$data['page'] = 'quiz_panel';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['title'] = $data['app_setting']->app_meta_title;
			$data['auth_id'] = $no;
			$this->load->view('front/index', $data);
		} else {
			redirect('/');
		}
	}

	public function quiz_start($no)
	{
		$cookieValue = get_cookie('free_quiz');
		if ($cookieValue == 'value_' . $no) {
			// $data['page'] = 'free_quiz';
			$data['app_setting'] = $this->setting_model->get_all_app_setting();
			$data['title'] = $data['app_setting']->app_meta_title;
			$data['auth_id'] = $no;
			$this->load->view('front/quiz', $data);
		} else {
			redirect('/');
		}
	}

	public function career_page()
	{
		$data['page'] = 'career_page';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['all_job'] = $this->career_model->view_all_job();
		$data['all_category'] = $this->career_model->get_all_category();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	public function job_details($slug)
	{
		$data['page'] = 'job_details';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['job'] = $this->career_model->view_job_by_slug($slug);
		$data['all_category'] = $this->career_model->get_all_category();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	function login()
	{
		$data['page'] = 'login';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}

	function our_courses()
	{
		$data['page'] = 'our_courses';
		$data['app_setting'] = $this->setting_model->get_all_app_setting();
		$data['title'] = $data['app_setting']->app_meta_title;
		$this->load->view('front/index', $data);
	}
}
