<?php
/**
 * Created by PhpStorm.
 * User: UNTREF
 * Date: 3/5/2018
 * Time: 11:16 AM
 */

namespace App\Listeners;

use App\Eventlog;
use App\Tasktag;
use Illuminate\Support\Facades\Auth;
use App\Events\TaskInactive;
use App\Notifications;
use App\Unit;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;




class TaskInactiveListener
{

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function handle(TaskInactive $event)
    {
        $task = $event->task;
        $user = $event->user;

        $userTag = Tasktag::where('user_id',$user->id)
            ->where('unit_id' ,$task->unit_id)
            ->where('active',1)
            ->where('relation_id',$task->id)
            ->where('type','task')->latest()->first();



// es team-leader de la unidad de la tarea y verifica si el usuario está activo o si está etiquetado en la tarea

        if(($user->isTeamLeaderInUnitTest($task->unit_id,$user->id) && $user->active == 1 ) ||( $userTag!= null && count($userTag) >0 )){

            $eventlog = new Eventlog();
            $eventlog->user_id = $user->id;
            $eventlog->type = 'TaskInactive';
            $eventlog->relation_id = $task->id;
            $eventlog->save();





            $notification = new Notifications();
            $notification->eventlogs_id = $eventlog->id;
            $notification->type = 'TaskInactive';
            $notification->user_to = $user->id;
            $notification->is_delivered = 0;
            $notification->is_read = 0;
            $notification->is_archived = 0;
            $notification->save();

        }


    }

}