<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\AttendanceDefer;
use App\Models\EmployeeChangeSchedule;
use App\Models\EmployeeLeave;
use App\Models\EmployeeOb;
use App\Models\EmployeeOvertime;
use App\Models\EmployeeTimeAdjustment;
use App\Models\EmployeeUndertime;
use DB;
use Illuminate\Http\Request;

class AttendanceDeferController extends Controller
{
    public function index(){

    }

    public function show(){

    }

    public function store(Request $request){
        $id = $request->attendance_id;
        
        switch ($request->attendance_type){
            case "leave":   
                $data = EmployeeLeave::find($id);
                break;
            case "overtime":
                $data = EmployeeOvertime::find($id);
                break;
            case "official-business":
                $data = EmployeeOb::find($id);
                break;
            case "undertime":
                $data = EmployeeUndertime::find($id);
                break;
            case "time-adjustment":
                $data = EmployeeTimeAdjustment::find($id);
                break;
            case "change-schedule":
                $data = EmployeeChangeSchedule::find($id);
                break;
        }

        if ($data->fill($request->all())->save()) {
            $request['employee_id'] = $data->employee_id;
            $request['reason'] = $request->defer_reason;

            // dd($request->all());
            $data = AttendanceDefer::create($request->all());

            DB::connection()->commit();

            return response()->json([
                'message' => 'Record Successfully added!',
                'status' => 'success',
            ],201);
        } else {
            DB::connection()->rollback();
            return response()->json([
                'status'  => false,
                'message' => 'Unable to process request. Please try again.',
            ]);
        }
    }
}
