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

class Index extends MY_Controller
{
    public $provinces;

    public function __construct()
    {
        parent::__construct();
        $this->load->Model("Common_Model");
        $this->load->Model("CMS_Model");

        // $this->provinces = $this->Common_Model->loadPrvinces();
        $this->load->library('session');

        $arrDefaultlocation = $this->Common_Model->loadDefaultPrvinces();

        if (empty($this->session->province)) {
            $this->session->province = $arrDefaultlocation->id;
        }

        $this->load->library("email");

        $this->load->helper('custom'); // custom_helper.php
    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function index()
    {
        $data = $this->data;

		$data["pageContent"] = $this->CMS_Model->pageContent('home');

        if ($this->uri->segment(1) != "") {
            $converted = str_replace('_', ' ', $this->uri->segment(1));
            $result = $this->Common_Model->getProvinceId($converted);

            if (!empty($result['id']))
                $this->session->province = $result['id'];
        }

        if ($this->input->post("province") != "") {
            $this->session->province = $this->input->post("province");
        }

        // $data["aboutUs"] = $this->Common_Model->loadABoutUs()->description;
        $data["banners"] = $this->Common_Model->loadBanners();
        // $data["provinces"] = $this->provinces;
        $data["schedules"] = $this->Common_Model->loadHomepageSchedule($this->session->province);
        // $data["scheduleCms"] = $this->Common_Model->loadScheduleCms($this->session->province);

        // $data['upcoming_courses'] = $this->Common_Model->loadUpcomingcourses($this->session->province);

        $data["packages"] = $this->Common_Model->loadPackages($this->session->province);
        $data["biggerPackages"] = $this->Common_Model->loadBiggerPackages($this->session->province, [4]);
        
        $this->load->model("Package_Model");
        $data["private_packages"] = $this->Package_Model->getCategoriesWithPackages($this->session->province, 'private');
        $data["package_categories"] = $this->Package_Model->getCategories();
        
        $this->load->model("WhyUs_Model");
        $data["whyUsList"] = $this->WhyUs_Model->getDataList($this->session->province);

        $this->load->config('recaptcha');
        $data["recaptcha_site_key"] = $this->config->item('recaptcha_site_key');

        // Form submit start
        $this->load->library('form_validation');
        $this->form_validation->set_rules('qc_location', 'Location', 'required');
        $this->form_validation->set_rules('qc_name', 'Name', 'required');
        $this->form_validation->set_rules('qc_email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('qc_phone', 'Phone', 'required');
        $this->form_validation->set_rules('package_type', 'Package Type', 'required');
        $this->form_validation->set_rules('package', 'Package', 'required');

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('index', $data);

                return;
            } else {
				if ( $this->validate_captcha() ) {
                    //Refresh the Page without any error if phone validation fails.
                    if (!$this->Common_Model->checkAllowedPhoneNumberCode($this->input->post('qc_phone'))) {
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('invalid_phone').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');

                        redirect(base_url()."#quickContact");
                        return;
                    }

                    $insertData = array(
						'location' => $this->input->post('locationName'),
						'name' => $this->input->post('qc_name'),
						'email' => $this->input->post('qc_email'),
						'phone' => $this->input->post('qc_phone'),
						'package_type' => $this->input->post('packageTypeName'),
						'package' => $this->input->post('package')
					);

					$saved = $this->Common_Model->insertData("quick_inquries", $insertData);
					if ($saved) {
                        $emailerContent = $this->Common_Model->loadEmailer(6);
                        $emailer = $this->load->view('email/email_header', [], TRUE).$emailerContent->emailer.$this->load->view('email/email_footer', [], TRUE);
                        
                        $emailer = str_replace("@currentyear", date("Y"), $emailer);
                        $emailer = str_replace("@name", $this->input->post("qc_name"), $emailer);
                        $emailer = str_replace("@email", $this->input->post("qc_email"), $emailer);
                        $emailer = str_replace("@phone", $this->input->post("qc_phone"), $emailer);
                        $emailer = str_replace("@location", $this->input->post("locationName"), $emailer);
                        $emailer = str_replace("@package_type", $this->input->post("packageTypeName"), $emailer);
                        $emailer = str_replace("@package", $this->input->post("package"), $emailer);

                        $email_setting  = array('mailtype' => 'html', 'wordwrap' => true);
                        $this->email->initialize($email_setting);
                        $this->email->from($this->config->item('mail_from_email'), $this->config->item('mail_from_name'));

                        $cc_email = $emailerContent->cc_email;
                        $bcc_email = $emailerContent->bcc_email;
                        
                        $locationEmail = $this->Common_Model->loadLocationEmail($this->session->province);

                        $emailToList = array(
                            $this->input->post("qc_email"),
                            $locationEmail->email
                        );

                        $this->email->to($emailToList);

                        if ($cc_email != '') {
                            $this->email->cc($cc_email);
                        }
                        if ($bcc_email != '') {
                            $this->email->bcc($bcc_email);
                        }

                        $this->email->subject('Quick Contact Enquiry');
                        $this->email->message($emailer);

                        if ($this->email->send()) {
                            $this->session->set_flashdata("message", '<div class="alert alert-success" role="alert">
								<h4 class="alert-heading">Thank You!</h4>
								<p>Your message has been successfully submitted.</p>
								<p>We appreciate you reaching out to us and will get back to you as soon as possible.</p>
								<p>If your inquiry is urgent, please feel free to contact us directly.</p>
								<button type="button" class="close" data-dismiss="alert" aria-label="Close">
									<span aria-hidden="true">&times;</span>
								</button>
							</div>');

                            redirect(base_url()."#quickContact");
                        } else {
                            $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
								<strong>Error!</strong>
								'.$this->lang->line('email_failed').'
								<button type="button" class="close" data-dismiss="alert" aria-label="Close">
									<span aria-hidden="true">&times;</span>
								</button>
							</div>');
                            // echo $this->email->print_debugger(); // useful for debugging
                            
                            redirect(base_url()."#quickContact");
                        }
                    }
                    else{
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('insert_failed').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');

                        redirect(base_url()."#quickContact");
                    }
                }
			}
		}

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

    public function locations()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Locations".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Locations";

        if ($this->uri->segment(2) != "") {
            $data["locationDetails"] = $this->Common_Model->loadLocations(base64_decode($this->uri->segment(2)));
        } else {
            $data["locationDetails"] = $this->Common_Model->loadLocations($this->session->province);
        }

        // $data["provinces"] = $this->provinces;

        $this->load->view('locations', $data);
    }

    public function locationDetail()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Location Detail".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Location Detail";

        $locationId = base64_decode($this->uri->segment(2));
        $data["location_id"] = $locationId;

        $locations = $this->Common_Model->loadLocations($locationId);

        // $data["provinces"] = $this->provinces;
        $data["locations"] = $locations;

        $this->load->config('recaptcha');
        $data["recaptcha_site_key"] = $this->config->item('recaptcha_site_key');

        // Form submit start
        $this->load->library('form_validation');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('phone', 'Phone', 'required');
        $this->form_validation->set_rules('message', 'Message', 'required');

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('location_detail', $data);

                return;
            } else {
                if ( $this->validate_captcha() ) {
                    //Refresh the Page without any error if phone validation fails.
                    if (!$this->Common_Model->checkAllowedPhoneNumberCode($this->input->post('phone'))) {
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('invalid_phone').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');

                        $this->load->view('location_detail', $data);
                        return;
                    }

                    $insertData = array(
                        'location' => $this->input->post('location'),
                        'name' => $this->input->post('name'),
                        'email' => $this->input->post('email'),
                        'phone' => $this->input->post('phone'),
                        'message' => $this->input->post('message')
                    );

                    $saved = $this->Common_Model->insertData("inquries", $insertData);
                    if ($saved) {
                        $emailerContent = $this->Common_Model->loadEmailer(4);
                        $emailer = $this->load->view('email/email_header', [], TRUE).$emailerContent->emailer.$this->load->view('email/email_footer', [], TRUE);
                        
                        $emailer = str_replace("@currentyear", date("Y"), $emailer);
                        $emailer = str_replace("@name", $this->input->post("name"), $emailer);
                        $emailer = str_replace("@email", $this->input->post("email"), $emailer);
                        $emailer = str_replace("@phone", $this->input->post("phone"), $emailer);
                        $emailer = str_replace("@location", $this->input->post("location"), $emailer);
                        $emailer = str_replace("@message", $this->input->post("message"), $emailer);

                        $email_setting  = array('mailtype' => 'html', 'wordwrap' => true);
                        $this->email->initialize($email_setting);
                        $this->email->from($this->config->item('mail_from_email'), $this->config->item('mail_from_name'));

                        $cc_email = $emailerContent->cc_email;
                        $bcc_email = $emailerContent->bcc_email;
                        
                        $locationEmail = $this->Common_Model->loadLocationEmail($this->session->province);

                        $emailToList = array(
                            $this->input->post("email"),
                            $locationEmail->email
                        );

                        $this->email->to($emailToList);

                        if ($cc_email != '') {
                            $this->email->cc($cc_email);
                        }
                        if ($bcc_email != '') {
                            $this->email->bcc($bcc_email);
                        }

                        $this->email->subject('Contact Us Enquiry');
                        $this->email->message($emailer);

                        if ($this->email->send()) {
                            $this->session->set_flashdata("message", '<div class="alert alert-success" role="alert">
                                <h4 class="alert-heading">Thank You!</h4>
                                <p>Your message has been successfully submitted.</p>
                                <p>We appreciate you reaching out to us and will get back to you as soon as possible.</p>
                                <p>If your inquiry is urgent, please feel free to contact us directly.</p>
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                        } else {
                            $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                                <strong>Error!</strong>
                                '.$this->lang->line('email_failed').'
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                            // echo $this->email->print_debugger(); // useful for debugging
                        }
                    }
                    else{
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('insert_failed').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');
                    }
                }
            }
        }

