<?php
namespace WpBookingEventFrontend\app\models;
defined('_WPBOOKINGEVENT_EXEC') or die;

use WpBookingEvent\Factory;
use WpBookingEvent\Mvc\Model\ModelForm;
use WpBookingEvent\Form\Form;
use WpBookingEvent\Mvc\Model\ModelList;
use WpBookingEvent\Registry\Registry;
class EventModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "event";
    /**
     * @var string
     */
    public $table_name = "event";

    public function getItem($id = 0)
    {
        if($id>0){
            $event= parent::getItem($id); // TODO: Change the autogenerated stub
            $input=Factory::getInput();
            $input->set('id',$id);
            $query=$this->db->getQuery(true);
            $query->select('gallery.*')
                ->from($this->getTableName('gallery') . ' AS gallery')
                ->where('gallery.object_id=' . (int)$event->id)
                ->where('gallery.type=' . $query->quote("event"))
            ;
            $event->list_image=$this->db->setQuery($query)->loadObjectList();
            $query->clear()
                ->select('employee.*')
                ->from($this->getTableName('employee') . ' AS employee')
                ->leftJoin($this->getTableName('event_employee') . ' AS event_employee ON event_employee.employee_id=employee.id')
                ->where('event_employee.event_id=' . (int)$event->id);
            $list_employee = $this->db->setQuery($query)->loadObjectList();
            $list_employee_id = [];
            foreach ($list_employee as $employee) {
                $employee->full_name = $employee->first_name.' '.$employee->last_name;
                array_push($list_employee_id, $employee->id);
            }
            $event->list_employee = $list_employee;
            $event->list_employee_id = $list_employee_id;
            $query->clear()
                ->select('location.*')
                ->from($this->getTableName('location') . ' AS location')
                ->where('location.id=' . (int)$event->location_id)
            ;
            $location = $this->db->setQuery($query)->loadObjectList();
            $event->location = reset($location);
            return $event;
        }

    }

    public function getListReviewbyEventId($id = 0){
        $list_reviews=[];
        $query=$this->db->getQuery(true);
        $query->select('review.*')
            ->from($this->getTableName('review') . ' AS review')
            ->where('review.event_id=' . (int)$id)
        ;
        $list_reviews=$this->db->setQuery($query)->loadObjectList();
        foreach ($list_reviews as &$review){
            $review->rating_avg=($review->rating_service + $review->rating_staff + $review->rating_ambience)/3;
        }
        return $list_reviews;

    }

}
