-- TBCASO_USUARIOS (Trigger de inclusão de parâmetros)
 SET TERM ^ ;
CREATE OR ALTER TRIGGER TTBCASO_USUARIOS_B02I FOR TBCASO_USUARIOS
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE PARAMETRO_ID INTEGER;
BEGIN

  FOR
    SELECT
      U.ID
    FROM
        TBCASO_PARAMETROS_USUARIO U
    WHERE U.PAINEL_ID = NEW.PAINEL_ID
  INTO
    :PARAMETRO_ID
  DO
  BEGIN
    INSERT INTO TBCASO_USUARIO_PARAMETRO (USUARIO_ID, PARAMETRO_ID, VALOR, PAINEL_ID)
    VALUES (NEW.USUARIO_ID, :PARAMETRO_ID, 0, NEW.PAINEL_ID);
  END

END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TBCASO_USUARIOS_A01I FOR TBCASO_USUARIOS
ACTIVE AFTER INSERT POSITION 0
AS
DECLARE VARIABLE PARAMETRO_ID INTEGER;
BEGIN

  FOR
    SELECT
      U.ID
    FROM
        TBCASO_PARAMETROS_USUARIO U
    WHERE U.PAINEL_ID = NEW.PAINEL_ID
  INTO
    :PARAMETRO_ID
  DO
  BEGIN
    INSERT INTO TBCASO_USUARIO_PARAMETRO (USUARIO_ID, PARAMETRO_ID, VALOR, PAINEL_ID)
    VALUES (NEW.USUARIO_ID, :PARAMETRO_ID, 0, NEW.PAINEL_ID);
  END

END
^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_USUARIOS_B02I FOR TBCASO_USUARIOS
INACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE PARAMETRO_ID INTEGER;
BEGIN

  FOR
    SELECT
      U.ID
    FROM
        TBCASO_PARAMETROS_USUARIO U
    WHERE U.PAINEL_ID = NEW.PAINEL_ID
  INTO
    :PARAMETRO_ID
  DO
  BEGIN
    INSERT INTO TBCASO_USUARIO_PARAMETRO (USUARIO_ID, PARAMETRO_ID, VALOR, PAINEL_ID)
    VALUES (NEW.USUARIO_ID, :PARAMETRO_ID, 0, NEW.PAINEL_ID);
  END

END
^

SET TERM ; ^

-- TBCASO_PAINEL (Exclusão do SQL do Email Contato)

SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBCASO_PAINEL_B02D FOR TBCASO_PAINEL
ACTIVE BEFORE DELETE POSITION 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBCASO_VISUALIZACAO_CONF V WHERE V.PAINEL_ID = OLD.ID;    -- VISUALIZAÇÃO ESTÁ VINCULADA COM PAINEL DURANTE INCLUSÃO.
      DELETE FROM TBCASO_SQL S WHERE S.ID = OLD.SQL_EMAIL_CONTATO;          -- SQL DO CAMPO DOS EMAILS DOS CONATATOS

      DELETE FROM TBCASO_PAINEL_CONF A WHERE A.PAINEL_ID = OLD.ID;          -- CAMPOS (ALT+2)
      DELETE FROM TBCASO_USUARIOS B WHERE B.PAINEL_ID = OLD.ID;             -- USUÁRIOS (ALT+3)
      DELETE FROM TBCASO_PARAMETROS_USUARIO C WHERE C.PAINEL_ID = OLD.ID;   -- PARÂMETROS (ALT+4)
      DELETE FROM TBCASO_STATUS D WHERE D.PAINEL_ID = OLD.ID;               -- STATUS (ALT+5)
      DELETE FROM TBCASO_MOTIVO E WHERE E.PAINEL_ID = OLD.ID;               -- MOTIVO (ALT+6)
      DELETE FROM TBCASO_VISUALIZACAO_CAMPO F WHERE F.PAINEL_ID = OLD.ID;   -- VISUALIZAÇÃO (ALT+8)
      DELETE FROM TBCASO_VALIDACAO G WHERE G.PAINEL_ID = OLD.ID;            -- VALIDAÇÃO (ALT+9)
      DELETE FROM TBCASO_CONTATO_CONF H WHERE H.PAINEL_ID = OLD.ID;         -- CONTATO (ALT+0)
      DELETE FROM TBCASO_CAMPOS_AGRUPAMENTO I WHERE I.PAINEL_ID = OLD.ID;   -- AGRUPAMENTOS (ALT+7)
   END
END
^

SET TERM ; ^

