<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;
use Cviebrock\EloquentSluggable\Sluggable;
use App\Forum;
use App\Komentar;
use App\Eoffice;
use App\Laravel;
use App\Backendp;
use App\Katartikel;
use App\User;
use Carbon\Carbon;
use OjiSatriani\Fungsi;
use Illuminate\Support\Str;
use Validator;

class forumController extends Controller
{
    public function index(Request $request)
    {
        $forum = Forum::all();
        $eoffice = Eoffice::all();
        $laravel = Laravel::all();
        $backendp = Backendp::all();
        $user = User::all();
        $katartikel = Katartikel::whereIn('id',[3])->get();

        $user = User::all();
        $request->request->add(['user_id' => auth()->user()->nama]);

        return view('backend.forum.index', [ 'eoffice' => $eoffice, 'laravel' => $laravel, 'backendp' => $backendp, 'katartikel' => $katartikel ]);
    }

    public function data_detail(Request $request, $id)
    {
      //  if ($request->ajax()) {
        //    $users = Aksesgrup::find($id)->user;
        $eoffice = Eoffice::all();
        $laravel = Laravel::all();
        $backendp = Backendp::all();
        $user = User::all();
        $katartikel = Katartikel::whereIn('id',[3])->get();

        $user = User::all();
        $request->request->add(['user_id' => auth()->user()->nama]);

        return view('backend.forum.detail', [ 'eoffice' => $eoffice, 'laravel' => $laravel, 'backendp' => $backendp, 'katartikel' => $katartikel ]);
    

            //return view('backend.forum.view');
            
     //   } else {
     //       exit("Not an AJAX request -_-");
       // }
    }


    // public function create(Request $request){
    //     $request->request->add(['user_id' => auth()->user()->id]);
    //     $forum = Forum::all($request->all());
    //     return view('backend.forum.view');
        
    // }

     /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function create()
    {
        return view('backend.forum.tambah');
    }


    // public function create(Reuest $request){
    //     $request->request->add('user_id' => auth()->user()->id]);
    //     $forum = Forum::create($request->all());
    //     return redirect()->back()->with('sukses','Berhasil');
    // }

    // public function view(Forum $forum){
    //     return view('forum.view', compact(['forum']));
    // }


     /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'judul' => 'required',
            'konten' => 'konten'
             
          ]);
        if ($validator->fails()) {
            $respon = array('status'=>false, 'pesan' => $validator->messages());
        } else {
                //check berkas
                $path       = NULL;
                $namafile   = NULL;
                if($request->hasFile('lampiran')){
                    $path				= 'app/formal/'.date('Y').'/'.date('m').'/'.date('d').'/';
                    $destinationPath	= storage_path($path);
                    $namafile	        = with(new Fungsi)->namaBaru($request->file('lampiran')->getClientOriginalName(), Str::slug($request->nama.'-'.Carbon::now()));
                    $request->file('lampiran')->move($destinationPath, $namafile);
                }
                $request->request->add([
                    'file'  =>  [    
                                    'path'      => $path,
                                    'nama'      => $namafile,
                                ],           
                ]);
                // akhir check berkas

            if (Formal::create($request->all())) {
                $respon = array('status'=>true, 'pesan' => ['msg' => 'Data berhasil disimpan']);
            } else {
                $respon = array('status'=>false, 'pesan' => ['msg' => 'Data gagal disimpan']);
            }
        }
        return response()->json($respon);
    }

    //  public function view(Request $request, $id){
    //  $request->request->add(['user_id' => auth()->user()->id]);

    //    // $forum = Forum::all();
    //      $laravel = Laravel::all();
    //       //$frmlaravel = Laravel::find('id');

    //      return view('backend.forum.view', compact('laravel', $laravel));
    //   //  return View::make('backend.forum.view', ['laravels' => $laravels]);
    // }

     public function show()
     {
         $laravel = Laravel::all();
         return view('backend.forum.view', compact('laravel', $laravel));
     }
 

    //  public function show($id)
    // {
    //     $aksesgrup = Aksesgrup::find($id);
    //     return view('backend.aksesmenu.index', compact('aksesgrup'));
    // }
    
    public function postkomentar(Request $request)
    {
        
         $request->request->add(['user_id' => auth()->user()->id]);
        dd($request);
        // $komentar = Komentar::create();
        
        // return view('backend.forum.view', compact('komentar'));
    }



}
