<?php

namespace App\Http\Controllers;

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

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

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

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

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

    public function delete($id)
    {
        if ($id) {
            $category  = SubCategory::find($id);
            if ($category->delete()) {
                return redirect()->route('admin.sub.category.view')->with('error', 'Sub 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);
        }
        
        $slug = SubCategory::createSlug($request->name);

        $category                = new SubCategory();
        $category->name          = $request->name;
        $category->cat_id        = $request->category_id;
        $category->slug          = $slug;
        $category->status        = $request->status;
        if ($category->save()) {

            return redirect()->route('admin.sub.category.view')->with('success', 'Sub 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;
        }
        
        $slug = SubCategory::createSlug($request->name);

        $category                = SubCategory::find($id);
        $category->name          = $request->name;
        $category->cat_id        = $request->category_id;
        $category->slug          = $slug;
        $category->status        = $request->status;

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