<title>Maklumat Tuntutan</title>

@extends('ui::layouts.app')

@section('content')
<style>
    body {
        background-color: #f0f2f5;
    }
    .ui.container {
        max-width: 70% !important;
        margin-top: 2rem;
    }
    .ui.segment {
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        border-radius: 10px;
        background-color: #fff;
        padding: 2rem;
    }
    table.ui.definition.table td:first-child {
        width: 40%;
        font-weight: 600;
        background-color: #f9fafb;
    }
    .totals-row td {
        font-weight: 700;
        background: #f7f7f8;
    }
    .small-muted { font-size: .9rem; color: #666; }
</style>

<div class="ui container">
    <h2 class="ui header">Maklumat Tuntutan</h2>

    <div class="ui segment">
        <table class="ui definition table">
            <tr>
                <td>Tarikh Tuntutan</td>
                <td>{{ \Carbon\Carbon::parse($claim->claim_date)->format('d/m/Y') }}</td>
            </tr>

            <tr>
                <td>Jenis Kenderaan</td>
                <td>{{ $claim->jenisKenderaan->keterangan ?? $claim->jenisKenderaan->name ?? '-' }}</td>
            </tr>

            <tr>
                <td>Nama Projek</td>
                <td>{{ $claim->project->p_project_description ?? 'Tanpa Projek' }}</td>
            </tr>

            <tr>
                <td>Butiran Tuntutan</td>
                <td>{{ $claim->description ?? '-' }}</td>
            </tr>

            <tr>
                <td>Jarak Dari</td>
                <td>{{ $claim->distance_from ?? '-' }}</td>
            </tr>

            <tr>
                <td>Jarak Ke</td>
                <td>{{ $claim->distance_to ?? '-' }}</td>
            </tr>

            <tr>
                <td>Jumlah Perbatuan (KM)</td>
                <td>{{ number_format($claim->total_mileage ?? 0, 2) }} km</td>
            </tr>

            {{-- Claimable mileage (deduct 40km rule) --}}
            <tr>
                <td>Jumlah Perbatuan Layak Dituntut</td>
                <td>
                    @php
                        $totalMileage = floatval($claim->total_mileage ?? 0);
                        $claimable = $claim->claimable_mileage ?? max(0, $totalMileage - 40);
                    @endphp

                    {{ number_format($claimable, 2) }} km
                </td>
            </tr>

            {{-- Show per-km rate and mileage multiplication --}}
            <tr>
                <td>Kiraan (Perbatuan × Kadar)</td>
                <td>
                    @php
                        // Determine rate by vehicle
                        $jenisId = $claim->jenis_kenderaan_id ?? ($claim->jenisKenderaan->id ?? null);
                        if ($jenisId == 1) {
                            $rate = ($claimable <= 500) ? 0.55 : 0.50; // Car
                        } else {
                            $rate = 0.30; // Motorcycle
                        }
                        $mileageAmount = round($claimable * $rate, 2);
                    @endphp

                    <span class="small-muted">{{ number_format($claimable, 2) }} km × RM {{ number_format($rate, 2) }}</span>
                    &nbsp;=&nbsp;
                    <strong>RM {{ number_format($mileageAmount, 2) }}</strong>
                </td>
            </tr>

            {{-- Toll amount --}}
            <tr>
                <td>Tol (RM)</td>
                <td>RM {{ number_format(floatval($claim->toll_amount ?? 0), 2) }}</td>
            </tr>

            {{-- Others amount --}}
            <tr>
                <td>Lain-lain (RM)</td>
                <td>RM {{ number_format(floatval($claim->others_amount ?? 0), 2) }}</td>
            </tr>

            {{-- Butiran Lain-lain (always shown) --}}
            <tr>
                <td>Butiran Lain-lain</td>
                <td>{{ $claim->others_description ?: 'Tiada Nilai Lain-lain' }}</td>
            </tr>

            {{-- Subtotal: mileageAmount + toll + others --}}
            <tr>
                <td>Jumlah Tuntutan (Perjalanan + Tol + Lain-lain)</td>
                <td>
                    @php
                        $toll = floatval($claim->toll_amount ?? 0);
                        $others = floatval($claim->others_amount ?? 0);
                        $subtotal = round($mileageAmount + $toll + $others, 2);
                    @endphp
                    RM {{ number_format($subtotal, 2) }}
                </td>
            </tr>

            <tr class="totals-row">
                <td>Jumlah Akhir (RM)</td>
                <td>
                    @php
                        $final = $claim->total_claim_amount ?? round($subtotal - ($displayPenalty ?? 0), 2);
                    @endphp
                    <strong>RM {{ number_format(floatval($final), 2) }}</strong>
                </td>
            </tr>
        </table>
    </div>

    <a href="{{ route('mileage::applications.index') }}" class="ui button">
        <i class="arrow left icon"></i> Kembali
    </a>
</div>
@endsection
