<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Widgets;
use App\Pages;
use Validator;
use App\Engineer;
use App\Settings;
use Mail;
use App\EmailsResponders;
use App\Helpers\Functions;
use App\Helpers\EmailTemplates;
use Illuminate\Support\Facades\Route;

class BecomeEngineerController extends Controller {

    public function __construct() {
        
    }

    public function index($key) {

        $model = Pages::where("Template", "None")->where('AccessURL', $key)->first();
        d($model, 1);

        if (!empty($model)) {
            $data["model"] = $model;
            $data["sec2"] = Widgets::where('code', 'become_an_engineer_sec_2')->first();
            $data["sec3"] = Widgets::where('code', 'become_an_engineer_sec_3')->first();
            $data["sec4"] = Widgets::where('code', 'index_sec_5')->first();
            $data["headerClass"] = '';
            return view('front/become_an_engineer', $data);
        } else {
            return abort(404);
        }
    }

    public function register(Request $request) {
        $rules = [
            'name' => 'required',
            'email' => 'required|string|email',
            'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:11',
            'address1' => 'required',
            //'address2' => 'required',
            'city' => 'required',
            'postcode' => 'required',
                //'policyNumber' => 'required|integer',
                //'providersName' => 'required',
                //'coverLimit' => 'required|integer',
                //'renewalDate' => 'required|date|date_format:Y-m-d',
        ];

        $setting = Settings::where("code", "email")->first();
        $input = $request->all();

        if ($input["code"] == "become_an_engineer") {

            $rules["mobile"] = 'regex:/^([0-9\s\-\+\(\)]*)$/|min:11';
            $rules["companyName"] = 'required|string';
            $rules["companyRegNo"] = 'required|integer';
            $rules["policyNumber"] = 'required|integer';
            $rules["providersName"] = 'required';
            $rules["vatRegisteredNumber"] = 'required';
            $rules["coverLimit"] = 'required|integer';
            $rules["renewalDate"] = 'required|date';
        } else {
            $rules["trade"] = 'required';
            $rules["experience"] = 'required';
        }

        $message = [];
        $validator = Validator::make($input, $rules, $message);

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

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $response['error'] = true;
            $header = 402;
        } else {

            $input["type"] = $input["code"];

            unset($input["_token"]);
            unset($input["code"]);
            $model = new Engineer();
            $input["created_at"] = date("Y-m-d H:i:s");

            //$data = url()->previous();

            $id = $model->insertGetId($input);

            $response["success"] = true;
            $header = 200;
           
            if ($input["type"] == "become_an_engineer") {
                $emailsResponders = EmailsResponders::where('Title', 'Become an Engineer')->first();
                $data["body"] = $emailsResponders->Content;
                $data["title"] = $emailsResponders->Title;
                $data["subject"] = $emailsResponders->Subject;
                $replaces["First_Name"] = $input["name"];
                $replaces["Phone_Number"] = $input["phone"];
                $replaces["Mobile_Number"] = $input["mobile"];
                $replaces["Email_Address"] =  $input["email"];
                $replaces["address1"] = $input["address1"];
                $replaces["address2"] = $input["address2"];
                $replaces["city"] = $input["city"];
                $replaces["postcode"] = $input["postcode"];
                $replaces["companyName"] = $input["companyName"];
                $replaces["companyRegNo"] = $input["companyRegNo"];
                $replaces["safeRegistered"] = $input["safeRegistered"];
                $replaces["registrationNumber"] = $input["registrationNumber"];
                $replaces["policyNumber"] = $input["policyNumber"];
                $replaces["providersName"] = $input["providersName"];
                $replaces["coverLimit"] = $input["coverLimit"];
                $replaces["renewalDate"] = $input["renewalDate"];
                $replaces["VAT_Registered"] = $input["vatRegistered"];
                $replaces["VAT_RegisteredNumber"] = $input["vatRegisteredNumber"];
            }else{
                $emailsResponders = EmailsResponders::where('Title', 'Become A Tradesman')->first();
                $data["body"] = $emailsResponders->Content;
                $data["title"] = $emailsResponders->Title;
                $data["subject"] = $emailsResponders->Subject;
                $replaces["First_Name"] = $input["name"];
                $replaces["Phone_Number"] = $input["phone"];
                $replaces["Email_Address"] =  $input["email"];
                $replaces["address1"] = $input["address1"];
                $replaces["address2"] = $input["address2"];
                $replaces["city"] = $input["city"];
                $replaces["postcode"] = $input["postcode"];
                $replaces["trade"] = $input["trade"];
                $replaces["experience"] = $input["experience"];
                $replaces["information"] = $input["information"];
            }
            $template = Functions::replaces($data, $replaces);

            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"));
                    //"aftabafzaal@live.com" $customer->email

                    if ($input["type"] == "become_an_engineer") {
                        $subject = "Become An Engineer Form Request";
                    } else {
                        $subject = "Become A Tradesman Form Request";
                    }

                    $m->to($setting->content, $input["name"])->subject($subject);
                });
            } catch (\Throwable $th) {
                echo $th->getMessage();
                die("Throwable 1");
                // throw $th;
            }

            //mail($setting->content, "Become A Tradesman Form Request", $msg);
        }

        return response()->json($response, $header);
    }

}
