<?php
namespace TourOperatorProFrontend\app\models;
defined('_TOUROPERATOR_EXEC') or die;

use TourOperatorPro\Factory as FactoryAlias;
use TourOperatorPro\Mvc\Model\ModelForm;
use TourOperatorPro\Form\Form;
use TourOperatorPro\Mvc\Model\ModelList;
use TourOperatorPro\Registry\Registry;
use TourOperatorProFrontend\app\helpers\FrontendHelper;

class ToursModel extends ModelList
{
    /**
     * @var string
     */
    public $context = "tours";
    /**
     * @var string
     */
    public $table_name = "tour";
    public function getListQuery()
    {
        $input=FactoryAlias::getInput();
        $data=$input->getData();
        $destination_id=$input->getInt('destination',0);
        $activity_id=$input->getInt('activity_id',0);
        $price_search=$input->getString('price_search',"");
        if($price_search!==""){
            $price_search=(array)json_decode($price_search);
        }
        $tour_type=[];
        if(array_key_exists('tour_type',(array)$data)) {
            $tour_type=$data['tour_type'];
            if(!empty($tour_type)){
                $tour_type=(array)$tour_type;
            }
        }
        $query=$this->db->getQuery(true);
        $query->select("a.*")
            ->from($this->getTableName("tour").' AS a')
            ->where('a.published=1')
        ;
        $query->order($this->getState('list.ordering', 'a.ordering') . ' ' . $this->getState('list.direction', 'ASC'));
        if($destination_id!=0){
            $query
                ->leftJoin($this->getTableName("tourproperty").' AS tourproperty1 ON tourproperty1.tour_id=a.id')
                ->where('tourproperty1.object_id='.(int)$destination_id)
                ->where('tourproperty1.type='.$query->quote(FrontendHelper::getTourDestinationKey()))
            ;
        }
        if($activity_id!=0){
            $query
                ->leftJoin($this->getTableName("tourproperty").' AS tourproperty3 ON tourproperty3.tour_id=a.id')
                ->where('tourproperty3.object_id='.(int)$activity_id)
                ->where('tourproperty3.type='.$query->quote(FrontendHelper::getTourActivityKey()))
            ;
        }


        if(is_array($price_search) && !empty($price_search)){
            $min=reset($price_search);
            $max=end($price_search);

            $query->where('a.price>='.(float)$min);
            $query->where('a.price<='.(float)$max);
        }
        if(is_array($tour_type) && !empty($tour_type)){

            $query
                ->leftJoin($this->getTableName("tourproperty").' AS tourproperty2 ON tourproperty2.tour_id=a.id')
                ->where('tourproperty2.object_id IN ('.implode($tour_type,",").')')
                ->where('tourproperty2.type='.$query->quote(FrontendHelper::getTourTypeKey()))
            ;
        }
        $query->group('a.id');
        return $query;
    }
    public function getList()
    {
        $db=FactoryAlias::getDBO();
        $query=$this->getListQuery();
        $list_tour = $db->setQuery($query)->loadObjectList();
        foreach ($list_tour as &$tour){
            $query->clear()
                ->select('DISTINCT tourtype.*')
                ->from($this->getTableName('tourproperty').' AS tourproperty')
                ->leftJoin($this->getTableName('tourtype').' AS tourtype ON tourtype.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourTypeKey()))
            ;
            $list_tour_type= $this->_db->setQuery($query)->loadObjectList();
            $tour->list_tour_type=$list_tour_type;

            $query->clear()
                ->select('DISTINCT location.*')
                ->from($this->getTableName('tourproperty').' AS tourproperty')
                ->leftJoin($this->getTableName('location').' AS location ON location.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDestinationKey()))
            ;
            $list_destination= $this->_db->setQuery($query)->loadObjectList();
            $tour->list_destination=$list_destination;

            $query->clear()
                ->select('DISTINCT discount.*')
                ->from($this->getTableName('tourproperty').' AS tourproperty')
                ->leftJoin($this->getTableName('discount').' AS discount ON discount.id=tourproperty.object_id')
                ->where('tour_id='.(int)$tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDiscountKey()))
            ;
            $list_discount= $this->_db->setQuery($query)->loadObjectList();
            $tour->discount= reset($list_discount);
        }
        return $list_tour;
    }
    public function getList_destination(){
        $db= FactoryAlias::getDBO();
        $query=$db->getQuery(true);
        $query->select('DISTINCT country.countryname, country.id')
            ->from($this->getTableName('country').' AS country ')
            ->leftJoin($this->getTableName('state').' AS state ON state.country_id=country.id')
            ->where('country.id=state.country_id')
            ->group('country.id')
        ;

        $list_country=$db->setQuery($query)->loadObjectList();

        foreach ($list_country as &$country) {
            $query->clear()
                ->select('location.name AS location_name, location.id AS location_id')
                ->from($this->getTableName('location').' AS location')
                ->leftJoin($this->getTableName('state'). ' AS state ON state.id=location.state_id')
                ->where('state.country_id='.(int)$country->id)
            ;
            $list_location = $db->setQuery($query)->loadObjectList();
            $country->list_location = $list_location;
        }

        return $list_country;
    }
    public function getList_tourtype(){
        $db= FactoryAlias::getDBO();
        $query=$db->getQuery(true);
        $query->select('tourtype.*')
            ->from($this->getTableName('tourtype').' AS tourtype ')
        ;

        $list_tourtype=$db->setQuery($query)->loadObjectList();
        return $list_tourtype;
    }


}
