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

use TourOperatorPro\Factory;
use TourOperatorPro\Mvc\Model\ModelForm;
use TourOperatorPro\Form\Form;
use TourOperatorPro\Mvc\Model\ModelList;
use TourOperatorPro\Registry\Registry;
use TourOperatorProFrontend\app\helpers\FrontendHelper;
use TourOperatorProFrontend\app\Html\HtmlFrontend;

class OrderModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "order";
    /**
     * @var string
     */
    public $table_name = "order";
    public function getItem($id = 0)
    {
        $order = parent::getItem($id); // TODO: Change the autogenerated stub
        $query= $this->_db->getQuery(true);
        $query->clear()
            ->select('customer.*')
            ->from($this->getTableName('customer') . ' AS customer')
            ->where('customer.id=' . (int)$order->customer_id);
        $order->customer=$this->_db->setQuery($query)->loadObjectList();
        $order->customer_full_name = $order->customer[0]->first_name .' '.$order->customer[0]->last_name;
        $query->clear()
            ->select('order_detail.*')
            ->from($this->getTableName('order_detail').' AS order_detail')
            ->where('order_detail.order_id='.(int)$id);
        $order->list_order_detail=$this->_db->setQuery($query)->loadObjectList();
        $list_rate = [];
        foreach ($order->list_order_detail as $order_detail){
            $query->clear()
                ->select("order_detail_rate.*")
                ->from($this->getTableName('order_detail_rate') . ' AS order_detail_rate')
                ->leftJoin($this->getTableName('service') . ' AS service ON service.id=order_detail_rate.service_id')
                ->select('service.name as service_name,service.price as service_price,service.image as service_image, service.description AS service_description')
                ->leftJoin($this->getTableName('location') . ' AS location  ON location.id=order_detail_rate.location_id')
                ->select('location.address AS location_address')
                ->leftJoin($this->getTableName('employee') . ' AS employee  ON employee.id=order_detail_rate.employee_id')
                ->select('employee.first_name AS employee_first_name,employee.last_name AS employee_last_name,employee.cover AS employee_cover')
                ->where('order_detail_id=' . (int)$order_detail->id);
            $list_rate = $this->db->setQuery($query)->loadObjectList();
            foreach ($list_rate as &$rate) {
                $start_date = Factory::getDate($rate->start_date);
                $end_date = Factory::getDate($rate->end_date);
                $since_start = $start_date->diff($end_date);
                $rate->duration = $since_start->h. ' hours '. $since_start->i. ' minutes';
                /////////////////////
                $startDate = HtmlFrontend::render_date($rate->start_date,'Y-m-d');
                $endDate = HtmlFrontend::render_date($rate->end_date,'Y-m-d');
                $startTime = HtmlFrontend::render_date($rate->start_date,'H:i');
                $endTime = HtmlFrontend::render_date($rate->end_date,'H:i');
                $rate->list_date_detail = ($start_date == $end_date) ? $startDate : $startDate . ' - ' . $endDate;
                $rate->list_time_detail = $startTime . ' - ' . $endTime;
                $rate->employee_fullname = $rate->employee_first_name.' '.$rate->employee_last_name;
            }
            $order_detail->rate = $list_rate;
        }
        $order->list_rate = $list_rate;
        $query->clear()
            ->select('tour.*')
            ->from($this->getTableName('tour'). ' AS tour')
            ->where('tour.id='.(int)$order->list_order_detail[0]->tour_id)
            ;
        $tour=$this->_db->setQuery($query)->loadObjectList();
        foreach ($tour as &$item_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)$item_tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourTypeKey()))
            ;
            $list_tour_type= $this->_db->setQuery($query)->loadObjectList();
            $item_tour->tourtype=$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)$item_tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDestinationKey()))
            ;
            $list_destination= $this->_db->setQuery($query)->loadObjectList();
            $item_tour->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)$item_tour->id)
                ->where('type='.$query->quote(FrontendHelper::getTourDiscountKey()))
            ;
            $list_discount= $this->_db->setQuery($query)->loadObjectList();
            $item_tour->discount= reset($list_discount);
        }
        $order->tour = reset($tour);
        return $order;
    }
}
