<?php

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

class CurrencyController extends Controller
{
    
    public function index()
    {
        $currencys = modelCurrency::selectAllCurrency(Session::get('PropertyCode'));

        return view('/Currency', compact('currencys'));
    }

    public function homecurrencyindex()
    {
        $allcurrencys = modelCurrency::selectAllCurrency(Session::get('PropertyCode'));
        $currencys = modelCurrency::selectHomeCurrency(Session::get('PropertyCode'));

        return view('/HomeCurrency', compact('currencys','allcurrencys'));
    }

    public function ratesindex()
    {
        $allcurrencys = modelCurrency::selectAllForegnCurrency(Session::get('PropertyCode'));
        $rates = modelCurrency::selectAllRates(Session::get('PropertyCode'));

        return view('/Rates', compact('allcurrencys','rates'));
    }

    public function saveRate(Request $request)
    {
       // Validate the request data
            $request->validate([
                'currency' => 'required|string',
                 'rate' => 'required|string'
            ]);
    
            $currency = $request->input('currency');
            $rate = $request->input('rate');
    

            try
            {


            

            $result = $accounts = modelCurrency::saveRate(Session::get('PropertyCode'), $currency,$rate);
            
            if ($result) 
            {
                 return redirect()->route('Rates')->with('success', 'Rate Saved successfully.');
           } 
            else 
            {
                return redirect()->route('Rates')->withErrors(['error' => 'Failed to save Rate.']);
            }


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

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

            try
            {


            $exists = DB::table('currencys')->where('Name', $name)  ->where('Code', Session::get('PropertyCode'))->exists();

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

            $result = $accounts = modelCurrency::saveCurrency(Session::get('PropertyCode'), $symbol,$name);
            
            if ($result) 
            {
                 return redirect()->route('Currency')->with('success', 'Currency Saved successfully.');
           } 
            else 
            {
                return redirect()->route('Currency')->withErrors(['error' => 'Failed to save Currency.']);
            }

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

        }

        public function savehomecurrency(Request $request)
        {
           // Validate the request data
                $request->validate([
                    'currency' => 'required|string'
                ]);
        
                $currency = $request->input('currency');
        
    
                try
                {
    
    
                $exists = modelCurrency::selectHomeCurrency(Session::get('PropertyCode'));
    
                if ($exists) 
                {
                 
                    return redirect()->route('homeCurrency')->withErrors(['error' => 'Home Currency already exists.']);
                }
    
                $result = $accounts = modelCurrency::saveHomeCurrency(Session::get('PropertyCode'), $currency);
                
                if ($result) 
                {
                     return redirect()->route('homeCurrency')->with('success', 'Home Currency Saved Successfully.');
               } 
                else 
                {
                    return redirect()->route('homeCurrency')->withErrors(['error' => 'Failed to Save Home Currency.']);
                }
    
            } catch (\Exception $e) {
                
                return redirect()->route('homeCurrency')->withErrors(['error' => 'An error occurred!!' . $e->getMessage()]);
            }
    
            }

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

                        $result = $accounts = modelCurrency::editHomeCurrency($id,Session::get('PropertyCode'), $currency);
                    
                    if ($result) 
                    {
                         return redirect()->route('homeCurrency')->with('success', 'Home Currency Updated Successfully.');
                   } 
                    else 
                    {
                        return redirect()->route('homeCurrency')->withErrors(['error' => 'Failed to Update Home Currency.']);
                    }
                    
                    
                    }

                    else
                    {

                        return redirect()->route('homeCurrency')->withErrors(['error' => 'Home Currency Not Found.']);
                    

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

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

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


                $result = $accounts = modelCurrency::editCurrency($id,Session::get('PropertyCode'), $symbol,$name);
                
                if ($result) 
                {
                     return redirect()->route('Currency')->with('success', 'Currency Updated Successfully.');
               } 
                else 
                {
                    return redirect()->route('Currency')->withErrors(['error' => 'Failed to Update Currency.']);
                }
                 
                 
                }
                else
                {

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

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


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


                $result = $accounts = modelCurrency::deleteCurrency($id,Session::get('PropertyCode'));
                
                if ($result) 
                {
                     return redirect()->route('Currency')->with('success', 'Currency Removed Successfully.');
               } 
                else 
                {
                    return redirect()->route('Currency')->withErrors(['error' => 'Failed to Remove Currency.']);
                }
                 
                 
                }
                else
                {

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

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