<?php

namespace Gisplus\Package\Crm\Models;

use Illuminate\Database\Eloquent\Model;
use Gisplus\Package\Crm\Models\CrmUser;
use Gisplus\Package\Crm\Models\ReferralHistory;
use Gisplus\Package\Crm\Models\BusinessReferral;
use DB;
use Auth;

class BusinessReferral extends Model
{
    public function storeBusinessReferral($object)
    {
        return DB::transaction(function() use ($object){
        
            $business_referral = new BusinessReferral;
            $referral_history = new ReferralHistory;

            // $referral_history->lead_id = $object['lead'];
            if(isset($object['business_referral']))
                {
                    $business_referral_id      = $object['business_referral'];
                    $object['lead']   = $object['lead'] ;

                }
            else{

                $business_referral->name = $object['referral_name'];
                $business_referral->cnic = $object['referral_cnic'];
                $business_referral->email = $object['referral_email'];
                $business_referral->contact = $object['referral_contact'];
                $business_referral->company_name = $object['company_name'];
                $business_referral->added_by = Auth::user()->id;
    
                $business_referral->save();
                $object['business_referral']   = $business_referral->id;

            }
                $referral_history = $referral_history->storeReferralHistory($object);
           
           

            return with($business_referral);
        });

    }

    public function updateBusinessReferral($object)
    {
        // dd($object);
        return DB::transaction(function() use ($object){
        
            $business_referral = BusinessReferral::find($object['business_referral']);
       
            $business_referral->name = $object['referral_name'];
            $business_referral->cnic = $object['referral_cnic'];
            $business_referral->email = $object['referral_email'];
            $business_referral->contact = $object['referral_contact'];
            $business_referral->company_name = $object['company_name'];
            $business_referral->added_by = Auth::user()->id;
            

            // dd($business_referral);
            $business_referral->update();

            // $referral_history               = new ReferralHistory;
            // $object['business_referral']    = $business_referral->id ;

            // $referral_history = $referral_history->updateReferralHistory($object);

            return with($business_referral);
        });

    }

    public function removeBusinessReferral($id)
    {
        return DB::transaction(function() use ($id){
            
            $business_referral = BusinessReferral::find($id);

            if(isset($business_referral->id))
            {
                $business_referral = BusinessReferral::where(['id'=>$id])->delete();

            }
            return with($business_referral);

        });

    }

    public function getReferralList()
    {
        return BusinessReferral::all();

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

    }

}
