<?php

namespace App\Models;

use Carbon\Carbon;
use HTMLHelper;

/**
 * App\Models\Feedback
 *
 * @property int $id
 * @property int|null $owner
 * @property string|null $model
 * @property int|null $lvl
 * @property int $owner_second
 * @property int|null $catalog_id
 * @property int|null $catalog_index
 * @property string|null $slug
 * @property int|null $travel_program
 * @property int|null $status
 * @property int|null $show_in_navi
 * @property int|null $order
 * @property string|null $title
 * @property string|null $pagetitle
 * @property string|null $description
 * @property string|null $keywords
 * @property string|null $content
 * @property string|null $content_new
 * @property string|null $buma_destination
 * @property int|null $OLD_CatalogID
 * @property int|null $OLD_OwnerID
 * @property int|null $buma_gjr
 * @property string|null $date
 * @property int $price-tags
 * @property string|null $text_right
 * @property string|null $keyword
 * @property string|null $canonical_url
 * @property int|null $country_id
 * @property string|null $template
 * @property int|null $lft
 * @property int|null $rgt
 * @property int|null $tree_root
 * @property int|null $parent_id
 * @property string|null $real_url_path
 * @property string|null $box_body
 * @property string|null $box_image_url
 * @property string|null $box_star
 * @property string|null $box_discount
 * @property string|null $cms_settings
 * @property int|null $fewo_lodging
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Feedback[] $children
 * @property-read \App\Models\Feedback|null $parent
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBoxBody($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBoxDiscount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBoxImageUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBoxStar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBumaDestination($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereBumaGjr($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCanonicalUrl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCatalogId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCatalogIndex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCmsSettings($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereContentNew($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCountryId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereDate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereDescription($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereFewoLodging($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereKeyword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereKeywords($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereLft($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereLvl($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereModel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereOLDCatalogID($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereOLDOwnerID($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereOrder($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereOwner($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereOwnerSecond($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback wherePagetitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereParentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback wherePriceTags($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereRealUrlPath($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereRgt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereShowInNavi($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereTemplate($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereTextRight($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereTravelProgram($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereTreeRoot($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Feedback whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Feedback extends Page
{
    protected $table = 'page';

    public static function boot()
    {
        parent::boot();

        static::addGlobalScope(function ($query) {
            $query->where('model', 'feedback');
            $query->orderBy('lvl', 'ASC');
            $query->orderBy('date', 'DESC');

        });
    }

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

    public function getParent()
    {
        if ($this->lvl == 2) {
            $this->parent();
        }
        return false;
    }

    public function children()
    {
        return $this->hasMany('App\Models\Feedback', 'parent_id');
    }


    public function setContentAttribute($value)
    {
        if (!$value) {
            $this->attributes['content'] = $value;
        } else {
            $this->attributes['content'] = HTMLHelper::filterHTML($value, ['src' => ['removeHost']], true);
        }
    }



    public function getParentsArray(){
        //lvl 0
        return Page::where('model', 'feedback')->where('lvl', 1)->get()->pluck('title', 'id');

    }
    //$feedbacks = Feedback::where('lvl', 1)->get();
    public function getDateRow()
    {
        return$this->attributes['date'];
    }

    public function getDateAttribute()
    {
        return isset($this->attributes['date']) ? Carbon::parse($this->attributes['date'])->format("d.m.Y") : '';
    }

    public function setDateAttribute($value)
    {
        if (!$value) {
            $this->attributes['date'] = null;
        } else {
            $this->attributes['date'] = (new Carbon($value))->format('Y-m-d');

        }
    }
}
