<?php

namespace App\Filament\Resources;

use App\Filament\Resources\SuratKeteranganAhliWarisResource\Pages;
use App\Models\SuratKeteranganAhliWaris;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Filament\Notifications\Notification;
use Barryvdh\DomPDF\Facade\Pdf;
use Filament\Support\RawJs;
use Illuminate\Support\HtmlString;

class SuratKeteranganAhliWarisResource extends Resource
{
    protected static ?string $model = SuratKeteranganAhliWaris::class;

    protected static ?string $navigationIcon = 'heroicon-o-users'; // Contoh ikon
    protected static ?string $navigationGroup = 'Kelola Surat';
    protected static ?string $navigationLabel = 'Surat Keterangan Ahli Waris';
    protected static ?int $navigationSort = 9; // Sesuaikan urutan navigasi
    protected static bool $shouldRegisterNavigation = false;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Data Pewaris (Almarhum/Almarhumah)')
                    ->columns(2)
                    ->schema([
                        Forms\Components\TextInput::make('nama_pewaris')
                            ->label('Nama Lengkap Pewaris')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Masukkan nama lengkap almarhum/almarhumah'),
                        Forms\Components\TextInput::make('nik_pewaris')
                            ->label('NIK Pewaris')
                            ->required()
                            ->numeric()
                            ->placeholder('Masukkan NIK pewaris (16 digit)'),
                        Forms\Components\TextInput::make('tempat_lahir_pewaris')
                            ->label('Tempat Lahir Pewaris')
                            ->nullable()
                            ->maxLength(255)
                            ->placeholder('Contoh: Tasikmalaya'),
                        Forms\Components\DatePicker::make('tanggal_lahir_pewaris')
                            ->label('Tanggal Lahir Pewaris')
                            ->nullable()
                            ->maxDate(now())
                            ->placeholder('Pilih tanggal lahir pewaris'),
                        Forms\Components\Select::make('agama_pewaris')
                            ->label('Agama Pewaris')
                            ->options([
                                'Islam' => 'Islam',
                                'Kristen Protestan' => 'Kristen Protestan',
                                'Kristen Katolik' => 'Kristen Katolik',
                                'Hindu' => 'Hindu',
                                'Buddha' => 'Buddha',
                                'Konghucu' => 'Konghucu',
                            ])
                            ->nullable()
                            ->placeholder('Pilih agama pewaris'),
                        Forms\Components\TextInput::make('pekerjaan_pewaris')
                            ->label('Pekerjaan Pewaris')
                            ->nullable()
                            ->maxLength(255)
                            ->placeholder('Contoh: Pegawai Negeri, Petani'),
                        Forms\Components\Textarea::make('alamat_terakhir_pewaris')
                            ->label('Alamat Terakhir Pewaris')
                            ->required()
                            ->rows(3)
                            ->maxLength(65535)
                            ->columnSpanFull()
                            ->placeholder('Contoh: Jl. Mawar No. 5, RT 003 RW 001, Desa Raksajaya'),
                        Forms\Components\DatePicker::make('tanggal_meninggal_pewaris')
                            ->label('Tanggal Meninggal')
                            ->required()
                            ->maxDate(now())
                            ->placeholder('Pilih tanggal meninggal pewaris'),
                        Forms\Components\TextInput::make('tempat_meninggal_pewaris')
                            ->label('Tempat Meninggal')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Contoh: Rumah Sakit Umum Daerah, Rumah Pribadi'),
                    ]),

