@extends('layouts.app')
@section('title', 'Payroll')
@section('content')
<div class="card">
    <div class="card-header pb-0 d-flex justify-content-between align-items-center flex-wrap">
        <h6 class="mb-0">Payslips</h6>
        <div class="d-flex gap-2">
            <form method="GET" class="d-flex gap-2">
                <select name="month" class="form-select form-select-sm" onchange="this.form.submit()">
                    @for($m=1;$m<=12;$m++)<option value="{{ $m }}" @selected($month==$m)>{{ \Carbon\Carbon::create()->month($m)->format('M') }}</option>@endfor
                </select>
                <input type="number" name="year" value="{{ $year }}" class="form-control form-control-sm" style="width:90px" onchange="this.form.submit()">
            </form>
            <a href="{{ route('payroll.batch', ['month'=>$month,'year'=>$year]) }}" class="btn btn-sm btn-outline-primary mb-0"><i class="bi bi-file-earmark-pdf me-1"></i>Batch PDF</a>
            <a href="{{ route('payroll.create', ['month'=>$month,'year'=>$year]) }}" class="btn btn-sm btn-primary mb-0"><i class="bi bi-plus-lg me-1"></i>New Payslip</a>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive"><table class="table align-items-center mb-0">
            <thead><tr class="text-xxs text-uppercase text-secondary"><th class="ps-2">Staff</th><th>Period</th><th>Earning</th><th>Deductions</th><th>Nett</th><th>Status</th><th></th></tr></thead>
            <tbody>
            @forelse($payslips as $p)
                <tr>
                    <td class="ps-2"><a href="{{ route('payroll.show', $p) }}" class="font-weight-bold text-sm">{{ $p->staff->name }}</a>
                        <div class="text-xs text-muted">{{ $p->staff->passport_no }}</div></td>
                    <td class="text-sm">{{ $p->periodLabel() }}</td>
                    <td class="text-sm">RM {{ number_format($p->total_earning,2) }}</td>
                    <td class="text-sm">RM {{ number_format($p->total_deductions,2) }}</td>
                    <td class="text-sm font-weight-bold">RM {{ number_format($p->nett_pay,2) }}</td>
                    <td><span class="badge bg-{{ $p->status==='finalized'?'success':'secondary' }}">{{ ucfirst($p->status) }}</span></td>
                    <td class="text-end pe-2">
                        <a href="{{ route('payroll.show', $p) }}" class="text-secondary me-2"><i class="bi bi-eye"></i></a>
                        <a href="{{ route('payslips.pdf', $p) }}" class="text-secondary"><i class="bi bi-file-earmark-pdf"></i></a>
                    </td>
                </tr>
            @empty
                <tr><td colspan="7" class="text-center text-muted py-4">No payslips for this period. <a href="{{ route('payroll.create', ['month'=>$month,'year'=>$year]) }}">Create one</a>.</td></tr>
            @endforelse
            </tbody>
        </table></div>
        <div class="mt-3">{{ $payslips->links() }}</div>
    </div>
</div>
@endsection
