<?php
namespace WpBookingProAdmin\app\models;
defined('_WPBOOKINGPRO_EXEC') or die;

use WpBookingPro\Factory;
use WpBookingPro\Mvc\Model\ModelForm;
use WpBookingPro\Form\Form;
use WpBookingPro\Mvc\Model\ModelList;
use WpBookingPro\Registry\Registry;
class VendorsModel extends ModelList
{
    /**
     * @var string
     */
    public $context = "vendors";
    /**
     * @var string
     */
    public $table_name = "user";

    public function getListVendor() {
        $db = Factory::getDBO();
        $query = $db->getQuery(true);
        $query->clear()
            ->select('user.*')
            ->from($this->getTableName('user').' AS user')
            ->leftJoin($this->getTableName('groupuser').' AS groupuser ON groupuser.id=user.group_user_id')
            ->where('groupuser.alias='.$query->quote('vendor'))
        ;
        $list_vendor = $db->setQuery($query)->loadObjectList();
        foreach ($list_vendor as $vendor) {
            $query->clear()
                ->select('*')
                ->from($this->getTableName('category'))
                ->where('user_id='.(int)$vendor->id)
            ;
            $list_category = $db->setQuery($query)->loadObjectList();
            $vendor->list_category = $list_category;

            $query->clear()
                ->select('*')
                ->from($this->getTableName('service'))
                ->where('user_id='.(int)$vendor->id)
            ;
            $list_service = $db->setQuery($query)->loadObjectList();
            $vendor->list_service = $list_service;

            $query->clear()
                ->select('*')
                ->from($this->getTableName('employee'))
                ->where('user_id='.(int)$vendor->id)
            ;
            $list_employee = $db->setQuery($query)->loadObjectList();
            $vendor->list_employee = $list_employee;
        }
        return  $list_vendor;
    }

}
