<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\SliderRequest;
use App\Http\Requests\Dashboard\UpdateSliderRequest;
use App\Models\Slider;
use Illuminate\Http\Request;

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

        $sliders = Slider::all();
        return view('admin.sliders.index', compact('sliders'));
    }

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

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

        $inputs = $request->except('check_vedio_or_image', 'photo', 'type_video', 'video_device', 'video_youtube');

        if ($request->check_vedio_or_image == 'image') {
            $inputs['photo'] = $request->photo;
            $inputs['type'] = 'image';
        } elseif ($request->check_vedio_or_image == 'video' && $request->type_video == 'device') {

            $fileName = time() . '_' . $request->video_device->getClientOriginalName();
            $inputs['photo'] = $request->file('video_device')->storeAs('sliders', $fileName, 'public');
            $inputs['type'] = 'device';
        } elseif ($request->check_vedio_or_image == 'video' && $request->type_video == 'youtube') {
            $inputs['photo'] = $request->video_youtube;
            $inputs['type'] = 'youtube';
        }


        $slider = Slider::create($inputs);

        if ($request->keyword_ar) {
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $slider->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('slide_create_msg', awtTrans('تم الاضافة بنجاح'));
        return redirect('/admin/sliders');
    }

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

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

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

        $inputs = $request->except('check_vedio_or_image', 'photo', 'type_video', 'video_device', 'video_youtube');

        $slide = Slider::findOrFail($id);

        if ($request->check_vedio_or_image == 'image') {
            $inputs['photo'] = $request->photo;
            $inputs['type'] = 'image';
        } elseif ($request->check_vedio_or_image == 'video' && $request->type_video == 'device') {
            if($request->video_device)
            {
                $fileName = time() . '_' . $request->video_device->getClientOriginalName();
                $inputs['photo'] = $request->file('video_device')->storeAs('sliders', $fileName, 'public');
            } else {
                $inputs['photo'] = $slide->photo;
            }
            $inputs['type'] = 'device';
        } elseif ($request->check_vedio_or_image == 'video' && $request->type_video == 'youtube') {
            $inputs['photo'] = $request->video_youtube;
            $inputs['type'] = 'youtube';
        }

        $slide->update($inputs);

        if ($request->keyword_ar) {
            $slide->keywords()->delete();
            foreach ($request->keyword_ar as $key => $keyword_ar) {
                $slide->keywords()->create([
                    'body_ar' => $keyword_ar,
                    'body_en' => $request->keyword_en[$key],
                ]);
            }
        }

        session()->flash('slide_update_msg', awtTrans('تم التعديل بنجاح'));
        return redirect()->back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $slide = Slider::findOrFail($id);
        $slide->delete();
        return response()->json();
    }


    // bluckDelete
    public function bluckDelete(Request $request)
    {

        $slides = Slider::findOrFail($request->ids);

        foreach ($slides as $slide) {
            $slide->delete();
        } // end foeach

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