                Forms\Components\Section::make('Data Ahli Waris (Pemohon)')
                    ->columns(2)
                    ->schema([
                        Forms\Components\TextInput::make('nama_ahli_waris')
                            ->label('Nama Lengkap Ahli Waris')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Masukkan nama lengkap ahli waris (pemohon)'),
                        Forms\Components\TextInput::make('nik_ahli_waris')
                            ->label('NIK Ahli Waris')
                            ->required()
                            ->numeric()
                            ->placeholder('Masukkan NIK ahli waris (16 digit)'),
                        Forms\Components\TextInput::make('nomor_kartu_keluarga_ahli_waris')
                            ->label('Nomor Kartu Keluarga (KK) Ahli Waris')
                            ->required()
                            ->numeric()
                            ->placeholder('Masukkan Nomor KK ahli waris (16 digit)'),
                        Forms\Components\TextInput::make('tempat_lahir_ahli_waris')
                            ->label('Tempat Lahir Ahli Waris')
                            ->nullable()
                            ->maxLength(255)
                            ->placeholder('Contoh: Tasikmalaya'),
                        Forms\Components\DatePicker::make('tanggal_lahir_ahli_waris')
                            ->label('Tanggal Lahir Ahli Waris')
                            ->nullable()
                            ->maxDate(now())
                            ->placeholder('Pilih tanggal lahir ahli waris'),
                        Forms\Components\Select::make('jenis_kelamin_ahli_waris')
                            ->label('Jenis Kelamin Ahli Waris')
                            ->options([
                                'Laki-laki' => 'Laki-laki',
                                'Perempuan' => 'Perempuan',
                            ])
                            ->nullable()
                            ->placeholder('Pilih jenis kelamin ahli waris'),
                        Forms\Components\Select::make('agama_ahli_waris')
                            ->label('Agama Ahli Waris')
                            ->options([
                                'Islam' => 'Islam',
                                'Kristen Protestan' => 'Kristen Protestan',
                                'Kristen Katolik' => 'Kristen Katolik',
                                'Hindu' => 'Hindu',
                                'Buddha' => 'Buddha',
                                'Konghucu' => 'Konghucu',
                            ])
                            ->nullable()
                            ->placeholder('Pilih agama ahli waris'),
                        Forms\Components\TextInput::make('pekerjaan_ahli_waris')
                            ->label('Pekerjaan Ahli Waris')
                            ->nullable()
                            ->maxLength(255)
                            ->placeholder('Contoh: Ibu Rumah Tangga, Swasta'),
                        Forms\Components\TextInput::make('hubungan_dengan_pewaris')
                            ->label('Hubungan dengan Pewaris')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Contoh: Anak Kandung, Istri Sah'),
                        Forms\Components\Textarea::make('alamat_ahli_waris')
                            ->label('Alamat Ahli Waris')
                            ->required()
                            ->rows(3)
                            ->maxLength(65535)
                            ->columnSpanFull()
                            ->placeholder('Contoh: Jl. Kenanga No. 10, RT 001 RW 002, Desa Raksajaya'),
                    ]),

