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

class BedroomsController extends Controller
{     
    public function index(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
         if($request->ajax()){
             return datatables()->of(Bedroom::select('id', 'bedroom', 'status', 'ordering'))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/bedrooms/'.$row->id.'/edit').'" uk-tooltip="Edit Bedroom"><i class="fa-solid fa-pen-to-square"></i></a>  <a data-id="'.$row->id.'" class="uk-text-danger delete-bedroom" href="javascript:void(0)" uk-tooltip="Delete Bedroom"><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']  = 'Bedrooms'; 
            $this->bladeContent['active'] = 'bedrooms';
            return view('admin.bedrooms.index',$this->bladeContent);
        }
    }
    
    public function create(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']  = 'Add Bedroom';
        $this->bladeContent['active'] = 'bedrooms';
        return view ('admin.bedrooms.create', $this->bladeContent);
    }
    
    public function store(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $bedrooms           = new Bedroom();
        $bedrooms->bedroom = request('bedroom');
        $bedrooms->ordering = request('ordering');
        $bedrooms->save();
        return redirect('admin/bedrooms')->with('success','Bedroom successfully created.');
    }

    public function edit($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $bedrooms                     = Bedroom::select('id', 'bedroom', 'ordering')->where('id', $id)->first();
        $this->bladeContent['title']  = 'Edit Bedroom';
        $this->bladeContent['active'] = 'bedrooms';
        $this->bladeContent['data']   =  $bedrooms;
        return view('admin.bedrooms.edit', $this->bladeContent);
    }

    public function update(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $bathrooms           = Bedroom::select('id', 'bedroom', 'ordering')->where('id', $id)->first();
        $bathrooms->bedroom  = request('bedroom');
        $bathrooms->ordering = request('ordering');
        $bathrooms->save();
        return redirect('admin/bedrooms')->with('success','Bedroom successfully updated.');
    }

    public function destroy($id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        Bedroom::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';
        }
       Bedroom::where('id', $id)->update(array('status' => $value));
        return response()->json([
        'success' => 1,
        'message' => 'status changed.'
      ]);
    } 
}