<?php

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

class AccountsController extends Controller
{
 
    public function index()
    {
        $accounts = modelAccounts::selectAccountsTypes(Session::get('PropertyCode'));

        return view('/AccountTypes', compact('accounts'));
    }


    public function mainAccountsIndex()
    {
        
        $accountsTypes = modelAccounts::selectAccountsTypes(Session::get('PropertyCode'));
        $mainAccounts = modelAccounts::selectMainAccounts(Session::get('PropertyCode'));
        $generatedCode = modelAccounts::selectGenerateCode(Session::get('PropertyCode'));
        $generatedCodeValue=0;
        if(count($generatedCode)>0)
        {

            $generatedCodeValue = intval($generatedCode[0]->AccCode); // Convert to integer
            $generatedCodeValue = $generatedCodeValue + 1;  // Increment by 1
            
            // Convert back to string if needed
            $generatedCodeValue = (string) $generatedCodeValue;

        }
        else
        {

            $generatedCodeValue=1000;
        }

        return view('/MainAccounts', compact('accountsTypes','mainAccounts','generatedCodeValue'));
    }

    public function subAccountsIndex($AccCode, $AccName)
    {
        
       $SubAccounts = modelAccounts::selectSubAccounts(Session::get('PropertyCode'),$AccCode);
        $generatedCode = modelAccounts::selectGenerateSubCode(Session::get('PropertyCode'),$AccCode);
        $generatedCodeValue=0;
        if(count($generatedCode)>0)
        {

            $generatedCodeValue = intval($generatedCode[0]->SubCode); // Convert to integer
            $generatedCodeValue = $generatedCodeValue + 1;  // Increment by 1
            
            // Convert back to string if needed
            $generatedCodeValue = (string) $generatedCodeValue;

        }
        else
        {

            $generatedCodeValue= (string)100;
        }

        return view('/SubAccounts', compact('SubAccounts','generatedCodeValue','AccCode','AccName'));
    }
    
