<?php

namespace App\Http\Controllers;
use App\Models\modelProperty;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Middleware\AuthMiddleware;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class PropertyController extends Controller
{
    public function index()
    {

        $properties = modelProperty::selectProperty(Session::get('RegNo'));

        return view('/Landing', compact('properties'));
    }
    public function showPropertyDetails($code, $name)
    {
        
        Session::put('PropertyName', $name);
        Session::put('PropertyCode', $code);

        $dashboard = modelProperty::selectDashboard($code);

  return view('/Dashboard',compact('dashboard'));
    }
    public function deletePropertyDetails($code, $name)
    {
        try
        {
            
        $exists = DB::table('propertydetails')->where('Code', $code)->exists();
        if ($exists) 
        {
            
        $result = modelProperty::DeleteProperty($code,Session::get('RegNo'));
        if ($result) 
        
        {
             
            return redirect()->route('landing')->with('success', 'Property '.$code.' '.$name.' removed successfully.');
        } 
    
        else 
        {
            return redirect()->route('landing')->withErrors(['error' => 'Failed to Remove property.']);
        }
         
             
        }
        else
        {
            return redirect()->route('landing')->withErrors(['error' => 'Property not found.']);
      
        }

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

    
    public function Save(Request $request)
    {
       // Validate the request data
            $request->validate([
                'pcode' => 'required|string',
                'name' => 'required|string',
                'address' => 'required|string',
                'type' => 'required|string',
                'floors' => 'required|string',
                'area' => 'required|string',
                'group' => 'required|string',
                'rooms' => 'required|string',
                'space' => 'required|string'
            ]);
    
            $pcode = $request->input('pcode');
            $name = $request->input('name');
            $address = $request->input('address');
            $type = $request->input('type');
            $floors = $request->input('floors');
            $area = $request->input('area');
            $group = $request->input('group');
            $rooms = $request->input('rooms');
            $space = $request->input('space');
    

            try
            {


            $exists = DB::table('propertydetails')->where('Code', Session::get('RegNo').$pcode)->exists();

            if ($exists) {
             
                return redirect()->route('landing')->withErrors(['error' => 'Property with this code already exists.']);
            }

            $result = modelProperty::saveProperty(Session::get('RegNo'),$pcode, $name,$address,$type, $floors, $area,$group, $rooms,$space);
            if ($result) 
            {
                 return redirect()->route('landing')->with('success', 'Property saved successfully.');
           } 
            else 
            {
                return redirect()->route('landing')->withErrors(['error' => 'Failed to save property details.']);
            }

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

        }


        public function Edit(Request $request)
{
    // Validate the request data
    $request->validate([
        'pcode' => 'required|string',
        'name' => 'required|string',
        'address' => 'required|string',
        'type' => 'required|string',
        'floors' => 'required|string',
        'area' => 'required|string',
        'group' => 'required|string',
        'rooms' => 'required|string',
        'space' => 'required|string',
        'id' => 'required|int'
    ]);

    $id = $request->input('id');
    $pcode = $request->input('pcode');
    $name = $request->input('name');
    $address = $request->input('address');
    $type = $request->input('type');
    $floors = $request->input('floors');
    $area = $request->input('area');
    $group = $request->input('group');
    $rooms = $request->input('rooms');
    $space = $request->input('space');

    try
    {
        
            $property = DB::table('propertydetails')->where('ID', $id)->first();

            if ($property) 
            {
               $result = modelProperty::editProperty($id,Session::get('RegNo'),$pcode, $name,$address,$type, $floors, $area,$group, $rooms,$space);
                if ($result) 
                {
                     
                    return redirect()->route('landing')->with('success', 'Property updated successfully.');
               
                } 
                else 
                {
                    return redirect()->route('landing')->with(['success' => 'Nothing to Update.']);
                
                }
            } 
            else 
            {
                return redirect()->route('landing')->withErrors(['error' => 'Property not found.']);
            }
        
    } catch (\Exception $e) {
        return redirect()->route('landing')->withErrors(['error' => 'An error occurred: ' . $e->getMessage()]);
    }
}
}
