<?php

namespace App\Http\Controllers;
use App\Models\modelRooms;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class RoomsController extends Controller
{
      
    public function Index()
    {
        
      
        $rooms = modelRooms::selectrooms(Session::get('PropertyCode'));
        $floors = modelRooms::selectfloors(Session::get('PropertyCode'));
        $tenants = modelRooms::selectTenant(Session::get('PropertyCode'));
        
        return view('/Rooms', compact('rooms','floors','tenants'));
    }

    public function addrooms(Request $request)
    {
       // Validate the request data
            $request->validate([
                'floor' => 'required|string',
                 'room' => 'required|string',
                 'space' => 'required|string',
                   'tenant' => 'required|string'
            ]);
    
            $floor = $request->input('floor');
            $room = $request->input('room');
            $tenant = $request->input('tenant');
            $space = $request->input('space');
    

            try
            {


            $exists = modelRooms::selectroomsName(Session::get('PropertyCode'),$room);

            if ($exists) {
             
                return redirect()->route('rooms')->withErrors(['error' => 'Room already exists.']);
            }

            $result  = modelRooms::saveRoom(Session::get('PropertyCode'),$floor,$room, $space,$tenant);
            
            if ($result) 
            {
                 return redirect()->route('rooms')->with('success', 'Room saved successfully.');
           } 
            else 
            {
                return redirect()->route('rooms')->withErrors(['error' => 'Failed to save Room.']);
            }

        } catch (\Exception $e) {
            
            return redirect()->route('rooms')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
        }

        }
    

        public function editrooms(Request $request)
        {
            // Validate the request data
            $request->validate([
                'floor' => 'required|string',
                 'room' => 'required|string',
                 'space' => 'required|string',
                 'id' => 'required|string',
                   'tenant' => 'required|string'
            ]);
    
            $floor = $request->input('floor');
            $room = $request->input('room');
            $tenant = $request->input('tenant');
            $space = $request->input('space');
    
            $id = $request->input('id');
        
    
                try
                {
    
    
                $exists = DB::table('rooms')->where('ID', $id)->exists();
    
                if ($exists) {

                    $result = modelRooms::editRoom(Session::get('PropertyCode'),$id,$floor,$room, $space,$tenant);
                
                    if ($result) 
                    {
                         return redirect()->route('rooms')->with('success', 'Room Updated Successfully.');
                   } 
                    else 
                    {
                        return redirect()->route('rooms')->with(['success' => 'Nothing to Update.']);
                    }
                }
                else 
                {
                    return redirect()->route('rooms')->withErrors(['error' => 'Room Not Found.']);
                }
    
                
    
            } catch (\Exception $e) {
                
                return redirect()->route('rooms')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }
    
            }

            public function deleterooms($id)
            {
            
        
                    try
                    {
        
        
                    $exists = DB::table('rooms')->where('ID', $id)->exists();
        
                    if ($exists) {

                        $result = $rooms = modelRooms::deleterooms(Session::get('PropertyCode'),$id);
                    
                        if ($result) 
                        {
                             return redirect()->route('rooms')->with('success', 'Room Removed Successfully.');
                       } 
                        else 
                        {
                            return redirect()->route('rooms')->withErrors(['error' => 'Failed to Remove Room.']);
                        }
                    }
                    else 
                    {
                        return redirect()->route('rooms')->withErrors(['error' => 'Room Not Found.']);
                    }
        
                    
        
                } catch (\Exception $e) {
                    
                    return redirect()->route('rooms')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                }
        
                }
}
