<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Models\Video;
use App\Models\systemAdministration\category;
use App\Models\systemAdministration\language;
use Illuminate\Http\Request;
use Validator;


class videoController extends Controller
{

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $videos = Video::orderBy("id", "desc")->paginate(12);
        return view("back-end.videos.index", compact("videos"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $language = language::all();
        $categories = category::all();
        return view("back-end.videos.create",compact('language','categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        $video = new Video();
        $video->title = $request->title;
        $video->language_id = $request->language;
        $video->category_id = $request->category;
        $video->body = $request->body;
        $video->url = $request->video_url;
         $is_saved = $video->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('video.index');
        }
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $video = Video::find($id);
        $language = language::all();
        $categories = category::all();
        return view("back-end.videos.edit", compact("video","language",'categories'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $video = Video::find($id);
        $video->title = $request->title;
        $video->language_id = $request->language;
        $video->category_id = $request->category;
        $video->body = $request->body;
        $video->url = $request->video_url;
        $is_saved = $video->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('video.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Post  $post
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $video = Video::find($id);
        $is_saved = $video->delete();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->back();
        }
    }

}
