<?php

use App\Http\Controllers\LaporanController;
use App\Http\Controllers\RegisterUserController;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;

Route::get('/', function () {
    return redirect('/admin');
});


Route::get('/register', [RegisterUserController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [RegisterUserController::class, 'register']);
Route::get('/register/success', [RegisterUserController::class, 'showRegistrationSuccess'])->name('register.success');

// laporan route
Route::get('/export/laporan-penduduk', [LaporanController::class, 'exportPendudukPdf'])->name('export.laporan-penduduk');
Route::get('/export/laporan-keluarga', [LaporanController::class, 'exportKeluargaPdf'])->name('export.laporan-keluarga');
Route::get('/export/laporan-kelahiran', [LaporanController::class, 'exportKelahiranPdf'])->name('export.laporan-kelahiran');
Route::get('/export/laporan-kematian', [LaporanController::class, 'exportKematianPdf'])->name('export.laporan-kematian');
Route::get('/export/laporan-pendatang', [LaporanController::class, 'exportPendatangPdf'])->name('export.laporan-pendatang');
Route::get('/export/laporan-pindah', [LaporanController::class, 'exportPindahPdf'])->name('export.laporan-pindah');
Route::get('/export/laporan-surat', [LaporanController::class, 'exportSuratPdf'])->name('export.laporan-surat');

Route::get('/surat-utama-dummy', function () {

    // --- Data Dummy untuk Surat Kematian ---
    $recordKematian = (object) [
        'id_tipe_surat' => 'surat_kematian',
        'nomor_surat' => '470/SK.002/DS.RKJ/V/2025',
        'nama_lengkap' => 'Almarhum Dummy',
        'no_hp' => '1234567890123457',
        'nik' => '9999999999999999',
        'tempat_lahir' => 'Jakarta',
        'tanggal_lahir' => '1950-01-01',
        'jenis_kelamin' => 'laki-laki',
        'pekerjaan' => 'Pensiunan',
        'agama' => 'Islam',
        'alamat' => 'Jl. Dummy No. 5, Desa Raksajaya',
        'tanggal_kematian' => '2025-05-25',
        'waktu_kematian' => '10:00',
        'tempat_kematian' => 'Rumah',
        'sebab_kematian' => 'Sakit Tua',
        'tempat_pemakaman' => 'TPU Desa Raksajaya',
        'nama_pelapor_kematian' => 'Pelapor Kematian Dummy',
        'nik_pelapor_kematian' => '8888888888888888',
        'hubungan_pelapor_dengan_almarhum' => 'Anak',
        'nama_saksi_kematian_1' => 'Saksi 1 Dummy',
        'nik_saksi_kematian_1' => '7777777777777777',
        'nama_saksi_kematian_2' => 'Saksi 2 Dummy',
        'nik_saksi_kematian_2' => '6666666666666666',
        'status' => 'disetujui',
        'disetujuiOleh' => (object) ['name' => 'Kepala Desa Dummy'],
    ];
    
    // Anda bisa memilih salah satu dummy record di bawah ini untuk dites
    // $record = $recordKelahiran;
    $record = $recordKematian;
    // ... tambahkan dummy data untuk jenis surat lainnya

    // Render the view with the chosen dummy record
    $htmlContent = view('pdf.surat_template', compact('record'))->render();
    
    $pdf = Pdf::loadHtml($htmlContent);

    // Tentukan path penyimpanan
    $folderName = 'surat_tes'; // misal: 'kelahiran', 'kematian'
    $fileName = $folderName . '_' . uniqid() . '.pdf';
    $filePath = 'surat_dokumen/' . $folderName . '/' . $fileName; // Path: surat_dokumen/kelahiran/xxx.pdf

    // Simpan PDF ke storage
    Storage::disk('public')->put($filePath, $pdf->output());

    return redirect(Storage::url($filePath));
    // Replace placeholders
    // $htmlContent = str_replace('[NAMA DESA ANDA]', 'Raksajaya', $htmlContent);
    // $htmlContent = str_replace('[NAMA KECAMATAN ANDA]', 'Sodonghilir', $htmlContent);
    // $htmlContent = str_replace('[NAMA KABUPATEN ANDA]', 'Tasikmalaya', $htmlContent);

    // return $htmlContent;
});

Route::get('/symlink', function () {
    \Illuminate\Support\Facades\Artisan::call('storage:link');
    echo 'Storage link created successfully!';
});
Route::get('/installapi', function () {
    \Illuminate\Support\Facades\Artisan::call('install:api');
    echo 'Api installed successfully!';
});

