<?php
namespace  WpBookingEventBlock\block_top_event;
use WpBookingEvent\Factory;
use WpBookingEvent\Factory as FactoryAlias;
use WpBookingEvent\Mvc\Model\Model as ModelAlias;
use WpBookingEvent\Utilities\Utility as UtilityAlias;

class block_top_event_helper{
    public static function getList_event(){

        $db= FactoryAlias::getDBO();
        $query=$db->getQuery(true);
        $query->select('event.*')
            ->from(UtilityAlias::getTableName('event').' AS event ')
            ->leftJoin(UtilityAlias::getTableName('service').' AS service ON service.id=event.service_id')
            ->select('service.image as service_image , service.name as service_name, service.duration as duration')
            ->leftJoin(UtilityAlias::getTableName('location').' AS location ON location.id=event.location_id')
            ->select('location.address as location_name')
            ->leftJoin(UtilityAlias::getTableName('eventtype').' AS eventtype ON eventtype.id=event.event_type_id')
            ->select('eventtype.name as eventtype_name');
        $list=$db->setQuery($query)->loadObjectList();
        foreach ($list as &$item) {
            $query->clear()
                ->select('order_detail.*')
                ->from(UtilityAlias::getTableName('order_detail').' AS order_detail ')
                ->where('order_detail.event_id=' . (int)$item->id);
            $list_order = $db->setQuery($query)->loadObjectList();
            $item->total_order = count($list_order);
        }
        return $list;
    }

    /**
     * @return array|mixed|null
     */
    public static function get_top_event(){
        $db = FactoryAlias::getDBO();
        $query=$db->getQuery(true);
        $query->select('event.*')
            ->from(UtilityAlias::getTableName('event').' AS event ')
            ->where("event.top = 1")
            ->leftJoin(UtilityAlias::getTableName('location').' AS location ON location.id=event.location_id')
            ->select('location.address as location_name')
            ->leftJoin(UtilityAlias::getTableName('service').' AS service ON service.id=event.service_id')
            ->select('service.image as service_image , service.name as service_name, service.duration as duration')
            ->leftJoin(UtilityAlias::getTableName('eventtype').' AS eventtype ON eventtype.id=event.event_type_id')
            ->select('eventtype.name as eventtype_name')
        ;
        $list = $db->setQuery($query)->loadObjectList();
        return $list;
    }

    /**
     * @return mixed
     * @throws \Exception
     */
    public static function get_style_block_top_event(){
        $modelConfig = ModelAlias::getInstance('config');
        $item = $modelConfig->getItem(1);
        return $item->params->get('select_style_top_event','');
    }
    public static function get_style_booking(){
        $modelConfig = ModelAlias::getInstance('config');
        $item = $modelConfig->getItem(1);
        return $item->params->get('select_form_booking_style','');
    }
    public static function get_page_event_detail(){
        $modelConfig = ModelAlias::getInstance('config');
        $item = $modelConfig->getItem(1);
        return $item->params->get('page_event_detail',0);

    }
    public static function get_page_service_detail(){
        $modelConfig = ModelAlias::getInstance('config');
        $item = $modelConfig->getItem(1);
        return $item->params->get('page_service_detail',0);

    }
    public function getList()
    {
        $db = FactoryAlias::getDBO();
        $query = $db->getQuery(true)
            ->select("*")
            ->from(UtilityAlias::getTableName());
        $limit_per_page = (UtilityAlias::get_limit_per_page());
        return $db->setQuery($query,0,$limit_per_page)->loadObjectList();

    }



}