<?php

namespace App\Http\Controllers\Hr;

use App\Http\Controllers\Controller;
use App\Models\EmployeeTaxBand;
use App\Models\EmployeeTaxRelief;
use Illuminate\Http\Request;

class EmployeeTaxReliefController extends Controller
{
    public function index()
    {
        $relieves = EmployeeTaxRelief::all();

        return view('hr.relieves.setup-employee-relieves', compact('relieves'));
    }

    /**Display popup window to Add a New Tax Relief**/
    public function showAddTaxRelief(){
        return view('hr.relieves.add-tax-relief');
    }

    /**Store the Tax Relief Entry**/
    public function storeAddTaxRelief(Request $request)
    {
        $reliefType = $request['relief-type'];
        if($reliefType == 'on-deductions'){
            $name = null;
            $hasDeduction = 1;
            $deductionId = $request['deduction-account'] ?? null;
        }else{
            $hasDeduction = 0;
            $deductionId = null;
            $name = $reliefType;
        }

        $rate = $request['rate'];
        $amount = $request['amount'];

        $create = EmployeeTaxRelief::create([
            'name' => $name,
            'has_deduction' => $hasDeduction,
            'deduction_id' => $deductionId,
            'rate' => $rate,
            'amount' => $amount,
            'created_by' => auth()->user()->id,
        ]);

        $res = 'error';
        if($create){
            $res = 'success';
        }
        return $res;
    }
}
