<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


/**
 * App\Models\IqSite
 *
 * @property int $id
 * @property string $slug
 * @property string|null $headline
 * @property string|null $copy
 * @property array|null $products
 * @property array|null $set_products
 * @property int|null $iq_image_id
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\IqImage|null $iq_image
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereCopy($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereHeadline($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereIqImageId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereProducts($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereSetProducts($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\IqSite whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class IqSite extends Model
{

    protected $table = 'iq_sites';

    protected $casts = ['products' => 'array', 'set_products' => 'array'];

    protected $fillable = [
        'slug', 'headline', 'copy', 'products', 'set_products',
    ];

    public function iq_image()
    {
        return $this->belongsTo('App\Models\IqImage', 'iq_image_id');
    }


    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 getProductModels(){
        $ret = [];
        foreach($this->products as $product_id){
            $ret[] = Product::findOrFail($product_id);

        }
        return $ret;
    }

    public function getProductSetModels(){
        $ret = [];
        foreach($this->set_products as $product_id){
            $ret[] = Product::findOrFail($product_id);

        }
        return $ret;
    }

    public function hasImage(){
        if($this->iq_image_id){
           if($this->iq_image && isset($this->iq_image->slug) && $this->iq_image->active){
               return true;
           }

        }
        return false;
    }
}
