<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\SidebarWidget
 *
 * @property int $id
 * @property string $name
 * @property string|null $component
 * @property string|null $html
 * @property array|null $show_at
 * @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\SidebarWidget whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereComponent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereHtml($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget wherePos($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereShowAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\SidebarWidget whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class SidebarWidget extends Model
{

    protected static $shows = [
        'home' => 'Startseite',
        'search' => 'Suche',
        'default' => 'Standartseiten',
        'overview' => 'Übersicht',
        'program' => 'Programme',
        'booking' => 'Buchungen',
        'bookingconfirm' => 'Buchungsbestätigung',


    ];

    protected static $components = [
        'aboutSternToursWidget' => 'Wir: STERN TOURS',
        'searchSidebarWidget' => 'Kulturreisen suchen',
        'navSidebarWidget' => 'Reiseprogramme',
        'topVotingWidget' => 'TOP bewertet',
        'feedbacksSidebarWidget' => 'Kundenfeedback',
        'travelGuideSidebarWidget' => 'Reiseführer',
        'travelMagazineSidebarWidget' => 'Reisemagazin',
        'offersSidebarWidget' => 'Angebote',

    ];


    protected $connection = 'mysql_stern';

    protected $table = 'sidebar_widgets';

    protected $casts = ['show_at' => 'array'];

    protected $fillable = [
        'name', 'component', 'html', 'show_at', 'pos', 'active'
    ];


    public static function getComponentsOptions($setKey = false){
        $options = self::$components;
        $ret = '<option value="">Keine Komponente laden (nur HTML)</option>\n';
        foreach ($options as $key => $option){
            $attr = ($key == $setKey) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
        }
        return $ret;
    }

    public static function getShowsOptions($setKey){
        if(!is_array($setKey))
            $setKey = [];

        $options = self::$shows;
        $ret = "";
        foreach ($options as $key => $option){
            $attr =  in_array($key, $setKey)  ? 'selected="selected"' : '';
            $ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
        }
        return $ret;
    }

    public  function getShowsAtString(){
        $ret = "";
        if($this->show_at){
            foreach($this->show_at as $show_at){
                if(isset(self::$shows[$show_at])){
                    $ret .= self::$shows[$show_at].", ";
                }
            }
        }
        return rtrim($ret, ", ");
    }





    /*
     * public function getVotesDetailAttribute($details)
{
    return json_decode($details, true);
}
then when you will call $store->votes_detail you will get the expected result.

After that you can use mutators to convert an array back to JSON when it is saved back in the DB. Define the method setVotesDetailAttribute($value) as follows:

public function setVotesDetailsAttribute($value)
{
     $this->attributes['votes_detail'] = json_encode($value);
}
    */

}
