@extends('layouts.app')
@section('title', 'Payslip')
@section('content')
<div class="row">
    <div class="col-lg-8 mx-auto">
        <div class="card mb-3">
            <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-2">
                <div>
                    <h6 class="mb-0">{{ $payslip->staff->name }} — {{ $payslip->periodLabel() }}</h6>
                    <span class="badge bg-{{ $payslip->status==='finalized'?'success':'secondary' }}">{{ ucfirst($payslip->status) }}</span>
                </div>
                <div class="d-flex gap-2">
                    <a href="{{ route('payslips.pdf', $payslip) }}" class="btn btn-sm btn-outline-primary mb-0"><i class="bi bi-file-earmark-pdf me-1"></i>PDF</a>
                    @if($payslip->status!=='finalized')
                        <a href="{{ route('payroll.edit', $payslip) }}" class="btn btn-sm btn-outline-secondary mb-0"><i class="bi bi-pencil me-1"></i>Edit</a>
                        <form action="{{ route('payroll.finalize', $payslip) }}" method="POST" onsubmit="return confirm('Finalize? It can no longer be edited.')">@csrf<button class="btn btn-sm btn-primary mb-0">Finalize</button></form>
                    @endif
                </div>
            </div>
        </div>

        <div class="card"><div class="card-body">
            <div class="row">
                <div class="col-6">
                    <h6 class="text-sm text-uppercase text-muted">Gross Earning</h6>
                    <table class="table table-sm text-sm mb-0">
                        <tr><td>Basic Salary</td><td class="text-end">{{ number_format($payslip->basic_salary,2) }}</td></tr>
                        <tr><td>Overtime</td><td class="text-end">{{ number_format($payslip->ot_amount,2) }}</td></tr>
                        <tr><td>Public Holiday / Sunday</td><td class="text-end">{{ number_format($payslip->ph_amount,2) }}</td></tr>
                        <tr><td>Annual Leave</td><td class="text-end">{{ number_format($payslip->al_amount,2) }}</td></tr>
                        @foreach($payslip->allowanceLines as $l)<tr><td>{{ $l->label }}</td><td class="text-end">{{ number_format($l->amount,2) }}</td></tr>@endforeach
                        <tr class="font-weight-bold border-top"><td>Total Earning</td><td class="text-end">{{ number_format($payslip->total_earning,2) }}</td></tr>
                    </table>
                </div>
                <div class="col-6">
                    <h6 class="text-sm text-uppercase text-muted">Deductions</h6>
                    <table class="table table-sm text-sm mb-0">
                        @foreach(['epf_employee'=>'EPF','socso_employee'=>'SOCSO','eis_employee'=>'EIS','income_tax_pcb'=>'PCB','income_tax_cp38'=>'CP38','unpaid_leave'=>'Unpaid Leave','advance'=>'Advance','loan'=>'Loan'] as $f=>$lbl)
                            @if($payslip->$f > 0)<tr><td>{{ $lbl }}</td><td class="text-end">{{ number_format($payslip->$f,2) }}</td></tr>@endif
                        @endforeach
                        @foreach($payslip->deductionLines as $l)<tr><td>{{ $l->label }}</td><td class="text-end">{{ number_format($l->amount,2) }}</td></tr>@endforeach
                        <tr class="font-weight-bold border-top"><td>Total Deductions</td><td class="text-end">{{ number_format($payslip->total_deductions,2) }}</td></tr>
                    </table>
                </div>
            </div>
            <hr class="horizontal dark">
            <div class="d-flex justify-content-between h5"><span>NETT PAY</span><span class="text-primary">RM {{ number_format($payslip->nett_pay,2) }}</span></div>
        </div></div>
    </div>
</div>
@endsection
