<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Enquiry;
use Mail;
use Illuminate\Support\Facades\Route;


class EnquireController extends Controller
{
    public function equiry(Request $request){
        $rules = [
            'name' =>'required',
            'email' => 'required|string|email',
            'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
            'message' => 'required|min:20',
        ];

        $input = $request->all();
        
        $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 {
           
            unset($input["_token"]);
            $model = new Enquiry();
            $input["created_at"] = date("Y-m-d H:i:s");
//            $input["type"]=redirect()->getUrlGenerator()->previous();
           
           $data =url()->previous();

          $input["type"]=substr($data, strrpos($data, '/') + 1);

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


            $response["success"] = true;
            $header = 200;
            $data = array('data'=>$input);
            
            $msg='<div style="margin: auto;width: 50%;padding: 10px;  text-align: center;" >
            <h1>Hi,'.$input["name"].'</h1>
            <p >we recieved a request a callback  for the account <b>'.$input["email"].'</b></p>
            <p>Your phone number is  <b>'.$input["phone"].'</b></p>
            <p>Thank you</b></p>
            </div>';
            
//            mail($input["email"],"Enquire Form Request",$msg);
            /*
            Mail::send('front.templates.enquire_email', $data, function ($m) use ($input) {
                $m->from(env("MAIL_FROM_ADDRESS"), env("MAIL_FROM_NAME"));
            $m->to($input["email"], $input["name"])->subject("Enquire Form Request");
                                          
                });
             * 
             */
           
        }
        
       return response()->json($response, $header);
       
    }
}
