<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\ProfileUser;

class Taskdata extends Model
{
    //

    protected $table = 'taskdata';

    protected $fillable = array('title', 'description', 'deadline', 'event_date', 'event_time', 'date_string', 'venue', 'expositor', 'task_id');

    public $timestamps = true;


    protected $casts = array(
        'event_coverage' => 'array',
        'event_interest' => 'array',
        'event_type_activity' => 'array',
    );

    public function task()
    {
        return $this->belongsTo('App\Task', 'taskdata_id', 'id');
//        return $this->hasOne('App\Task', 'id', 'profile_id');
    }

    public function tasksRel()
    {
        return $this->hasMany('App\Task', 'taskdata_id', 'id');
    }

    public static function taskdataTL($id)
    {
        $task=Task::where('profile_id', '=',$id);
        return Taskdata::where('id', '=',$task)->get();

    }

    public static function taskRelByStatus($st)
    {
        return Task::where('status_id', '=',$st)->get();
    }

    /**
     * @return bool  si el user se relaciona en la tabla user_profile con el profile_id de la tarea
     */
    public function userProfileInTasdata(){

        $user = ProfileUser::where('user_id',Auth::user()->id)->get();
//        Auth::user()->id

        if(count($user) > 0){

            foreach ($this->tasksRel as $task){

                foreach ($user as $u){
                    if($task->profile_id == $u->profile_id){

                        return true;
                    }
                }


            }
            return false;
        }else{

            return false;
        }


    }
}
