<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Mail\ContactMail;
use App\Mail\RecrutementMail;
use App\Recrutement;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;

class ContactController extends Controller
{
    public function contact (Request $request ,Contact $contact , Recrutement $recrutement){
        if($request->isMethod('POST')){
            if($request->contact =='contact' || $request->contact =='service_client'){
                $check = [
                    'nom' => 'required|string',
                    'prenom' => 'required|string',
                    'email' => 'required|email:rfc,dns',
                    'telephone' => 'required',
                    'objet' => 'required|string',
                    'message' => 'required|string', 
                    
                ];
                // dd('$request->validate($check)');
                $request->validate($check);
    
                $nom = $request->nom;
                $prenom = $request->prenom;
                $email = $request->email;
                $telephone = $request->telephone;
                $objet = $request->objet;
                $message = $request->message;
                $contact = $contact->create([
                    'nom' => $nom,
                    'prenom' => $prenom,
                    'email' => $email,
                    'telephone' => $telephone,
                    'objet' => $objet,
                    'message' => $message,
    
                ]);
                
                 if($request->contact == 'contact')
                $type_contact = 'Contact';
                else
                $type_contact = 'Service Client';
                Mail::to("ma.pommedepain@gmail.com")->send(new ContactMail($contact,$type_contact));


            }
            if($request->contact =='rh'){
                // $hasFile = $request->hasFile('file');
                // dd($request->file('file'));
                $check = [
                    'nom' => 'required|string',
                    'prenom' => 'required|string',
                    'email' => 'required|email:rfc,dns',
                    'telephone' => 'required',
                    'file' => 'required|mimes:pdf,docx,docs|max:10000', 
        
                ];
                $request->validate($check);
                $nom = $request->nom;
                $prenom = $request->prenom;
                $email = $request->email;
                $telephone = $request->telephone;
                $file = $request->file('file');
                $path =  Storage::putFile('recrutements',$file);
                // dd($path);
                $recrutement = $recrutement->create([
                    'nom' => $nom,
                    'prenom' => $prenom,
                    'email' => $email,
                    'telephone' => $telephone,
                    'cv' => $path,
                ]);
                
                Mail::to("ma.pommedepain@gmail.com")->send(new RecrutementMail($recrutement));
            }
          
            return view('frontend.contact.index');
        }
       
    }
}
