@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('pembayaran') }}
@endsection

@section('content')
    <div class="grid gap-5 mx-auto w-full lg:gap-7.5">
        <div class="pb-2.5 border card border-agi-100">
            <div class="card-header bg-agi-50" id="basic_settings">
                <div class="flex flex-row gap-1.5 card-title">
                    Form Pembayaran Lebih Bayar
                </div>
                <div class="flex gap-2 items-center">
                    <a href="{{ route('pembayaran.lebih.index') }}" class="btn btn-xs btn-info"><i
                            class="ki-filled ki-exit-left"></i> Back</a>
                </div>
            </div>
            <div class="card-body">
                <!-- Detail Informasi -->
                <div class="p-4 mb-6 bg-gray-50 rounded-lg">
                    <h4 class="mb-4 text-lg font-semibold text-gray-800">Detail Pembayaran</h4>
                    <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                        <div>
                            <label class="text-sm font-medium text-gray-600">Nomor Registrasi:</label>
                            <p class="text-sm font-bold text-gray-800">{{ $noc->permohonan->nomor_registrasi ?? '-' }}</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Nomor Tiket:</label>
                            <p class="text-sm font-bold text-gray-800">{{ $noc->nomor_tiket ?? '-' }}</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Tanggal Permohonan:</label>
                            <p class="text-sm font-bold text-gray-800">{{ $noc->permohonan->tanggal_permohonan ?? '-' }}</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">User Pemohon:</label>
                            <p class="text-sm font-bold text-gray-800">{{ $noc->permohonan->user->name ?? '-' }}</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Cabang Pemohon:</label>
                            <p class="text-sm font-bold text-gray-800">
                                @if ($noc->permohonan->branch ?? null)
                                    {{ $noc->permohonan->branch->code }} - {{ $noc->permohonan->branch->name }}
                                @else
                                    -
                                @endif
                            </p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Debitur:</label>
                            <p class="text-sm font-bold text-gray-800">{{ $noc->permohonan->debiture->name ?? '-' }}</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Status Bayar:</label>
                            <p class="text-sm font-bold text-green-600 uppercase">Lebih Bayar</p>
                        </div>
                        <div>
                            <label class="text-sm font-medium text-gray-600">Nominal Lebih Bayar:</label>
                            <p class="text-sm font-bold text-green-600">
                                {{ formatRupiah($noc->nominal_lebih_bayar ?? 0, 2) }}</p>
                        </div>
                    </div>
                </div>

                <form action="{{ route('pembayaran.store') }}" method="POST" class="grid gap-5"
                    enctype="multipart/form-data">
                    @csrf
                    <input type="hidden" name="noc_id" value="{{ $noc->id ?? '' }}">
                    <input type="hidden" name="type" value="lebih_bayar">

                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Bukti KSL Lebih Bayar <span class="text-red-500">*</span>
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <input type="file" name="bukti_ksl_lebih_bayar" id="bukti_ksl_lebih_bayar"
                                class="file-input w-full @error('bukti_ksl_lebih_bayar') border-danger bg-danger-light @enderror"
                                accept=".pdf,.jpg,.jpeg,.png" required>
                            @error('bukti_ksl_lebih_bayar')
                                <em class="text-sm alert text-danger">{{ $message }}</em>
                            @enderror
                            <small class="mt-1 text-xs text-gray-500">
                                Format yang diizinkan: PDF, JPG, JPEG, PNG (Max: 2MB)
                            </small>
                        </div>
                    </div>

                    <div class="flex flex-wrap gap-2.5 items-baseline lg:flex-nowrap">
                        <label class="form-label max-w-56">
                            Catatan
                        </label>
                        <div class="flex flex-wrap items-baseline w-full">
                            <textarea name="catatan" id="catatan" rows="4"
                                class="textarea w-full @error('catatan') border-danger bg-danger-light @enderror"
                                placeholder="Masukkan catatan (opsional)">{{ old('catatan') }}</textarea>
                            @error('catatan')
                                <em class="text-sm alert text-danger">{{ $message }}</em>
                            @enderror
                        </div>
                    </div>

                    <div class="flex gap-2 justify-end">
                        <a href="{{ route('pembayaran.lebih.index') }}" class="btn btn-secondary">
                            Batal
                        </a>
                        <button type="submit" class="btn btn-primary">
                            <i class="ki-filled ki-check"></i>
                            Proses Pengembalian
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        // Validasi file upload
        document.getElementById('bukti_ksl_lebih_bayar').addEventListener('change', function() {
            const file = this.files[0];
            if (file) {
                // Validasi ukuran file (max 2MB)
                if (file.size > 2 * 1024 * 1024) {
                    alert('Ukuran file tidak boleh lebih dari 2MB');
                    this.value = '';
                    return;
                }

                // Validasi tipe file
                const allowedTypes = ['application/pdf', 'image/jpeg', 'image/jpg', 'image/png'];
                if (!allowedTypes.includes(file.type)) {
                    alert('Format file tidak didukung. Gunakan PDF, JPG, JPEG, atau PNG');
                    this.value = '';
                    return;
                }
            }
        });
    </script>
@endpush
