@extends('layouts.master')

@section('content')
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h3>Employee Overtime</h3>
        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addOtModal">
            <i class="fa fa-plus"></i> Add OT
        </button>
    </div>
    <div class="card-body">

        @if(session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        <table class="table table-bordered table-hover table-sm">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th>Employee</th>
                    <th>Year</th>
                    <th>Month</th>
                    <th class="text-end">OT Hours</th>
                    <th class="text-end">Multiplier</th>
                    <th class="text-end">OT Amount</th>
                    <th>Notes</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($overtimes as $ot)
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>{{ $ot->employee->first_name }} {{ $ot->employee->last_name }}</td>
                        <td class="text-center">{{ $ot->year }}</td>
                        <td class="text-center">{{ \Carbon\Carbon::createFromDate($ot->year, $ot->month, 1)->format('F') }}</td>
                        <td class="text-end">{{ number_format($ot->ot_hours, 2) }}</td>
                        <td class="text-center">{{ $ot->ot_multiplier }}×</td>
                        <td class="text-end fw-bold text-success">{{ number_format($ot->ot_amount, 0) }}</td>
                        <td>{{ $ot->notes }}</td>
                        <td>
                            <button class="btn btn-warning btn-xs"
                                    data-bs-toggle="modal"
                                    data-bs-target="#editOtModal{{ $ot->id }}">
                                <i class="fa fa-edit"></i>
                            </button>
                            <form method="POST" action="{{ route('overtimes.destroy', $ot) }}" class="d-inline"
                                  onsubmit="return confirm('Delete this OT record?')">
                                @csrf @method('DELETE')
                                <button class="btn btn-danger btn-xs"><i class="fa fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>

                    {{-- Edit Modal --}}
                    <div class="modal fade" id="editOtModal{{ $ot->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <form method="POST" action="{{ route('overtimes.update', $ot) }}">
                                @csrf @method('PUT')
                                <div class="modal-content">
                                    <div class="modal-header"><h5>Edit OT — {{ $ot->employee->first_name }} {{ \Carbon\Carbon::createFromDate($ot->year, $ot->month, 1)->format('M Y') }}</h5></div>
                                    <div class="modal-body row g-3">
                                        <div class="col-6">
                                            <label>OT Hours</label>
                                            <input type="number" name="ot_hours" class="form-control" step="0.5" min="0" value="{{ $ot->ot_hours }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Multiplier (e.g. 1.5)</label>
                                            <input type="number" name="ot_multiplier" class="form-control" step="0.25" min="1" value="{{ $ot->ot_multiplier }}" required>
                                        </div>
                                        <div class="col-12">
                                            <label>Notes</label>
                                            <textarea name="notes" class="form-control" rows="2">{{ $ot->notes }}</textarea>
                                        </div>
                                        <div class="col-12 text-muted small">
                                            OT Amount = (Basic Salary ÷ 208 hrs) × OT Hours × Multiplier
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-primary">Update</button>
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                @empty
                    <tr><td colspan="9" class="text-center text-muted">No overtime records found.</td></tr>
                @endforelse
            </tbody>
        </table>

        {{ $overtimes->links() }}

    </div>
</div>

{{-- Add OT Modal --}}
<div class="modal fade" id="addOtModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="{{ route('overtimes.store') }}">
            @csrf
            <div class="modal-content">
                <div class="modal-header"><h5>Add Overtime</h5></div>
                <div class="modal-body row g-3">
                    <div class="col-12">
                        <label>Employee</label>
                        <select name="employee_id" class="form-control" required>
                            <option value="">— Select Employee —</option>
                            @foreach($employees as $emp)
                                <option value="{{ $emp->id }}">{{ $emp->first_name }} {{ $emp->last_name }} ({{ $emp->employee_no }})</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6">
                        <label>Year</label>
                        <input type="number" name="year" class="form-control"
                               value="{{ now()->year }}" min="2000" max="2099" required>
                    </div>
                    <div class="col-6">
                        <label>Month</label>
                        <select name="month" class="form-control" required>
                            @foreach(range(1,12) as $m)
                                <option value="{{ $m }}" {{ now()->month == $m ? 'selected' : '' }}>
                                    {{ \Carbon\Carbon::createFromDate(null, $m, 1)->format('F') }}
                                </option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6">
                        <label>OT Hours</label>
                        <input type="number" name="ot_hours" class="form-control" step="0.5" min="0" required>
                    </div>
                    <div class="col-6">
                        <label>Multiplier</label>
                        <input type="number" name="ot_multiplier" class="form-control"
                               step="0.25" min="1" value="1.5" required>
                    </div>
                    <div class="col-12 text-muted small">
                        OT Amount = (Basic Salary ÷ 208 hrs) × OT Hours × Multiplier
                    </div>
                    <div class="col-12">
                        <label>Notes</label>
                        <textarea name="notes" class="form-control" rows="2"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Save OT</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection