<?php

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

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

class EmployeeRejoinController extends Controller
{
      public function index(Request $request)
    {
        if ($request->ajax()) {
            $query = EmployeeRejoin::with('employee');

            return DataTables::of($query)
                ->addColumn('emp_no', fn ($r) => $r->employee->employee_code)
                ->addColumn('name', fn ($r) =>
                    trim($r->employee->first_name.' '.$r->employee->last_name)
                )
                ->editColumn('exit_date', fn ($r) => $r->exit_date)
                ->addColumn('created_date', fn ($r) => $r->created_at->format('Y-m-d'))
                ->addColumn('system_generated', fn ($r) =>
                    $r->currently_working ? 'Yes' : 'No'
                )
                
                ->addColumn('action', fn ($r) =>
                    view('core::employee_rejoins/partials/actions', compact('r'))->render()
                )
                ->rawColumns(['action'])
                ->make(true);
        }

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

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

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

        EmployeeRejoin::create([
            'employee_id'      => $request->employee_id,
            'exit_date'        => $request->exit_date,
            'remarks'          => $request->remarks,
            'attendance'=> $request->boolean('attendance'),
            'payroll'   => $request->boolean('payroll'),
            'currently_working'   => $request->boolean('currently_working'),
        ]);

       return response()->json([
            'status'  => true,
            'message' => 'Employee rejoin successfully',
        ]);
    }

    public function edit(EmployeeRejoin $employee_rejoin)
    {
        
        return view('core::employee_rejoins/edit', [
            'rejoin'      => $employee_rejoin,
            'employees' => Employee::orderBy('employee_code')->get()
        ]);
    }

    public function update(Request $request, EmployeeRejoin $rejoin)
    {
        if ($rejoin->system_generated) {
            abort(403, 'System generated rejoins cannot be edited');
        }

        $request->validate([
            'exit_date' => 'required|date',
        ]);

        $rejoin->update([
            'exit_date'         => $request->exit_date,
            'remarks'           => $request->remarks,
        ]);

       return response()->json([
            'status'  => true,
            'message' => 'Employee rejoin updated successfully',
        ]);
    }

    public function destroy(EmployeeRejoin $rejoin)
    {
        if ($rejoin->system_generated) {
            return response()->json([
                'status' => false,
                'message' => 'System rejoins cannot be deleted'
            ], 422);
        }

        $rejoin->delete();

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