@extends('layouts.main')

@section('breadcrumbs')
    {{ Breadcrumbs::render('admin-kredit.slik.show', $slik) }}
@endsection

@section('content')
    <div class="grid">
        <div class="w-full">
            <!-- Header -->
            <div class="flex flex-wrap gap-4 justify-between items-center mb-6">
                <div>
                    <h3 class="text-xl font-semibold text-gray-900">Detail SLIK</h3>
                    <p class="mt-1 text-sm text-gray-600">Informasi lengkap debitur {{ $slik->nama_debitur }}</p>
                </div>
                <div class="flex gap-2">
                    <button class="btn btn-sm btn-primary" onclick="moveToLaporan({{ $slik->id }})"
                        title="Pindahkan ke Laporan SLIK">
                        <i class="ki-filled ki-file-up"></i>
                        SLIK
                    </button>
                    <a href="{{ route('admin-kredit.slik.index') }}" class="btn btn-sm btn-light">
                        <i class="ki-filled ki-arrow-left"></i>
                        Kembali
                    </a>
                </div>
            </div>

            <!-- Card Detail -->
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">Informasi Debitur</h3>
                </div>
                <div class="card-body">
                    <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                        <!-- Informasi Debitur -->
                        <div class="space-y-4">
                            <h4 class="mb-3 text-base font-semibold text-gray-800">Data Debitur</h4>

                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Nama Debitur:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->nama_debitur ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">No. Rekening:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->no_rekening ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">CIF:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->cif ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">NPWP:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->npwp ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">No. KTP:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->no_ktp ?? '-' }}</span>
                                </div>
                            </div>
                        </div>

                        <!-- Informasi Fasilitas -->
                        <div class="space-y-4">
                            <h4 class="mb-3 text-base font-semibold text-gray-800">Data Fasilitas</h4>

                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Fasilitas:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->fasilitas ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Kolektibilitas:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->kolektibilitas ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Jenis Agunan:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->jenis_agunan ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Nilai Agunan:</span>
                                    <span class="text-sm text-gray-900">Rp
                                        {{ number_format($slik->nilai_agunan ?? 0, 0, ',', '.') }}</span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Informasi Tambahan -->
                    <div class="pt-6 mt-8 border-t border-gray-200">
                        <h4 class="mb-4 text-base font-semibold text-gray-800">Informasi Tambahan</h4>

                        <div class="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3">
                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Mulai:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_mulai ? \Carbon\Carbon::parse($slik->tanggal_mulai)->format('d/m/Y') : '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Jatuh Tempo:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_jatuh_tempo ? \Carbon\Carbon::parse($slik->tanggal_jatuh_tempo)->format('d/m/Y') : '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Restrukturisasi:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_restrukturisasi ? \Carbon\Carbon::parse($slik->tanggal_restrukturisasi)->format('d/m/Y') : '-' }}</span>
                                </div>
                            </div>

                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Kondisi:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_kondisi ? \Carbon\Carbon::parse($slik->tanggal_kondisi)->format('d/m/Y') : '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Lapor:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_lapor ? \Carbon\Carbon::parse($slik->tanggal_lapor)->format('d/m/Y') : '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Tanggal Selesai:</span>
                                    <span
                                        class="text-sm text-gray-900">{{ $slik->tanggal_selesai ? \Carbon\Carbon::parse($slik->tanggal_selesai)->format('d/m/Y') : '-' }}</span>
                                </div>
                            </div>

                            <div class="space-y-3">
                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Kode Kantor:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->kode_kantor ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Kode Cabang:</span>
                                    <span class="text-sm text-gray-900">{{ $slik->kode_cabang ?? '-' }}</span>
                                </div>

                                <div class="flex justify-between">
                                    <span class="text-sm font-medium text-gray-600">Status:</span>
                                    <span class="text-sm text-gray-900">
                                        <span
                                            class="badge badge-light-{{ $slik->status == 'aktif' ? 'success' : 'danger' }}">
                                            {{ $slik->status ?? 'aktif' }}
                                        </span>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        /**
         * Fungsi untuk memindahkan data SLIK ke laporan
         * @param {number} id - ID data SLIK yang akan dipindahkan
         */
        function moveToLaporan(id) {
            window.Swal.fire({
                title: 'Konfirmasi',
                text: 'Apakah Anda yakin ingin memindahkan data ini ke laporan SLIK?',
                icon: 'question',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Ya, Pindahkan!',
                cancelButtonText: 'Batal',
                reverseButtons: true
            }).then((result) => {
                if (result.isConfirmed) {
                    // Tampilkan loading
                    window.Swal.fire({
                        title: 'Memproses...',
                        text: 'Sedang memindahkan data ke laporan SLIK',
                        allowOutsideClick: false,
                        showConfirmButton: false,
                        willOpen: () => {
                            window.Swal.showLoading();
                        }
                    });

                    fetch(`{{ route('admin-kredit.laporan-slik.store') }}`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                                'X-CSRF-TOKEN': '{{ csrf_token() }}'
                            },
                            body: JSON.stringify({
                                slik_id: id
                            })
                        })
                        .then(response => response.json())
                        .then(data => {
                            window.Swal.close();

                            if (data.success) {
                                window.Swal.fire({
                                    title: 'Berhasil!',
                                    text: data.message || 'Data berhasil dipindahkan ke laporan SLIK',
                                    icon: 'success',
                                    confirmButtonText: 'OK'
                                }).then(() => {
                                    // Disable tombol setelah berhasil
                                    const button = document.querySelector(
                                        `button[onclick="moveToLaporan(${id})"]`);
                                    if (button) {
                                        button.disabled = true;
                                        button.innerHTML =
                                            '<i class="ki-filled ki-check"></i> Sudah di SLIK';
                                    }
                                });
                            } else {
                                window.Swal.fire({
                                    title: 'Gagal!',
                                    text: data.message || 'Gagal memindahkan data',
                                    icon: 'error',
                                    confirmButtonText: 'OK'
                                });
                            }
                        })
                        .catch(error => {
                            console.error('Error:', error);
                            window.Swal.close();
                            window.Swal.fire({
                                title: 'Error!',
                                text: 'Terjadi kesalahan saat memindahkan data',
                                icon: 'error',
                                confirmButtonText: 'OK'
                            });
                        });
                }
            });
        }

        // Add any additional JavaScript for detail page
        document.addEventListener('DOMContentLoaded', function() {
            // Initialize any required components
            KTApp.init();
        });
    </script>
@endpush