                Forms\Components\Section::make('Dokumen Pendukung')
                    ->columns(2)
                    ->schema([
                        Forms\Components\FileUpload::make('foto_ktp_pewaris_path')
                            ->label('Foto KTP Pewaris (Jika Ada)')
                            ->image()
                            ->disk('public')
                            ->directory('skaw_dokumen/foto_ktp_pewaris')
                            ->nullable()
                            ->helperText('Unggah foto KTP pewaris (jika ada).'),
                        Forms\Components\FileUpload::make('foto_ktp_ahli_waris_path')
                            ->label('Foto KTP Ahli Waris')
                            ->image()
                            ->disk('public')
                            ->directory('skaw_dokumen/foto_ktp_ahli_waris')
                            ->required()
                            ->helperText('Unggah foto KTP ahli waris.'),
                        Forms\Components\FileUpload::make('foto_kartu_keluarga_path')
                            ->label('Foto Kartu Keluarga (KK)')
                            ->acceptedFileTypes(['image/*', 'application/pdf'])
                            ->disk('public')
                            ->directory('skaw_dokumen/foto_kk')
                            ->required()
                            ->helperText('Unggah foto Kartu Keluarga (yang mencantumkan pewaris dan/atau ahli waris).'),
                        Forms\Components\FileUpload::make('file_surat_kematian_pewaris_path')
                            ->label('File Surat Kematian Pewaris')
                            ->acceptedFileTypes(['application/pdf', 'image/*'])
                            ->disk('public')
                            ->directory('skaw_dokumen/surat_kematian')
                            ->required()
                            ->helperText('Unggah file Surat Kematian pewaris (format PDF/Gambar).'),
                        Forms\Components\FileUpload::make('file_akta_kelahiran_ahli_waris_path')
                            ->label('File Akta Kelahiran Ahli Waris')
                            ->acceptedFileTypes(['application/pdf', 'image/*'])
                            ->disk('public')
                            ->directory('skaw_dokumen/akta_lahir')
                            ->required()
                            ->helperText('Unggah file Akta Kelahiran ahli waris (format PDF/Gambar).'),
                        Forms\Components\FileUpload::make('file_akta_nikah_path')
                            ->label('File Akta Nikah (Jika Berlaku)')
                            ->acceptedFileTypes(['application/pdf', 'image/*'])
                            ->disk('public')
                            ->directory('skaw_dokumen/akta_nikah')
                            ->nullable()
                            ->helperText('Unggah file Akta Nikah (jika hubungan pewaris adalah suami/istri).'),
                        Forms\Components\FileUpload::make('file_surat_pengantar_path')
                            ->label('File Surat Pengantar (Optional)')
                            ->acceptedFileTypes(['application/pdf', 'image/*'])
                            ->disk('public')
                            ->directory('skaw_dokumen/surat_pengantar')
                            ->nullable()
                            ->helperText('Unggah surat pengantar dari RT/RW jika ada (format PDF/Gambar).'),

                        // ----- PLACEHOLDER UNTUK LINK UNDUH DI HALAMAN EDIT -----
                        Forms\Components\Section::make('Link Dokumen Terunggah')
                            ->description('Klik link di bawah untuk melihat atau mengunduh dokumen yang telah diunggah.')
                            ->visibleOn('edit') // Hanya tampil di halaman edit
                            ->columns(2)
                            ->schema([
                                Forms\Components\Placeholder::make('download_foto_ktp_pewaris_placeholder')
                                    ->label('Foto KTP Pewaris')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->foto_ktp_pewaris_path) {
                                            $url = Storage::url($record->foto_ktp_pewaris_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Foto KTP Pewaris</a>");
                                        }
                                        return 'Belum ada foto KTP Pewaris.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->foto_ktp_pewaris_path)), // Only show if file exists

                                Forms\Components\Placeholder::make('download_foto_ktp_ahli_waris_placeholder')
                                    ->label('Foto KTP Ahli Waris')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->foto_ktp_ahli_waris_path) {
                                            $url = Storage::url($record->foto_ktp_ahli_waris_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Foto KTP Ahli Waris</a>");
                                        }
                                        return 'Belum ada foto KTP Ahli Waris.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->foto_ktp_ahli_waris_path)),

                                Forms\Components\Placeholder::make('download_foto_kartu_keluarga_placeholder')
                                    ->label('Foto Kartu Keluarga (KK)')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->foto_kartu_keluarga_path) {
                                            $url = Storage::url($record->foto_kartu_keluarga_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Foto KK</a>");
                                        }
                                        return 'Belum ada foto Kartu Keluarga.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->foto_kartu_keluarga_path)),

                                Forms\Components\Placeholder::make('download_file_surat_kematian_pewaris_placeholder')
                                    ->label('File Surat Kematian Pewaris')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->file_surat_kematian_pewaris_path) {
                                            $url = Storage::url($record->file_surat_kematian_pewaris_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Surat Kematian</a>");
                                        }
                                        return 'Belum ada file Surat Kematian Pewaris.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->file_surat_kematian_pewaris_path)),

                                Forms\Components\Placeholder::make('download_file_akta_kelahiran_ahli_waris_placeholder')
                                    ->label('File Akta Kelahiran Ahli Waris')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->file_akta_kelahiran_ahli_waris_path) {
                                            $url = Storage::url($record->file_akta_kelahiran_ahli_waris_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Akta Kelahiran</a>");
                                        }
                                        return 'Belum ada file Akta Kelahiran Ahli Waris.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->file_akta_kelahiran_ahli_waris_path)),

                                Forms\Components\Placeholder::make('download_file_akta_nikah_placeholder')
                                    ->label('File Akta Nikah')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->file_akta_nikah_path) {
                                            $url = Storage::url($record->file_akta_nikah_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Akta Nikah</a>");
                                        }
                                        return 'Belum ada file Akta Nikah.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->file_akta_nikah_path)),

                                Forms\Components\Placeholder::make('download_file_surat_pengantar_placeholder')
                                    ->label('File Surat Pengantar')
                                    ->content(function (?SuratKeteranganAhliWaris $record) {
                                        if ($record && $record->file_surat_pengantar_path) {
                                            $url = Storage::url($record->file_surat_pengantar_path);
                                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh/Lihat Surat Pengantar</a>");
                                        }
                                        return 'Belum ada file Surat Pengantar.';
                                    })
                                    ->visible(fn (?SuratKeteranganAhliWaris $record) => filled($record?->file_surat_pengantar_path)),
                            ]),
                    ]),

