<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helper\admin\ImageUpload;
use App\Models\ProductCategory;
use App\Models\Subcat;

class ProductCategoryController extends Controller
{
    //

    public function index(){
        $allcat = ProductCategory::all();
        return view('admin.pages.productcategory.index',compact('allcat'));
    }

    public function subindext(){
        $allsubcat = Subcat::all();
        return view('admin.pages.productsubcategory.index',compact('allsubcat'));
    }

    public function add(){

        return view('admin.pages.productcategory.add');
    }

    public function addsubcat(){
        $maincat = ProductCategory::all();
        return view('admin.pages.productsubcategory.add',compact('maincat'));
    }

    public function save(Request $request){

        {


            $request->validate([
                  'image' => 'required',
                'name' => 'required',
           ]);
    
            if ($request->hasFile('image')){
                $file = $request->file('image');
                $path = "upload/images/categoryimage/";
                $filename = ImageUpload::saveImage($path, $file);
                $image_name = $path.$filename;
            }else{
                $image_name = '';
            }
    
            $banners = new ProductCategory();
            $banners->name = $request->get('name');
            $banners->image = $image_name;
            $banners->save();
    
    
            return redirect()->route('admin::category')->with('success','banner Add Successfully');
    
        }

    }

    public function subcat_save(Request $request){

        {
            $request->validate([
                'maincatid' => 'required',
                'subcatname' => 'required',
            ],

            [
                'maincatid.required' => 'Please Select Category.',
                'subcatname.required' => 'Please Select Subcategory.',
                
            ]);
    
            $banners = new Subcat();
            $banners->maincatid = $request->get('maincatid');
            $banners->subcatname = $request->get('subcatname');
            $banners->save();
            return redirect()->route('admin::sub_category')->with('success','Sub Category Added Successfully');
    
        }

    }


    public function edit($id){
        $categories=ProductCategory::find($id);
            return view('admin.pages.productcategory.edit',['categories'=>$categories]);
        }

    public function subcatedit($id){
        $subcategories=Subcat::find($id);
        $categories = ProductCategory::all();
        return view('admin.pages.productsubcategory.edit',['subcategories'=>$subcategories,'categories' => $categories]);
    }    

        public function update(Request $request, $id){

            $request->validate([

                'name' => 'required',
            ]);
    
            $category = ProductCategory::where('id',$id)->first();
            $old = $category->image;
            if ($request->hasFile('image')){
                $file = $request->file('image');
                $path = "upload/images/categoryimage/";
                $filename = ImageUpload::updateImage($old, $path, $file);
                $image_name = $path.$filename;
            }else{
                $image_name = $old;
            }
    
            //$banners =Banner::find($id);
            $category->name = $request->get('name');
            $category->image = $image_name;
            $category->save();
    
    
            return redirect()->route('admin::category')->with('success','Category Edited Successfully');

        }

        public function delete($id)
{
    $data = ProductCategory::find($id);
    
    if ($data) {
        $oldImage = $data->image;
        if (!empty($oldImage)) {
            @unlink(public_path() . '/' . $oldImage);
        }
        $data->delete();
        return response()->json(['status' => 200]);
    } else {
        return response()->json(['status' => 400]);
    }
}


}   
