<?php

namespace Bookingor\Backend\Controller\Calendar;



class FullCalendarControl
{
	public static $bookedingorDb;
	public static $dp_prefix = "bookingor_";

	public function show_events_full_calender()
	{
		global $wpdb;
		self::$bookedingorDb = $wpdb;
		$customer = $wpdb->prefix . self::$dp_prefix . 'customer_add';
		$booked = $wpdb->prefix . self::$dp_prefix . 'booked_appointment';
		$services = $wpdb->prefix . self::$dp_prefix . 'services';
		$staff = $wpdb->prefix . self::$dp_prefix . 'staff_add';
		$event_calender = self::$bookedingorDb->get_results(
			$wpdb->prepare(
				"SELECT s.service_id, s.service_name, st.staff_first_name, st.staff_last_name, st.staff_icon, b.picked_date_end, b.start_time, b.end_time, b.picked_date 
		FROM %1\$s s 
		LEFT JOIN %2\$s b ON s.service_id = b.service_get_id 
		LEFT JOIN %3\$s st ON st.staff_id = b.staff_get_id",
				$services,
				$booked,
				$staff
			)
		);
		$calendarEvents = [];
		foreach ($event_calender as $event) {
			$staff_icon = wp_get_attachment_url($event->staff_icon);
			$calendarEvents[] = [
				'title' => $event->service_name,
				'start' => $event->picked_date . 'T' . $event->start_time,
				'end' => $event->picked_date . 'T' . $event->end_time,
				'employee_name' => $event->staff_first_name . ' ' . $event->staff_last_name,
				'employee_icon' => $staff_icon,
			];
		}
		echo wp_json_encode($calendarEvents);
		die();
	}
}
