<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Customer;
use Session;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $q_name = $request->q_name;
        $customer = Customer::orderBy('id', 'asc');
        
        if (!empty($q_name)) {
            $customer->where('name', 'like', '%'.$q_name.'%');
        }

        $customer = $customer->simplePaginate(15);

        return view('apps.admin.customer.index')->with('customer', $customer)
                                                ->with('q_name', $q_name);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete(Request $request)
    {
        $customer = Customer::findOrFail($request->id);
        $user = User::where('email', $customer->email)->first();

        $user->delete();
        $customer->delete();
        
        return redirect()->back();
    }
}
 