245 lines
10 KiB
PHP
245 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Resources\TicketResource\Pages;
|
|
use App\Filament\Resources\TicketResource\RelationManagers;
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketResponse;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Components\DatePicker;
|
|
use Filament\Forms\Components\Repeater;
|
|
use Filament\Forms\Components\Select;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Forms\Form;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Actions\Action;
|
|
use Filament\Tables\Columns\BadgeColumn;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Filters\Filter;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Illuminate\Support\HtmlString;
|
|
|
|
class TicketResource extends Resource
|
|
{
|
|
|
|
protected static ?string $model = Ticket::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-ticket';
|
|
protected static ?string $navigationGroup = 'Support';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Select::make('user_id')
|
|
->relationship('user', 'name')
|
|
->searchable()
|
|
->preload()
|
|
->required(),
|
|
|
|
Select::make('status')
|
|
->label('Status')
|
|
->options([
|
|
'open' => 'Open',
|
|
'pending' => 'In Progress',
|
|
'closed' => 'Closed',
|
|
])
|
|
->default('open')
|
|
->required(),
|
|
|
|
TextInput::make('subject')
|
|
->label('Subject')
|
|
->required()
|
|
->maxLength(255)
|
|
->columnSpanFull(),
|
|
|
|
TextArea::make('message')
|
|
->label('Message')
|
|
->required()
|
|
->rows(7)
|
|
->maxLength(2000)
|
|
->columnSpanFull(),
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->defaultSort('created_at', 'desc')
|
|
->columns([
|
|
TextColumn::make('ticket_id')
|
|
->label('Ticket ID')
|
|
->sortable(),
|
|
TextColumn::make('user.name'),
|
|
TextColumn::make('subject')
|
|
->limit(50)
|
|
->label('Subject'),
|
|
BadgeColumn::make('status')
|
|
->colors([
|
|
'success' => fn ($state) => $state === 'open',
|
|
'warning' => fn ($state) => $state === 'pending',
|
|
'danger' => fn ($state) => $state === 'closed',
|
|
])
|
|
->sortable(),
|
|
TextColumn::make('created_at')
|
|
->label('Created At')
|
|
->dateTime('F d, Y • h:i A')->sortable(),
|
|
TextColumn::make('last_response_at')
|
|
->label('Last Response')
|
|
->sortable()
|
|
->formatStateUsing(fn ($state) => $state?->diffForHumans()),
|
|
])
|
|
->filters([
|
|
SelectFilter::make('status')
|
|
->label('Status')
|
|
->options([
|
|
'open' => 'Open',
|
|
'pending' => 'In Progress',
|
|
'closed' => 'Closed',
|
|
])
|
|
->attribute('status'),
|
|
Filter::make('created_at')
|
|
->form([
|
|
DatePicker::make('created_from')->label('Created From'),
|
|
DatePicker::make('created_until')->label('Created Until'),
|
|
])
|
|
->query(function ($query, array $data) {
|
|
return $query
|
|
->when($data['created_from'], fn ($query, $date) => $query->whereDate('created_at', '>=', $date))
|
|
->when($data['created_until'], fn ($query, $date) => $query->whereDate('created_at', '<=', $date));
|
|
}),
|
|
])
|
|
// ->actions([
|
|
// Tables\Actions\EditAction::make(),
|
|
// ])
|
|
->actions([
|
|
Action::make('view')
|
|
->label('View & Respond')
|
|
->icon('heroicon-o-eye')
|
|
->form(function (Ticket $ticket): array {
|
|
return [
|
|
TextArea::make('response')
|
|
->label('Your Response')
|
|
->required()
|
|
->rows(7)
|
|
->placeholder('Type your response here...'),
|
|
|
|
Select::make('status')
|
|
->label('Ticket Status')
|
|
->options([
|
|
'open' => 'Open',
|
|
'pending' => 'In Progress',
|
|
'closed' => 'Closed',
|
|
])
|
|
->default($ticket->status === 'open' ? 'pending' : $ticket->status)
|
|
->required(),
|
|
];
|
|
})
|
|
->modalContent(function (Ticket $ticket) {
|
|
$html = '<div class="space-y-3 mb-3 text-sm">';
|
|
|
|
// Ticket Subject & Message
|
|
$html .= '<div class="bg-gray-100 dark:bg-gray-900 border p-2 rounded-md">';
|
|
$html .= '<h6 class="xs font-semibold text-gray-800 dark:text-gray-100">Subject: ' . e($ticket->subject) . '</h6>';
|
|
$html .= '<p class="mt-1 text-gray-700 dark:text-gray-300">Message: ' . nl2br(e($ticket->message)) . '</p>';
|
|
$html .= '</div>';
|
|
|
|
// Responses Section
|
|
$html .= '<h6 class="text-xs font-bold text-gray-700 dark:text-gray-200 mt-2">Previous Responses</h6>';
|
|
|
|
foreach ($ticket->responses as $response) {
|
|
$html .= '<div class="rounded-md border p-2 bg-gray-50 dark:bg-gray-800">';
|
|
$html .= '<div class="text-xs text-gray-600 dark:text-gray-300">';
|
|
$html .= '<strong>' . e($response->user->name) . '</strong>';
|
|
$html .= '<span class="ml-2 text-[11px] text-gray-500">' . e($response->created_at->diffForHumans()) . '</span>';
|
|
$html .= '</div>';
|
|
$html .= '<p class="mt-1 text-gray-800 dark:text-gray-100">' . nl2br(e($response->response)) . '</p>';
|
|
$html .= '</div>';
|
|
}
|
|
|
|
if ($ticket->responses->isEmpty()) {
|
|
$html .= '<p class="text-xs text-gray-500">No responses yet.</p>';
|
|
}
|
|
|
|
$html .= '</div>';
|
|
|
|
return new \Illuminate\Support\HtmlString($html);
|
|
})
|
|
|
|
->action(function (array $data, Ticket $ticket) {
|
|
TicketResponse::create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => auth()->id(),
|
|
'response' => $data['response'],
|
|
]);
|
|
|
|
// Update ticket status and last response time
|
|
$ticket->update([
|
|
'status' => $data['status'],
|
|
'last_response_at' => now(),
|
|
]);
|
|
|
|
// Send success notification
|
|
Notification::make()
|
|
->title('Response sent successfully!')
|
|
->success()
|
|
->send();
|
|
})
|
|
->modalHeading('View & Respond to Ticket')
|
|
->modalSubmitActionLabel('Send Reply'),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
Tables\Actions\BulkAction::make('notify_users')
|
|
->label('Send Email Notification')
|
|
->color('success')
|
|
->icon('heroicon-o-envelope')
|
|
->requiresConfirmation()
|
|
->deselectRecordsAfterCompletion()
|
|
->action(function (\Illuminate\Support\Collection $records) {
|
|
foreach ($records as $ticket) {
|
|
$responses = $ticket->responses()
|
|
->with('user')
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
if ($ticket->user && $ticket->user->email) {
|
|
\Illuminate\Support\Facades\Mail::to($ticket->user->email)
|
|
->send(new \App\Mail\TicketResponseNotification($ticket, $responses));
|
|
}
|
|
}
|
|
|
|
\Filament\Notifications\Notification::make()
|
|
->title('Email notifications sent successfully!')
|
|
->success()
|
|
->send();
|
|
}),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RelationManagers\ResponsesRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListTickets::route('/'),
|
|
'create' => Pages\CreateTicket::route('/create'),
|
|
'edit' => Pages\EditTicket::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|