161 lines
5.9 KiB
PHP
161 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Pages;
|
|
|
|
use App\Enums\VerificationStatus;
|
|
use App\Filament\Resources\Manage\Partners\PartnerResource;
|
|
use App\Models\Company;
|
|
use App\Models\User;
|
|
use App\Models\VerificationRequest;
|
|
use App\Notifications\BroadcastNotification;
|
|
use App\Services\VerificationService;
|
|
use BackedEnum;
|
|
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\Concerns\InteractsWithActions;
|
|
use Filament\Actions\Contracts\HasActions;
|
|
use Filament\Forms\Concerns\InteractsWithForms;
|
|
use Filament\Forms\Contracts\HasForms;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Pages\Page;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use UnitEnum;
|
|
|
|
class Verification extends Page implements HasActions, HasForms
|
|
{
|
|
use HasPageShield, InteractsWithActions, InteractsWithForms;
|
|
|
|
public ?Company $company = null;
|
|
|
|
protected static string|UnitEnum|null $navigationGroup = 'Kelola';
|
|
|
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::CheckBadge;
|
|
|
|
protected static ?string $navigationLabel = 'Verifikasi';
|
|
|
|
protected static ?int $navigationSort = 5;
|
|
|
|
protected static ?string $slug = 'manage/verification';
|
|
|
|
protected static ?string $title = 'Verifikasi';
|
|
|
|
public ?VerificationRequest $verificationRequest = null;
|
|
|
|
protected VerificationService $verificationService;
|
|
|
|
protected string $view = 'filament.pages.verification';
|
|
|
|
public function boot(): void
|
|
{
|
|
$this->verificationService = app(VerificationService::class);
|
|
}
|
|
|
|
public function mount(): void
|
|
{
|
|
$user = auth()->user();
|
|
$this->company = $user->company;
|
|
|
|
// Load unified verification request
|
|
if ($this->company) {
|
|
$this->verificationRequest = $this->company->verificationRequest()
|
|
->with(['reviews.reviewer'])
|
|
->latest()
|
|
->first();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get unified verification progress
|
|
*/
|
|
public function getVerificationProgress(): array
|
|
{
|
|
$progress = $this->verificationService->getVerificationProgress(
|
|
$this->company,
|
|
$this->verificationRequest
|
|
);
|
|
|
|
// Add additional data for view
|
|
$progress['missingDataButtons'] = $this->verificationService->getMissingDataButtons($this->company);
|
|
$progress['alert'] = $this->verificationService->getAlertData($this->verificationRequest);
|
|
$progress['lastSubmittedAt'] = $this->verificationRequest?->updated_at?->translatedFormat('l, d M Y H:i');
|
|
|
|
return $progress;
|
|
}
|
|
|
|
/**
|
|
* Submit unified verification action
|
|
*/
|
|
public function submitVerificationAction(): Action
|
|
{
|
|
$progress = $this->getVerificationProgress();
|
|
|
|
return Action::make('submitVerification')
|
|
->label(fn (): string => $this->verificationRequest && $this->verificationRequest->status === VerificationStatus::NEED_REVISION
|
|
? 'Kirim Ulang'
|
|
: 'Ajukan Verifikasi')
|
|
->icon('heroicon-o-paper-airplane')
|
|
->color('primary')
|
|
->requiresConfirmation()
|
|
->modalHeading('Ajukan Verifikasi')
|
|
->modalDescription(function () use ($progress): string {
|
|
if (! $progress['isDataComplete']) {
|
|
return 'Data belum lengkap. Silakan lengkapi terlebih dahulu: '.implode(', ', $progress['missingData']);
|
|
}
|
|
|
|
return 'Apakah Anda yakin ingin mengajukan verifikasi? Pastikan semua data sudah benar dan lengkap.';
|
|
})
|
|
->modalSubmitActionLabel('Ya, Ajukan')
|
|
->visible(fn (): bool => $progress['canSubmit'])
|
|
->disabled(fn (): bool => ! $progress['isDataComplete'])
|
|
->action(function () use ($progress): void {
|
|
if (! $progress['isDataComplete']) {
|
|
Notification::make()
|
|
->title('Data Belum Lengkap')
|
|
->body('Silakan lengkapi data terlebih dahulu: '.implode(', ', $progress['missingData']))
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
$isRevision = $this->verificationRequest && $this->verificationRequest->status === VerificationStatus::NEED_REVISION;
|
|
|
|
$this->verificationRequest = $this->verificationService->submitVerification(
|
|
$this->company,
|
|
$this->verificationRequest
|
|
);
|
|
|
|
Notification::make()
|
|
->title('Berhasil Diajukan! 🚀')
|
|
->body('Yeay! Pengajuan verifikasi Anda sudah terkirim ke Admin. Mohon ditunggu ya, semoga hasilnya memuaskan! ✨')
|
|
->success()
|
|
->send();
|
|
|
|
User::superAdmin()
|
|
->get()
|
|
->each(function ($admin) use ($isRevision): void {
|
|
$user = auth()->user();
|
|
|
|
$admin->notify(new BroadcastNotification([
|
|
'title' => $isRevision ? 'Pembaruan Data Verifikasi ✨' : 'Ada Pengajuan Verifikasi Baru! 🚀',
|
|
'body' => $isRevision
|
|
? "Cihuy! {$user->name} baru saja memperbarui data verifikasi untuk perusahaan {$user->company->name}. Yuk, cek perubahannya!"
|
|
: "Halo Admin! {$user->name} baru saja mengajukan verifikasi data untuk perusahaan {$user->company->name}. Segera diproses ya! 😊",
|
|
'action' => [
|
|
Action::make('view')
|
|
->label('Lihat')
|
|
->url(PartnerResource::getUrl('view', ['record' => $user])),
|
|
],
|
|
]));
|
|
});
|
|
});
|
|
}
|
|
|
|
public function getViewData(): array
|
|
{
|
|
return [
|
|
'verificationProgress' => $this->getVerificationProgress(),
|
|
];
|
|
}
|
|
}
|