<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\cabe;
use App\Http\Requests;
use App\Http\Requests\cabe\StoreRequest;
use App\Http\Requests\cabe\UpdateRequest;
use Illuminate\Support\Facades\Input;
use Image;
class cabecontroller extends Controller
{
    public function index()
    {
        $cabes = cabe::all();
        return view('Admin.MonitoringCabe.monitoringcabe', compact('cabes'));
    }

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



    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreRequest $request)
    {
        $cabes = new cabe();

        $cabes->nama = $request->nama;
        $cabes->family = $request->family;
        $cabes->tanggal_tanam = $request->tanggal_tanam;
        $cabes->usia = $request->usia;
        $cabes->hama = $request->hama;
        $cabes->syarat_tumbuh = $request->syarat_tumbuh;
        $cabes->pemeliharaan = $request->pemeliharaan;

        $file = $request->file('foto');
        $fileName = $file->getClientOriginalName();
        $request->file('foto')->move("image/",$fileName);
        $cabes->foto = $fileName;
//
//        $foto = Input::file('image');
//        var_dump($foto);
//        Input::file('image')->getClientOriginalExtension();
//        $filename = date('Y-m-d-H:i:s')."-".$foto->getClientOriginalName();
//        Image::make($foto->getRealPath())->resize(468, 249)->save('public/image/'. $filename);
//        $cabes->foto = 'image/ '. $filename;
//
//        $filename = $foto[0]->getClientOriginalName();
//        print_($filename);


//        if ($request->hasFile('files_field')) {
//            $file= $request->Input('files_field');
//
//            $destination_path='image/';
//
//            $Orname = $file->getClientOriginalName();
//
//            $filename= str_random(6).'_'.$Orname;
//
//            $file->move($destination_path,$filename);
//
//            $cabes->foto = $destination_path.$filename;
//        }

        $cabes->save();
        return redirect()->route('cabe.index')->with('alert-success', 'Data Berhasil Disimpan.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $cabes = cabe::findOrFail($id);
        return view('Admin.MonitoringCabe.lihat', compact('cabes'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cabes = cabe::findOrFail($id);
        return view('Admin.MonitoringCabe.edit', compact('cabes'));
    }



    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateRequest $request, $id)
    {
        $cabes = cabe::findOrFail($id);
        $cabes->foto = $request->foto;
        $cabes->nama = $request->nama;
        $cabes->family = $request->family;
        $cabes->tanggal_tanam = $request->tanggal_tanam;
        $cabes->usia = $request->usia;
        $cabes->hama = $request->hama;
        $cabes->syarat_tumbuh = $request->syarat_tumbuh;
        $cabes->pemeliharaan = $request->pemeliharaan;
        $cabes->save();
        return redirect()->route('cabe.index')->with('alert-success', 'Data Berhasil Diubah.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $cabes = cabe::findOrFail($id);
        $cabes->delete();
        return redirect()->route('cabe.index')->with('alert-success', 'Data Berhasil Dihapus.');
    }
}