<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Description;
use App\Models\User;
use Illuminate\Http\Request;
use Auth;
use Illuminate\Database\Eloquent;

use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function member_all_categories()
    {
        $categories = Category::all();

        return view('categories.index', compact('categories'));
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function member_save_category(Request $request)
    {


        $this->validate($request, [
            'name'     => 'required',
            'description'  => 'required',
            'image' => 'required',
            'meta_title' => 'required',
            'meta_description' => 'required',
            'keywords'   => 'required'
        ]);


        If($request->hasFile('image'))
        {

            $file = $request->file('image');

            $destinationPath = 'uploads/bigImage';
            $filename = time(). '-'.$file->getClientOriginalName();
            $thumbPath = 'uploads/thumbImage/'.$filename;
            Image ::make($file->getRealPath())->resize(300, null, function ($constraint) {
                $constraint->aspectRatio();
            })->save($thumbPath);

            $file->move($destinationPath, $filename);

            $name = $request->name;
            $description = $request->description;
            $created_by = Auth::user()->id;
            $updated_by = Auth::user()->id;
            $meta_title = $request->meta_title;
            $meta_description = $request->meta_description;
            $keywords = $request->keywords;
            $image = $filename;

//        $desc = new Description([
//            'title' => $name,
//            'body' => $description,
//            'meta_title' => $meta_title,
//            'meta_description' => $meta_description,
//            'meta_keywords' => $keywords,
//            'created_by'=> Auth::user()->id,
//            'updated_by' => Auth::user()->id
//
//        ]);

//            $desc->save();

        $category = new Category([
            'name' => $name,
            'description'=> $description,
//            'meta_title' => $meta_title,
//            'meta_description' => $meta_description,
            'keywords' => $keywords,
            'created_by' => $created_by,
            'updated_by' => $updated_by,
            'image' => $image,
            'status' => 0
        ]);

//        dd($category);

        $category->save();

        return redirect()->back()->with('success', 'Category ' .$name. ' was Created Successfully');
        }


       return null;
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $category = Category::where('id', $id )->get();

        return view('categories.edit', compact('category'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $category = Category::where('id', $id)->get();

        if (sizeof($category) > 0)
        {

            $category[0]['name'] = $request->catname;

            $category['0']->save();
        }

        return redirect()->back()->with('success', 'Category Updated Successfully');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $flight = Category::find($id);

        $flight->delete();

        return redirect()->back()->with('success', 'Category Deleted Successfully');
    }
}