                Forms\Components\Section::make('Detail Surat')
                    ->columns(1)
                    ->schema([
                        Forms\Components\Textarea::make('keperluan')
                            ->label('Keperluan Pembuatan Surat')
                            ->required()
                            ->rows(3)
                            ->maxLength(65535)
                            ->placeholder('Contoh: Untuk pengurusan hak waris atas tanah / pencairan dana pensiun'),
                        Forms\Components\TextInput::make('nomor_surat')
                            ->label('Nomor Surat')
                            ->disabledOn('create')
                            ->helperText('Nomor surat akan dibuat otomatis saat disetujui oleh admin desa.')
                            ->placeholder('Nomor surat akan terisi otomatis'),
                        Forms\Components\DatePicker::make('tanggal_dikeluarkan')
                            ->label('Tanggal Surat Dibuat')
                            ->required()
                            ->maxDate(now())
                            ->placeholder('Pilih tanggal surat dibuat'),
                    ]),

                Forms\Components\Select::make('status')
                    ->options([
                        'menunggu_acc_admin_desa' => 'Menunggu Persetujuan Admin Desa',
                        'disetujui' => 'Disetujui',
                        'ditolak' => 'Ditolak',
                    ])
                    ->visible(fn (): bool => Auth::user()->role !== 'warga')
                    ->disabledOn('create')
                    ->visibleOn('edit')
                    ->placeholder('Pilih status surat'),

                Forms\Components\Textarea::make('catatan_penolakan')
                    ->label('Catatan Penolakan')
                    ->visible(fn (string $operation, Forms\Get $get): bool => $get('status') === 'ditolak' && $operation === 'edit')
                    ->placeholder('Masukkan alasan penolakan surat'),

