@extends('layouts.master')

@section('content')
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h3>Employee Advances</h3>
        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addAdvanceModal">
            <i class="fa fa-plus"></i> Add Advance
        </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>Advance Amount</th>
                    <th>Recovered</th>
                    <th>Balance</th>
                    <th>Monthly Ded.</th>
                    <th>Given Date</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($advances as $adv)
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>{{ $adv->employee->first_name }} {{ $adv->employee->last_name }}</td>
                        <td class="text-end">{{ number_format($adv->amount, 0) }}</td>
                        <td class="text-end">{{ number_format($adv->recovered, 0) }}</td>
                        <td class="text-end fw-bold text-danger">{{ number_format($adv->balance, 0) }}</td>
                        <td class="text-end">{{ number_format($adv->monthly_deduction, 0) }}</td>
                        <td>{{ \Carbon\Carbon::parse($adv->given_date)->format('d/m/Y') }}</td>
                        <td>
                            <span class="badge bg-{{ $adv->status === 'active' ? 'warning text-dark' : ($adv->status === 'recovered' ? 'success' : 'danger') }}">
                                {{ ucfirst($adv->status) }}
                            </span>
                        </td>
                        <td>
                            <button class="btn btn-warning btn-xs"
                                    data-bs-toggle="modal"
                                    data-bs-target="#editAdvModal{{ $adv->id }}">
                                <i class="fa fa-edit"></i>
                            </button>
                            <form method="POST" action="{{ route('advances.destroy', $adv) }}" class="d-inline"
                                  onsubmit="return confirm('Delete this advance?')">
                                @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="editAdvModal{{ $adv->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <form method="POST" action="{{ route('advances.update', $adv) }}">
                                @csrf @method('PUT')
                                <div class="modal-content">
                                    <div class="modal-header"><h5>Edit Advance</h5></div>
                                    <div class="modal-body row g-3">
                                        <div class="col-6">
                                            <label>Amount</label>
                                            <input type="number" name="amount" class="form-control" value="{{ $adv->amount }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Recovered</label>
                                            <input type="number" name="recovered" class="form-control" value="{{ $adv->recovered }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Monthly Deduction</label>
                                            <input type="number" name="monthly_deduction" class="form-control" value="{{ $adv->monthly_deduction }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Status</label>
                                            <select name="status" class="form-control">
                                                @foreach(['active','recovered','cancelled'] as $s)
                                                    <option value="{{ $s }}" {{ $adv->status === $s ? 'selected' : '' }}>{{ ucfirst($s) }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="col-6">
                                            <label>Given Date</label>
                                            <input type="date" name="given_date" class="form-control" value="{{ $adv->given_date->format('Y-m-d') }}" required>
                                        </div>
                                        <div class="col-12">
                                            <label>Notes</label>
                                            <textarea name="notes" class="form-control" rows="2">{{ $adv->notes }}</textarea>
                                        </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 advance records found.</td></tr>
                @endforelse
            </tbody>
        </table>

        {{ $advances->links() }}

    </div>
</div>

{{-- Add Advance Modal --}}
<div class="modal fade" id="addAdvanceModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="{{ route('advances.store') }}">
            @csrf
            <div class="modal-content">
                <div class="modal-header"><h5>Add New Advance</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>Advance Amount</label>
                        <input type="number" name="amount" class="form-control" step="0.01" min="1" required>
                    </div>
                    <div class="col-6">
                        <label>Monthly Deduction</label>
                        <input type="number" name="monthly_deduction" class="form-control" step="0.01" min="0" required>
                    </div>
                    <div class="col-6">
                        <label>Given Date</label>
                        <input type="date" name="given_date" class="form-control" required>
                    </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 Advance</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection