<?php

namespace App\Http\Controllers\Hr;

use App\Http\Controllers\Controller;
use App\Models\EmployeeAllowanceCalculator;
use App\Models\EmployeeAllowanceType;
use App\Models\EmployeeDeductionType;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class EmployeeAllowanceController extends Controller
{
    /**View a Page for Available Employee Allowances Types**/
    public function index(){
        $allowances = EmployeeAllowanceType::orderBy('name', 'asc')->get();
        return view('hr.allowances.setup-allowances', compact('allowances'));
    }

    /**Display a Pop Up window to Add New Allowance Type**/
    public function showAddEmployeeAllowanceType(){
        return view('hr.allowances.add-employee-allowance-type');
    }

    /**Add a Function to Store the Allowance Type Set**/
    public function storeAddEmployeeAllowanceType(Request $request):string
    {
        $name = $request->input('name');
        $basis = $request->input('basis');
        $minAmount = $request->input('min-amount') ?? 0;

        $res = 'empty fields';
        if (!empty($name) && !empty($basis)) {
            $res = 'exists';
            if(!EmployeeAllowanceType::where('name', $name)->exists()){
                $res = 'error';
                if(EmployeeAllowanceType::create(['name' => $name, 'basis' => $basis, 'min_amount'=>$minAmount,  'created_by' => Auth::id()])) {
                    $res = 'success';
                }
            }
        }
        return $res;
    }

    /**Add a Calculator to the Allowance Table**/
    public function showAddEmployeeAllowanceCalculator($id){
        $list = EmployeeAllowanceCalculator::where('allowance_id', $id)->get();
        return view('hr.allowances.add-employee-allowance-calculator', compact('id', 'list'));
    }

    /**Store the Allowance Calculator Details Entered**/
    public function storeAddEmployeeAllowanceCalculator($id,Request $request):string
    {
        $results = [];
        $res = 'no rates';
        if ($request->has('from')){
            foreach ($request['from'] as $key => $value) {
                $cal = $request['calculator-id'][$key] ?? 'empty';
                $from = $request['from'][$key] ?? 0;
                $to = $request['to'][$key] ?? 0;
                $rate = $request['rate'][$key] ?? 0;
                $minimum = $request['minimum'][$key] ?? 0;
                $maximum = $request['maximum'][$key] ?? 0;

                /**Only ipload things where the from, to and rate are not 0**/
                if($from >= 0 && $to != 0 && $rate != 0){
                    if ($cal != 'empty'){
                        /**We are updating the available Calculator**/
                        $update = EmployeeAllowanceCalculator::where('id', $cal)->update([
                            'from' => $from,
                            'to' => $to,
                            'rate' => $rate,
                            'minimum' => $minimum,
                            'maximum' => $maximum,

                        ]);
                        /**Check the Status of the Update**/
                        if($update){
                            $results[] = 'success';
                        }else{
                            $results[] = 'error';
                        }

                    }else{
                        /**We are creating a New Entry**/
                        $create = EmployeeAllowanceCalculator::create([
                            'allowance_id' => $id,
                            'from' => $from,
                            'to' => $to,
                            'rate' => $rate,
                            'minimum' => $minimum,
                            'maximum' => $maximum,
                            'created_by' => auth()->user()->id,
                        ]);

                        /**Check the Status of the Update**/
                        if($create){
                            $results[] = 'success';
                        }else{
                            $results[] = 'error';
                        }
                    }
                }

            }
            $res = 'error';
            if(!in_array('error', $results)){
                $res = 'success';
            }
        }

        return  $res;
    }
}
