<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\Task;

class Unit extends Model
{


    protected $table = 'units';

    protected $fillable = ['name', 'slug', 'color', 'order', 'unitType_id'];

    public $timestamps = true;


    public function pipelines()
    {

        return $this->belongsToMany('App\Pipeline', 'unit_pipeline');

    }


    public function users()
    {
        return $this->belongsToMany('App\User', 'user_unit')->withPivot(['id', 'teamrole_id']);


    }


    public function tasks()
    {
        return $this->hasMany('App\Task', 'unit_id', 'id')->orderBy('created_at','desc');;
    }

    public function opentasks()
    {
        return $this->hasMany('App\Task', 'unit_id', 'id')->whereNotIn('tasks.status_id',[3,6])->orderBy('created_at','desc');
    }

    public function closedtasks()
    {
        return $this->hasMany('App\Task', 'unit_id', 'id')->whereIn('tasks.status_id',[3,6])->orderBy('created_at','desc');

    }

    public function opentasksFilterProgress()
    {
        $units = Unit::orderBy('order', 'asc')->get();
        $openUnit = $units->first()->id;

        $arrayIds = array();
        $tasks = Task::where('unit_id',$openUnit)->get();
        foreach ($tasks as $t){
            if (count($t->taskdata->tasksRel) == 1){
                array_push($arrayIds,$t->id);
            }
        }
        return $this->hasMany('App\Task', 'unit_id', 'id')->whereNotIn('tasks.status_id',[3,6])->whereNotIn('id',$arrayIds)->orderBy('created_at','desc');
    }

    public function opentasksFilterUninitiated()
    {
        $units = Unit::orderBy('order', 'asc')->get();
        $openUnit = $units->first()->id;

        $arrayIds = array();
        $tasks = Task::where('unit_id',$openUnit)->get();
        foreach ($tasks as $t){
            if (count($t->taskdata->tasksRel) > 1){
                array_push($arrayIds,$t->id);
            }
        }
        return $this->hasMany('App\Task', 'unit_id', 'id')->whereNotIn('tasks.status_id',[3,6])->whereNotIn('id',$arrayIds)->orderBy('created_at','desc');
    }

    public function teammembers()
    {
        return $this->belongsToMany('App\User', 'user_unit')->where('teamrole_id', '=', 2);
    }


    public function teamleaders()
    {
        return $this->belongsToMany('App\User', 'user_unit')->where('teamrole_id', '=', 1);
    }

    public function autotags()
    {
        return $this->belongsToMany('App\User', 'user_unit')->where('teamrole_id', '=', 3);
    }

    /**
     * @return devuelve la relacion entre user y user_unit y la columnas id,tea,role_id donde es igual a 2 (teammember)
     */
    public function teamMemberByUnit()
    {
        return $this->belongsToMany('App\User', 'user_unit')->withPivot(['id', 'teamrole_id'])->where('teamrole_id',2);


    }

    public function type()
    {
        return $this->hasOne('App\Unittype','id','unitType_id');
    }

    public function usersUnitExtern()
    {
        $query = DB::table('user_unit')->select('user_unit.user_id')->whereIn('user_unit.teamrole_id', [1,2])->where('units.unitType_id', 2)->join('units', 'user_unit.unit_id', '=', 'units.id')->distinct()->get() ;
        return $query;
    }

    public function usersUnitInternal()
    {
        $query = DB::table('user_unit')->select('user_unit.user_id')->where('units.unitType_id', 1)->join('units', 'user_unit.unit_id', '=', 'units.id')->distinct()->get();
        return $query;
    }




}