                Forms\Components\Placeholder::make('file_skaw_surat')
                    ->label('File Surat Keterangan Ahli Waris')
                    ->content(function (?SuratKeteranganAhliWaris $record) {
                        if ($record && $record->file_path) {
                            $url = Storage::url($record->file_path);
                            return new HtmlString("<a href='{$url}' target='_blank' class='filament-link text-primary-600 hover:text-primary-500'>Unduh Surat SKAW</a>");
                        }
                        return 'Belum ada file surat SKAW.';
                    })
                    ->visibleOn('edit'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('nomor_surat')
                    ->label('No. Surat')
                    ->placeholder('Belum Tersedia')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('nama_pewaris')
                    ->label('Nama Pewaris')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('nama_ahli_waris')
                    ->label('Nama Ahli Waris')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\TextColumn::make('hubungan_dengan_pewaris')
                    ->label('Hubungan')
                    ->sortable(),
                Tables\Columns\TextColumn::make('tanggal_meninggal_pewaris')
                    ->label('Tgl. Meninggal')
                    ->date('d M Y')
                    ->sortable(),
                Tables\Columns\TextColumn::make('keperluan')
                    ->label('Keperluan')
                    ->limit(50)
                    ->wrap(),
                Tables\Columns\TextColumn::make('status')
                    ->badge()
                    ->color(fn (string $state): string => match ($state) {
                        'menunggu_acc_admin_desa' => 'warning',
                        'disetujui' => 'success',
                        'ditolak' => 'danger',
                    })
                    ->sortable(),
                Tables\Columns\TextColumn::make('disetujuiOleh.name')
                    ->label('Disetujui Oleh')
                    ->placeholder('Belum Disetujui')
                    ->sortable(),
                Tables\Columns\TextColumn::make('tanggal_disetujui')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\SelectFilter::make('status')
                    ->options([
                        'menunggu_acc_admin_desa' => 'Menunggu Persetujuan Admin Desa',
                        'disetujui' => 'Disetujui',
                        'ditolak' => 'Ditolak',
                    ]),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\ActionGroup::make([
                    Tables\Actions\Action::make('download_draft')
                        ->label('Unduh Draft PDF')
                        ->icon('heroicon-o-arrow-down-tray')
                        ->color('info')
                        ->action(function (SuratKeteranganAhliWaris $record) {
                            if (!$record->file_path) {
                                $html = view('pdf.surat_keterangan_ahli_waris', compact('record'))->render();
                                $pdf = Pdf::loadHtml($html);
                                $filePath = 'surat_keterangan_ahli_waris/' . uniqid() . '.pdf';
                                Storage::disk('public')->put($filePath, $pdf->output());
                                $record->update(['file_path' => $filePath]);
                                Notification::make()->title('Draft PDF berhasil digenerate.')->success()->send();
                            }
                            return response()->download(Storage::disk('public')->path($record->file_path));
                        }),

                    Tables\Actions\Action::make('setujui_surat')
                        ->label('Setujui & Unggah PDF')
                        ->icon('heroicon-o-check-circle')
                        ->color('success')
                        ->modalSubmitActionLabel('Unggah & Setujui')
                        ->form([
                            Forms\Components\TextInput::make('nomor_surat_acc')
                                ->label('Nomor Surat Resmi')
                                ->required()
                                ->unique('surat_keterangan_ahli_waris', 'nomor_surat', ignoreRecord: true)
                                ->placeholder('Contoh: 474.3/SKAW.001/DS.RKJ/V/2025'),
                            Forms\Components\FileUpload::make('signed_pdf')
                                ->label('Unggah PDF Bertanda Tangan')
                                ->acceptedFileTypes(['application/pdf'])
                                ->directory('surat_keterangan_ahli_waris')
                                ->required(),
                        ])
                        ->action(function (array $data, SuratKeteranganAhliWaris $record) {
                            DB::transaction(function () use ($data, $record) {
                                $record->update([
                                    'status' => 'disetujui',
                                    'nomor_surat' => $data['nomor_surat_acc'],
                                    'file_path' => $data['signed_pdf'],
                                    'disetujui_oleh' => Auth::id(),
                                    'tanggal_disetujui' => now(),
                                ]);

                                Notification::make()
                                    ->title('Surat Keterangan Ahli Waris berhasil disetujui.')
                                    ->success()
                                    ->send();
                            });
                        })
                        ->visible(fn (SuratKeteranganAhliWaris $record): bool => Auth::user()->role !== 'warga' && $record->status === 'menunggu_acc_admin_desa'),

                    Tables\Actions\Action::make('tolak_surat')
                        ->label('Tolak Surat')
                        ->icon('heroicon-o-x-circle')
                        ->color('danger')
                        ->modalSubmitActionLabel('Tolak')
                        ->form([
                            Forms\Components\Textarea::make('catatan_penolakan')
                                ->label('Alasan Penolakan')
                                ->required()
                                ->rows(5),
                        ])
                        ->action(function (array $data, SuratKeteranganAhliWaris $record) {
                            DB::transaction(function () use ($data, $record) {
                                $record->update([
                                    'status' => 'ditolak',
                                    'catatan_penolakan' => $data['catatan_penolakan'],
                                    'disetujui_oleh' => Auth::id(),
                                    'tanggal_disetujui' => now(),
                                ]);

                                Notification::make()
                                    ->title('Surat Keterangan Ahli Waris berhasil ditolak.')
                                    ->danger()
                                    ->send();
                            });
                        })
                        ->visible(fn (SuratKeteranganAhliWaris $record): bool => Auth::user()->role !== 'warga' && $record->status === 'menunggu_acc_admin_desa'),
                ]),
                Tables\Actions\DeleteAction::make()->visible(fn (): bool => Auth::user()->role === 'admin_desa'),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()->visible(fn (): bool => Auth::user()->role === 'admin_desa'),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListSuratKeteranganAhliWaris::route('/'),
            'create' => Pages\CreateSuratKeteranganAhliWaris::route('/create'),
            'edit' => Pages\EditSuratKeteranganAhliWaris::route('/{record}/edit'),
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                //
            ]);
    }

    public static function afterCreate(SuratKeteranganAhliWaris $record): void
    {
        try {
            $html = view('pdf.surat_keterangan_ahli_waris', compact('record'))->render();
            $pdf = Pdf::loadHtml($html);
            $filePath = 'surat_keterangan_ahli_waris/' . uniqid() . '.pdf';
            Storage::disk('public')->put($filePath, $pdf->output());

            $record->update(['file_path' => $filePath]);
            Notification::make()->title('Draft PDF surat Keterangan Ahli Waris berhasil digenerate.')->success()->send();
        } catch (\Throwable $th) {
            Notification::make()->title('Gagal mengenerate draft PDF surat Keterangan Ahli Waris.')->danger()->body($th->getMessage())->send();
            report($th);
        }
    }
}