<?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;
use WpBookingEventFrontend\app\helpers\FrontendHelper;
use WpBookingEventFrontend\app\Html\HtmlFrontend;

class OrderModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "order";
    /**
     * @var string
     */
    public $table_name = "order";
    public function getItem($id = 0)
    {
        $item = (object)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)$item->customer_id);
        $item->customer = $this->db->setQuery($query)->loadObjectList();
        $item->customer_full_name = $item->customer[0]->first_name .' '.$item->customer[0]->last_name;
        $query->clear()
            ->select("order_detail.*")
            ->from($this->getTableName('order_detail') . ' AS order_detail')
            ->where('order_detail.order_id=' . (int)$id);
        $item->list_order_detail = $this->db->setQuery($query)->loadObjectList();
        $list_rate=[];
        foreach ($item->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('event') . ' AS event  ON event.service_id=order_detail_rate.service_id')
                ->select('event.price AS event_price')
                ->leftJoin($this->getTableName('event') . ' AS event1  ON event1.id=order_detail_rate.event_id')
                ->select('event1.image AS event_image, event1.name AS event_name, event1.duration as event_duration, event1.description as event_description ')
                ->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;
        }
        $item->list_rate = $list_rate;
        return $item;
    }
}
