152 lines
No EOL
4.5 KiB
PHP
152 lines
No EOL
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Schedule;
|
|
|
|
use App\Models\ScheduledUserDish;
|
|
use Carbon\Carbon;
|
|
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()
|
|
{
|
|
$this->currentMonth = now()->month;
|
|
$this->currentYear = now()->year;
|
|
$this->generateCalendar();
|
|
}
|
|
|
|
protected $listeners = ['schedule-generated' => 'refreshCalendar'];
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.schedule.schedule-calendar');
|
|
}
|
|
|
|
public function refreshCalendar()
|
|
{
|
|
$this->generateCalendar();
|
|
}
|
|
|
|
public function generateCalendar()
|
|
{
|
|
$this->calendarDays = [];
|
|
|
|
// Get first day of the month and total days
|
|
$firstDay = Carbon::createFromDate($this->currentYear, $this->currentMonth, 1);
|
|
$daysInMonth = $firstDay->daysInMonth;
|
|
|
|
// Generate 31 days for consistency with React version
|
|
for ($day = 1; $day <= 31; $day++) {
|
|
if ($day <= $daysInMonth) {
|
|
$date = Carbon::createFromDate($this->currentYear, $this->currentMonth, $day);
|
|
|
|
// Get scheduled dishes for this date
|
|
$scheduledDishes = ScheduledUserDish::with(['user', 'dish'])
|
|
->whereDate('date', $date->format('Y-m-d'))
|
|
->get();
|
|
|
|
$this->calendarDays[] = [
|
|
'day' => $day,
|
|
'date' => $date,
|
|
'isToday' => $date->isToday(),
|
|
'scheduledDishes' => $scheduledDishes,
|
|
'isEmpty' => $scheduledDishes->isEmpty()
|
|
];
|
|
} else {
|
|
// Empty slot for days that don't exist in this month
|
|
$this->calendarDays[] = [
|
|
'day' => null,
|
|
'date' => null,
|
|
'isToday' => false,
|
|
'scheduledDishes' => collect(),
|
|
'isEmpty' => true
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
public function previousMonth()
|
|
{
|
|
if ($this->currentMonth === 1) {
|
|
$this->currentMonth = 12;
|
|
$this->currentYear--;
|
|
} else {
|
|
$this->currentMonth--;
|
|
}
|
|
$this->generateCalendar();
|
|
}
|
|
|
|
public function nextMonth()
|
|
{
|
|
if ($this->currentMonth === 12) {
|
|
$this->currentMonth = 1;
|
|
$this->currentYear++;
|
|
} else {
|
|
$this->currentMonth++;
|
|
}
|
|
$this->generateCalendar();
|
|
}
|
|
|
|
|
|
public function regenerateForUserDate($date, $userId)
|
|
{
|
|
$this->regenerateDate = $date;
|
|
$this->regenerateUserId = $userId;
|
|
$this->showRegenerateModal = true;
|
|
}
|
|
|
|
public function confirmRegenerate()
|
|
{
|
|
try {
|
|
// Delete existing scheduled dish for this user on this date
|
|
ScheduledUserDish::whereDate('date', $this->regenerateDate)
|
|
->where('user_id', $this->regenerateUserId)
|
|
->delete();
|
|
|
|
// You could call a specific regeneration method here
|
|
// For now, we'll just delete and let the user generate again
|
|
|
|
$this->showRegenerateModal = false;
|
|
$this->generateCalendar(); // Refresh calendar
|
|
|
|
session()->flash('success', 'Schedule regenerated for the selected date!');
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Error regenerating schedule: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function skipDay($date, $userId)
|
|
{
|
|
try {
|
|
// Mark this day as skipped or delete the assignment
|
|
ScheduledUserDish::whereDate('date', $date)
|
|
->where('user_id', $userId)
|
|
->delete();
|
|
|
|
$this->generateCalendar(); // Refresh calendar
|
|
|
|
session()->flash('success', 'Day skipped successfully!');
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'Error skipping day: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function cancel()
|
|
{
|
|
$this->showRegenerateModal = false;
|
|
$this->regenerateDate = null;
|
|
$this->regenerateUserId = null;
|
|
}
|
|
|
|
public function getMonthNameProperty()
|
|
{
|
|
return Carbon::createFromDate($this->currentYear, $this->currentMonth, 1)->format('F Y');
|
|
}
|
|
} |