<?php
namespace WpBookingEventAdmin\app\models;
defined('_WPBOOKINGEVENT_EXEC') or die;

use WpBookingEvent\Factory;
use WpBookingEvent\Mvc\Model\Model;
use WpBookingEvent\Mvc\Model\ModelForm;
use WpBookingEvent\Form\Form;
use WpBookingEvent\Mvc\Model\ModelList;
use WpBookingEvent\Registry\Registry;
use WpBookingEvent\Utilities\Utility;
use WpBookingEventAdmin\app\helpers\AdminHelper;
use WpBookingEventAdmin\app\Html\HtmlBackend;

class AppointmentsModel extends ModelList
{
    /**
     * @var string
     */
    public $context = "appointments";
    /**
     * @var string
     */
    public $table_name = "order";


    public function getListQuery()
    {
        $query=$this->db->getQuery(true);
        $query->select('a_order.*')
            ->from($this->getTableName('order').' AS a_order')
            ->leftJoin($this->getTableName('customer').' AS customer ON customer.id=a_order.customer_id')
            ->select('customer.first_name AS customer_first_name,customer.last_name AS customer_last_name')
        ;
        return $query;
    }

    public function getList()
    {
        $list = parent::getList(); // TODO: Change the autogenerated stub
        $query = $this->db->getQuery(true);
        foreach ($list as &$item) {
            $query->clear()
                ->select('customer.*, CONCAT(customer.first_name," ", customer.last_name) AS customer_full_name')
                ->from($this->getTableName('customer').' AS customer')
                ->where('customer.id='. (int)$item->customer_id)
            ;
            $customer=$this->db->setQuery($query)->loadObjectList();
            $item->customer=reset($customer);
        }
        return $list;
    }


}
