<title>Butiran Tuntutan Staf</title>
@extends('ui::layouts.app')

@section('content')
<style>
    body { background-color: #f0f2f5; }
    .ui.container { padding: 0 5rem 3rem 2rem; margin-top: 3rem; }
    h2.ui.header { font-size: 1.8rem; font-weight: 700; margin-bottom: .5rem; }
    .ui.table { background: white; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
    .ui.segment {
        background: white;
        border-radius: 10px;
        box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        margin-top: 2rem;
        padding: 1.5rem;
    }
    .ui.button { margin-top: 1rem; }

    .status-label {
        display: inline-block;
        padding: 6px 12px;
        border-radius: 8px;
        font-size: 0.85rem;
        font-weight: 600;
        text-transform: capitalize;
        opacity: 0.9;
    }
    .status-Diproses { background-color: rgba(33, 133, 208, 0.3); color: #1b4f72; }
    .status-Disokong { background-color: rgba(46, 204, 113, 0.3); color: #1d5d38; }
    .status-Disahkan { background-color: rgba(241, 196, 15, 0.3); color: #7a6000; }
    .status-Disemak { background-color: rgba(155, 89, 182, 0.3); color: #4a2c63; }
    .status-Diluluskan { background-color: rgba(26, 188, 156, 0.3); color: #0b5449; }
    .status-Ditolak { background-color: rgba(231, 76, 60, 0.3); color: #7e2018; }

    @media (max-width: 768px) {
        .ui.container { padding: 0 2rem; }
    }

    .totals-row td {
        font-weight: 700;
        background: #f7f7f8;
    }
</style>

<div class="ui container">
    {{-- Header --}}
    <h2 class="ui header">{{ $user->name }}</h2>
    <p><strong>Bulan:</strong> {{ \Carbon\Carbon::parse($month)->translatedFormat('F Y') }}</p>

    {{-- Back button --}}
    <a href="{{ route('mileage::HR.index', ['month' => $month]) }}" class="ui grey button">
        <i class="arrow left icon"></i> Kembali
    </a>

    {{-- Claims Table --}}
    <table class="ui celled table">
        <thead>
            <tr>
                <th>Tarikh</th>
                <th>Butiran</th>
                <th>Dari</th>
                <th>Ke</th>
                <th>Jumlah KM</th>
                <th>Tol (RM)</th>
                <th>Lain-lain (RM)</th>
                <th>Status</th>
                <th>Jumlah (RM)</th>
                <th>Tindakan</th>
            </tr>
        </thead>

        <tbody>
            @php
                $total = 0;
                $totalMileage = 0;
                $totalToll = 0;
                $totalOthers = 0;
            @endphp

            @forelse($claims as $claim)
                @php
                    $total += $claim->total_claim_amount;
                    $totalMileage += $claim->total_mileage ?? 0;
                    $totalToll += $claim->toll_amount ?? 0;
                    $totalOthers += $claim->others_amount ?? 0;
                @endphp

                <tr>
                    <td>{{ \Carbon\Carbon::parse($claim->claim_date)->format('d/m/Y') }}</td>
                    <td>{{ $claim->description ?? '-' }}</td>
                    <td>{{ $claim->distance_from ?? '-' }}</td>
                    <td>{{ $claim->distance_to ?? '-' }}</td>
                    <td>{{ $claim->total_mileage }}</td>
                    <td>{{ number_format($claim->toll_amount ?? 0, 2) }}</td>
                    <td>{{ number_format($claim->others_amount ?? 0, 2) }}</td>
                    <td>
                        <span class="status-label status-{{ $claim->status }}">
                            {{ $claim->status }}
                        </span>
                    </td>
                    <td>{{ number_format($claim->total_claim_amount, 2) }}</td>
                    <td style="text-align:center;">
                        {{-- Lihat (View) --}}
                        <a href="{{ route('mileage::HR.viewEach', ['id' => $claim->id]) }}" class="ui blue button tiny">
                            <i class="eye icon"></i> Lihat
                        </a>

                        {{-- Tolak (Reject) --}}
                        @if(!in_array($claim->status, ['Ditolak', 'Diluluskan']))
                            <form action="{{ route('mileage::HR.reject', $claim->id) }}" method="POST" style="display:inline;">
                                @csrf
                                <button type="submit" class="ui red button tiny" onclick="return confirm('Tolak tuntutan ini?')">
                                    <i class="times icon"></i> Tolak
                                </button>
                            </form>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="10" class="center aligned">Tiada tuntutan untuk bulan ini.</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    {{-- combined Calculation + Penalty Summary --}}
    @if($claims->count() > 0)
        @php
            $uniqueDates = $claims->pluck('claim_date')->unique()->count();
            $deduction = 40 * $uniqueDates;
            $claimableDistance = max(0, $totalMileage - $deduction);

            // Vehicle-based rate note
            $vehicleType = strtolower($claims->first()->jenisKenderaan->name ?? 'kereta');
            if ($vehicleType === 'motosikal' || $vehicleType === 'motorcycle') {
                $rateNote = "Motosikal – RM0.30/km";
            } else {
                $rateNote = ($claimableDistance <= 500)
                    ? "Kereta – RM0.55/km (≤500km)"
                    : "Kereta – 500km × RM0.55 + " . number_format($claimableDistance - 500, 1) . "km × RM0.50";
            }

            // Penalty data
            $latestClaim = $claims->sortByDesc('claim_date')->first();
            $penaltyAmount = $latestClaim->penalty_amount ?? 0;
            $penaltyReason = $latestClaim->penalty_reason ?? '-';
        @endphp

        <div class="ui segment">
            <h4><i class="calculator icon"></i> Ringkasan Pengiraan (Mengikut Rekod Sistem)</h4>
            <table class="ui definition table">
                <tbody>
                    <tr><td><strong>Jumlah Jarak (Sebenar)</strong></td><td>{{ number_format($totalMileage, 1) }} KM</td></tr>
                    <tr><td><strong>Jumlah Hari Tuntutan</strong></td><td>{{ $uniqueDates }} hari</td></tr>
                    <tr><td><strong>Potongan 40KM/Hari</strong></td><td>40 × {{ $uniqueDates }} = {{ $deduction }} KM</td></tr>
                    <tr><td><strong>Jumlah Jarak Layak Tuntut</strong></td><td>{{ number_format($claimableDistance, 1) }} KM</td></tr>
                    <tr><td><strong>Kadar Kiraan</strong></td><td>{{ $rateNote }}</td></tr>
                    <tr><td><strong>Jumlah Tol</strong></td><td>RM {{ number_format($totalToll, 2) }}</td></tr>
                    <tr><td><strong>Jumlah Lain-lain</strong></td><td>RM {{ number_format($totalOthers, 2) }}</td></tr>
                    <tr><td><strong>Penalti</strong></td><td>RM {{ number_format($penaltyAmount, 2) }}</td></tr>
                    <tr><td><strong>Sebab Penalti</strong></td><td>{{ $penaltyReason }}</td></tr>
                    <tr class="totals-row">
                        <td><strong>Jumlah Keseluruhan Akhir</strong></td>
                        <td><strong>RM {{ number_format($total - $penaltyAmount, 2) }}</strong></td>
                    </tr>
                </tbody>
            </table>
        </div>
    @endif

    {{-- Info Section --}}
    <div class="ui segment">
        <h4><i class="info circle icon"></i> Seksyen Pengiraan:</h4>
        <p>
            • Tuntutan bermula selepas 40KM pertama setiap hari.<br>
            • Kadar (Kereta): RM0.55/km (&lt;500km), RM0.50/km (&gt;500km).<br>
            • Kadar (Motosikal): RM0.30/km.<br>
            • Penalti dikenakan sekiranya tuntutan lewat dihantar (&gt;1 bulan).
        </p>
    </div>
</div>
@endsection
