<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use App\Post;
use Illuminate\Support\Facades\Validator;
use App\Aws\FileSystem;
class PressRelaseConttroller extends AdminController
{
    
     public function __construct() {
        $this->middleware('auth');

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

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

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

        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/press/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']=2;

            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();
            $id = $model->insertGetId($input);
            $folder = "uploads/";
            $path = public_path($folder);
             createfolder($path);
            $folder2="uploads/press/";
            $path2=public_path($folder2);
            createthumb($path2);
            if ($request->hasFile('image')) {

                $file = $request->file('image');
                $fileName = rand(111, 999) . time();
                  
                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/press/', 'uploads/press/thumbnails/', 875, 576);
                Post::where('id', $id)->update(array("image" => $input['image']));
            }

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

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

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

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

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


        Post::where('id', $id)->delete();
        \Session::flash('error', 'Press Relase deleted successfully!');
        return redirect('admin/press');
    }
    
      public function edit($id) {
        $data["model"] = Post::find($id);
        $data["pageHeader"] = false;
        $data["title"] = "Press Relase";
        $data["subTitle"] = "Edit";
        $data["date"]=date("j F, Y");
        $data["id"] = $id;
        $this->breadcrumbs[2]['link'] = "#_";
        $this->breadcrumbs[2]['name'] = "Edit";
        $data['breadcrumbs'] = $this->breadcrumbs;
        return view('admin/press/edit', $data);
    }
    
      public function update($id, 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"]));
          
            if (!isset($input["feature"])) {
                $input["feature"] = 0;
            }
           
            unset($input["_token"]);
            if (empty($input["key"])) {
                $input["key"] = slugify($input["title"]);
            } else {
                $input["key"] = slugify($input["key"]);
            }
            
            if (!isset($input["status"])) {
                $input["status"] = 0;
            }
            $folder = "uploads/";
            $path = public_path($folder);
            $folder2="uploads/press/";
            $path2=public_path($folder2);
            createfolder($path);
            createthumb($path2);
             if ($request->hasFile('image')) {

                $file = $request->file('image');
                $fileName = rand(111, 999) . time();
                $input['image'] = FileSystem::storeImage($fileName, $file, 'uploads/press/', 'uploads/press/thumbnails/', 875, 576);
            }


            $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/press');
        }
    }
}
