USE [FastMoneyPro_Remit]
GO

/****** Object:  StoredProcedure [dbo].[Proc_UserLogs]    Script Date: 7/4/2019 11:35:48 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Proc_UserLogs]
	   @FLAG				VARCHAR(1)
	  ,@USER				VARCHAR(50) 	= NULL
      ,@logType				VARCHAR(50)		= NULL
	  ,@IP					VARCHAR(500)	= NULL
	  ,@rowId				INT				= NULL
      ,@Reason				VARCHAR(2000)	= NULL
	  ,@Country				VARCHAR(50)		= NULL
      ,@createdBy			VARCHAR(100)	= NULL
      ,@createdDateGMT		DATETIME 		= NULL
      ,@UserData			VARCHAR(MAX)	= NULL
      ,@sortBy				VARCHAR(50)		= NULL
      ,@sortOrder			VARCHAR(5)		= NULL
	  ,@pageSize			INT				= NULL
	  ,@pageNumber			INT				= NULL 
	  
AS
BEGIN
SET NOCOUNT ON;
IF @FLAG='S'
	BEGIN	
		DECLARE 
				 @selectFieldList	VARCHAR(MAX)
				,@extraFieldList	VARCHAR(MAX)
				,@table				VARCHAR(MAX)
				,@sqlFilter			VARCHAR(MAX)
				,@CountryId			VARCHAR(10)
		
		SELECT @sortBy = 'createdDate',@sortOrder = 'DESC'	
			
		SET @table = '(SELECT rowId, logType,IP, Reason, C.createdBy, C.createdDate,M.nativeCountry FROM dbo.LoginLogs(nolock) c
						LEFT JOIN customerMaster M(NOLOCK) ON M.email = C.createdBy where logtype not in (''Admin Login'',''Agent Login'') ) x'	

		SET @sqlFilter = ''

		IF @Country IS NOT NULL
		BEGIN
			SELECT @CountryId = countryId FROM countryMaster(NOLOCK) WHERE countryName = @Country
				SET @sqlFilter +=' AND nativeCountry ='''+@CountryId+''''
		END
		IF @createdBy IS NOT NULL
				SET @sqlFilter +=' AND  createdBy LIKE '''+@createdBy + '%'''			

		IF @logType IS NOT NULL
		BEGIN
			SET @sqlFilter +=' AND logType ='''+@logType+''''
		END				
		
		SET @selectFieldList = 'rowId, logType, IP, Reason, createdBy, createdDate,nativeCountry'
				
				EXEC dbo.proc_paging
				@table					
				,@sqlFilter			
				,@selectFieldList		
				,@extraFieldList		
				,@sortBy				
				,@sortOrder			
				,@pageSize				
				,@pageNumber

		RETURN
	END
IF @FLAG='T'
	BEGIN	
		
		SELECT TOP 1 REPLACE(UserData,SUBSTRING(UserData, CHARINDEX(',Password:',UserData), CHARINDEX(',User Type:',UserData)-CHARINDEX(',Password:',UserData)),',Password: *******') as UserData
		FROM dbo.LoginLogs(nolock)
		WHERE rowId = @rowId
		
		RETURN
	END
END

GO
