<?php



namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Post;
use App\User;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function __construct()
    {
        return $this->middleware('auth');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function postindex(Request $request)
    {
        $posts = Post::all();
        $user_id = $request->User()["id"];
        return view('post.view', compact('posts','user_id'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $post =  new Post;
        // $post->title = $request->get('title');
        // $post->body = $request->get('body');

        // $post->image = $request->file('image');
        // $name = $post->getClientOriginalName();
        // $name = time().$name;
        // $path = 'public/images';

        if($post = $request->file('image')){
            $name = $post->getClientOriginalName();

            if($post->move('image',$name)){
                $user = new Post(
                [
                    'title' => $request->get('title'),
                    'body' => $request->get('body'),
                    'image' => $request->get('image')
                ]
                );
                $user->image = $name;
                $user->user()->associate($request->user());
                $user->save();
                return redirect('posts')->with('success','บันทึกข้อมูลเรียบร้อย');
                // return redirect('posts')->route('post.comments')->with('success','บันทึกข้อมูลเรียบร้อย');
                // $post = new Post();
                // $post->title = $request->get('title');
                // $post->body = $request->get('body');
                // $post->image = $name;
                // $post->save();
                // return redirect('posts');
                // return back();
            };

        };
        return back()->with('success','บันทึกข้อมูลเรียบร้อย');
        // $post->image = $request->get('image');

        // $post->save();

        // return redirect('posts');
    }

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

      $post = Post::find($id);
      $user_id = $request->User()["id"];
      return view('post.show', compact('post','user_id'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request ,$id)
    {
      $post = Post::find($id);
      $user_id = $request->User()["id"];
      return view('update.edit', compact('post','id','user_id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
      $this->validate($request,
      ['title'=>'required',
      'body'=>'required',
      'image'=>'required']);
      $admin = Post::find($id);
      $posts = Post::all();
      $user_id = $request->User()["id"];
      $admin->title = $request->get('title');
      $admin->body = $request->get('body');
      $admin->image = $request->get('image');
      $admin->save();
      return redirect('posts');
      }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $post = Post::find($id);
        $post->delete();
        return redirect('posts');
    }
}
