<?php

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

class FloorController extends Controller
{
    
    public function Index()
    {
        
      
        $floors = modelFloor::selectFloors(Session::get('PropertyCode'));
        
        return view('/Floor', compact('floors'));
    }

    public function addfloors(Request $request)
    {
       // Validate the request data
            $request->validate([
                'name' => 'required|string',
                 'space' => 'required|string',
                   'rooms' => 'required|string'
            ]);
    
            $name = $request->input('name');
            $space = $request->input('space');
            $rooms = $request->input('rooms');
    

            try
            {


            $exists = modelFloor::selectFloorsName(Session::get('PropertyCode'),$name);

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

            $result  = modelFloor::saveFloor(Session::get('PropertyCode'),$name,$space, $rooms);
            
            if ($result) 
            {
                 return redirect()->route('floors')->with('success', 'Floor saved successfully.');
           } 
            else 
            {
                return redirect()->route('floors')->withErrors(['error' => 'Failed to save Floor.']);
            }

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

        }
    

        public function editfloors(Request $request)
        {
            $request->validate([
                'name' => 'required|string',
                 'space' => 'required|string',
                 'id' => 'required|string',
                   'rooms' => 'required|string'
            ]);
    
            $name = $request->input('name');
            $space = $request->input('space');
            $rooms = $request->input('rooms');
                $id = $request->input('id');
        
    
                try
                {
    
    
                $exists = DB::table('floors')->where('ID', $id)->exists();
    
                if ($exists) {

                    $result = $Floors = modelFloor::editFloor(Session::get('PropertyCode'),$id, $name,$space, $rooms);
                
                    if ($result) 
                    {
                         return redirect()->route('floors')->with('success', 'Floor Updated Successfully.');
                   } 
                    else 
                    {
                        return redirect()->route('floors')->with(['success' => 'Nothing to Update.']);
                    }
                }
                else 
                {
                    return redirect()->route('floors')->withErrors(['error' => 'Floor Not Found.']);
                }
    
                
    
            } catch (\Exception $e) {
                
                return redirect()->route('floors')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }
    
            }

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

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