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

class Frontend extends CI_Controller {

    protected $browserResponse;
    protected $data;

    public function __construct(){

        parent::__construct();
        checkSiteSettings();
        $this->browserResponse = array('status'=>'error','message'=>'','redirect'=>'');
        $this->data = array('header_data' => [], 'footer_data' => [], 'page_info' => [], 'csrf' => $this->csrf_token);
    }

    public function index() {
        $settings = $this->Qdb->select_data('*', 'frontend_settings', '', '', '', array('id', 'desc'));
        $this->data['site_settings'] = isset($settings[0]['site_settings']) ? json_decode($settings[0]['site_settings'], true) : [];
        $this->data['about_us'] = isset($settings[0]['about_us']) ? json_decode($settings[0]['about_us'], true) : [];
        $this->data['privacy_policy'] = isset($settings[0]['privacy_policy']) ? $settings[0]['privacy_policy'] : '';
        $this->data['terms_conditions'] = isset($settings[0]['terms_conditions']) ? $settings[0]['terms_conditions'] : '';
        $this->data['testimonial_count']     = $settings[0]['testimonials'] ? count(json_decode($settings[0]['testimonials'])->testimonial_title) : '';
        $this->data['testimonial_settings'] = isset($settings[0]['testimonials']) ? json_decode($settings[0]['testimonials'], true) : [];
        $this->data['plans_list'] = $this->Qdb->select_data('*', 'plans_list', ['p_status'=>1], 3, '', array('id', 'desc'));
        $this->load->helper('currency_helper'); 
        $this->data['symbol_ar'] = getCurrencySymbols();
        $this->load->view('landing-page', $this->data);
    }

    public function contact_us() {
        $site_count = $this->Qdb->query("SELECT * FROM site_settings ORDER BY id DESC","num_rows");
        $site_settings = $this->Qdb->select_data('support_email', 'site_settings', '', '', '', array('id', 'desc'));
        $data = array(
            'c_name'        => $this->input->post('c_name'),
            'c_email'       => $this->input->post('c_email'),
            'c_mobile'        => $this->input->post('c_mobile'),
            'c_subject'       => $this->input->post('c_subject'),
            'c_message'     => $this->input->post('c_message'),
            'created_date'  => date('Y-m-d H:i:s')
        );
        $insert = $this->Qdb->insert_data('contact_us' , $data);
        if($insert){
            if($site_count>0 && !empty($site_settings[0]['support_email'])){
                $body = '<h1>Contact Form Submission</h1>';
                $body .= '<table border="1" cellpadding="10" cellspacing="0">';
                $body .= '<tr><th>Name</th><td>' . htmlspecialchars($data['c_name']) . '</td></tr>';
                $body .= '<tr><th>Email</th><td>' . htmlspecialchars($data['c_email']) . '</td></tr>';
                $body .= '<tr><th>Mobile</th><td>' . htmlspecialchars($data['c_mobile']) . '</td></tr>';
                $body .= '<tr><th>Message</th><td>' . nl2br(htmlspecialchars($data['c_message'])) . '</td></tr>';
                $body .= '</table>';
                sendEmailToUser($site_settings[0]['support_email'],$data['c_subject'],$body);
                echo json_encode(array('status' => 1, 'message' => 'Your message has been sent successfully!'));
            }else{
                echo json_encode(array('status' => 1, 'message' => 'Your message has been saved successfully!'));
            }
        }else{
            echo json_encode(array('status' => 0, 'message' => 'Sending Failed'));
        }
    }

    public function render_page($type) {
        $settings = $this->Qdb->select_data('site_settings,' . $type, 'frontend_settings', '', '', '', array('id', 'desc'));
        $site_settings = isset($settings[0]['site_settings']) ? json_decode($settings[0]['site_settings'], true) : [];
        $site_title = $site_settings['site_title'] ? $site_settings['site_title'] . ' | ' : '';
        $site_favicon = $site_settings['site_favicon'] ? '<link rel="shortcut icon" href="' . base_url($site_settings['site_favicon']) . '" type="image/x-icon">' : '';
        $content = isset($settings[0][$type]) ? $settings[0][$type] : '';
        $output = '<!DOCTYPE html>
        <html lang="en">
        <head>
            <title>' . $site_title . ucfirst(str_replace('_', ' ', $type)) . '</title>
            ' . $site_favicon . '
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css" crossorigin="anonymous" />
        </head>
        <body>
            <div class="container">' . $content . '</div>
        </body>
        </html>';
        echo $output;
    }
    
    public function privacy_policy() {
        $this->render_page('privacy_policy');
    }
    
    public function terms_conditions() {
        $this->render_page('terms_conditions');
    }

}