<?php
namespace WpBookingHotelAdmin\app\models;
defined('_WPBOOKINGHOTEL_EXEC') or die;

use WpBookingHotel\Factory;
use WpBookingHotel\Mvc\Model\ModelForm;
use WpBookingHotel\Filesystem\File as FileAlias;
use WpBookingHotel\Mvc\Model\Model;
use WpBookingHotel\Form\Form;
use WpBookingHotel\Mvc\Model\ModelList;
use WpBookingHotel\Registry\Registry;
use WpBookingHotelAdmin\app\helpers\AdminHelper;
use WpBookingHotelAdmin\app\helpers\CouponHelper;

class CouponModel extends ModelForm
{
    /**
     * @var string
     */
    public $context = "coupon";
    /**
     * @var string
     */
    public $table_name = "coupon";

    public function save($data = array())
    {
        $item= parent::save($data); // TODO: Change the autogenerated stub
        $key_coupon=AdminHelper::getHotelcouponKey();
        if(isset($data['hotel_id'])) {
            AdminHelper::deletePropertyByKey($key_coupon,$item->id);
            $data_hotel_id=$data['hotel_id'];
            CouponHelper::insertNewPropertyByKey($key_coupon,$data_hotel_id, $item->id);
            $item->{$key_hotel}=$item->id;
        }
        return $item;
    }
    public function getItem($id = 0)
    {
        $db=$this->_db;
        $query=$db->getQuery(true);
        $coupon= parent::getItem($id); // TODO: Change the autogenerated stub
        if (isset($coupon->id) && $coupon->id!=0){
            $query->clear()
                ->select('DISTINCT hotel_property.hotel_id')
                ->from($this->getTableName('hotelproperty'.' AS hotel_property'))
                ->where('object_id='.(int)$coupon->id)
                ->where('type='.$query->quote(AdminHelper::getHotelcouponKey()))
                ;
            $list_hotel=$this->_db->setQuery($query)->loadColumn();
            $coupon->hotel_id=$list_hotel;
        }
        return $coupon;
    }


}
