<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Messages extends Model {

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'messages';

	/**
	 * The attributes that are mass assignable.
	 *
	 * @var array
	 */
	protected $fillable = ['sender_profile_id','receiver_profile_id','title','msg_content','message_type'];

	protected function getmessage($profile_id,$folder)
	{
		if($folder!='T'):
			$result = $this->select(['message_users.id','sender_profile_id','receiver_profile_id','title','messages.created_at','messages.id as msg_id','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
					->where('message_users.profile_id','=',$profile_id)
					->where('message_users.folder','=',$folder)
					->where('message_users.is_deleted','=','N');
		elseif($folder=='T'):			
			$result = $this->select(['message_users.id','sender_profile_id','receiver_profile_id','title','messages.created_at','messages.id as msg_id','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
					->where('message_users.profile_id','=',$profile_id)
					->where('message_users.is_deleted','=','Y');
		endif;	
		return $result;
	}
        
        protected function getmessageNew($profile_id,$folder='I')
	{ 
		//echo $profile_id; exit;
			/* $result = $this->select(['message_users.id','sender_profile_id','profiles.name','profile_image','receiver_profile_id','title','messages.created_at','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
                                        ->leftjoin('profiles','message_users.profile_id','=','profiles.id')
					->where('message_users.profile_id','=',$profile_id)
                                        ->where('receiver_profile_id','=','message_users.profile_id')
					->where('message_users.is_readed','=','N')->get(); */
                        
                       /* $result = $this->select(['message_users.id','profiles.name','profile_image','sender_profile_id','receiver_profile_id','title','messages.created_at','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
                                        ->leftjoin('profiles','profiles.user_id','=','messages.sender_profile_id')
                                        ->where('messages.sender_profile_id','=',$profile_id)
					->where('message_users.folder','=','I');					
					//->where('message_users.is_deleted','=','N');*/
                        
                        
                  if($folder!='T'):
			$result = $this->select(['message_users.id','sender_profile_id','profiles.name','profile_image','receiver_profile_id','title','messages.created_at','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
                                         ->join('profiles','profiles.user_id','=','messages.sender_profile_id')
					->where('message_users.profile_id','=',$profile_id)
                                        ->where('messages.receiver_profile_id','=',$profile_id)
					->where('message_users.folder','=',$folder)
                                        ->where('message_users.is_readed','=','N')
                                        ->groupBy('messages.sender_profile_id')
                                        ->where('message_users.is_deleted','=','N'); 
                       if($result->count()==0):
                       $result = $this->select(['message_users.id','sender_profile_id','profiles.name','profile_image','receiver_profile_id','title','messages.created_at','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
                                        ->join('profiles','profiles.user_id','=','messages.receiver_profile_id')
					->where('message_users.profile_id','=',$profile_id)
                                        ->where('messages.sender_profile_id','=',$profile_id)
					->where('message_users.folder','=',$folder)
                                        ->where('message_users.is_readed','=','N')
                                        ->groupBy('messages.sender_profile_id')
					->where('message_users.is_deleted','=','N'); 
                           
                       endif;
		elseif($folder=='T'):			
			$result = $this->select(['message_users.id','sender_profile_id','receiver_profile_id','title','messages.created_at','folder','is_readed'])
					->leftjoin('message_users','message_users.message_id','=','messages.id')
					->where('message_users.profile_id','=',$profile_id)
					->where('message_users.is_deleted','=','Y');
		endif;
                        
		//echo '<pre>';print_r($result->get()->toArray()); exit;
		return $result;
	}
         protected function getinquiry($profile_id,$msg_type)
	{
                $result = $this->select(['title','id','sender_profile_id','receiver_profile_id','created_at','updated_at'])
                                ->where('sender_profile_id','=',$profile_id)
                                ->where('message_type','=',$msg_type);
		return $result;
	}
        

}
