<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Widgets;
use App\CMSPages;
use App\Settings;
use App\Enquiry;
use App\EmailsResponders;
use App\Helpers\Functions;
use App\Helpers\EmailTemplates;
use Mail;
use Illuminate\Support\Facades\Route;
use Session;
use Validator;
class ContactController extends FrontController
{
    public function __construct() {
        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "#_";
        $this->breadcrumbs[1]['name'] = "Contact US";
    }
    
     public function index() {
        $data["pageData"] = CMSPages::where('AccessURL','contact-us')->where('Status','Enabled')->first();
        $data["sec1"]= Widgets::where('code','contact_sec_1')->first();
        $data["sec2"]= Widgets::where('code','contact_sec_2')->first();
        $data["sec3"]= Widgets::where('code','index_sec_5')->first();
        $data["headerClass"]='';
        return view('front/contact',$data);
     }
     
     public function contact_submit(Request $request){
         $rules = [
            'name' => 'required',
            'surname' => 'required',
            'email' => 'required|string|email',
            'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:11',
        ];

        
        $input = $request->all();
        $message = [];
        $validator = Validator::make($input, $rules, $message);

        $response = array();
        $response['error'] = false;

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            unset($input["_token"]);
            //Enquiry::insertGetId($input);
            $emailsResponders = EmailsResponders::where('Title', 'Contact Us Email Responder')->first();
            $data["body"] = $emailsResponders->Content;
            $data["title"] = $emailsResponders->Title;
            $data["subject"] = $emailsResponders->Subject;
            $replaces["First_Name"] = $input["name"];
            $replaces["Surname"] = $input["surname"];
            $replaces["Phone_Number"] =  $input["phone"];
            $replaces["Email_Address"] = $input["email"];
            $replaces["Message"] = $input["message"];
            $template = Functions::replaces($data, $replaces);
            $subject = $template['subject'];
            $toemail = $emailsResponders->ToEmail;
            try {
                Mail::send('front.emails.template', ['content' => $template["body"]], function ($m) use ($input) {
                    
                    $setting = Settings::where("category", "email")->where('title','Website Email Receivers')->first();
                    $m->from(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
                    $m->to($setting->content, $input["name"])->subject("Homeflo Contact Us From");                    
                });
                return redirect('/thank-you');
             } catch (\Throwable $th) {
                 throw $th;
            }
            
        }
     }
     
     
     
     
     
}
