--SELECT * FROM Tip_Operador

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

CREATE PROCEDURE WMAN_Plantillas_GetInfo
	@id_tip_oper  INT,
	@vEliminado BIT
AS
BEGIN

	SELECT id_tip_oper, LTRIM(RTRIM(nombre)), LTRIM(RTRIM(descripcion))
	FROM Tip_Operador

	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'WMAN_Plantillas_RepInfo') )
DROP PROCEDURE WMAN_Plantillas_RepInfo
GO

CREATE PROCEDURE WMAN_Plantillas_RepInfo
	@vEliminado BIT
AS
BEGIN

	SELECT id_tip_oper, nombre, descripcion 
	FROM Tip_Operador 
	ORDER BY id_tip_oper

	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'WMAN_Plantillas_PutInfo') )
DROP PROCEDURE WMAN_Plantillas_PutInfo
GO

CREATE PROCEDURE WMAN_Plantillas_PutInfo
	@id_tip_oper INT,
	@nombre VARCHAR(50),
	@descripcion VARCHAR(100),
	@eliminado INT
AS
BEGIN
	DECLARE @MensajeError VARCHAR(255) = ''

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

		IF @id_tip_oper = 0
		BEGIN
		
			-- Crear
		
			INSERT INTO Tip_Operador
			(
				nombre, descripcion
			)
			VALUES
			(
				@nombre, @descripcion
			)

			SET @id_tip_oper = @@IDENTITY
		
		END
		ELSE
		BEGIN
		
			-- Editar
		
			UPDATE Tip_Operador
			SET nombre = @nombre,
			    descripcion = @descripcion
			WHERE id_tip_oper = @id_tip_oper
			
		
		END

	END
	ELSE
	BEGIN
			DELETE FROM Tip_Operador WHERE id_tip_oper = @id_tip_oper
	END

	SELECT @id_tip_oper AS Id
	
	RETURN

	ERROR:
		RAISERROR (@MensajeError, 16, 1)

END
GO