Current File : /home/tradevaly/www/app/Http/Controllers/Admin/BusinessController.php
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Item;
use App\Models\Category;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Str;
use DataTables;
class BusinessController extends Controller
{
    public function index(Request $request){
        if ($request->ajax()) {
            $data = Item::where('parent_id', 0)->get();
            return Datatables::of($data)
                ->addIndexColumn()
                ->addColumn('action', function($row){
                    $actionBtn = '<a href="'.route('admin.btype.add',$row->id).'"  class="text-success" title="Add Subcategory" style="color: #fff">
                      <i class="fa fa-plus"></i>
                    </a>  <a href="'.route('admin.btype.edit',$row->id).'"  class="text-info" title="Edit" style="color: #fff">
                      <i class="fas fa-pencil-alt"></i>
                    </a>
                    
                     <a  onclick="return confirm(\'Are you sure?\')" href="'.route('admin.btype.delete',$row->id).'" class="text-danger confirmDelete"><i class="mdi mdi-delete font-size-18"></i></a>';
                    return $actionBtn;
                })
                ->addColumn('statuschange', function($row){
                    if($row->status == 0) { return '<span class="badge bg-danger">Inactive</span>'; } else { return '<span class="badge bg-success">Active</span>'; }
                })
                ->addColumn('parent', function($row){
                    if($row->parent_id != 0) { return $row->parent->name; } else { return 'Main Category'; }
                })
                ->addColumn('subcat', function($row){
                    if(Item::where('parent_id', $row->id)->count() > 0){ return '<a class="badge bg-warning" href="'.route('admin.btype.subcat', $row->id).'">View Subcategories</a>'; } else { return 'No Sub Category'; }
                })
                
                ->rawColumns(['action', 'subcat', 'statuschange'])
                ->make(true);
        }
        return view('backend.pages.btype.index');
    }
    
    
    public function subcat(Request $request, $id){
        $cat = Category::find($id);
        if ($request->ajax()) {
            $data = Category::where('parent_id', $id)->get();
            return Datatables::of($data)
                ->addIndexColumn()
               ->addColumn('action', function($row){
                    $actionBtn = '<a href="'.route('admin.btype.add',$row->id).'"  class="text-success" title="Add Subcategory" style="color: #fff">
                      <i class="fa fa-plus"></i>
                    </a>  <a href="'.route('admin.btype.edit',$row->id).'"  class="text-info" title="Edit" style="color: #fff">
                      <i class="fas fa-pencil-alt"></i>
                    </a>
                    
                     <a  onclick="return confirm(\'Are you sure?\')" href="'.route('admin.btype.delete',$row->id).'" class="text-danger confirmDelete"><i class="mdi mdi-delete font-size-18"></i></a>';
                    return $actionBtn;
                })
                ->addColumn('pic', function($row){
                    $actionBtn = ' <img style="height: 30px; width:30px; border-radius:10%;" src="'.asset('storage/images/admin/category/'.$row->image.'').'" alt="">';
                    return $actionBtn;
                })
                ->addColumn('parent', function($row){
                    if($row->parent_id != 0) { return $row->parent->name; } else { return 'Main Category'; }
                })
                ->addColumn('subcat', function($row){
                    if(Item::where('parent_id', $row->id)->count() > 0){ return '<a  class="btn btn-info" href="'.route('admin.category.subcat', $row->id).'">View Subcategories</a>'; } else { return 'No Sub Category'; }
                })
                ->addColumn('statuschange', function($row){
                    if($row->status == 0) { return '<span class="badge bg-danger">Inactive</span>'; } else { return '<span class="badge bg-success">Active</span>'; }
                })
                ->rawColumns(['action', 'statuschange', 'pic', 'subcat'])
                ->make(true);
        }
        return view('backend.pages.btype.subcat', compact('cat'));
    }
    
    
    
    
    public function updateCategoryStatus(Request $request){
        if($request->ajax()){
            $category = Item::find($request->category_id);
            if($category->status==1){
                $status=0;
            }else{
                $status=1;
            }
            $category->status = $status;
            $category->save();
           return response()->json(['status'=>$status, 'category_id'=>$request->category_id]);
         }
    }


    public function add(Request $request, $id=0){
        
        if($request->isMethod("post")){
             
             $data= new Item;
            
            
            
             $data->name=$request->name;
             $data->parent_id=$id;
             $data->status=1;
             $data->save();
             Toastr::success('Success!','Business Category uploaded!');
             return redirect()->back();
        }
        $categories=Item::where('parent_id', 0)->get();
        return view('backend.pages.btype.add')->with(compact('categories', 'id'));
       
    }

    public function delete($id){
            $work=Item::find($id);
            
            $work->delete();
            Toastr::success('Success!','Category deleted!');
            return redirect()->back();
    }
    public function edit(Request $request, $id){
        $data=Item::find($id);
        if($request->isMethod("post")){
            
            $data->name=$request->name;
            
           
            $data->status=1;
            $data->save();
            Toastr::success('Success!','Category Updated!');
            return redirect()->back();
        }
         $categories= Item::where('parent_id', 0)->get();
        return view('backend.pages.btype.edit')->with(compact('categories','data'));
        
    }
}