<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Post;
use App\Aws\FileSystem;
use App\BlogCategories;
use App\Media;

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

        $this->breadcrumbs = $this->getBreadcrumbs();
        $this->breadcrumbs[1]['link'] = "admin/posts";
        $this->breadcrumbs[1]['name'] = "Post";
    }
    
     public function index() {

        $model = Post::where('type_id',1)->get();

        $data["pageHeader"] = false;
        $data["model"] = $model;
        $data["title"] = "Post";
        $data["SubTitle"] = "List";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/posts/index', $data);
    }
    
    public function create() {
        $data["pageHeader"] = false;
        $data["model"] = new Post();
        $data["date"]=date("j F, Y");
        $data["title"] = "Post";
        $data["subTitle"] = "Create";
        $data["categories"]=BlogCategories::pluck('title','id');
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Create";

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/posts/create', $data);
    }
    
    public function save(Request $request) {

         $input = $request->all();

        $rules = [
            'title' => 'required|max:255',
            //'facilities' => 'required',
//            'image' => 'mimes:jpeg,png',
        ];
        
       
        

        $message = [];
        $validator = Validator::make($input, $rules, $message);
       
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            $input["publish_date"]=str_replace(","," ",$input["publish_date"]);
            $input["publish_date"] = date("Y-m-d", strtotime($input["publish_date"]));
            $input['type_id']=1;

            unset($input["_token"]);
            if (empty($input["key"])) {
                $input["key"] = slugify($input["title"]);
            } else {
                $input["key"] = slugify($input["key"]);
            }
            
           $input["created_at"] = date("Y-m-d H:i:s");


            $model = new Post();
             if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }
            $id = $model->insertGetId($input);


//            if ($request->hasFile('image')) {
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/posts/', 'uploads/posts/thumbnails/', 1600, 900);
//                Post::where('id', $id)->update(array("image" => $input['image']));
//            }

            \Session::flash('success', 'Post added successfully!');
            return redirect('admin/posts');
        }
    }
    
    public function show($id) {

           $q = new Post();
           $data['model'] = $q->find($id);
           $data["title"] = "Post";

           $this->breadcrumbs[2]['link'] = "#_";
           $this->breadcrumbs[2]['name'] = $data['model']->title;

           $data['breadcrumbs'] = $this->breadcrumbs;

           return view('admin/posts/show', $data);
       }
       
    public function delete($id) {


        Post::where('id', $id)->delete();
        \Session::flash('error', 'Post deleted successfully!');
        return redirect('admin/posts');
    }
    
    public function edit($id) {
        $data["model"] = Post::find($id);
        $data["pageHeader"] = false;
        $data["title"] = "Post";
        $data["subTitle"] = "Edit";
        $data["date"]=date("j F, Y");
        $data["categories"]=BlogCategories::pluck('title','id');
        $data["id"] = $id;
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/posts/edit', $data);
    }
    
    public function update($id, Request $request) {

        $input = $request->all();

        $rules = [
            'title' => 'required|max:255',
            //'facilities' => 'required',
            
        ];
       $message = [];
        $validator = Validator::make($input, $rules, $message);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator->errors())->withInput();
        } else {
            
            $input["publish_date"]=str_replace(","," ",$input["publish_date"]);
            $input["publish_date"] = date("Y-m-d", strtotime($input["publish_date"]));
            
            unset($input["_token"]);
            if (empty($input["key"])) {
                $input["key"] = slugify($input["title"]);
            } else {
                $input["key"] = slugify($input["key"]);
            }
            
//             if ($request->hasFile('image')) {
//
//                $file = $request->file('image');
//                $fileName = rand(111, 999) . time();
//                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/posts/', 'uploads/posts/thumbnails/', 1600, 900);
//            }
            if (!empty($request->image) && $request->image != null) {
                $media_id = Media::getMediaID($request->image);
                $input["image"] = $request->image;
                $input["image_media_id"] = $media_id;
            }

            $input["updated_at"] = date("Y-m-d H:i:s");
            Post::where('id', $id)->update($input);
            \Session::flash('success', 'Post updated successfully!');
            return redirect('admin/posts');
        }
    }
}
