<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\Package;
use App\Models\User;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(session()->has('loginId')){
            $categories = Category::orderBy('id','desc')->get();

            return view('admin.add-category',compact('categories'));
        }
        else{
             return redirect()->route('adminlogin');
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
         $category = new Category();
         $category->name = $request->name;
         $category->save();

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

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function change_status($type,$id,$status){
          
        if($type == 'package'){
            $package = Package::where('id',$id)->first();
            $package->status = $status;
            $package->save();
        }
        else if($type == 'category'){
            $category = Category::where('id',$id)->first();
            $category->status = $status;
            $category->save();
        }
        else if($type == 'user'){
            $user = User::where('id',$id)->first();
            $user->is_active = $status;
            $user->save();
        }

        return back();
    }
}
