<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Event;
use Input;
use File;
use App\FileData;
class EventController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        return view('Admin.Event');
    }

    public function getdelimage(Request $req)
    {
        $file= $req->index;
        $filename = public_path().'/img/Event/'.$file;
        File::delete($filename);
        FileData::where('file_name','=',$file)->delete();
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'title'=>'unique:events',
            'image' => 'required',
             'description' => 'required',
]);
                 // get the files
            $files = Input::file('image');
            // counting of uploaded images
            $file_count = count($files);
  // start count how many uploaded
            $i = 0;

        $save =new Event;
        $save->title=$request->title;
        $save->title_nepali=$request->title_nepali;
        $save->date=$request->date;
        $save->image_number=$file_count;
        $save->location=$request->location;
        $save->description=$request->description;
        $save->description_nepali=$request->description_nepali;
        $save->token=AdminlogController::getToken('Event');
        $save->save();
        AdminlogController::FileSaver( $request,'Event',1920,1285);

           
        return redirect('admin_event')->with('info', 'Data Saved Successfully!');
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($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)
    {
        $last=Event::where('id',$id)->first();
               // get the files
        $files = Input::file('image');
            // counting of uploaded images
        $file_count = count($files);
         $new_count=0;
         $new_count=AdminlogController::FileUpdater($request,'Event',1920,1285,$last->image_number,$last->token);

        

        $data=array(

        'title'=>$request->title,
        'title_nepali'=>$request->title_nepali,
        'date'=>$request->date,
        'location'=>$request->location,
        'description'=>$request->description,
        'description_nepali'=>$request->description_nepali,
        'image_number'=>$last->image_number+$file_count,
        
        
          
);


        Event::where('id',$id)->update($data);
        return redirect('admin_event')->with('info', 'Data updated Successfully!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
