<?php

namespace App\Http\Controllers;
use App\Models\modelTenants;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use App\Models\AgeingAnalysis;
class TenantsController extends Controller
{
    public function index()
    {
        $accounts = modelTenants::selectAccounts(Session::get('PropertyCode'));
        if($accounts==null)
        {
            $account="";
        }
        else
        {
            $account=$accounts[0]->AccCode;
        }

        $tenants = modelTenants::selecttenants(Session::get('PropertyCode'));

        $floors = modelTenants::selectFloors(Session::get('PropertyCode'));
        $ageingAnalyses = AgeingAnalysis::all(); // Fetch all AgeingAnalysis records

        return view('/tenants', compact('tenants', 'account', 'floors', 'ageingAnalyses'));

    }

    public function addtenants(Request $request)
    {

            $request->validate([
                'AccCode' => 'required|string',
                'code' => 'required|string',
                'name' => 'required|string',
                'contactperson' => 'required|string',
                'contact1' => 'required|string',
                'vat' => 'required|string',
                'tin' => 'required|string',
                'email' => 'required|string',
                'Address' => 'required|string',
                'floor' => 'required|string',
                'space' => 'required|string',
                'rooms' => 'required|string',
                'rent_amount' => 'required'

            ]);

            $AccCode = $request->input('AccCode');
            $code = $request->input('code');
            $name = $request->input('name');
            $contactperson = $request->input('contactperson');
            $contact1 = $request->input('contact1');
            $vat = $request->input('vat');
            $tin = $request->input('tin');
            $email = $request->input('email');
            $Address = $request->input('Address');
            $floor = $request->input('floor');
            $space = $request->input('space');
            $rooms = $request->input('rooms');
            $rent_amount = $request->input('rent_amount');



            try
            {


            $exists = DB::table('tenants')->where('TenantCode', $code)  ->where('Code', Session::get('PropertyCode'))->exists();

            if ($exists) {

                return redirect()->route('tenants')->withErrors(['error' => 'Tenant with this code:'.$code.' already exists.']);
            }

            $result = modelTenants::savetenants(Session::get('RegNo'),Session::get('PropertyCode'),$AccCode, $code,$name,$contactperson, $contact1, $vat,$tin,$email, $Address,$floor,$space,$rooms, $rent_amount);
            if ($result)
            {
                 return redirect()->route('tenants')->with('success', 'Tenant Saved Successfully.');
           }
            else
            {
                return redirect()->route('tenants')->withErrors(['error' => 'Failed to Save Tenant Details.']);
            }

        } catch (\Exception $e) {

            return redirect()->route('tenants')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
        }

        }

        public function edittenants(Request $request)
        {
            $request->validate([
                'AccCode' => 'required|string',
                'code' => 'required|string',
                'name' => 'required|string',
                'contactperson' => 'required|string',
                'contact1' => 'required|string',
                'vat' => 'required|string',
                'tin' => 'required|string',
                'email' => 'required|string',
                'Address' => 'required|string',
                'floor' => 'required|string',
                'space' => 'required|string',
                'rooms' => 'required|string',
                'rent_amount' => 'required'

            ]);

            $AccCode = $request->input('AccCode');
            $code = $request->input('code');
            $name = $request->input('name');
            $contactperson = $request->input('contactperson');
            $contact1 = $request->input('contact1');
            $vat = $request->input('vat');
            $tin = $request->input('tin');
            $email = $request->input('email');
            $Address = $request->input('Address');
            $floor = $request->input('floor');
            $space = $request->input('space');
            $rooms = $request->input('rooms');
            $rent_amount = $request->input('rent_amount');
            $id = $request->input('id');


                try
                {


                $exists = DB::table('tenants')->where('ID', $id)->exists();

                if ($exists)
                {
                $result =  modelTenants::updatetenants($id,Session::get('RegNo'),Session::get('PropertyCode'),$AccCode, $code,$name,$contactperson, $contact1,$email, $Address,$vat,$tin,$floor,$space,$rooms, $rent_amount);

                if ($result)
                {
                     return redirect()->route('tenants')->with('success', 'Tenant Updated Successfully.');
               }
                else
                {
                    return redirect()->route('tenants')->withErrors(['error' => 'Failed to Update Tenant.']);
                }


                }
                else
                {

                    return redirect()->route('tenants')->withErrors(['error' => 'Tenant Not Found.']);


                }



            } catch (\Exception $e) {

                return redirect()->route('tenants')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }

            }
            public function deletetenants( $id)
        {


                try
                {


                $exists = DB::table('tenants')->where('ID', $id)->exists();

                if ($exists)
                {


                $result =modelTenants::Deletetenants($id,Session::get('PropertyCode'));

                if ($result)
                {
                     return redirect()->route('tenants')->with('success', 'Tenant Removed Successfully.');
               }
                else
                {
                    return redirect()->route('tenants')->withErrors(['error' => 'Failed to Remove Tenant.']);
                }


                }
                else
                {

                    return redirect()->route('tenants')->withErrors(['error' => 'Tenant Not Found.']);


                }



            } catch (\Exception $e) {

                return redirect()->route('tenants')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }

            }



            public function Affiliateslisting($Type)
            {

                $Accounts = modelTenants::selectAffiliates(Session::get('PropertyCode'), $Type);

                $from="0";
                $to="0";
                $Affs = modelTenants::selectAffiliatesByID(Session::get('PropertyCode'), $Type,$from,$to);

                return view('/Affiliateslisting', compact('Accounts','Type','Affs','from','to'));

            }
            public function fetchAffiliateslisting(Request $request)
        {

            $request->validate([
                'Type' => 'required|string',
                 'from' => 'required|string',
                  'to' => 'required|string'

            ]);

            $from = $request->input('from');
            $to = $request->input('to');
            $Type = $request->input('Type');
            $Accounts = modelTenants::selectAffiliates(Session::get('PropertyCode'), $Type);
            $Affs = modelTenants::selectAffiliatesByID(Session::get('PropertyCode'), $Type,$from,$to);


            return view('/Affiliateslisting', compact('Accounts','Type','Affs','from','to'));

        }
}
