<?php

namespace App\Http\Controllers\frontend;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\Fromcontact;
use App\Models\Categorie;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;

class ContactController extends Controller
{
    public function Index(){

        
        $contacts = Contact::where('etat','1')->get();
        $categories = Categorie::where('etat','1')->get();


        return view('frontend/contact/contact',compact('contacts','categories'));

    }
    public function store(Request $request) { 

        $this->validate($request, [
            
            'email' => 'required|email',
            'objet' => 'required',
            
            'message' => 'required'
        ]);

        $contact = new Fromcontact;
        $contact->email = $request->email;
        $contact->objet = $request->objet;
        $contact->message = $request->message;

        $contact->save();


          Mail::send('frontend/contact/contact-email',
             array(
                 
                 'email' => $request->get('email'),
                 'objet' => $request->get('objet'),
                 
                 'user_message' => $request->get('message'),
             ), 
           
             function($msg) use ($request)
               {
               
                $contactRow = Contact::where('coorType_id',"=","3")->first();
     
                  $msg->from($request->email);
                  $msg->to($contactRow->coordonnees);
               });

        return back()->with('success', 'Merci de nous avoir contacter!');



    }
}
