<?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\Testimonial;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Spatie\Permission\Models\Role;

class TestimonialController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $testimonials = Testimonial::all();
        return view('admin.testimonials.index', compact('testimonials'));
    }

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

        $testimonial = Testimonial::create($inputs);

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

        return redirect('admin/testimonials');
    }

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

        $testimonial = Testimonial::findOrFail($id);
        return view('admin.testimonials.show', compact('user'));

    }

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

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

        $inputs = $request->all();

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

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

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

        $testimonial->delete();

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

        foreach ($testimonials as $testimonial) {
            $testimonial->delete();
        } // end foeach

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