<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\UserLevel
 *
 * @property int $id
 * @property string $name
 * @property array|null $trans_name
 * @property float|null $margin
 * @property int|null $pos
 * @property int $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereMargin($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereTransName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\UserLevel whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $content
 * @property array|null $trans_content
 * @method static \Illuminate\Database\Eloquent\Builder|UserLevel whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserLevel whereTransContent($value)
 * @property bool $partner_provision
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserLevelMargin[] $user_level_margins
 * @property-read int|null $user_level_margins_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\UserLevelMargin[] $user_level_margins_re
 * @property-read int|null $user_level_margins_re_count
 * @method static \Illuminate\Database\Eloquent\Builder|UserLevel wherePartnerProvision($value)
 */
class UserLevel extends Model
{
    protected $table = 'user_levels';

    protected $casts = ['trans_name' => 'array', 'trans_content' => 'array', 'partner_provision'=>'bool'];

    protected $fillable = [
        'name', 'content', 'pos', 'active', 'partner_provision',
    ];


   /* public function childrens()
    {
        return $this->hasMany('App\Models\Attribute', 'parent_id', 'id');
    }
   */


    public function user_level_margins()
    {
        return $this->hasMany('App\Models\UserLevelMargin', 'user_level_id', 'id');
    }

    public function user_level_margins_re()
    {
        return $this->hasMany('App\Models\UserLevelMargin', 'user_level_id', 'id')->orderBy('price_from', 'DESC');
    }

    public function setPosAttribute($value){
        $this->attributes['pos'] = is_numeric($value) ? $value : null;

    }


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

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

    public function getFormattedMargin()
    {
        if(!isset($this->attributes['margin'])){
            return "";
        }
        if(\App::getLocale() === "en"){
            return number_format($this->attributes['margin'], 2, '.', ',');
        }
        return number_format($this->attributes['margin'], 2, ',', '.');
    }

    public function getLang($key)
    {
        $lang = \App::getLocale();
        if ($lang === 'de') {
            return $this->{$key};
        }
        $trans = $this->getTrans($key, $lang);
        if (!$trans || $trans == '') {
            return $this->{$key};
        }
        return $trans;
    }

    public function getTrans($key, $lang)
    {
        $key = 'trans_' . $key;
        if (!empty($this->{$key}[$lang])) {
            return $this->{$key}[$lang];
        }
    }

    public function getTranNames()
    {
        $ret = "";
        foreach ((array) $this->trans_name as $value){
            $ret .= $value.', ';
        }
        return rtrim($ret, ', ');
    }
}
