<?php

namespace Gisplus\Package\Crm\Models;

use Illuminate\Database\Eloquent\Model;
use Gisplus\Package\Crm\Models\Lead;
use Gisplus\Package\Crm\Models\CrmUser;

use DB;

class TrackingHistory extends Model
{
    public function getTrackHistoryByLeadId($lead_id)
    {
        return TrackingHistory::where(['lead_id'=> $lead_id])->with(['user'])->orderBy('id' , 'asc')->get();
    }

    public function storeTrackingHistory($object)
    {

        return DB::transaction(function() use ($object){
            
             $tracking_history = new TrackingHistory;
             $tracking_history->lead_id = $object['lead'];
            //  dd($tracking_history->lead_id);

             $tracking_history->color = $object['color'];
            // dd($tracking_history->color);

             $tracking_history->icon =  $object['icon'];
             $tracking_history->description =  $object['description'];
             $tracking_history->created_at =  $object['created_at'];
             $tracking_history->added_by =  $object['added_by'];
             $tracking_history->save();

            //  dd($object);


        });
        return with($tracking_history);

    }

    public function leadTrackinghistory()
    {
        return $this->hasMany(Lead::class);
    }  

    public function visitTrackinghistory()
    {
        return $this->hasMany(Visit::class);
    }

    public function taskTrackinghistory()
    {
        return $this->hasMany(Task::class);
    }
    public function user()
    {
        return $this->belongsTo(CrmUser::class, 'added_by')->withDefault();
    }
}
