<?php
namespace App\Http\Controllers\Admin;
use App\Models\Amenity;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AmenitiesController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
         if($request->ajax()){
             return datatables()->of(Amenity::select('id', 'name', 'status', 'ordering'))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/amenities/'.$row->id.'/edit').'" uk-tooltip="Edit Amenity"><i class="fa-solid fa-pen-to-square"></i></a>  <a data-id="'.$row->id.'" class="uk-text-danger delete-amenity" href="javascript:void(0)" uk-tooltip="Delete Amenity"><i class="fa-solid fa-trash"></i></a></span>';
            })
            ->addColumn('status', function($row){
                if($row->status == '1'){ 
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-success active">Active</a>';
                }else{
                    $status = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="uk-label uk-label-danger inactive">Inactive</a>';
                }
                return $status;
            })
            ->rawColumns(['actions','status'])->toJson();
        }
        else{
            $this->bladeContent['title']  = 'Amenities Wishlist'; 
            $this->bladeContent['active'] = 'amenities';
            return view('admin.amenities.index',$this->bladeContent);
        }
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']  = 'Add Amenities Wishlist';
        $this->bladeContent['active'] = 'amenities';
        return view ('admin.amenities.create', $this->bladeContent);
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
     public function store(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $amenities       = new Amenity();
        $amenities->name = request('name');
        $amenities->ordering = request('ordering');
        $amenities->save();
        return redirect('admin/amenities')->with('success','Amenities wishlist successfully created.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        // $request->session()->put('name',$request->input('name'));
        // return view('task')->with('name',$request->session()->get('name'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $amenities                = Amenity::select('id', 'name', 'ordering')->where('id', $id)->first();
        $this->bladeContent['title']  = 'Edit Amenities Wishlist';
        $this->bladeContent['active'] = 'amenities';
        $this->bladeContent['data']   =  $amenities;
        return view('admin.amenities.edit', $this->bladeContent);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $amenities           = Amenity::select('id', 'name', 'ordering')->where('id', $id)->first();
        $amenities->name     = request('name');
        $amenities->ordering = request('ordering');
        $amenities->save();
        return redirect('admin/amenities')->with('success','Amenities wishlist successfully updated.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        Amenity::where('id',$id)->delete();
        return response()->json([
            'success' => 1,
            'message' => 'Data successfully deleted.'
        ]);
    } 
    
    public function status(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $id = request('id');
        if($request->type == 'active'){
            $value = '1';
        }else{
            $value = '0';
        }
       Amenity::where('id', $id)->update(array('status' => $value));
        return response()->json([
        'success' => 1,
        'message' => 'status changed.'
      ]);
    } 
}