<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Widgets;
use App\Faq;
use App\CMSPages;
use App\WidgetSection;
use App\WebsiteFaqs;
use App\Settings;
use App\EmailsResponders;
use App\Helpers\Functions;
use App\Helpers\EmailTemplates;
use Mail;
use Illuminate\Support\Facades\Route;
use Session;
use Validator;
class CorporateController extends Controller
{
    public function __construct() {
        
    }

    public function index() {
        
        $data["corporate"]= CMSPages::where('AccessURL','corporate')->where('Status','Enabled')->first();
        $data['meta_title'] = $data["corporate"]->MetaTitle;
        $data['meta_description'] = $data["corporate"]->MetaDescription;
//      $sec=WidgetSection::where('code','boiler_installation_trust_pilot_image')->first();
//      $widgetsData= $this->getWidgetData($sec->PKSectionID);
//      $data["images"]=$widgetsData;
       
        $data["faqs"] = WebsiteFaqs::where("type", "pages")->where("type_id", $data["corporate"]->PKPageID)->where("status", 1)->get();

//      $data["sec1"]= Widgets::where('code','boiler_installation_sec_1')->first();
//      $data["sec2"]= Widgets::where('code','boiler_installation_sec_2')->first();
        $data["sec3"]= Widgets::where('code','index_sec_2')->first();
        $data["sec4"]= Widgets::where('code','boiler_installation_sec_3')->first();
        $data["sec5"]= Widgets::where('code','index_sec_9')->first();
        $data["sec6"]= Widgets::where('code','boiler_installation_sec_4')->first();
        $data["sec7"]= Widgets::where('code','boiler_installation_sec_5')->first();
        $data["our_verification"] = Widgets::where('code','corporate_our_verification')->first();
        $data["some_of_our_clients"] = Widgets::where('code','corporate_some_of_our_clients')->first();
        
         $data["headerClass"]='corporate-page';
        return view('front/corporate',$data);

    }
     public static function getWidgetData($id) {
        $model = WidgetSection::

                join('cms_widget_navigation_sections as wn', 'cms_widget_sections.PKSectionID', '=', 'wn.FKSectionID')
                ->join('cms_widgets as w', 'w.PKWidgetID', '=', 'wn.FKWidgetID')
                ->where("FKSectionID", $id)
                ->get();

        return $model;
    }
    public function corporate_submit(Request $request){
         $rules = [
            'first_name' => 'required',
            'last_name' => 'required',
            'company_name' => 'required',
            'postcode' => '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', 'Corporate Get In Touch Form Responder')->first();
            $data["body"] = $emailsResponders->Content;
            $data["title"] = $emailsResponders->Title;
            $data["subject"] = $emailsResponders->Subject;
            $replaces["First_Name"] = $input["first_name"];
            $replaces["Last_Name"] = $input["last_name"];
            $replaces["Company_Name"] = $input["company_name"];
            $replaces["Postcode"] = $input["postcode"];
            $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["first_name"])->subject("Corporate Get In Touch Form");                    
                });
                return redirect('/thank-you');
             } catch (\Throwable $th) {
                 throw $th;
            }
            
        }
     }
}