<?php


namespace Gisplus\Package\Crm\Models;

use Gisplus\Package\Crm\Models\Lead;
use Gisplus\Package\Crm\Models\TrackingHistory;
use App\User;
use DB;
use Auth;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    private $generic_condition;

    public function __construct()
    {
        $condition = [];

        if(\Auth::user()->id != 1)
        {
            $condition += ['assign_to_id' => \Auth::user()->id];
        }

       $this->setCondition($condition);
    }

    public function setCondition($condition = [])
    {
        $this->generic_condition = $condition;
    }

    public function getCondition()
    {
        return $this->generic_condition;
    }

    public function getTaskList()
    {
        return Task::where($this->getCondition())->orderBy('id', 'desc')->get();
    }
    
    public function getTaskListByLead($id)
    {

        return Task::where(['lead_id' => $id])->get();
    }

    public function getTaskById($id)
    {
        return Task::where(['id' => $id])->first();

    }

    public function getChartTask()
    {
     
        $task_list_chart = array();
        $tasks[0] = 'data1';
        $count = 0;
        $month_list = ['Jan', 'Feb', 'March', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
    ];
        $year = date('Y');
       foreach ($month_list as $key => $month) {
            $first_date = date('Y-m-d',strtotime('first day of '.$month.' '.$year));

            $last_date = date('Y-m-d',strtotime('last day of '.$month.' '.$year));
            // echo 'from '.$first_date.' to '.$last_date.'<br>';
            $task_list_chart[$count] = $month;
            $tasks[$count+1] = Task::whereDate('created_at','>=',$first_date)->whereDate('created_at','<=',$last_date)
            ->count();
            $count++;
       }
        return ['tasks' => $tasks, 'task_list_chart' => $task_list_chart];
    }

    public function storeTask($object)
    {
        return DB::transaction(function() use ($object){
        
        $task = new Task;

        $task->lead_id = $object['lead'];

        if($object['visit'] > 0 )
        {
            $task->visit_id = $object['visit'];
            $task->description = $object['description'];

            // $task->description = $task->visitTask->description;

            if($object['assign_to'] > 0)
            {
                $task->assign_to_id = $object['assign_to'];
            }
            else{

                $task->assign_to_id = Auth::user()->id;

            }

        }
        else{

            $task->assign_to_id = $object['assign_to'];
            $task->description = $object['description'];


        }
        $task->title = $object['title'];
        $task->date = $object['date'];
        $task->added_by = Auth::user()->id;

        $task->save();

        $tracking_history     = new TrackingHistory;
       

        $track_info['color']        = 'warning';
        $track_info['icon']         = 'fa fa-plus';
        $track_info['lead']         = $task->lead_id;
        $track_info['description']  = 'New Task has been created';
        $track_info['created_at']   = $task->created_at;
        $track_info['added_by']     = $task->added_by;
        

        $tracking_history     = $tracking_history->storeTrackingHistory($track_info);

    });
    return with($task);
}

    public function updateTask($object)
    {

        return DB::transaction(function() use ($object){

            $task = Task::find($object['task']);

            if(isset($task->id))
            {

            if($object['visit'] > 0 )
            {
                $task->visit_id = $object['visit'];

                $task->description = $object['description'];

                // $task->description = $task->visitTask->description;

                if($object['assign_to'] > 0)
                {
                    $task->assign_to_id = $object['assign_to'];

                }
                else{

                    $task->assign_to_id = $task->visitTask->added_by;

                }

            }
            else{

                $task->assign_to_id = $object['assign_to'];

                $task->description = $object['description'];

            }
            $task->title = $object['title'];
            $task->date = $object['date'];
            $task->status = $object['status'];
            $task->save();

            }
            
        });
        return with($task);
    }

    public function removeTask($lead_id)
    {
        return DB::transaction(function () use ($lead_id) {

            Task::where(['lead_id' => $lead_id])->delete();
        });

    }


    public function getLatestTasks()
    {
     return Task::orderBy('id' , 'desc')->limit(20)->get();
 
    }
 

    public function assignTo()
    {
     return $this->belongsTo(User::class, 'assign_to_id')->withDefault();
 
    }
    public function visitTask()
    {
     return $this->belongsTo(Visit::class, 'visit_id')->withDefault();
 
    }
     public function lead()
    {
     return $this->belongsTo(Lead::class, 'lead_id')->withDefault();
 
    }



}
