<?php

namespace Gisplus\Package\Crm\Models;

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

class Visit extends Model
{
    private $generic_condition;

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

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

       $this->setCondition($condition);
    }

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

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

    public function getVisitListByLead($id)
    {

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

    }
    public function getVisitById($id)
    {
        return Visit::where(['id' => $id])->first();

    }
     public function getVisitList()
    {
        return Visit::where($this->getCondition())->orderBy('id' , 'DESC')->get();

    }

    public function storeVisit($object)
    {
        return DB::transaction(function() use ($object){
        // dd($object);
            $visit = new Visit;
    
            $visit->lead_id = $object['lead'];
            $visit->description = $object['description'];
            $visit->date = $object['date'];
            $visit->added_by = Auth::user()->id;
            $visit->type = $object['type'];
            $visit->visit_no = $this->generateVisitNo();
            
            $visit->save();

            $tracking_history     = new TrackingHistory;
       

            $track_info['color']        = 'info';
            $track_info['icon']         = 'fa fa-plus';
            $track_info['lead']         = $visit->lead_id;
            $track_info['description']  = 'New Correspondence has been created ';
            $track_info['created_at']   = $visit->created_at;
            $track_info['added_by']     = $visit->added_by;
            

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

        });
        return with($visit);

    }

    public function updateVisit($object)
    {

    
    return DB::transaction(function() use ($object){
        $visit = Visit::find($object['visit']);

        if(isset($visit->id))
        {
            $visit->type        = $object['type'];
            $visit->description = $object['description'];
            $visit->date        = $object['date'];
            $visit->save();

        }
        
    });
    return with($visit);
    }

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

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

    }

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

    public function generateVisitNo()
    {
        $count = Visit::count();
        return  'CRS-'.str_pad(($count+1), 5, '0', STR_PAD_LEFT);
    }
    public function lead()
    {
     return $this->belongsTo(Lead::class, 'lead_id')->withDefault();
 
    }
    
}
