<?php

namespace App\Http\Controllers;

use DateTime;
use App\Models\User;
use App\Models\Employee;
use App\Models\Overtime;
use App\Models\FingerDevices;
use App\Helpers\FingerHelper;
use App\Models\Leave;
use App\Http\Requests\AttendanceEmp;
use Illuminate\Support\Facades\Hash;
use App\Models\Cuti;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;

class LeaveController extends Controller
{
    public function index()
    {
        $leaves = Cuti::with('user')->orderBy('created_at', 'desc')->get();
        return view('admin.leave', compact('leaves'));
    }

    public function indexOvertime()
    {
        return view('admin.overtime')->with(['overtimes' => Overtime::all()]);
    }


    // public static function overTime(Employee $employee)
    // {
    //     $current_t = new DateTime(date('H:i:s'));
    //     $start_t = new DateTime($employee->schedules->first()->time_out);
    //     $difference = $start_t->diff($current_t)->format('%H:%I:%S');

    //     $overtime = new Overtime();
    //     $overtime->emp_id = $employee->id;
    //     $overtime->duration = $difference;
    //     $overtime->overtime_date = date('Y-m-d');
    //     $overtime->save();
    // }
    public static function overTimeDevice($att_dateTime, Employee $employee)
    {
        
            $attendance_time =new DateTime($att_dateTime);
            $checkout = new DateTime($employee->schedules->first()->time_out);
            $difference = $checkout->diff($attendance_time)->format('%H:%I:%S');

            $overtime = new Overtime();
            $overtime->emp_id = $employee->id;
            $overtime->duration = $difference;
            $overtime->overtime_date = date('Y-m-d', strtotime($att_dateTime));
            $overtime->save();
        
    }
    public function approve($id)
{
    $leave = Leave::findOrFail($id);
    $leave->status = 'Disetujui'; // Mengubah status menjadi Disetujui
    $leave->save();

    return response()->json(['success' => true, 'status' => 'Disetujui']);
}

public function reject($id)
{
    $leave = Leave::findOrFail($id);
    $leave->status = 'Ditolak'; // Mengubah status menjadi Ditolak
    $leave->save();

    return response()->json(['success' => true, 'status' => 'Ditolak']);
}

public function updateStatus(Request $request, $id)
{
    try {
        $leave = Cuti::findOrFail($id);
        $leave->status = $request->status;
        $leave->save();

        return response()->json([
            'success' => true,
            'message' => 'Status berhasil diperbarui'
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'success' => false,
            'message' => 'Gagal memperbarui status'
        ], 500);
    }
}

public function show($id)
{
    $leave = Cuti::findOrFail($id);
    return view('admin.leave-details', compact('leave'));
}

public function getDetails($id)
{
    try {
        $leave = Cuti::with('user')->findOrFail($id);
        
        // Format tanggal dengan Carbon
        $tanggalMulai = Carbon::parse($leave->tanggal_mulai)->format('d M Y');
        $tanggalSelesai = Carbon::parse($leave->tanggal_selesai)->format('d M Y');
        
        return response()->json([
            'success' => true,
            'data' => [
                'nama' => $leave->user->name,
                'keterangan' => $leave->keterangan,
                'jenis_cuti' => $leave->jenis_cuti,
                'status' => $leave->status,
                'tanggal_mulai' => $tanggalMulai,
                'tanggal_selesai' => $tanggalSelesai
            ]
        ]);
    } catch (\Exception $e) {
        return response()->json([
            'success' => false,
            'message' => 'Data tidak ditemukan'
        ], 404);
    }
}

}
