<?php

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

class TaxController extends Controller
{
    public function index()
    {
        $taxs = modelTax::selectTax(Session::get('PropertyCode'));

        return view('/Tax', compact('taxs'));
    }

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

            try
            {


            $exists = modelTax::selectTaxByCode(Session::get('PropertyCode'),$name);

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

            $result = $accounts = modelTax::saveTax(Session::get('PropertyCode'), $name,$rate);
            
            if ($result) 
            {
                 return redirect()->route('tax')->with('success', 'Tax Saved successfully.');
           } 
            else 
            {
                return redirect()->route('tax')->withErrors(['error' => 'Failed to save Tax.']);
            }

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

        }

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


                $result =  modelTax::EditTax($id,Session::get('PropertyCode'), $name,$rate);
                
                if ($result) 
                {
                     return redirect()->route('tax')->with('success', 'Tax Updated Successfully.');
               } 
                else 
                {
                    return redirect()->route('tax')->withErrors(['error' => 'Failed to Update Tax.']);
                }
                 
                 
                }
                else
                {

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

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


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


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

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

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