SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_ServidoresServicios_GetInfo') )
DROP PROCEDURE WGP_ServidoresServicios_GetInfo
GO

CREATE PROCEDURE WGP_ServidoresServicios_GetInfo
	@id_server  INT,
	@vEliminado BIT
AS
BEGIN

	SELECT serv.id_server, LTRIM(RTRIM(serv.descripcion)), serv.id_terminal, serv.remoteport from icsservers serv 
	INNER JOIN terminales on serv.id_terminal = terminales.id_terminal 
	
	RETURN

END
GO

---------------------------------------------------------------------------------
---------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_ServidoresServicios_RepInfo') )
DROP PROCEDURE WGP_ServidoresServicios_RepInfo
GO

CREATE PROCEDURE WGP_ServidoresServicios_RepInfo
	@Eliminado BIT
AS
BEGIN

	SELECT serv.id_server, LTRIM(RTRIM(serv.descripcion))server_descripcion, serv.id_terminal,LTRIM(RTRIM(TER.nombre))nombre , serv.remoteport, Dat.id_datos, LTRIM(RTRIM(DAT.descripcion))datos_descripcion, I.id_serv, LTRIM(RTRIM(IC.descripcion))services_descripcion, I.automatico, I.manual from icsservers serv 
	INNER JOIN terminales TER on serv.id_terminal = TER.id_terminal
	INNER JOIN IcsTermServ I on I.id_server = serv.id_server
	INNER JOIN datos DAT on DAT.id_datos = I.id_datos
	INNER JOIN IcsServices IC on IC.id_serv = I.id_serv
	Where I.automatico = 1 OR I.manual = 1
	ORDER BY serv.id_server
	
	--SELECT DISTINCT serv.id_server, LTRIM(RTRIM(serv.descripcion))server_descripcion, serv.id_terminal,LTRIM(RTRIM(TER.nombre))nombre , 
	--serv.remoteport, Dat.id_datos, LTRIM(RTRIM(DAT.descripcion))datos_descripcion, I.automatico, I.manual from icsservers serv 
	--INNER JOIN terminales TER on serv.id_terminal = TER.id_terminal
	--INNER JOIN IcsTermServ I on I.id_server = serv.id_server
	--INNER JOIN datos DAT on DAT.id_datos = I.id_datos
	--ORDER BY id_server

	RETURN

END
GO

---------------------------------------------------------------------------------
---------------------------------------------------------------------------------

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'WGP_ServidoresServicios_PutInfo') )
DROP PROCEDURE WGP_ServidoresServicios_PutInfo
GO

CREATE PROCEDURE WGP_ServidoresServicios_PutInfo
	@id_server INT,
	@id_terminal INT,
	@descripcion VARCHAR(50),
	@remoteport INT,
	@eliminado INT
AS
BEGIN
	DECLARE @MensajeError VARCHAR(255) = ''

	IF @eliminado = 0
	BEGIN
	
		IF (@descripcion = '')
		BEGIN
			SET @MensajeError = 'ERR_GEN_FAL_DAT'
			--Falta datos, no deje campos vacios 
			GOTO ERROR
		END
	
	IF EXISTS(select descripcion from IcsServers where descripcion=@descripcion AND id_server!=@id_server)
			BEGIN
				SET @MensajeError = 'ERR_EEM_ID_DUP'
				--Nombre repetido
			GOTO ERROR
		END

		IF @id_server = 0
		BEGIN
		
			-- Crear			SELECT * FROM syswinFOR
		
			INSERT INTO IcsServers
			(
				id_terminal, descripcion, remoteport
			)
			VALUES
			(
				@id_terminal, @descripcion, @remoteport
			)

			SET @id_server = @@IDENTITY
		
		END
		ELSE
		BEGIN
		
			-- Editar
		
			UPDATE IcsServers
			SET id_terminal = @id_terminal,
			    descripcion = @descripcion,
			    remoteport = @remoteport
			WHERE id_server = @id_server
		
		END

	END
	ELSE
	BEGIN
			
			DELETE FROM IcsTermServ WHERE id_server = @id_server
			DELETE FROM IcsServers WHERE id_server = @id_server

	END

	SELECT @id_server AS Id
	
	RETURN

	ERROR:
		RAISERROR (@MensajeError, 16, 1)

END
GO