<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;

/**
 * App\Models\Category
 *
 * @property int $id
 * @property int|null $parent_id
 * @property string $name
 * @property array|null $trans_name
 * @property int|null $pos
 * @property int $active
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Category[] $childrens
 * @property-read \App\Models\Category|null $parent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTransName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property string|null $slug
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductCategory[] $product_categories
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category findSimilarSlugs($attribute, $config, $slug)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category query()
 * @property string|null $headline
 * @property int|null $headline_image_id
 * @property array|null $trans_headline
 * @property-read int|null $childrens_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Image[] $image
 * @property-read int|null $image_count
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereHeadline($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereHeadlineImageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Category whereTransHeadline($value)
 * @property-read \App\Models\IqImage|null $iq_image
 * @property-read int|null $product_categories_count
 */
class Category extends Model
{
    use Sluggable;

    protected $table = 'categories';

    protected $casts = ['trans_name' => 'array', 'trans_headline' => 'array'];

    protected $fillable = [
        'parent_id', 'name', 'headline', 'pos', 'active',
    ];

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }

    public function parent()
    {
        return $this->belongsTo('App\Models\Category', 'parent_id');
    }

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

    public function product_categories()
    {
        return $this->hasMany('App\Models\ProductCategory', 'category_id', 'id');

    }

    public function iq_image()
    {
        return $this->belongsTo('App\Models\IqImage', 'headline_image_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];
        }
        return "";
    }

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

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

}
