<?php

namespace App\Models;

use App\Traits\NodeTrait;
use App\Traits\ModelTrait;
use App\Traits\TaggableTrait as Taggable;
use Laracasts\Presenter\PresentableTrait;

class VideoCate extends NodeTree
{
	use Taggable, PresentableTrait, ModelTrait, NodeTrait;

    protected $presenter = 'App\Presenters\CategoryPresenter';
    
    protected $table = 'video_cates';

    protected $guarded = ['description', 'id', 'parent_id', 'lft', 'rgt', 'depth', 'tags'];

    protected $orderColumn = 'orders';

    public function pages()
    {
        return $this->hasMany('App\Models\Video', 'pid');
    }

    public function descriptions()
    {
        return $this->hasMany('App\Models\VideoCateDescription', 'video_cid');
    }

    public function description()
    {
        $lang = config('lang') ?: config('setting.lang');

        if (config('visit') == 'frontend' && config('lang') != config('setting.lang')) {
            return $this->hasOne(VideoCateDescription::class, 'video_cid')->whereIn('lang', [config('lang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('lang') ."', '". config('setting.lang') ."')"));
        } elseif (config('visit') == 'admin' && config('managerLang') != config('setting.lang')) {
            return $this->hasOne(VideoCateDescription::class, 'video_cid')->whereIn('lang', [config('managerLang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('managerLang') ."', '". config('setting.lang') ."')"));
        }

        return $this->hasOne(VideoCateDescription::class, 'video_cid')->where('lang', $lang);
    }
}
