<?php

namespace App\Models;

use App\Models\JobCate;
use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use App\Traits\TaggableTrait as Taggable;
use Laracasts\Presenter\PresentableTrait;

class Job extends Model
{
    use Taggable, PresentableTrait, ModelTrait;

    protected $presenter = 'App\Presenters\ContentPresenter';

    protected $table = 'jobs';

    protected $guarded = ['tags', 'referer', 'description'];

    public function cates()
    {
        return $this->belongsTo(JobCate::class, 'pid');
    }

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

        if (config('visit') == 'frontend' && config('lang') != config('setting.lang')) {
            return $this->hasOne(JobDescription::class)->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(JobDescription::class)->whereIn('lang', [config('managerLang'), config('setting.lang')])->orderByRaw(\DB::raw("FIELD(lang, '". config('managerLang') ."', '". config('setting.lang') ."')"));
        }

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

}
