<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Draft
 *
 * @property int $id
 * @property string $name
 * @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\Draft whereActive($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Draft whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Draft whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Draft whereName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Draft whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\DraftItem[] $draft_items
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TravelProgramDraft[] $travel_program_drafts
 */
class Draft extends Model
{
    protected $connection = 'mysql';

    protected $table = 'drafts';

    public function draft_items()
    {
        return $this->hasMany('App\Models\DraftItem', 'draft_id', 'id')->orderBy('pos', 'ASC');
    }

    public function travel_program_drafts()
    {
        return $this->hasMany(TravelProgramDraft::class, 'draft_id', 'id');
    }


    public  function findBeforeRelation($reid)
    {
        $before = false;
        foreach($this->draft_items as $draft_item) {
            if ($draft_item->id == $reid) {
                return $before;
            }
            $before = $draft_item;
        }
        return false;
    }

    public function findAfterRelation($reid)
    {
        $next = false;
        foreach($this->draft_items as $draft_item) {
            if($next){
               return $draft_item;
            }
            if ($draft_item->id == $reid) {
                $next = true;
            }
        }
        return false;
    }



}
