<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * Class AttributeType
 *
 * @property int $id
 * @property int|null $parent_id
 * @property string $name
 * @property string|null $trans_name
 * @property int|null $pos
 * @property bool $active
 * @property string $slug
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Attribute|null $attribute
 * @property Collection|Attribute[] $attributes
 * @package App\Models
 * @property string|null $description
 * @property-read Collection<int, \App\Models\Attribute> $childrens
 * @property-read int|null $childrens_count
 * @property-read \App\Models\Attribute|null $parent
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType query()
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereTransName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|AttributeType whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class AttributeType extends Model
{
	protected $table = 'attribute_types';

	protected $casts = [
		'parent_id' => 'int',
		'pos' => 'int',
		'active' => 'bool',
		'trans_name' => 'array'
	];

	protected $fillable = [
		'parent_id',
		'name',
        'description',
		'trans_name',
		'pos',
		'active',
		'slug'
	];

	public function parent()
	{
		return $this->belongsTo(Attribute::class, 'parent_id');
	}

	public function childrens()
	{
		return $this->hasMany(Attribute::class, 'parent_id', 'id');
	}

	    public function setPosAttribute($value){
        $this->attributes['pos'] = is_numeric($value) ? $value : null;
    }
    
    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, ', ');
    }
}
