<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        $fleet = [
            [
                "image" => "/assets/images/car-1.jpg",
                "title" => "Ford Transit 250 High roof",
            ] ,
            [
                "image" => "/assets/images/car-2.jpg",
                "title" => "Ford Transit 250 Mid roof",
            ] ,
            [
                "image" => "/assets/images/car-3.jpg",
                "title" => "Ford Transit 250 Mid roof",
            ] ,
            [
                "image" => "/assets/images/car-1.jpg",
                "title" => "Ford Transit 250 Low roof",
            ] ,
            [
                "image" => "/assets/images/car-2.jpg",
                "title" => "Ford Transit 250 Mid roof",
            ] ,
            [
                "image" => "/assets/images/car-3.jpg",
                "title" => "Ford Transit 250 Low roof",
            ] ,

        ];
        $in["fleet"] = $fleet;

        return view('home', $in);
    }
    public function about(): string
    {
        return view('about-us');
    }
    public function contact(): string
    {



//        $email = service('email');
//
//
//        $config['SMTPHost'] = 'smtp.googlemail.com';
//        $config['SMTPUser'] = 'support@chiefsoft.com';
//        $config['SMTPPass'] = 'may12002!';
//        $config['SMTPPort'] = '465';
//
//        $config['SMTPCrypto'] = 'ssl';
//
//        $config['protocol'] = 'sendmail';
//        $config['mailPath'] = '/usr/sbin/sendmail';
//        $config['charset']  = 'iso-8859-1';
//        $config['wordWrap'] = true;
//
//        $email->initialize($config);
//
//        $email->setFrom('support@gmail.com', 'ChiefsoftWorks Support');
//        $email->setTo('ses66181@gmail.com');
////$email->setCC('another@another-example.com');
////$email->setBCC('them@their-example.com');
//
//        $email->setSubject('Email Test');
//        $email->setMessage('Testing the email class.');
//
//        $email->send();

        $data = [];
        if ($_POST){
            $message  = $this->contactMessage();
          //  echo "ameye 00 ......";
           $data['contact_sent'] = $this->pushEmail($message);
        }





        return view('contact',$data);
    }
    private function bookMessage():string{
        $errorMSG = "";
        $fname = $lname = $email = $phone = $message = '';
        // FIRSTNAME
        if (empty($_POST["fname"])) {
            $errorMSG = "Full Name is required. ";
        } else {
            $fname = $_POST["fname"];
        }

        // LASTNAME
        if (empty($_POST["lname"])) {
            $errorMSG = "Full Name is required. ";
        } else {
            $lname = $_POST["lname"];
        }

        // EMAIL
        if (empty($_POST["email"])) {
            $errorMSG .= "Email is required. ";
        } else {
            $email = $_POST["email"];
        }

        // PHONE
        if (empty($_POST["phone"])) {
            $errorMSG .= "Phone is required. ";
        } else {
            $phone = $_POST["phone"];
        }

        // MESSAGE
        if (empty($_POST["message"])) {
            $errorMSG .= "Message is required. ";
        } else {
            $message = $_POST["message"];
        }



        // prepare email body text
        $Body = "";
        $Body .= "Name: ";
        $Body .= $fname." ".$lname;
        $Body .= "\n";
        $Body .= "Email: ";
        $Body .= $email;
        $Body .= "\n";
        $Body .= "Phone: ";
        $Body .= $phone;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $message;
        $Body .= "\n";


        return $Body;
    }

    private function contactMessage():string{
        $errorMSG = "";
        $fname = $lname = $email = $phone = $message = '';
        // FIRSTNAME
        if (empty($_POST["fname"])) {
            $errorMSG = "Full Name is required. ";
        } else {
            $fname = $_POST["fname"];
        }

        // LASTNAME
        if (empty($_POST["lname"])) {
            $errorMSG = "Full Name is required. ";
        } else {
            $lname = $_POST["lname"];
        }

        // EMAIL
        if (empty($_POST["email"])) {
            $errorMSG .= "Email is required. ";
        } else {
            $email = $_POST["email"];
        }

        // PHONE
        if (empty($_POST["phone"])) {
            $errorMSG .= "Phone is required. ";
        } else {
            $phone = $_POST["phone"];
        }

        // MESSAGE
        if (empty($_POST["message"])) {
            $errorMSG .= "Message is required. ";
        } else {
            $message = $_POST["message"];
        }



        // prepare email body text
        $Body = "";
        $Body .= "Name: ";
        $Body .= $fname." ".$lname;
        $Body .= "\n";
        $Body .= "Email: ";
        $Body .= $email;
        $Body .= "\n";
        $Body .= "Phone: ";
        $Body .= $phone;
        $Body .= "\n";
        $Body .= "Message: ";
        $Body .= $message;
        $Body .= "\n";


        return $Body;
    }
    public function book(): string
    {
        $data = [];
        if ($_POST){
            $message  = $this->bookMessage();
            $data['contact_sent'] = $this->pushEmail($message);
        }
        return view('book',$data);
    }
    public function ourService(): string
    {
        return view('our-services');
    }

    private function pushEmail($message) : string
    {
        $to = 'johnbullenterprises@gmail.com'; //'ses66181@gmail.com';//Type here the mail address where you want to send
        $subject = 'Subject of Email';//Write here Subject of Email
        //$message="Congrats ! You did it.";//Write the message you want to send
        //$email = \Config\Services::email();

            $email = service('email');
            $email->setTo($to);
            $email->setFrom('support@chiefsoft.com', 'Contact Message');//set From
            $email->setSubject($subject);
            $email->setMessage($message);
         if($email->send())
        {
            $contact_sent = "Email has been Sent.";
        }
        else{
            $contact_sent= "Something went wrong !";
            $contact_sent ='';
        }
      /*   */
        return $contact_sent;
}


}
