<?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 SupplierController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $suppliers = User::whereRelation('roles' , 'name' , 'supplier')->get();
        return view('admin.suppliers.index', compact('suppliers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.suppliers.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';
        }

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

        $supplier->assignRole('supplier');

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

        return redirect('admin/suppliers');
    }

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

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

    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $supplier = 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;
        }

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

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

        $supplier->delete();

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

        foreach ($suppliers as $supplier) {
            $supplier->delete();
        } // end foeach

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