<?php

namespace App\Http\Controllers\backend;
use App\Http\Controllers\Controller;
use App\Models\Contact;
use App\Models\Coordonnee;


use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function __construct(){
        return $this->middleware(['auth','verified']);

    }

     public function index() {
        $contacts = Contact::with('coorType')->get();
        
        // dd($contacts);
         
        return view('backend.contact.index',['contacts'=>$contacts]);
    }
    
    

     public function create(){

        $coorTypes = Coordonnee::select('type', 'id')->oldest('type')->get();
        $contactNull = Contact::where('coorType_id', '=','3')->first();


        return view('backend.contact.add', compact('coorTypes','contactNull'));
        
    }

    public function store(Request $request){
        request()->validate([
            'coordonnees' => 'required',
            'Type' => 'required',
       ]);
         $contact = new Contact();
        
        $contact->coordonnees = $request->input('coordonnees');
         $contact->coorType_id = $request->input('Type');

        $contact->save();
        return redirect()->route('contact');
    } 
    
    public function edit($id){
        $contact = Contact::find($id);
        
        // $coorTypes = Coordonnee::select('type', 'id')->oldest('type')->get();
        //         $contacts = Contact::with('coorType')->get();
        return view('backend.contact.edit',['contact'=>$contact]);
    }

    public function update( Request $request, $id){
        $contact = Contact::find($id);
        $contact->coordonnees = $request->input('coordonnees');

        $contact->save();
        return redirect()->route('contact')->with('success','Modifié');
    }

    public function changeStatusContact(Request $request)
        {
            $contact = Contact::find($request->contactId);
            $contact->etat = $request->etat;
            $contact->save();
      
            return response()->json(['success'=>'Status change successfully.']);
        }
        public function destroyC(Request $request, $id){


            $contact = Contact::find($id);
            $contact->delete();
           
            return redirect()->route('contact');;
        }
}