        $this->load->view('location_detail', $data);
    }

    public function packages()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Packages".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Packages";

        $province_id = $this->session->province;

        // $data["provinces"] = $this->provinces;
        $data["biggerPackages"] = $this->Common_Model->loadBiggerPackages($province_id, [1]);

        $this->load->model("Package_Model");
        $data["general_packages"] = $this->Package_Model->getCategoriesWithPackages($province_id, 'general');
        $data["private_packages"] = $this->Package_Model->getCategoriesWithPackages($province_id, 'private');

        // Schedule start.
        $month_input = $this->input->post("month");

        if (!empty($month_input)) {
            $month_year = explode("_", $month_input);
            $month = isset($month_year[0]) ? $month_year[0] : '';
            $year  = isset($month_year[1]) ? $month_year[1] : '';

            $data["schedules"] = $this->Common_Model->loadSchedule($province_id, $month, $year);
        } else {
            $data["schedules"] = $this->Common_Model->loadSchedule($province_id);
        }

        // Load next year schedule in both cases
        $data["nextYearSchedule"] = $this->Common_Model->loadNextYearSchedule($province_id);

        // Load months for dropdown or filter
        $data["months"] = $this->Common_Model->loadScheduleMonths($province_id);
        // Schedule end.

        $this->load->view('packages', $data);
    }

    public function schedule()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Schedule".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Schedule";

        ($this->input->post("month") == "0") ? redirect(base_url("schedule")) : '';
        if ($this->input->post("month") != "") {

            $month_year = explode("_", $this->input->post("month"));

            $data["schedules"] = $this->Common_Model->loadSchedule($this->session->province, $month_year[0], $month_year[1]);
            $data["nextYearSchedule"] = $this->Common_Model->loadNextYearSchedule($this->session->province);
        } else {
            $data["schedules"] = $this->Common_Model->loadSchedule($this->session->province);
            $data["nextYearSchedule"] = $this->Common_Model->loadNextYearSchedule($this->session->province);
        }

        $data["months"] = $this->Common_Model->loadScheduleMonths($this->session->province);

        // $data["provinces"] = $this->provinces;//Must present.

        $this->load->view('schedule', $data);
    }

    public function faq()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "FAQ".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "FAQ";

        // $data["provinces"] = $this->provinces;//Must present.
        $data["faqs"] = $this->Common_Model->loadFaq();
        
        $this->load->view('faq', $data);
    }

    public function registration()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Registration".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Registration";

        // $data["provinces"] = $this->provinces;
        
        $this->load->model("Package_Model");
        $data["package_categories"] = $this->Package_Model->getCategories();

        $this->load->config('recaptcha');
        $data["recaptcha_site_key"] = $this->config->item('recaptcha_site_key');

        // Handle case when coming from individual package
        $data["package_id"] = '';
        $data["category_id"] = '';
        $data["packages"] = [];

        $package_id = $this->uri->segment(2);
        if (!empty($package_id)) {
            $data["package_id"] = $package_id;

            // Get package details
            $packageDetail = $this->Package_Model->getPackageDetail($package_id);
            if (!empty($packageDetail) && isset($packageDetail->category_id)) {
                $data["category_id"] = $packageDetail->category_id;

                // Fetch packages under the same category and province
                $data["packages"] = $this->Package_Model->getPackagesByCategory($this->session->province, $packageDetail->category_id);
            }
        }

        // Form submit start
        $this->load->library('form_validation');
        $this->form_validation->set_rules('first_name', 'First Name', 'required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'required');
        $this->form_validation->set_rules('phone_number', 'Phone Number', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('major_intersection', 'Major Intersection', 'required');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('package_type', 'Package Type', 'required');
        $this->form_validation->set_rules('package', 'Package', 'required');
        $this->form_validation->set_rules('course_start_date', 'Course Start Date', 'required');
        $this->form_validation->set_rules('payment', 'Payment', 'required');

        $payment = $this->input->post('payment');
        if ($payment === 'Full Payment') {
            $this->form_validation->set_rules('full_amount', 'Full Transaction Amount', 'required|numeric');
        } elseif ($payment === 'Partial Payment') {
            $this->form_validation->set_rules('partial_amount', 'Partial Transaction Amount', 'required|numeric|greater_than[0]');
        }

        // Only run this if form submitted
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            // Check if it's a local form submission (not redirected from payment)
            if ($this->input->post('hid_local_form') !== 'yes') {
                // Not a local form submission, skip processing
                redirect('registration');
                return;
            }

            if ($this->form_validation->run() == FALSE) {
                $this->load->view('registration', $data);
                return;
            } else {
                if ( $this->validate_captcha() ) {
                    //Refresh the Page without any error if phone validation fails.
                    if (!$this->Common_Model->checkAllowedPhoneNumberCode($this->input->post('phone_number'))) {
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('invalid_phone').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');

                        $this->load->view('registration', $data);
                        return;
                    }

                    $balanceAmount = null;
                    $partialAmount = null;
                    $fullAmount = null;
                    $paymentAmount = null;

                    $packageTotalPrice = $this->input->post("packageTotalPrice");

                    if ($payment === 'Full Payment') {
                        $fullAmount = $this->input->post("full_amount");

                        if (is_numeric($packageTotalPrice) && is_numeric($fullAmount)) {
                            if ($fullAmount > $packageTotalPrice) {
                                $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                                    <strong>Error!</strong>
                                    Full payment amount cannot exceed the total package amount.
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>');

                                $this->load->view('registration', $data);
                                return;
                            }
                        }

                        $paymentAmount = $fullAmount;
                    } elseif ($payment === 'Partial Payment') {
                        $partialAmount = $this->input->post("partial_amount");

                        if (is_numeric($packageTotalPrice) && is_numeric($partialAmount)) {
                            if ($partialAmount > $packageTotalPrice) {
                                $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                                    <strong>Error!</strong>
                                    Partial payment amount cannot exceed the total package amount.
                                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>');

                                $this->load->view('registration', $data);
                                return;
                            }

                            $balanceAmount = $packageTotalPrice - $partialAmount;

                            $paymentAmount = $fullAmount;
                        }
                    }

                    $insertData = array(
                        "first_name" => $this->input->post("first_name"),
                        "last_name" => $this->input->post("last_name"),
                        "phone_number" => $this->input->post("phone_number"),
                        "email" => $this->input->post("email"),
                        "major_intersection" => $this->input->post("major_intersection"),
                        "location" => $this->input->post("locationName"),
                        "package_type" => $this->input->post("packageTypeName"),
                        "package_name" => $this->input->post("packageName"),
                        "package_description" => ($this->input->post("packageValue")) ? "<ul>".$this->input->post("packageValue")."</ul>" : null,
                        "package_price" => $this->input->post("packagePrice"),
                        "package_total_price" => $packageTotalPrice,
                        "course_start_date" => $this->input->post("course_start_date"),
                        "note" => $this->input->post("note"),
                        "payment" => $payment,
                        "full_amount" => $fullAmount,
                        "partial_amount" => $partialAmount,
                        "balance_amount" => $balanceAmount,
                        "payment_amount" => $paymentAmount,
                        "payment_status" => 'Pending'
                    );

                    $regId = $this->Common_Model->insertDataReturnId("registrations", $insertData);

                    if($regId > 0){
                        // Store ID in session temporarily for later reference
                        $this->session->set_userdata('registration_id', $regId);

                        $paymentOn = true;//true or false
                        if($paymentOn == true){
                            // Redirect to payment page
                            redirect('registration-payment');
                        }
                        else{
                            // Redirect to same registration page
                            $this->session->set_flashdata("message", '<div class="alert alert-success" role="alert">
                                <h4 class="alert-heading">Confirmation!</h4>
                                <p>Thank you for choosing AMB Driving School! Your online registration has been successfully submitted. A representative from our office will contact you to finalize your registration within 1-2 business days.</p>
                                <p>Please be sure to check your spam/junk mail folder for their follow up email which will contain further instructions to complete the registration<br>If you do not receive a follow up, please contact us at AMB Driving School at (905)-699-5711.</p>
                                <p>Thank you!</p>
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                            redirect(base_url() . "registration");
                        }
                    }
                    else{
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('insert_failed').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');
                    }
                }
            }
        }
        // Form submit end

        $this->load->view('registration', $data);
    }

    public function redirect_to_payment()
    {
        $this->load->config('convergepay');

        $regId = $this->session->userdata('registration_id');

        if (!$regId) {
            $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                <strong>Error!</strong>
                Invalid registration session.
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>');

            redirect(base_url() . "registration");
        }

        $registration = $this->Common_Model->getRegistrationById($regId); // You create this method

        $ch = curl_init();    // initialize curl handle
        curl_setopt($ch, CURLOPT_URL, $this->config->item('convergepay_token_url')); // set POST target URL
        curl_setopt($ch,CURLOPT_POST, true); // set POST method
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        //Build the request for the session id. Make sure all payment field variables created above get included in the CURLOPT_POSTFIELDS section below.

        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
            'ssl_merchant_id'    => $this->config->item('convergepay_merchant_id'),
            'ssl_user_id'        => $this->config->item('convergepay_user_id'),
            'ssl_pin'            => $this->config->item('convergepay_pin'),
            'ssl_transaction_type' => 'ccsale',
            // 'ssl_amount'         => $registration->payment_amount,
            'ssl_amount'         => 1,
            'ssl_invoice_number' => 'REG-' . $regId,
            'ssl_description'    => 'Registration Payment for package type ' . $registration->package_type . ' and package ' . $registration->package_name,
        ]));

        $result = curl_exec($ch); // run the curl to post to Converge

        curl_close($ch); // Close cURL

        if ($result === false) {
            // echo 'Curl error message: ' . curl_error($ch) . '<br>';
            // echo 'Curl error code: ' . curl_errno($ch);

            $data["parentPageName"] = "Registration";
            $data["pageName"] = "Payment Failed";
            $data["message"] = curl_error($ch);
            $data['show_payment_retry_modal'] = true;
            $data['regId'] = $regId;
            $data['registration'] = $registration;

            $this->load->view('payment_failure', $data);
            return;
        } else {
            $httpstatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if ($httpstatus == 200) {
                $sessiontoken = urlencode($result);

                /* Now redirect to the HPP */
                header("Location: ".$this->config->item('convergepay_url')."?ssl_txn_auth_token=$sessiontoken"); //Prod Redirect
            } else {
                // echo 'Response: ' . $result . '<br>';
                // echo 'HTTP status: ' . $httpstatus;

                $data["parentPageName"] = "Registration";
                $data["pageName"] = "Payment Failed";
                $data["message"] = $result;
                $data['show_payment_retry_modal'] = true;
                $data['regId'] = $regId;
                $data['registration'] = $registration;

                $this->load->view('payment_failure', $data);
                return;
            }
        }
    }

    public function payment_response()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
        $data["parentPageName"] = "Registration";
        $data["pageName"] = "";

        // $data["provinces"] = $this->provinces;

        $response = $this->input->post();

        // Extract your reg_id and transaction result
        $invoiceNumber = $response['ssl_invoice_number'] ?? null;
        $result = $response['ssl_result_message'] ?? 'No result';
        $approved = $response['ssl_result'] ?? null; // '0' means success

        if ($invoiceNumber != '' && $approved === '0') {
            // Success
            // Break into array using "-"
            $parts = explode('-', $invoiceNumber);
            $regId  = isset($parts[1]) ? $parts[1] : '';

            if($regId > 0){
                $transactionId = $response['ssl_txn_id'] ?? null;

                $registration = $this->Common_Model->getRegistrationById($regId);

                $locationInfo = $this->Common_Model->loadLocations($this->session->province);
                $locationEmail = $this->Common_Model->loadLocationEmail($this->session->province);

                $emailerContent = $this->Common_Model->loadEmailer(3);

                $emailer = $this->load->view('email/email_header', [], TRUE).$emailerContent->emailer.$this->load->view('email/email_footer', [], TRUE);

                $emailer = str_replace("@currentyear", date("Y"), $emailer);
                $emailer = str_replace("@packageName", $registration->package_name, $emailer);
                $emailer = str_replace("@packagePrice", '$ ' . $registration->package_price, $emailer);
                $emailer = str_replace("@packageDescription", $registration->package_description ?? '', $emailer);
                $emailer = str_replace("@location", $registration->location, $emailer);
                $emailer = str_replace("@packageTypeName", $registration->package_type, $emailer);

                $emailer = str_replace("@first_name", $registration->first_name, $emailer);
                $emailer = str_replace("@last_name", $registration->last_name ?? '', $emailer);
                $emailer = str_replace("@phone_number", $registration->phone_number, $emailer);
                $emailer = str_replace("@email", $registration->email, $emailer);
                $emailer = str_replace("@major_intersection", $registration->major_intersection ?? '', $emailer);
                $emailer = str_replace("@course_start_date", $registration->course_start_date, $emailer);
                $emailer = str_replace("@note", $registration->note ?? '', $emailer);
                $emailer = str_replace("@payment", $registration->payment, $emailer);
                $emailer = str_replace("@full_amount", $registration->full_amount ?? '', $emailer);
                $emailer = str_replace("@partial_amount", $registration->partial_amount ?? '', $emailer);
                $emailer = str_replace("@balance_amount", $registration->balance_amount ?? '', $emailer);

                $emailer = str_replace("@lNumber", $locationInfo[0]->telephone, $emailer);
                $emailer = str_replace("@lEmail", $locationInfo[0]->email, $emailer);

                // Update
                $this->Common_Model->updateData("registrations", [
                    'emailer' => $emailer,
                    'payment_status' => 'Paid',
                    'transaction_id' => $transactionId,
                    'updated_at'     => date('Y-m-d H:i:s')
                ], ['id' => $regId]);

                // Send email
                $email_setting  = array('mailtype' => 'html', 'wordwrap' => true);
                $this->email->initialize($email_setting);
                $this->email->from($this->config->item('mail_from_email'), $this->config->item('mail_from_name'));

                $cc_email = $emailerContent->cc_email;
                $bcc_email = $emailerContent->bcc_email;

                $emailToList = array(
                    $registration->email,
                    $locationEmail->email
                );

                $this->email->to($emailToList);

                if ($cc_email != '') {
                    $this->email->cc($cc_email);
                }
                if ($bcc_email != '') {
                    $this->email->bcc($bcc_email);
                }

                $this->email->subject('Package Confirmation');
                $this->email->message($emailer);

                if ($this->email->send()) {
                    $registrationMessage = $this->CMS_Model->fetchMessage("registrationmessage", $this->session->province);

                    $data["message"] = !empty($registrationMessage->page_description) ? $registrationMessage->page_description : '
                        <h4>Payment Successful – Registration Confirmed!</h4>
                        <p>Thank you for choosing <strong>AMB Driving School</strong>! Your online registration and payment have been successfully completed. A confirmation email will be sent to you shortly.</p>
                        <p>One of our representatives will contact you within 1–2 business days to finalize your registration and provide next steps.</p>
                        <p><strong>Please check your spam or junk folder</strong> in case the email does not appear in your inbox. If you haven’t received any follow-up communication, feel free to contact us at <a href="tel:9056995711">(905) 699-5711</a>.</p>
                        <p>We appreciate your trust in us and look forward to helping you get started!</p>';
                } else {
                    $data["message"] = '<h4>Payment Successful – Registration Confirmed!</h4>
                    <p>Thank you for choosing <strong>AMB Driving School</strong>! Your online registration and payment have been successfully completed.</p>
                    <p><strong>However, we were unable to send the confirmation email at this time.</strong> A representative from our office will still contact you within 1–2 business days to finalize your registration and provide further instructions.</p>
                    <p>If you do not hear from us within that time, please reach out directly at <a href="tel:9056995711">(905) 699-5711</a> so we can assist you promptly.</p>
                    <p>We apologize for the inconvenience and appreciate your understanding.</p>';
                    // echo $this->email->print_debugger(); // useful for debugging
                }

                // Clear session
                $this->session->unset_userdata('registration_id');

                $data["pageName"] = "Payment Success";
            
                $pageData->pageContent->page_title = $data["pageName"].$this->page_title_sfx;
                $data["pageContent"] = $pageData->pageContent;

                $this->load->view('payment_success', $data);
            }
            else{
                // Failure
                $data["pageName"] = "Payment Failed";
                $data["message"] = $result;
            
                $pageData->pageContent->page_title = $data["pageName"].$this->page_title_sfx;
                $data["pageContent"] = $pageData->pageContent;

                $this->load->view('payment_failure', $data);
            }
        } else {
            // Failure
            $data["pageName"] = "Payment Failed";
            $data["message"] = $result;
            
            $pageData->pageContent->page_title = $data["pageName"].$this->page_title_sfx;
            $data["pageContent"] = $pageData->pageContent;

            $this->load->view('payment_failure', $data);
        }
    }


    public function get_packages_by_type()
    {
        $category_id = $this->input->post('type');
        $location = $this->input->post('location');

        $this->load->model("Package_Model");
        $packages = $this->Package_Model->getPackagesByCategory($location, $category_id);
        if (!empty($packages)) {
            $html = '<option value="">Select a package</option>';
            foreach ($packages as $package) {
            $html .= '<option value="' . $package->id . '">' . htmlspecialchars($package->package_name) . '</option>';
            }
        } else {
            $html = '<option value="">No packages available</option>';
        }

        echo $html;
    }


    public function get_packages_by_type2()
    {
        $category_id = $this->input->post('type');
        $location = $this->input->post('location');

        $this->load->model("Package_Model");
        $packages = $this->Package_Model->getPackagesByCategory($location, $category_id);
        if (!empty($packages)) {
            $html = '<option value="">Select Package</option>';
            foreach ($packages as $package) {
            $html .= '<option value="' . htmlspecialchars($package->package_name) . '">' . htmlspecialchars($package->package_name) . '</option>';
            }
        } else {
            $html = '<option value="">No packages available</option>';
        }

        echo $html;
    }

    public function loadPackage()
    { 
        $package = $this->Common_Model->loadPackageById($this->input->post("packageId"));
        $category = $this->Common_Model->getCategory($package->category_id ?? 0);

        // $totalAmount = number_format(floatval($package->price) + floatval($package->mto_fee) + floatval($package->hst), 2);

        $packageData = '<div class="package-card"><div class="package-card__head"><div class="package-card__head__item"><span class="package-card__head__item__price"><sup>$</sup>' . number_format($package->price, 2) . '<span class="package-card__head__item__name">+ HST</span></span><p class="package-card__head__item__name">(Certification fees included)</p><div class="package-card__head__item__shape__one"><svg width="90" height="95" viewBox="0 0 90 95" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M90 -0.000350952L0 69.6066L90 94.9996V-0.000350952Z" fill="" fill-opacity="0.06" /></svg></div><div class="package-card__head__item__shape__two"><svg width="60" height="64" viewBox="0 0 60 64" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0.789585 63.2843L59.4571 16.7158L0.00060816 0.747339L0.789585 63.2843Z" fill="" fill-opacity="0.06" /></svg></div></div><div class="package-card__head__shape"></div></div><div class="package-card__body"><h3 class="package-card__body__title">'. $package->package_name .'<br><span>'.$category->subtitle.'</span></h3><ul class="package-card__body__list">' . $package->description . '</ul></div></div>';

        echo $packageData;
    }

    public function certification()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Check Certification".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Check Certification";

        // $data["provinces"] = $this->provinces;

        $data["changeView"] = false;
        
        // Form submit start
        $this->load->library('form_validation');
        $this->form_validation->set_rules('firstname', 'First Name', 'required');
        $this->form_validation->set_rules('lastname', 'Last Name', 'required');
        $this->form_validation->set_rules('location', 'Location', 'required');
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('certification', $data);
                return;
            } else {
                $filterArr = [];
                $filterArr['province_id'] = ($this->input->post("location") > 0) ? $this->input->post("location") : 0;
                $filterArr['firstname'] = ($this->input->post("firstname") != '') ? $this->input->post("firstname") : '';
                $filterArr['lastname'] = ($this->input->post("lastname") != '') ? $this->input->post("lastname") : '';

                $dataRows = $this->Common_Model->checkCertification($filterArr);
                if (!empty($dataRows)) {
                    $this->session->set_flashdata('message', '');
                    $data["changeView"] = true;
                    $data["dataRows"] = $dataRows;
                } else {
                    $this->session->set_flashdata("message", '<div class="alert alert-info alert-dismissible show" role="alert">
                            Certificate not found for the given search criteria.
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');
                }
            }
        }
        // Form submit end

        $this->load->view('certification', $data);
    }

    public function subscribe_newsletter()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('sn_email', 'Email', 'required|valid_email|trim');

        if ($this->form_validation->run() == FALSE) {
            $this->session->set_flashdata("sn_message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                <strong>Error!</strong>
                '.strip_tags(validation_errors()).'
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>');
        } else {
            $email = $this->input->post('sn_email', TRUE);
            $this->load->model('Newsletter_model');

            // Check if email already exists
            if ($this->Newsletter_model->email_exists($email)) {
                $this->session->set_flashdata("sn_message", '<div class="alert alert-info alert-dismissible show" role="alert">
                You already subscribed.
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>');
            } else {
                // Save new subscriber
                $locationName = $this->Common_Model->getProvincename($this->session->province);
                if (empty($locationName)) {
                    $this->session->set_flashdata("sn_message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                        <strong>Error!</strong>
                        '.$this->lang->line('wrong').'
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>');
                }
                else{
                    $location_name = $locationName->province;
                    $result = $this->Newsletter_model->save_subscription(["email" => $email, "location_name" => $location_name]);
                    if($result){
                        $locationInfo = $this->Common_Model->loadLocations($this->session->province);
                        $locationEmail = $this->Common_Model->loadLocationEmail($this->session->province);

                        $emailerContent = $this->Common_Model->loadEmailer(5);

                        $emailer = $this->load->view('email/email_header', [], TRUE).$emailerContent->emailer.$this->load->view('email/email_footer', [], TRUE);

                        $emailer = str_replace("@currentyear", date("Y"), $emailer);

                        $emailer = str_replace("@email", $email, $emailer);
                        $emailer = str_replace("@locationName", $location_name, $emailer);

                        $emailer = str_replace("@lNumber", $locationInfo[0]->telephone, $emailer);
                        $emailer = str_replace("@lEmail", $locationInfo[0]->email, $emailer);

                        $email_setting  = array('mailtype' => 'html', 'wordwrap' => true);
                        $this->email->initialize($email_setting);
                        $this->email->from($this->config->item('mail_from_email'), $this->config->item('mail_from_name'));

                        $cc_email = $emailerContent->cc_email;
                        $bcc_email = $emailerContent->bcc_email;

                        $emailToList = array(
                            $locationEmail->email
                        );

                        $this->email->to($emailToList);

                        if ($cc_email != '') {
                            $this->email->cc($cc_email);
                        }
                        if ($bcc_email != '') {
                            $this->email->bcc($bcc_email);
                        }

                        $this->email->subject('New Newsletter Subscription Received');
                        $this->email->message($emailer);
                        if ($this->email->send()) {
                            $this->session->set_flashdata("sn_message", '<div class="alert alert-success alert-dismissible show" role="alert">
                                Thank you for subscribing!
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                        } else {
                            $this->session->set_flashdata("sn_message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                                <strong>Error!</strong>
                                '.$this->lang->line('email_failed').'
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                            // echo $this->email->print_debugger(); // useful for debugging
                        }
                    }
                    else{
                        $this->session->set_flashdata("sn_message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('insert_failed').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');
                    }
                }
            }
        }

        redirect($this->input->server('HTTP_REFERER', TRUE) . '#newsletter-section');
    }

    function validate_captcha()
    {
        $this->load->config('recaptcha');  //Load the config file

        $recaptcha = trim($this->input->post('g-recaptcha-response'));
        $userIp = $this->input->ip_address();
        $secret = $this->config->item('recaptcha_secret_key');
        if (!$secret) {
            die("Secret key not loaded");
        }
        $data = array(
            'secret' => "$secret",
            'response' => "$recaptcha",
            'remoteip' => "$userIp"
        );

        $verify = curl_init();
        curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
        curl_setopt($verify, CURLOPT_POST, true);
        curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($verify);
        $status = json_decode($response, true);
        
        if (empty($status['success'])) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    public function digitalELearning()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Digital e-learning".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Digital e-learning";

        $province_id = $this->session->province;

        $data["biggerPackages"] = $this->Common_Model->loadBiggerPackages($province_id, [4]);

        $this->load->view('digital-e-learning', $data);
    }

    public function registrationEnquiry()
    {
        $data = $this->data;
	
		$pageData = new stdClass();
		$pageData->pageContent = new stdClass();
		$pageData->pageContent->page_title = "Registration".$this->page_title_sfx;
		$data["pageContent"] = $pageData->pageContent;
        $data["pageName"] = "Registration";

        // $data["provinces"] = $this->provinces;
        
        $this->load->model("Package_Model");
        $data["package_categories"] = $this->Package_Model->getCategories();

        $this->load->config('recaptcha');
        $data["recaptcha_site_key"] = $this->config->item('recaptcha_site_key');

        // Form submit start
        $this->load->library('form_validation');
        $this->form_validation->set_rules('location', 'Location', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('phone_number', 'Phone Number', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('package_type', 'Package Type', 'required');
        $this->form_validation->set_rules('package', 'Package', 'required');

        // Only run this if form submitted
        if ($this->input->server('REQUEST_METHOD') === 'POST') {
            if ($this->form_validation->run() == FALSE) {
                $this->load->view('registration-enquiry', $data);
                return;
            } else {
                if ( $this->validate_captcha() ) {
                    //Refresh the Page without any error if phone validation fails.
                    if (!$this->Common_Model->checkAllowedPhoneNumberCode($this->input->post('phone_number'))) {
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('invalid_phone').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');

                        $this->load->view('registration-enquiry', $data);
                        return;
                    }

                    $insertData = array(
                        "location" => $this->input->post("locationName"),
                        "name" => $this->input->post("name"),
                        "phone_number" => $this->input->post("phone_number"),
                        "email" => $this->input->post("email"),
                        "package_type" => $this->input->post("packageTypeName"),
                        "package" => $this->input->post("packageName"),
                        "preferred_date" => $this->input->post("preferred_date"),
                        "parent_contact" => $this->input->post("parent_contact"),
                        "hear_about_us" => $this->input->post("hear_about_us"),
                        "postal_code" => $this->input->post("postal_code"),
                        "message" => $this->input->post("message")
                    );

                    $saved = $this->Common_Model->insertData("registration_inquiries", $insertData);
                    if ($saved) {
                        $emailerContent = $this->Common_Model->loadEmailer(7);
                        $emailer = $this->load->view('email/email_header', [], TRUE).$emailerContent->emailer.$this->load->view('email/email_footer', [], TRUE);
                        
                        $emailer = str_replace("@currentyear", date("Y"), $emailer);
                        $emailer = str_replace("@location", $this->input->post("locationName"), $emailer);
                        $emailer = str_replace("@name", $this->input->post("name"), $emailer);
                        $emailer = str_replace("@phone_number", $this->input->post("phone_number"), $emailer);
                        $emailer = str_replace("@email", $this->input->post("email"), $emailer);
                        $emailer = str_replace("@package_type", $this->input->post("packageTypeName"), $emailer);
                        $emailer = str_replace("@package", $this->input->post("packageName"), $emailer);
                        $emailer = str_replace("@preferred_date", $this->input->post("preferred_date"), $emailer);
                        $emailer = str_replace("@parent_contact", $this->input->post("parent_contact"), $emailer);
                        $emailer = str_replace("@hear_about_us", $this->input->post("hear_about_us"), $emailer);
                        $emailer = str_replace("@postal_code", $this->input->post("postal_code"), $emailer);
                        $emailer = str_replace("@message", $this->input->post("message"), $emailer);

                        $email_setting  = array('mailtype' => 'html', 'wordwrap' => true);
                        $this->email->initialize($email_setting);
                        $this->email->from($this->config->item('mail_from_email'), $this->config->item('mail_from_name'));

                        $cc_email = $emailerContent->cc_email;
                        $bcc_email = $emailerContent->bcc_email;
                        
                        $locationEmail = $this->Common_Model->loadLocationEmail($this->session->province);

                        $emailToList = array(
                            $this->input->post("email"),
                            $locationEmail->email
                        );

                        $this->email->to($emailToList);

                        if ($cc_email != '') {
                            $this->email->cc($cc_email);
                        }
                        if ($bcc_email != '') {
                            $this->email->bcc($bcc_email);
                        }

                        $this->email->subject('Registration Enquiry');
                        $this->email->message($emailer);

                        if ($this->email->send()) {
                            // Redirect to same registration page
                            $this->session->set_flashdata("message", '<div class="alert alert-success" role="alert">
                                <h4 class="alert-heading">Confirmation!</h4>
                                <p>Thank you for choosing AMB Driving School! Your online registration has been successfully submitted. A representative from our office will contact you to finalize your registration within 1-2 business days.</p>
                                <p>Please be sure to check your spam/junk mail folder for their follow up email which will contain further instructions to complete the registration<br>If you do not receive a follow up, please contact us at AMB Driving School at (905)-699-5711.</p>
                                <p>Thank you!</p>
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>');
                            redirect(base_url() . "registration-enquiry");
                        } else {
                            $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
								<strong>Error!</strong>
								'.$this->lang->line('email_failed').'
								<button type="button" class="close" data-dismiss="alert" aria-label="Close">
									<span aria-hidden="true">&times;</span>
								</button>
							</div>');
                            // echo $this->email->print_debugger(); // useful for debugging
                            
                            redirect(base_url() . "registration-enquiry");
                        }
                    }
                    else{
                        $this->session->set_flashdata("message", '<div class="alert alert-danger alert-dismissible show" role="alert">
                            <strong>Error!</strong>
                            '.$this->lang->line('insert_failed').'
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>');
                    }
                }
            }
        }
        // Form submit end

        $this->load->view('registration-enquiry', $data);
    }
}
