<?php

namespace App\Http\Controllers;

use App\Models\Agency;
use Illuminate\Http\Request;

class AgencyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $search =  $request->input('q');
        if(isset($search) && !empty($search)){
            $agencies = Agency::where(function ($query) use ($search) {
                $query->where('name', 'like', $search.'%');              
                    // ->orWhere('email', 'like', $search.'%')
                    // ->orWhere('mobile', 'like', $search.'%');
            })->paginate(15);
           $agencies->appends(['q' => $search]);
        } else {
            $agencies = Agency::paginate(15);
        }

        return view('agencies.index', compact('agencies', 'search'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('agencies.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name' =>'required',
            'enabled' =>'required',
        ]);
        
        $agency = new Agency([
            'name' => $request->get('name'),
            'description' => $request->get('description'),
            'enabled' => $request->get('enabled'),
            'user_id' => auth()->user()->id
        ]);

        $agency->save();
        return redirect('/agencies')->with('success', 'Agency Added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Agency  $agency
     * @return \Illuminate\Http\Response
     */
    public function show(Agency $agency)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Agency  $agency
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $agency = Agency::find($id);
        return view('agencies.edit', compact('agency'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Agency  $agency
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name' =>'required',
            'enabled' =>'required'
        ]);

        $agency = Agency::find($id);
        $agency->name =  $request->get('name');
        $agency->description =  $request->get('description');
        $agency->enabled = $request->get('enabled');
        $agency->user_id = auth()->user()->id;
        $agency->save();

        return redirect('/agencies')->with('success', 'Agency updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Agency  $agency
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $agency = Agency::find($id);
        $agency->delete();

        return redirect('/agencies')->with('success', 'Agency deleted!');
    }
}
