<?php

namespace App\Modules\HR\Core\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\HR\Core\Models\EmployeeGratuity;
use App\Modules\HR\Core\Models\Employee;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class EmployeeGratuityController extends Controller
{
    public function index(Request $request)
    {
        if ($request->ajax()) {

            $q = EmployeeGratuity::with('employee');

            return DataTables::of($q)

                ->addColumn('prev_no', fn ($r) => $r->previous_employee_no)
                ->addColumn('prev_name', fn ($r) => $r->employee->full_name)

                ->addColumn('curr_no', fn ($r) => $r->current_employee_no)
                ->addColumn('curr_name', fn ($r) => $r->employee->full_name)

                ->addColumn('date', fn ($r) => $r->effective_date->format('Y-m-d'))
                ->addColumn('gratuity_date', fn ($r) => $r->gratuity_date->format('Y-m-d'))

                ->addColumn('active', fn ($r) =>
                    '<input type="checkbox" '.($r->active?'checked':'').' disabled>'
                )

                ->addColumn('action', fn ($r) =>
                    view('core::gratuities/partials/actions', compact('r'))->render()
                )

                ->rawColumns(['active', 'action'])

                ->make(true);
        }

        return view('core::gratuities/index');
    }

    public function create()
    {
        return view('core::gratuities/create', [
            'employees' => Employee::orderBy('employee_code')->get()
        ]);
    }

    public function store(Request $request)
    {
        $request->validate([
            'employee_id'          => 'required|exists:employees,id',
            'current_employee_no'  => 'required',
            'effective_date'       => 'required|date',
            'gratuity_date'        => 'required|date',
        ]);

        EmployeeGratuity::create($request->only([
            'employee_id',
            'previous_employee_no',
            'current_employee_no',
            'effective_date',
            'gratuity_date',
            'remarks',
            'active',
        ]));

        return response()->json([
            'status'   => true,
            'message'  => 'Gratuity record saved',
            'redirect' => route('core.gratuities.index')
        ]);
    }

    public function edit(EmployeeGratuity $gratuity)
    {
        return view('core::gratuities/edit', [
            'gratuity'  => $gratuity,
            'employees' => Employee::orderBy('employee_code')->get()
        ]);
    }

    public function update(Request $request, EmployeeGratuity $gratuity)
    {
        $request->validate([
            'current_employee_no' => 'required',
            'effective_date'      => 'required|date',
            'gratuity_date'       => 'required|date',
        ]);

        $gratuity->update($request->only([
            'previous_employee_no',
            'current_employee_no',
            'effective_date',
            'gratuity_date',
            'remarks',
            'active',
        ]));

        return response()->json([
            'status'   => true,
            'message'  => 'Gratuity record updated',
            'redirect' => route('core.gratuities.index')
        ]);
    }

    public function destroy(EmployeeGratuity $gratuity)
    {
        $gratuity->delete();

        return response()->json([
            'status' => true,
            'message' => 'Gratuity record deleted'
        ]);
    }
}
