<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\DraftItem
 *
 * @property int $id
 * @property int $draft_id
 * @property int $draft_type_id
 * @property int|null $days_start
 * @property int|null $days_duration
 * @property string|null $name
 * @property float|null $price_adult
 * @property int|null $adult
 * @property float|null $price_children
 * @property int|null $children
 * @property string|null $content
 * @property int|null $pos
 * @property int $in_pdf
 * @property int $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\Draft $draft
 * @property-read \App\Models\DraftType $draft_type
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereAdult($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereChildren($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereDaysDuration($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereDaysStart($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereDraftId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereDraftTypeId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereInPdf($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem wherePriceAdult($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem wherePriceChildren($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $service
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem whereService($value)
 * @property float|null $price
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\DraftItem wherePrice($value)
 */
class DraftItem extends Model
{
    protected $connection = 'mysql';

    protected $table = 'draft_items';

    protected $fillable = [
        'pos',
    ];



    public function draft()
    {
        return $this->belongsTo('App\Models\Draft', 'draft_id');
    }

    public function draft_type()
    {
        return $this->belongsTo('App\Models\DraftType', 'draft_type_id');
    }


    public function _format_number($value){
        return preg_replace("/[^0-9,]/", "", $value);
    }

    public function setPriceAdultAttribute($value)
    {
        $value = $this->_format_number($value);
        $this->attributes['price_adult'] = floatval(str_replace(',', '.', $value));
    }

    public function getPriceAdultAttribute()
    {
        // 2 = decimal places |  '.' = decimal seperator | ',' = thousand seperator
        return number_format(($this->attributes['price_adult']), 2, ',', '.');
    }

    public function getPriceAdultRaw()
    {
        return isset($this->attributes['price_adult']) ? $this->attributes['price_adult'] : 0;
    }

    public function setPriceChildrenAttribute($value)
    {
        $value = $this->_format_number($value);
        $this->attributes['price_children'] = floatval(str_replace(',', '.', $value));
    }

    public function getPriceChildrenAttribute()
    {
        // 2 = decimal places |  '.' = decimal seperator | ',' = thousand seperator
        return number_format(($this->attributes['price_children']), 2, ',', '.');
    }

    public function getPriceChildrenRaw()
    {
        return isset($this->attributes['price_children']) ? $this->attributes['price_children'] : 0;
    }

    public function setPriceAttribute($value)
    {
        $value = $this->_format_number($value);
        $this->attributes['price'] = floatval(str_replace(',', '.', $value));
    }

    public function getPriceAttribute()
    {
        // 2 = decimal places |  '.' = decimal seperator | ',' = thousand seperator
        return number_format(($this->attributes['price']), 2, ',', '.');
    }

    public function getPriceRaw()
    {
        return isset($this->attributes['price']) ? $this->attributes['price'] : 0;
    }


}
