<?php


namespace Gisplus\Package\Crm\Models;

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

class LeadTransfer extends Model
{
   

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

    }

    public function storeLeadTransfer($object)
    {
        // dd($object);
        return DB::transaction(function() use ($object){
        
            
            $lead_transfer = new LeadTransfer;
            // dd($object['lead']);

            $lead_transfer->lead_id = $object['lead'];
            // dd($object['sales_person']);

            $lead_transfer->sales_person_id = $object['sales_person'];

            $lead_transfer->added_by        = Auth::user()->id;
           
            $lead_transfer->save();
            
            $prev_transfer = LeadTransfer::where(['lead_id' => $object['lead']])->where('id' , '<' , $lead_transfer->id)->first();
            // dd($prev_transfer);

            if(isset($prev_transfer))
            {
                $prev_transfer->status = 'Deactive';
                $prev_transfer->update();
                $tracking_history     = new TrackingHistory;

                $track_info['color']      = 'danger';
                $track_info['icon']       = 'fa fa-plus';
                $track_info['lead']        = $lead_transfer->lead_id;
                $track_info['description'] = 'Lead is transfered to '.$lead_transfer->user->name;
                $track_info['added_by']     = $lead_transfer->added_by;
                $track_info['created_at']   = $lead_transfer->created_at;
                
    
                $tracking_history     = $tracking_history->storeTrackingHistory($track_info);
            }
        //    if($lead_transfer->sales_person_id!==Auth::user()->id)
        //    {
        //     $tracking_history     = new TrackingHistory;

        //     $track_info['color']      = 'danger';
        //     $track_info['icon']       = 'fa fa-plus';
        //     $track_info['lead']        = $lead_transfer->lead_id;
        //     $track_info['description'] = 'Lead is assigned to '.$lead_transfer->user->name;
        //     $track_info['added_by']     = $lead_transfer->added_by;
        //     $track_info['created_at']   = $lead_transfer->created_at;
            

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

           

            return with($lead_transfer);
        });

    }
    public function updateLeadTransfer($object)
    {
        // dd($object);
        return DB::transaction(function() use ($object){

        $lead_transfer = LeadTransfer::find($object['lead_transfer']);
        if(isset($lead_transfer->id))
        {
            // $lead_transfer->lead_id = $object['lead'];

            $lead_transfer->sales_person_id = $object['sales_person'];
            $lead_transfer->save();

            $prev_transfer = LeadTransfer::where('id' , '<' , $lead_transfer->id)->first();

            if(isset($prev_transfer))
            {
                $prev_transfer->status = 'Deactive';
                $prev_transfer->update();

            }
        }

            return with($lead_transfer);
        });

    }
    
    public function user()
    {

        return $this->belongsTo(CrmUser::class, 'sales_person_id')->withDefault();

    }
    public function lead()
    {

        return $this->belongsTo(Lead::class, 'lead_id')->withDefault();

    }
}
