@extends('layouts.app')

@section('title', 'Detail Permohonan - Admin PPID')

@section('content')
<div class="container">
    <div class="row mb-4">
        <div class="col-md-12">
            <div class="d-flex justify-content-between align-items-center">
                <div>
                    <h3 class="mb-1"><i class="bi bi-file-earmark-text-fill me-2"></i>Detail Permohonan Informasi</h3>
                    <p class="text-muted mb-0">{{ $permohonan->nomor_registrasi }}</p>
                </div>
                <a href="{{ route('admin.ppid.permohonan.index') }}" class="btn btn-secondary">
                    <i class="bi bi-arrow-left me-1"></i> Kembali
                </a>
            </div>
        </div>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show shadow-sm" role="alert">
            <i class="bi bi-check-circle me-2"></i>
            <strong>Berhasil!</strong> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    <div class="row">
        <!-- Detail Permohonan -->
        <div class="col-md-8">
            <div class="card card-gov shadow-sm mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-person-fill me-2"></i>Data Pemohon</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tbody>
                            <tr>
                                <td width="30%" class="fw-bold">Nomor Registrasi</td>
                                <td>: <span class="badge bg-primary">{{ $permohonan->nomor_registrasi }}</span></td>
                            </tr>
                            <tr>
                                <td class="fw-bold">Nama Pemohon</td>
                                <td>: {{ $permohonan->nama_pemohon }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold">Email</td>
                                <td>: {{ $permohonan->email }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold">No. HP</td>
                                <td>: {{ $permohonan->no_hp }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold">Alamat</td>
                                <td>: {{ $permohonan->alamat }}</td>
                            </tr>
                            <tr>
                                <td class="fw-bold">Kategori Data</td>
                                <td>:
                                    @if($permohonan->kategori_data == 'publik')
                                        <span class="badge bg-info">Publik</span>
                                    @else
                                        <span class="badge bg-warning">Private</span>
                                    @endif
                                </td>
                            </tr>
                            <tr>
                                <td class="fw-bold">Tanggal Permohonan</td>
                                <td>: {{ \Carbon\Carbon::parse($permohonan->tanggal_permohonan)->format('d F Y') }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="card card-gov shadow-sm mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-info-circle-fill me-2"></i>Detail Permohonan</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="fw-bold d-block mb-2">Informasi yang Dimohon:</label>
                        <div class="p-3 bg-light rounded">
                            {{ $permohonan->informasi_dimohon }}
                        </div>
                    </div>

                    <div class="mb-3">
                        <label class="fw-bold d-block mb-2">Tujuan Penggunaan Informasi:</label>
                        <div class="p-3 bg-light rounded">
                            {{ $permohonan->tujuan_penggunaan }}
                        </div>
                    </div>

                    @if($permohonan->catatan_admin)
                        <div class="mb-3">
                            <label class="fw-bold d-block mb-2">Catatan Admin:</label>
                            <div class="p-3 bg-warning bg-opacity-10 rounded border border-warning">
                                {{ $permohonan->catatan_admin }}
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Update Status -->
        <div class="col-md-4">
            <div class="card card-gov shadow-sm mb-4">
                <div class="card-header">
                    <h5 class="mb-0"><i class="bi bi-gear-fill me-2"></i>Status Permohonan</h5>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <label class="fw-bold d-block mb-2">Status Saat Ini:</label>
                        @if($permohonan->status == 'pending')
                            <span class="badge bg-secondary fs-6">
                                <i class="bi bi-clock"></i> Pending
                            </span>
                        @elseif($permohonan->status == 'diproses')
                            <span class="badge bg-info fs-6">
                                <i class="bi bi-gear"></i> Diproses
                            </span>
                        @elseif($permohonan->status == 'disetujui')
                            <span class="badge bg-success fs-6">
                                <i class="bi bi-check-circle"></i> Disetujui
                            </span>
                        @elseif($permohonan->status == 'ditolak')
                            <span class="badge bg-danger fs-6">
                                <i class="bi bi-x-circle"></i> Ditolak
                            </span>
                        @elseif($permohonan->status == 'keberatan')
                            <span class="badge bg-warning fs-6">
                                <i class="bi bi-exclamation-triangle"></i> Keberatan
                            </span>
                        @endif
                    </div>

                    @if($permohonan->tanggal_ditindaklanjuti)
                        <div class="mb-3">
                            <label class="fw-bold d-block mb-2">Ditindaklanjuti:</label>
                            <small class="text-muted">
                                {{ \Carbon\Carbon::parse($permohonan->tanggal_ditindaklanjuti)->format('d F Y') }}
                            </small>
                        </div>
                    @endif

                    <hr>

                    <form action="{{ route('admin.ppid.permohonan.update-status', $permohonan->id) }}" method="POST">
                        @csrf

                        <div class="mb-3">
                            <label for="status" class="form-label fw-bold">Ubah Status:</label>
                            <select class="form-select @error('status') is-invalid @enderror"
                                    id="status"
                                    name="status"
                                    required>
                                <option value="">Pilih Status</option>
                                <option value="pending" {{ $permohonan->status == 'pending' ? 'selected' : '' }}>Pending</option>
                                <option value="diproses" {{ $permohonan->status == 'diproses' ? 'selected' : '' }}>Diproses</option>
                                <option value="disetujui" {{ $permohonan->status == 'disetujui' ? 'selected' : '' }}>Disetujui</option>
                                <option value="ditolak" {{ $permohonan->status == 'ditolak' ? 'selected' : '' }}>Ditolak</option>
                                <option value="keberatan" {{ $permohonan->status == 'keberatan' ? 'selected' : '' }}>Keberatan</option>
                            </select>
                            @error('status')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="mb-3">
                            <label for="catatan_admin" class="form-label fw-bold">Catatan Admin:</label>
                            <textarea class="form-control @error('catatan_admin') is-invalid @enderror"
                                      id="catatan_admin"
                                      name="catatan_admin"
                                      rows="4"
                                      placeholder="Tambahkan catatan untuk permohonan ini...">{{ old('catatan_admin', $permohonan->catatan_admin) }}</textarea>
                            @error('catatan_admin')
                                <div class="invalid-feedback">{{ $message }}</div>
                            @enderror
                        </div>

                        <button type="submit" class="btn btn-gov-primary w-100">
                            <i class="bi bi-save me-1"></i> Update Status
                        </button>
                    </form>
                </div>
            </div>

            <div class="card card-gov shadow-sm">
                <div class="card-header bg-danger text-white">
                    <h5 class="mb-0"><i class="bi bi-trash-fill me-2"></i>Zona Berbahaya</h5>
                </div>
                <div class="card-body">
                    <p class="text-muted small mb-3">Hapus permohonan ini secara permanen dari database</p>
                    <button type="button"
                            class="btn btn-danger w-100"
                            onclick="confirmDelete()">
                        <i class="bi bi-trash me-1"></i> Hapus Permohonan
                    </button>
                    <form id="delete-form"
                          action="{{ route('admin.ppid.permohonan.destroy', $permohonan->id) }}"
                          method="POST"
                          style="display: none;">
                        @csrf
                        @method('DELETE')
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
function confirmDelete() {
    if (confirm('Apakah Anda yakin ingin menghapus permohonan ini? Tindakan ini tidak dapat dibatalkan!')) {
        document.getElementById('delete-form').submit();
    }
}
</script>
@endpush
@endsection
