<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function create()
    {
        return view('admin.categories.add');
    }

    public function view()
    {
        $category = Category::get();
        return view('admin.categories.view', compact('category'));
    }

    public function edit($id)
    {
        $category = Category::where('id', $id)->first();
        return view('admin.categories.edit', compact('category'));
    }

    public function delete($id)
    {
        if ($id) {
            $category  = Category::find($id);
            if ($category->delete()) {
                return redirect()->route('admin.category.view')->with('error', 'Category Deleted Successfully');
            }
            else{
                return redirect()->back()->with('error', 'Something Wrong');
            }
        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }
    }

    public function store(Request $request)
    {
        // dd($request->all());
        $request->validate([
            'name'          => 'required|max:255'
        ]);

        $filename = "";
        if ($request->hasFile('category_mage')) {
            $image = $request->file('category_mage');
            $filename = time().'.'.$image->extension();
            $destinationPath = public_path('/category');
            $image->move($destinationPath, $filename);
        }
        
        $filename1 = "";
        if ($request->hasFile('main_image')) {
            $image = $request->file('main_image');
            $filename1 = time().'main_image.'.$image->extension();
            $destinationPath = public_path('/main_image');
            $image->move($destinationPath, $filename1);
        }
        
        $slug = Category::createSlug($request->name);

        $category                = new Category();
        $category->name          = $request->name;
        $category->image         = $slug;
        $category->banner_image  = $filename;
        $category->main_image    = $filename1;
        $category->tilte_one     = $request->tilte_one;
        $category->tilte_two     = $request->tilte_two;
        $category->tilte_three   = $request->tilte_three;
        $category->front_display = $request->featured;
        $category->status        = $request->status;
        if ($category->save()) {

            return redirect()->route('admin.category.view')->with('success', 'Category Created Successfully');

        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }
    }

    public function update(Request $request, $id)
    {
       
        // dd($request->all());
        $request->validate([
            'name'          => 'required|max:255'
        ]);

        $filename = "";
        if ($request->hasFile('category_mage')) {
            $image = $request->file('category_mage');
            $filename = time().'.'.$image->extension();
            $destinationPath = public_path('/category');
            $image->move($destinationPath, $filename);
        }
        elseif ($request->old_image) {
            $filename = $request->old_image;
        }
        
        $filename1 = "";
        if ($request->main_image) {
            $image = $request->file('main_image');
            $filename1 = time().'main_image.'.$image->extension();
            $destinationPath = public_path('/main_image');
            $image->move($destinationPath, $filename1);
        }
        elseif ($request->old_main_image) {
            $filename1 = $request->old_main_image;
        }
        // dd($filename);
        $slug = Category::createSlug($request->name);

        $category                = Category::find($id);
        $category->name          = $request->name;
        $category->image         = $slug;
        $category->banner_image  = $filename;
        $category->main_image    = $filename1;
        $category->tilte_one     = $request->tilte_one;
        $category->tilte_two     = $request->tilte_two;
        $category->tilte_three   = $request->tilte_three;
        $category->front_display = $request->featured;
        $category->status        = $request->status;

        if ($category->save()) {
            return redirect()->route('admin.category.view')->with('warning', 'Category Edited Successfully');
        }
        else{
            return redirect()->back()->with('error', 'Something Wrong');
        }
    }

}
