<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\UserCreateRequest;
use App\Http\Requests\Dashboard\UserUpdateRequest;
use App\Models\Photo;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $customers = User::whereRelation('roles' , 'name' , 'customer')->get();
        return view('admin.customers.index', compact('customers'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $inputs = $request->all();
        $inputs['password'] = bcrypt($request->password);

        if ($request->photo) {
            $inputs['photo'] = $request->photo;
        } else{
            $inputs['photo'] = 'http://127.0.0.1:8000/storage/1/download (1).jpg';
        }

        $customer = User::create($inputs);

        $customer->assignRole('customer');

        session()->flash('customer_create_message', awtTrans('تم الاضافة بنجاح'));

        return redirect('admin/customers');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $customer = User::findOrFail($id);
        return view('admin.customers.show', compact('user'));

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $customer = User::findOrFail($id);
        return view('admin.customers.edit', compact('customer'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $customer = User::findOrFail($id);

        if (trim($request->password) == '') {
            $inputs = $request->except('password');
        } else {
            $inputs = $request->all();
            $inputs['password'] = bcrypt($request->password);
        }

        if ($request->photo) {
            $inputs['photo'] = $request->photo;
        }

        $customer->update($inputs);
        session()->flash('customer_update_msg', awtTrans('تم التعديل بنجاح'));
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $customer = User::findOrFail($id);

        $customer->delete();

        return response()->json();
    }
    // bluckDelete
    public function bluckDelete(Request $request)
    {
      
        $customers = User::findOrFail($request->ids);

        foreach ($customers as $customer) {
            $customer->delete();
        } // end foeach

        session()->flash('delete_customer_msg', awtTrans('تم الحذف بنجاح'));
        return redirect()->back();
    }
}