    public function Save(Request $request)
    {
       // Validate the request data
            $request->validate([
                'name' => 'required|string'
               
            ]);
    
            $name = $request->input('name');
    

            try
            {


            $exists =  modelAccounts::SellectAccType(Session::get('PropertyCode'), $name);

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

            $result = $accounts = modelAccounts::saveAccountType(Session::get('PropertyCode'), $name);
            
            if ($result) 
            {
                 return redirect()->route('accountType')->with('success', 'Account type saved successfully.');
           } 
            else 
            {
                return redirect()->route('accountType')->withErrors(['error' => 'Failed to save Account type.']);
            }

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

        }
    
       
        public function Edit(Request $request)
        {
           // Validate the request data
                $request->validate([
                    'name' => 'required|string',
                   'id' => 'required|string'
                ]);
        
                $name = $request->input('name');
                $id = $request->input('id');
    
                try
                {
    
                    
                $exists = DB::table('accountstypes')->where('ID', $id)->exists();
    
                if ($exists) 
                {

                    
                $result = $accounts = modelAccounts::editAccountType(Session::get('PropertyCode'), $name , $id);
                
                if ($result) 
                {
                     return redirect()->route('accountType')->with('success', 'Account type updated successfully.');
               } 
                else 
                {
                    return redirect()->route('accountType')->withErrors(['error' => 'Update Failed Account type.']);
                }
                    
                }
                else
                {

                    return redirect()->route('accountType')->withErrors(['error' => 'Update Failed Account type.']);
               

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

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

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

            public function saveMainAccount(Request $request)
            {
               // Validate the request data
                    $request->validate([
                        'type' => 'required|string',
                         'AccCode' => 'required|string',
                           'AccName' => 'required|string'
                    ]);
            
                    $type = $request->input('type');
                    $AccCode = $request->input('AccCode');
                    $AccName = $request->input('AccName');
            
        
                    try
                    {
        
        
                        $exists = DB::table('mainaccounts') ->where('AccCode', $AccCode)  ->where('Code', Session::get('PropertyCode')) ->exists();
        
                    if ($exists) {
                     
                        return redirect()->route('mainAccounts')->withErrors(['error' => 'Account already exists.']);
                    }
        
                    $result = $accounts = modelAccounts::saveMainAccount(Session::get('PropertyCode'), $type,$AccCode,$AccName);
                    
                    if ($result) 
                    {
                         return redirect()->route('mainAccounts')->with('success', 'Account saved successfully.');
                   } 
                    else 
                    {
                        return redirect()->route('mainAccounts')->withErrors(['error' => 'Failed to save Account.']);
                    }
        
                } catch (\Exception $e) {
                    
                    return redirect()->route('mainAccounts')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                }
        
                }
            

                public function editMainAccount(Request $request)
                {
                   // Validate the request data
                        $request->validate([
                            'type' => 'required|string',
                             'AccCode' => 'required|string',
                               'AccName' => 'required|string'
                        ]);
                
                        $type = $request->input('type');
                        $AccCode = $request->input('AccCode');
                        $AccName = $request->input('AccName');
                        $id = $request->input('id');
                
            
                        try
                        {
            
            
                        $exists = DB::table('mainaccounts')->where('ID', $id)->exists();
            
                        if ($exists) {

                            $result = $accounts = modelAccounts::editMainAccount(Session::get('PropertyCode'),$id, $type,$AccCode,$AccName);
                        
                            if ($result) 
                            {
                                 return redirect()->route('mainAccounts')->with('success', 'Account Updated Successfully.');
                           } 
                            else 
                            {
                                return redirect()->route('mainAccounts')->with(['success' => 'Nothing to Update.']);
                            }
                        }
                        else 
                        {
                            return redirect()->route('mainAccounts')->withErrors(['error' => 'Account Not Found.']);
                        }
            
                        
            
                    } catch (\Exception $e) {
                        
                        return redirect()->route('mainAccounts')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                    }
            
                    }

                    public function deleteMainAccount($id)
                    {
                    
                
                            try
                            {
                
                
                            $exists = DB::table('mainaccounts')->where('ID', $id)->exists();
                
                            if ($exists) {
    
                                $result = $accounts = modelAccounts::deleteMainAccount(Session::get('PropertyCode'),$id);
                            
                                if ($result) 
                                {
                                     return redirect()->route('mainAccounts')->with('success', 'Account Removed Successfully.');
                               } 
                                else 
                                {
                                    return redirect()->route('mainAccounts')->withErrors(['error' => 'Failed to Remove Account.']);
                                }
                            }
                            else 
                            {
                                return redirect()->route('mainAccounts')->withErrors(['error' => 'Account Not Found.']);
                            }
                
                            
                
                        } catch (\Exception $e) {
                            
                            return redirect()->route('mainAccounts')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                        }
                
                        }
    



                        public function saveSubAccount(Request $request)
                        {
                           // Validate the request data
                                $request->validate([
                                    'SubAccCode' => 'required|string',
                                     'AccCode' => 'required|string',
                                       'SubAccName' => 'required|string',
                                       'AccName' => 'required|string'
                                ]);
                        
                                $SubAccCode = $request->input('SubAccCode');
                                $AccCode = $request->input('AccCode');
                                $SubAccName = $request->input('SubAccName');
                                $AccName = $request->input('AccName');
                        
                    
                                try
                                {
                    
                    
                                $exists =  modelAccounts::selectValidateSubAccounts(Session::get('PropertyCode'), $AccCode,$SubAccCode);
                    
                                if ($exists) {
                                 
                                    return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'Account already exists.']);
                                }
                    
                                $result = $accounts = modelAccounts::saveSubAccount(Session::get('PropertyCode'), $AccCode,$SubAccCode,$SubAccName);
                                
                                if ($result) 
                                {
                                     return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->with('success', 'Account saved successfully.');
                               } 
                                else 
                                {
                                    return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'Failed to save Account.']);
                                }
                    
                            } catch (\Exception $e) {
                                
                                return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                            }
                    
                            }


                            public function editSubAccount(Request $request)
                            {
                               // Validate the request data
                                    $request->validate([
                                        'SubAccCode' => 'required|string',
                                         'AccCode' => 'required|string',
                                           'SubAccName' => 'required|string',
                                           'AccName' => 'required|string',
                                           'id' => 'required|string'
                                    ]);
                            
                                    $SubAccCode = $request->input('SubAccCode');
                                    $AccCode = $request->input('AccCode');
                                    $SubAccName = $request->input('SubAccName');
                                    $AccName = $request->input('AccName');
                                    $id = $request->input('id');
                            
                        
                                    try
                                    {
                        
                        
                                    $exists =  modelAccounts::selectValidateSubAccounts(Session::get('PropertyCode'), $AccCode,$SubAccCode);
                        
                                    if ($exists) {
                                     
                                        
                                    $result = $accounts = modelAccounts::editSubAccount($id,Session::get('PropertyCode'), $AccCode,$SubAccCode,$SubAccName);
                                    
                                    if ($result) 
                                    {
                                         return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->with('success', 'Account Updated Successfully.');
                                   } 
                                    else 
                                    {
                                        return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'Failed to Update Account.']);
                                    }
                                        
                                    
                                    }

