<?php

namespace YSquare\Shipcloud\Application\Model;

use OxidEsales\Eshop\Application\Model\Country;
use OxidEsales\Eshop\Core\Model\BaseModel;
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactory;
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;

class ShipcloudAddress extends BaseModel
{
    protected $sCoreTable = 'ysshipcloudaddress';
    /**
     * @var QueryBuilderFactory
     */
    protected $queryBuilderFactory;

    /**
     * Class constructor, initiates parent constructor (parent::oxBase()).
     */
    public function __construct()
    {
        parent::__construct();
        $this->init($this->sCoreTable);
        if (method_exists($this, "setDisableShopCheck")) {
            $this->setDisableShopCheck(false);
        }

        $container = ContainerFactory::getInstance()->getContainer();
        $this->queryBuilderFactory = $container->get(QueryBuilderFactoryInterface::class);
    }

    public function save()
    {
        $countryId = $this->getFieldData('oxcountryid');
        if ($countryId) {
            $country = oxNew(Country::class);
            $country->load($countryId);
            $this->assign([
                'oxcountry' => $country->getFieldData('oxtitle'),
            ]);
        }

        parent::save();
    }

    public function getData()
    {
        return [
            'company'  => $this->getFieldData('oxcompany'),
            'fname'    => $this->getFieldData('oxfname'),
            'lname'    => $this->getFieldData('oxlname'),
            'addinfo'  => $this->getFieldData('oxaddinfo'),
            'street'   => $this->getFieldData('oxstreet'),
            'streetnr' => $this->getFieldData('oxstreetnr'),
            'zip'      => $this->getFieldData('oxzip'),
            'city'     => $this->getFieldData('oxcity'),
            'country'  => $this->getFieldData('oxcountry'),
            'phone'    => $this->getFieldData('oxfon'),
        ];
    }
}