154 lines
4.3 KiB
PHP
154 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Schedule;
|
|
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use DishPlanner\Schedule\Services\ScheduleCalendarService;
|
|
use DishPlanner\ScheduledUserDish\Actions\DeleteScheduledUserDishForDateAction;
|
|
use DishPlanner\ScheduledUserDish\Actions\SkipScheduledUserDishForDateAction;
|
|
use Exception;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Livewire\Component;
|
|
|
|
class ScheduleCalendar extends Component
|
|
{
|
|
public $currentMonth;
|
|
public $currentYear;
|
|
public $calendarDays = [];
|
|
public $showRegenerateModal = false;
|
|
public $regenerateDate = null;
|
|
public $regenerateUserId = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
$this->currentMonth = now()->month;
|
|
$this->currentYear = now()->year;
|
|
$this->loadCalendar();
|
|
}
|
|
|
|
protected $listeners = ['schedule-generated' => 'refreshCalendar'];
|
|
|
|
public function render(): View
|
|
{
|
|
return view('livewire.schedule.schedule-calendar');
|
|
}
|
|
|
|
public function refreshCalendar(): void
|
|
{
|
|
$this->loadCalendar();
|
|
}
|
|
|
|
public function loadCalendar(): void
|
|
{
|
|
$service = new ScheduleCalendarService();
|
|
$this->calendarDays = $service->getCalendarDays(
|
|
auth()->user(),
|
|
$this->currentMonth,
|
|
$this->currentYear
|
|
);
|
|
}
|
|
|
|
public function previousMonth(): void
|
|
{
|
|
if ($this->currentMonth === 1) {
|
|
$this->currentMonth = 12;
|
|
$this->currentYear--;
|
|
} else {
|
|
$this->currentMonth--;
|
|
}
|
|
$this->loadCalendar();
|
|
}
|
|
|
|
public function nextMonth(): void
|
|
{
|
|
if ($this->currentMonth === 12) {
|
|
$this->currentMonth = 1;
|
|
$this->currentYear++;
|
|
} else {
|
|
$this->currentMonth++;
|
|
}
|
|
$this->loadCalendar();
|
|
}
|
|
|
|
public function regenerateForUserDate($date, $userId): void
|
|
{
|
|
if (!$this->authorizeUser($userId)) {
|
|
session()->flash('error', 'Unauthorized action.');
|
|
return;
|
|
}
|
|
|
|
$this->regenerateDate = $date;
|
|
$this->regenerateUserId = $userId;
|
|
$this->showRegenerateModal = true;
|
|
}
|
|
|
|
public function confirmRegenerate(): void
|
|
{
|
|
try {
|
|
if (!$this->authorizeUser($this->regenerateUserId)) {
|
|
session()->flash('error', 'Unauthorized action.');
|
|
return;
|
|
}
|
|
|
|
$action = new DeleteScheduledUserDishForDateAction();
|
|
$action->execute(
|
|
auth()->user(),
|
|
Carbon::parse($this->regenerateDate),
|
|
$this->regenerateUserId
|
|
);
|
|
|
|
$this->showRegenerateModal = false;
|
|
$this->loadCalendar();
|
|
|
|
session()->flash('success', 'Schedule regenerated for the selected date!');
|
|
} catch (Exception $e) {
|
|
Log::error('Schedule regeneration failed', ['exception' => $e, 'date' => $this->regenerateDate]);
|
|
session()->flash('error', 'Unable to regenerate schedule. Please try again.');
|
|
}
|
|
}
|
|
|
|
public function skipDay($date, $userId): void
|
|
{
|
|
try {
|
|
if (!$this->authorizeUser($userId)) {
|
|
session()->flash('error', 'Unauthorized action.');
|
|
return;
|
|
}
|
|
|
|
$action = new SkipScheduledUserDishForDateAction();
|
|
$action->execute(
|
|
auth()->user(),
|
|
Carbon::parse($date),
|
|
$userId
|
|
);
|
|
|
|
$this->loadCalendar();
|
|
|
|
session()->flash('success', 'Day skipped successfully!');
|
|
} catch (Exception $e) {
|
|
Log::error('Skip day failed', ['exception' => $e, 'date' => $date, 'userId' => $userId]);
|
|
session()->flash('error', 'Unable to skip day. Please try again.');
|
|
}
|
|
}
|
|
|
|
private function authorizeUser(int $userId): bool
|
|
{
|
|
$user = User::find($userId);
|
|
return $user && $user->planner_id === auth()->id();
|
|
}
|
|
|
|
public function cancel(): void
|
|
{
|
|
$this->showRegenerateModal = false;
|
|
$this->regenerateDate = null;
|
|
$this->regenerateUserId = null;
|
|
}
|
|
|
|
public function getMonthNameProperty(): string
|
|
{
|
|
$service = new ScheduleCalendarService();
|
|
return $service->getMonthName($this->currentMonth, $this->currentYear);
|
|
}
|
|
}
|