                                    else
                                    {

                                        return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'Account Not Found.']);
                                    

                                    }
                        
                        
                                } catch (\Exception $e) {
                                    
                                    return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                                }
                        
                                }


                                public function deleteSubAccount( $id,$AccCode,$AccName)
                            {
                               
                        
                                    try
                                    {
                        
                        
                                   
                                    $result = $accounts = modelAccounts::deleteSubAccount($id,Session::get('PropertyCode'), $AccCode);
                                    
                                    if ($result) 
                                    {
                                         return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->with('success', 'Account Removed Successfully.');
                                   } 
                                    else 
                                    {
                                        return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'Failed to Remove Account.']);
                                    }
                                        
                                    
                                    }
                                    catch (\Exception $e) {
                                    
                                    return redirect()->route('subAccounts', [ 'id' => $AccCode, 'name' => $AccName, ])->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                                }
                        
                                }




                                public function bankIndex()
                                {
                                    $accounts = modelAccounts::selectBanks(Session::get('PropertyCode'));
                            
                                    return view('/Bank', compact('accounts'));
                                }


                                public function savebank(Request $request)
                                {
                                   // Validate the request data
                                        $request->validate([
                                            'acc' => 'required|string',
                                             'name' => 'required|string'
                                        ]);
                                
                                        $acc = $request->input('acc');
                                        $name = $request->input('name');
                                
                            
                                        try
                                        {
                            
                            
                                        $exists = DB::table('banks')->where('Account', $acc)  ->where('Code', Session::get('PropertyCode'))->exists();
                            
                                        if ($exists) {
                                         
                                            return redirect()->route('bankIndex')->withErrors(['error' => 'Account already exists.']);
                                        }
                            
                                        $result  = modelAccounts::saveBankAccount(Session::get('PropertyCode'), $acc,$name);
                                        
                                        if ($result) 
                                        {
                                             return redirect()->route('bankIndex')->with('success', 'Bank Account Saved successfully.');
                                       } 
                                        else 
                                        {
                                            return redirect()->route('bankIndex')->withErrors(['error' => 'Failed to Save Bank Account.']);
                                        }
                            
                                    } catch (\Exception $e) {
                                        
                                        return redirect()->route('bankIndex')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                                    }
                            
                                    }


                                    public function editbank(Request $request)
                                    {
                                       // Validate the request data
                                            $request->validate([
                                                'acc' => 'required|string',
                                                 'name' => 'required|string',
                                                 'id' => 'required|string'
                                            ]);
                                    
                                            $acc = $request->input('acc');
                                            $name = $request->input('name');
                                            $id = $request->input('id');
                                    
                                
                                            try
                                            {
                                
                                
                                            $exists = DB::table('banks')->where('ID', $id)->exists();
                                
                                            if ($exists) 
                                            {
                            
                            
                                            $result  = modelAccounts::editBank($id,Session::get('PropertyCode'), $acc,$name);
                                            
                                            if ($result) 
                                            {
                                                 return redirect()->route('bankIndex')->with('success', 'Bank Account Updated Successfully.');
                                           } 
                                            else 
                                            {
                                                return redirect()->route('bankIndex')->withErrors(['error' => 'Failed to Update Bank Account.']);
                                            }
                                             
                                             
                                            }
                                            else
                                            {
                            
                                                return redirect()->route('bankIndex')->withErrors(['error' => 'Bank Account Not Found.']);
                                            
                            
                                            }
                                
                                            
                                
                                        } catch (\Exception $e) {
                                            
                                            return redirect()->route('bankIndex')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                                        }
                                
                                        }
                            
                            
                                        public function deletebank( $id)
                                    {
                                      
                                
                                            try
                                            {
                                
                                
                                            $exists = DB::table('banks')->where('ID', $id)->exists();
                                
                                            if ($exists) 
                                            {
                            
                            
                                            $result  = modelAccounts::deleteBank($id,Session::get('PropertyCode'));
                                            
                                            if ($result) 
                                            {
                                                 return redirect()->route('bankIndex')->with('success', 'Bank Account Removed Successfully.');
                                           } 
                                            else 
                                            {
                                                return redirect()->route('bankIndex')->withErrors(['error' => 'Failed to Remove Bank Account.']);
                                            }
                                             
                                             
                                            }
                                            else
                                            {
                            
                                                return redirect()->route('bankIndex')->withErrors(['error' => 'Bank Account Not Found.']);
                                            
                            
                                            }
                                
                                            
                                
                                        } catch (\Exception $e) {
                                            
                                            return redirect()->route('bankIndex')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
                                        }
                                
                                        }
}
