<?php
namespace WpBookingEventAdmin\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 WpBookingEventAdmin\app\helpers\AdminHelper;

class AppointmentModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "appointment";
    /**
     * @var string
     */
    public $table_name = "order";

    /**
     * @param int $id
     * @return mixed|null
     * @throws \Exception
     */
    public function getItem($id = 0)
    {
        $table=$this->getTable();
        $key = $table->getKeyName();
        $input=Factory::getInput();
        if(!$id){
            $id=$input->getInt($key,0);
        }
        $db=Factory::getDBO();
        $query=$db->getQuery(true);
        $query->select('*')
            ->from($this->getTableName())
            ->where($key.'='.(int)$id)
        ;
        $item=$this->db->setQuery($query)->loadObject();
        $query->clear()
            ->select("order_detail.*")
            ->from($this->getTableName('order_detail') . ' AS order_detail')
            ->leftJoin($this->getTableName('employee') . ' AS employee  ON employee.id=order_detail.employee_id')
            ->select('employee.first_name AS employee_first_name,employee.last_name AS employee_last_name,employee.image AS employee_image')
            ->leftJoin($this->getTableName('service') . ' AS service ON service.id=order_detail.service_id')
            ->select('service.name AS service_name')
            ->leftJoin($this->getTableName('location') . ' AS location ON location.id=order_detail.location_id')
            ->select('location.address as location_address')
            ->where('order_id=' . (int)$item->id);
        $item->list_order_detail = $this->db->setQuery($query)->loadObjectList();
        $list_employee_full_name = [];
        $list_service_name = [];
        foreach ($item->list_order_detail as &$order_detail) {
            $order_detail->employee_full_name = $order_detail->employee_first_name. ' ' . $order_detail->employee_last_name;
            $list_employee_full_name[] = $order_detail->employee_full_name;
            $list_service_name[] = $order_detail->service_name;
            $query->clear()
                ->select("*")
                ->from($this->getTableName('order_detail_rate'))
                ->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';


            }
            $order_detail->rate = reset($list_rate);
        }
        $item->list_employee_full_name = $list_employee_full_name;
        $item->list_service_name = $list_service_name;
        return $item;
    }

}
