<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Models\gallery;
use Illuminate\Http\Request;
use Validator;

class GalleryController extends Controller {

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

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

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        return view("back-end.gallery.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
        $data = Validator::make($request->all(), [
                    "title" => "required|max:255"
                ])->validate();
        $isSlider = 0;
        if ($request->isSlider == "on") {
            $isSlider = 1;
        }
        $gallery = new gallery();
        $gallery->user_id = Auth::id();
        $gallery->title = $request->title;
        $gallery->status = $isSlider;
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'gallery_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('gallery_images/' . $date, $filename, 'public');
        } else {
            $path = "image/noimage.jpg";
        }
        $gallery->image = $path;
        $is_saved = $gallery->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('gallery.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) {
        $gallery = gallery::find($id);
        return view("back-end.gallery.edit", compact("gallery"));
    }

    /**
     * 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) {
        $isSlider = 0;
        if ($request->isSlider == "on") {
            $isSlider = 1;
        }
        $gallery = gallery::find($id);
        $gallery->user_id = Auth::id();
        $gallery->title = $request->title;
        $gallery->status = $isSlider;
       
        if (request()->hasFile('image')) {
            $date = date('Y-M');
            $filename = 'gallery_images-' . time() . '-' . request()->file('image')->getClientOriginalName();
            $path = request()->file('image')->storeAs('gallery_images/' . $date, $filename, 'public');
        } else {
            $path = $gallery->image;
        }
        $gallery->image=$path;
        $is_saved = $gallery->save();
        if ($is_saved) {
            session()->flash("msg", "Success!");
            return redirect()->route('gallery.index');
        }
    }

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

}
