<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\VideoRequest;
use App\Models\video;
use Illuminate\Http\Request;

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //

        return view('admin.videos.create');
    }

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

        $inputs = $request->all();
        $video = Video::create($inputs);

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

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

    /**
     * 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)
    {
        //
        $video = Video::findOrFail($id);
        return view('admin.videos.edit', compact('video'));
    }

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

        $inputs = $request->all();

        $video = Video::findOrFail($id);

        $video->update($inputs);

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

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

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

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

        $videos = Video::findOrFail($request->ids);

        foreach ($videos as $video) {
            $video->delete();
        } // end foeach

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