added support ticket and added more stat widgets

This commit is contained in:
Gitea
2025-05-17 05:11:29 +05:30
parent f30d7fa096
commit 175a7203f3
19 changed files with 975 additions and 6 deletions

View File

@@ -0,0 +1,220 @@
<?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(),
]),
]);
}
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'),
];
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;
class CreateTicket extends CreateRecord
{
protected static string $resource = TicketResource::class;
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
class EditTicket extends EditRecord
{
protected static string $resource = TicketResource::class;
protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TicketResource\Pages;
use App\Filament\Resources\TicketResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
class ListTickets extends ListRecords
{
protected static string $resource = TicketResource::class;
protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Filament\Resources\TicketResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class ResponsesRelationManager extends RelationManager
{
protected static string $relationship = 'responses';
public function form(Form $form): Form
{
return $form
->schema([
Select::make('user_id')
->relationship('user', 'name')
->searchable()
->preload()
->default(1)
->required()
->columnSpanFull(),
Textarea::make('response')
->required()
->rows(7)
->maxLength(255)
->columnSpanFull(),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('response')
->columns([
TextColumn::make('user.name'),
TextColumn::make('ip_address'),
TextColumn::make('response'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}

View File

@@ -4,6 +4,8 @@ namespace App\Filament\Widgets;
use App\Models\Log;
use App\Models\Meta;
use App\Models\PremiumEmail;
use App\Models\Ticket;
use App\Models\User;
use DB;
use Filament\Widgets\StatsOverviewWidget as BaseWidget;
@@ -15,10 +17,14 @@ class StatsOverview extends BaseWidget
{
return [
Stat::make('Total Users', $this->getUser()),
Stat::make('Customers', $this->getCustomerCount()),
Stat::make('Paid Users', $this->getUserPaid()),
Stat::make('Logs Count', $this->getLogsCount()),
Stat::make('Total Mailbox', $this->getTotalMailbox()),
Stat::make('Emails Received', $this->getTotalEmailsReceived()),
Stat::make('Emails Stored', $this->getStoreEmailsCount()),
Stat::make('Open Tickets', $this->getOpenTicketsCount()),
Stat::make('Closed Tickets', $this->getClosedTicketsCount()),
];
}
private function getUser(): int
@@ -44,4 +50,22 @@ class StatsOverview extends BaseWidget
{
return Meta::select('value')->where(['key' => 'messages_received'])->first()->value;
}
private function getCustomerCount(): int
{
return User::whereNotNull('stripe_id')->count();
}
private function getStoreEmailsCount(): int
{
return PremiumEmail::all()->count();
}
private function getOpenTicketsCount(): int
{
return Ticket::whereIn('status', ['open', 'pending'])->count();
}
private function getClosedTicketsCount(): int
{
return Ticket::whereIn('status', ['closed'])->count();
}
}