@extends('layouts.main')

@section('content')
    <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
        <div class="card border border-agi-100 pb-2.5">
            <div class="card-header bg-agi-50" id="basic_settings">
                <div class="card-title flex flex-row gap-1.5">
                    Input Nilai Liquidasi (LPJ)
                </div>
                <div class="flex items-center gap-2">
                    <a href="{{ route('laporan.index') }}" class="btn btn-xs btn-info"><i class="ki-filled ki-exit-left"></i> Back</a>
                </div>
            </div>
            <div class="card-body">
                @php
                    $lpj = json_decode($permohonan->penilai->lpj);
                @endphp
                <form action="{{ route('laporan.store') }}" method="POST" class="grid gap-5" enctype="multipart/form-data">
                    @csrf
                    <input type="hidden" name="permohonan_id" value="{{ $permohonan->id }}">

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Nomor Registrasi
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input readonly type="text" name="nomor_registrasi" id="nomor_registrasi" class="input w-full @error('nomor_registrasi') border-danger bg-danger-light @enderror" value="{{ old('nomor_registrasi', $permohonan->nomor_registrasi ?? '') }}" placeholder="Nomor Registrasi">
                            @error('nomor_registrasi')
                            <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Debitur
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input readonly type="text" name="debitur" id="debitur" class="input w-full @error('debitur') border-danger bg-danger-light @enderror" value="{{ old('debitur', $permohonan->debiture->name ?? '') }}" placeholder="Debitur">
                            @error('debitur')
                            <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>
                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Total Nilai Pasar Wajar
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input type="text" name="total_niilai_pasar_wajar" id="total_niilai_pasar_wajar" class="currency input w-full @error('total_niilai_pasar_wajar') border-danger bg-danger-light @enderror" value="{{ old('total_niilai_pasar_wajar', $lpj->total_nilai_pasar_wajar ?? '') }}" placeholder="Masukkan Nilai Pasar Wajar">
                            @error('total_niilai_pasar_wajar')
                            <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Liquidasi (%)
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input type="text" name="liquidasi" max="100" min="0" id="liquidasi" class="persen input w-full @error('liquidasi') border-danger bg-danger-light @enderror" value="{{ old('liquidasi', $lpj->likuidasi ?? '') }}" placeholder="Masukkan Persentase Liquidasi">
                            @error('liquidasi')
                            <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
                        <label class="form-label max-w-56">
                            Nilai Liquidasi
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input type="text" name="nilai_liquidasi" id="nilai_liquidasi" class="currency input w-full @error('nilai_liquidasi') border-danger bg-danger-light @enderror" placeholder="Nilai Liquidasi" value="{{ old('nilai_liquidasi', $lpj->likuidasi_nilai_2 ?? '') }}">
                            @error('nilai_liquidasi')
                            <em class="alert text-danger text-sm">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex justify-end">
                        <button type="submit" class="btn btn-primary">
                            Proses
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection


@push('scripts')
    <script>
        function calculateNilaiLiquidasi() {
            const totalNilaiPasarWajar = parseFloat(document.getElementById('total_niilai_pasar_wajar').value.replace(/\./g, '').replace(',', '.'));
            const liquidasiPercentage = parseFloat(document.getElementById('liquidasi').value);
            const nilaiLiquidasiInput = document.getElementById('nilai_liquidasi');

            if (!isNaN(totalNilaiPasarWajar) && !isNaN(liquidasiPercentage)) {
                const nilaiLiquidasi = totalNilaiPasarWajar * (liquidasiPercentage / 100);
                nilaiLiquidasiInput.value = nilaiLiquidasi.toLocaleString('id-ID', {maximumFractionDigits: 0});
            } else {
                nilaiLiquidasiInput.value = '';
            }
        }

        // Calculate initially in case values are pre-filled
        document.addEventListener('DOMContentLoaded', calculateNilaiLiquidasi);

        // Ensure the calculation happens when Total Nilai Pasar Wajar changes as well
        document.getElementById('liquidasi').addEventListener('change', calculateNilaiLiquidasi);
    </script>
@endpush
