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

class Website extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->model('Setting_model');
    }
    
    /**
     * Get common data for all pages
     */
    private function get_common_data() {
        return [
            'app_name' => $this->Setting_model->get('app_name', 'Meridian Bank'),
            'app_tagline' => $this->Setting_model->get('app_tagline', 'Secure Digital Banking'),
            'support_email' => $this->Setting_model->get('support_email', 'support@meridianbank.com'),
            'support_phone' => $this->Setting_model->get('support_phone', '+1 (800) 123-4567'),
            'logo_url' => $this->Setting_model->get_logo_url(),
            'logo_dark_url' => $this->Setting_model->get_logo_url(true),
            'favicon_url' => $this->Setting_model->get_favicon_url()
        ];
    }
    
    /**
     * Home page
     */
    public function index() {
        $data = $this->get_common_data();
        $data['title'] = 'Welcome';
        $data['page'] = 'home';
        
        $this->load->view('website/home', $data);
    }
    
    /**
     * About page
     */
    public function about() {
        $data = $this->get_common_data();
        $data['title'] = 'About Us';
        $data['page'] = 'about';
        
        $this->load->view('website/about', $data);
    }
    
    /**
     * Contact page
     */
    public function contact() {
        $data = $this->get_common_data();
        $data['title'] = 'Contact Us';
        $data['page'] = 'contact';
        
        $this->load->view('website/contact', $data);
    }
    
    /**
     * Accounts page
     */
    public function accounts() {
        $data = $this->get_common_data();
        $data['title'] = 'Our Accounts';
        $data['page'] = 'accounts';
        
        $this->load->view('website/accounts', $data);
    }
    
    /**
     * Cards page
     */
    public function cards() {
        $data = $this->get_common_data();
        $data['title'] = 'Our Cards';
        $data['page'] = 'cards';
        
        $this->load->view('website/cards', $data);
    }
    
    /**
     * FAQ page
     */
    public function faq() {
        $data = $this->get_common_data();
        $data['title'] = 'FAQ';
        $data['page'] = 'faq';
        
        $this->load->view('website/faq', $data);
    }
    
    /**
     * Services page
     */
    public function services() {
        $data = $this->get_common_data();
        $data['title'] = 'Our Services';
        $data['page'] = 'services';
        
        $this->load->view('website/services', $data);
    }
    
    /**
     * Security page
     */
    public function security() {
        $data = $this->get_common_data();
        $data['title'] = 'Security';
        $data['page'] = 'security';
        
        $this->load->view('website/security', $data);
    }
    
    /**
     * Privacy Policy page
     */
    public function privacy() {
        $data = $this->get_common_data();
        $data['title'] = 'Privacy Policy';
        $data['page'] = 'privacy';
        
        $this->load->view('website/privacy', $data);
    }
    
    /**
     * Terms page
     */
    public function terms() {
        $data = $this->get_common_data();
        $data['title'] = 'Terms & Conditions';
        $data['page'] = 'terms';
        
        $this->load->view('website/terms', $data);
    }
    
    /**
     * Handle contact form submission
     */
    public function send_contact() {
        // Check if it's an AJAX request
        if (!$this->input->is_ajax_request()) {
            show_404();
        }
        
        // Load form validation library
        $this->load->library('form_validation');
        
        // Set validation rules
        $this->form_validation->set_rules('name', 'Name', 'required|trim');
        $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
        $this->form_validation->set_rules('phone', 'Phone', 'trim');
        $this->form_validation->set_rules('subject', 'Subject', 'required|trim');
        $this->form_validation->set_rules('message', 'Message', 'required|trim');
        
        if ($this->form_validation->run() === FALSE) {
            echo json_encode([
                'success' => false,
                'message' => validation_errors()
            ]);
            return;
        }
        
        // Get form data
        $name = $this->input->post('name', true);
        $email = $this->input->post('email', true);
        $phone = $this->input->post('phone', true);
        $subject = $this->input->post('subject', true);
        $message = $this->input->post('message', true);
        
        // Get support email from settings
        $support_email = $this->Setting_model->get('support_email', 'info@meridianbank.com');
        $app_name = $this->Setting_model->get('app_name', 'Meridian Bank');
        
        // Load Mailer library
        $this->load->library('mailer');
        
        try {
            // Prepare email content
            $email_subject = "Contact Form: $subject";
            $email_body = "
                <div style='font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;'>
                    <div style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 30px; text-align: center;'>
                        <h1 style='color: white; margin: 0;'>New Contact Message</h1>
                    </div>
                    <div style='background: #f8f9fa; padding: 30px;'>
                        <div style='background: white; padding: 25px; border-radius: 10px; margin-bottom: 20px;'>
                            <h3 style='color: #2d3748; margin-top: 0;'>Contact Details</h3>
                            <table style='width: 100%; border-collapse: collapse;'>
                                <tr>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'><strong>Name:</strong></td>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'>{$name}</td>
                                </tr>
                                <tr>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'><strong>Email:</strong></td>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'>{$email}</td>
                                </tr>
                                <tr>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'><strong>Phone:</strong></td>
                                    <td style='padding: 10px 0; border-bottom: 1px solid #e0e0e0;'>" . ($phone ?: 'Not provided') . "</td>
                                </tr>
                                <tr>
                                    <td style='padding: 10px 0;'><strong>Subject:</strong></td>
                                    <td style='padding: 10px 0;'>{$subject}</td>
                                </tr>
                            </table>
                        </div>
                        <div style='background: white; padding: 25px; border-radius: 10px;'>
                            <h3 style='color: #2d3748; margin-top: 0;'>Message</h3>
                            <p style='color: #6c757d; line-height: 1.6; margin: 0;'>{$message}</p>
                        </div>
                        <div style='text-align: center; margin-top: 20px; color: #6c757d; font-size: 14px;'>
                            <p>This message was sent from the {$app_name} contact form</p>
                        </div>
                    </div>
                </div>
            ";
            
            // Send email
            $sent = $this->mailer->send_email($support_email, $email_subject, $email_body);
            
            if ($sent) {
                echo json_encode([
                    'success' => true,
                    'message' => 'Thank you for contacting us! We will get back to you within 24 hours.'
                ]);
            } else {
                echo json_encode([
                    'success' => false,
                    'message' => 'Failed to send message. Please try again or contact us directly at ' . $support_email
                ]);
            }
        } catch (Exception $e) {
            log_message('error', 'Contact form error: ' . $e->getMessage());
            echo json_encode([
                'success' => false,
                'message' => 'An error occurred. Please contact us directly at ' . $support_email
            ]);
        }
    }
}
