<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UsefulLink;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;
use Input;
use File;
use Intervention\Image\Facades\Image;
use DB;
use App\FileData;

class UsefulLinkController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        return view('Admin.UsefulLink');
    }

    /**
     * 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:useful_links',
            'image' => 'required',
            'links' => 'required',
          
        ]);
        $save= new UsefulLink;
        $save->title=$request->title;
        $save->links=$request->links;
        $save->token=AdminlogController::getToken('UsefulLink');
        $save->save();
 $files = Input::file('image');
           $rules = array('files' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
        $validator = Validator::make(array('files' => $files), $rules);
        if ($validator->passes()) {
            $filename = DB::table('tokens')->max('tokenid') . '.png';

          $location = public_path('img/UsefulLink/' . $filename);
           Image::make($files)->save($location);

            $save = new FileData;

         $save->token=DB::table('tokens')->max('tokenid');
         $save->file_name=$filename;
         $save->token_name="UsefulLink";


        $save->save();

        }
    
       return redirect('admin_useful_link')->with('info', 'Datas 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=UsefulLink::where('id',$id)->first();
    

        $data=array(

        'title'=>$request->title,
        'links'=>$request->links,
       
);
        UsefulLink::where('id',$id)->update($data);
        return redirect('admin_useful_link')->with('info', 'Data updated Successfully!');
    }

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