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

class Home extends CI_Controller
{
    public function index()
    {
        $this->load->view('templates/header');
        // $this->load->view('templates/page1');
        // $this->load->view('templates/page2');
        // $this->load->view('templates/page3');
        // $this->load->view('templates/page4');
        // $this->load->view('templates/page5');
        // $this->load->view('templates/page6');
        $this->load->view('templates/footer');
    }

    public function page5()
    {

        $this->form_validation->set_rules('nama', 'Nama', 'required|trim|min_length[5]|max_length[25]');
        $this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email|max_length[35]');
        $this->form_validation->set_rules('phone', 'Phone', 'required|trim|integer|min_length[10]|max_length[15]');
        $this->form_validation->set_rules('pesan', 'Pesan', 'required|trim|min_length[5]|max_length[100]');

        if ($this->form_validation->run() ==  FALSE) {

            $this->load->view('templates/header');
            // $this->load->view('templates/page5');
        } else {

            $this->model_mail->addemail();

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

            if (!empty($email)) {

                $config = [
                    'protocol'  => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_user' => 'kirimsurat8@gmail.com',
                    'smtp_pass' => '#GHProduction2020',
                    'smtp_port' => 465,
                    'mailtype'  => 'html',
                    'chartset'  => 'utf-8',
                    'newline'   => "\r\n"
                ];
                $this->load->library('email', $config);
                $this->email->initialize($config);

                $this->email->from('#No-Replay : ');
                $this->email->to('info@kanamaprimaenergi.com, namasayahario@gmail.com, mynamehrio@yahoo.com');
                $this->email->subject('pesan dari website Kanama Prima Energi');
                $this->email->message('#Please Do Not-Replay This Email <br>'.
                '<br> Email pengirim : ' . $email .
                '<br><br> Di kirim oleh : ' . $nama .
                '<br><br> Nomor telephone : ' . $phone .
                '<br><br> Isi pesan adalah : <br>' . $pesan);

                if ($this->email->send()) {
                    $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert"> Email terkirim !</div>');
                    redirect('');
                } else {
                    show_error($this->email->print_debugger());
                }
            }
        }
    }
}