<?php

namespace App\Models;

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

/**
 * App\Models\CMSContent
 *
 * @property int $id
 * @property string $name
 * @property string $slug
 * @property string $field
 * @property string|null $text
 * @property string|null $full_text
 * @property int|null $integer
 * @property float|null $decimal
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent findSimilarSlugs($attribute, $config, $slug)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereDecimal($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereField($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereFullText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereInteger($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\CMSContent whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class CMSContent extends Model
{

    use Sluggable;


    protected static $fields = [
        'text' => 'Text (190 Zeichen)',
        'full_text' => 'Full Text (50K Zeichen)',
        'integer' => 'Zahl (10 Stellen)',
        'decimal' => 'Kommazahl (10,2 Stellen)',
    ];

    protected $connection = 'mysql_stern';
    protected $table = 'c_m_s_contents';

    protected $fillable = [
        'name', 'field', 'text', 'full_text', 'integer', 'decimal',
    ];

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

    public static function getFieldsOptions($setKey = false){
        $options = self::$fields;
        $ret = "";
        foreach ($options as $key => $option){
            $attr = ($key == $setKey) ? 'selected="selected"' : '';
            $ret .= '<option value="'.$key.'" '.$attr.'>'.$option.'</option>\n';
        }
        return $ret;
    }

    public function getFieldName(){
        return isset(self::$fields[$this->field]) ? self::$fields[$this->field] : '';
    }

    public function getPreviewContent(){

        $content = $this->{$this->field};
        if(strlen($content) > 40){
            return substr($content, 0, 40)." ...";
        }
        return $content;
    }


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

    public function setDecimalAttribute($value)
    {
        $value = $this->_format_number($value);
        $value = substr($value, -13);
        $this->attributes['decimal'] = floatval(str_replace(',', '.', $value));
    }

    public function getDecimalAttribute()
    {
        if(isset($this->attributes['decimal'])){
            // 2 = decimal places |  '.' = decimal seperator | ',' = thousand seperator
            return number_format(($this->attributes['decimal']), 2, ',', '.');
        }
        return "";
    }

    public function setIntegerAttribute($value)
    {
        $value = $this->_format_number($value);
        $value = substr($value, -10);
        $this->attributes['integer'] = intval($value);
    }


}
