<?php 
  error_reporting(0);
  ini_set(“display_errors”, 0 );
  if (!isset($_SESSION)) {
    session_start();
  }
?>
<?php



##################################################  FUNCTIONS ##############################################################################3


   function inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente ){

     $inserir =   "INSERT INTO config_email(url_cliente, host, port, username, password, remetente_desc, resposta_email, resposta_desc, $tipo_email, empreendimento_id, nome_remetente) VALUES('$url_cliente', '$host_email', '$porta_email', '$email_usuario', '$senha_usuario', '$assunto_email' , '$email_resposta', '$ass_email_resp', '$aux', '$idempreendimento', '$nome_remetente') ";
     

     return $inserir;

   };



   function atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente ){

    $atualizar =  "UPDATE config_email SET $tipo_email = '$aux', url_cliente = '$url_cliente', host = '$host_email', port = '$porta_email', username = '$email_usuario', password = '$senha_usuario', remetente_desc = '$assunto_email', resposta_email = '$email_resposta', resposta_desc = '$ass_email_resp', nome_remetente = '$nome_remetente'  WHERE empreendimento_id = $idempreendimento";


    return $atualizar;

   };


##############################################################################################################################################





   if(isset($_GET['id'])){

        include "conexao.php";

        $id = $_GET['id'];
        $idempreendimento = $_GET['idempreendimento'];
        
    


    $query_consulta = "SELECT * FROM config_email WHERE empreendimento_id = $idempreendimento";

    $executa_consulta = mysqli_query($db, $query_consulta);


    while($busca = mysqli_fetch_assoc($executa_consulta)){

               $mostra_url = $busca['url_cliente'];

               $mostra_host = $busca['host'];

               $mostra_username = $busca['username'];

               $mostra_password = $busca['password'];
               
               $mostra_port = $busca['port'];

               $mostra_remetente_desc = $busca['remetente_desc'];

               $mostra_email_resposta = $busca['resposta_email'];

               $mostra_desc_resposta = $busca['resposta_desc'];
               
               $mostra_nome_remetente = $busca['nome_remetente'];

    }

   };


    include "conexao.php";


  $confirma = 0;

  //Atualiza o modelo do e-mail
  if(isset($_POST['conteudo'])){
  
   

    $aux = $_POST['conteudo'];

    $url_cliente = $_POST['host_geral'];

    $host_email = $_POST['host_email'];

    $email_usuario = $_POST['email_rem'];

    $senha_usuario = $_POST['senha'];

    $porta_email = $_POST['port'];

    $assunto_email = $_POST['email_desc'];

    $email_resposta = $_POST['email_resp'];

    $ass_email_resp = $_POST['email_desc_resp'];
    
    $email_remetente = $_POST['email_remetente'];
    
    $nome_remetente  = $_POST['nome_remetente'];



    //query para saber se existe um registro do empreendimento no banco

        $query_modelo =  "SELECT * FROM config_email WHERE empreendimento_id = $idempreendimento";

        $executa_modelo = mysqli_query($db, $query_modelo);

        $num_rows = mysqli_num_rows($executa_modelo);

      



         
    switch ($id){
      case 1:

         if($num_rows > 0 ){         

                    $tipo_email = 'avencer_10';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  
            
            
                    ?>
                    
                       
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                         
                    <?php 


                 die();

            break;

         }else{

            $tipo_email = 'avencer_10';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;
            
                     
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

              
        
      case 2:
         if($num_rows > 0 ){         

                    $tipo_email = 'avencer_5';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  

                      
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                 die();

            break;

         }else{

            $tipo_email = 'avencer_5';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;

                     
            ?>
            
                       
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

      case 3:

          if($num_rows > 0 ){         

                    $tipo_email = 'avencer_0';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  

                      
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                 die();

            break;

         }else{

            $tipo_email = 'avencer_0';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;

                     
            ?>
            
                       
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

      case 4:
          if($num_rows > 0 ){         

                    $tipo_email = 'venceu_5';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  

                      
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                 die();

            break;

         }else{

            $tipo_email = 'venceu_5';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;

                    
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

      case 5:
         if($num_rows > 0 ){         

                    $tipo_email = 'venceu_10';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  

                     
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                 die();

            break;

         }else{

            $tipo_email = 'venceu_10';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;

                     
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

      case 6:
          if($num_rows > 0 ){         

                    $tipo_email = 'venceu_15';

                    $query_atualizar = atualizar($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_atualizar)? $confirma = 1 : $confirma = 5;  

                      
            ?>
            
               
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                 die();

            break;

         }else{

            $tipo_email = 'venceu_15';

                    $query_inserir = inserir($tipo_email, $url_cliente, $host_email, $porta_email, $email_usuario,$senha_usuario , $assunto_email, $email_resposta, $ass_email_resp, $aux, $idempreendimento, $nome_remetente);

            1 == mysqli_query($db, $query_inserir)? $confirma = 1 : $confirma = 5;

                     
            ?>
            
                       
                  <script>
        
                    window.location = "edita_email_emp.php?id=<?= $_GET["id"]; ?>&idempreendimento=<?= $_GET["idempreendimento"]; ?>";
        
                  </script>
                 
            <?php 

                die();
          break;
         }

      default:
        break;
    }
  }

    
 

  //exibe o modelo cadastrado no campo de texto
  if(isset($_GET["id"])){
    $id = $_GET["id"];


    switch ($id){
      case 1:
        $aux ="10 dias para vencer!"; 
        $executa = mysqli_query($db,"SELECT avencer_10 FROM config_email WHERE empreendimento_id = $idempreendimento");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['avencer_10'];

        break;
      case 2:
        $aux ="5 dias para vencer!";
        $executa = mysqli_query($db,"SELECT avencer_5 FROM config_email WHERE empreendimento_id = $idempreendimento ");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['avencer_5'];
        break;
      case 3:
        $aux ="0 dias para vencer!";
        $executa = mysqli_query($db,"SELECT avencer_0 FROM config_email WHERE empreendimento_id = $idempreendimento");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['avencer_0'];
        break;
      case 4:
        $aux ="Venceu a 5 dias!";
        $executa = mysqli_query($db,"SELECT venceu_5 FROM config_email WHERE empreendimento_id = $idempreendimento");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['venceu_5'];
        break;
      case 5:
        $aux ="Venceu a 10 dias!";
        $executa = mysqli_query($db,"SELECT venceu_10 FROM config_email WHERE empreendimento_id = $idempreendimento");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['venceu_10'];
        break;
      case 6:
        $aux ="Venceu a 15 dias!";
        $executa = mysqli_query($db,"SELECT venceu_15 FROM config_email WHERE empreendimento_id = $idempreendimento");
        $busca = mysqli_fetch_assoc($executa);
        $email_html = $busca['venceu_15'];
        break;
      default:
        $email_html = " ";
        break;
    }
  }
?>
<!DOCTYPE html>

<html lang="pt-br">
<!--<![endif]-->
  <head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <title>Immobile business</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    
    <!-- ================== BEGIN BASE CSS STYLE ================== -->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/themes/base/minified/jquery-ui.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/animate.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/style.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/style-responsive.min.css" rel="stylesheet" />
    <link href="https://immobilebusiness.com.br/admin/assets/css/theme/default.css" rel="stylesheet" id="theme" />
    <!-- ================== END BASE CSS STYLE ================== -->
    
    <!-- ================== BEGIN PAGE CSS ================== -->
    <link href="https://immobilebusiness.com.br/admin/assets/plugins/summernote/summernote.css" rel="stylesheet" />
    <!-- ================== END PAGE CSS ================== -->
    
    <!-- ================== BEGIN BASE JS ================== -->
    <script src="https://immobilebusiness.com.br/admin/assets/plugins/pace/pace.min.js"></script>
    <script src="https://cloud.tinymce.com/5/tinymce.min.js?apiKey=shgcpb5mcjj2aclo6xy02pw1nff4235ewyrdu7x558wq6xcs"></script>
    <script>
        tinymce.init({
        selector: "textarea",  // change this value according to your HTML
        plugins: "print code",
        height : 400,
        menubar: "file ",
        toolbar: "print | code| newdocument| undo| redo| visualaid| cut| copy| paste| selectall| bold| italic| underline|strikethrough| subscript| superscript| removeformat| formats | alignleft aligncenter alignright"
      });
    </script>
    <!-- ================== END BASE JS ================== -->
  </head>
  <body>
    <!-- begin #page-loader -->
    <div id="page-loader" class="fade in"><span class="spinner"></span></div>
    <!-- end #page-loader -->
    
    <!-- begin #page-container -->
    <div id="page-container" class="fade page-header-fixed page-sidebar-fixed">
      <!-- begin #header -->
      <?php include "topo.php";?>

      <div class="sidebar-bg"></div>
      <!-- end #sidebar -->
      
      <!-- begin #content -->
      <div id="content" class="content">
        <!-- begin breadcrumb -->
        <?php 

          //Função para mostrar uma div de sucesso de cadastro.
          if($confirma == 1){ ?>
            <div class="alert alert-success fade in m-b-15">
              <strong><font>Sucesso!</font></strong>

              <font>
                Suas alterações foram salvas.
              </font>

              <span class="close" data-dismiss="alert" ><font>X</font></span>

            </div>
          <?php }else if($confirma == 5){ ?>
            <div class="alert alert-danger fade in m-b-15">
              <strong><font>Não foi possível salvar os dados!</font></strong>

              <font>
                Suas alterações não foram salvas.
              </font>

              <span class="close" data-dismiss="alert" ><font>X</font></span>

            </div>
          <?php }
        ?>
        <!-- end breadcrumb -->
        <!-- begin page-header -->
        <h1 class="page-header">Cadastro E-mail  </h1>
        <!-- end page-header -->
        
        <!-- begin row -->
        <div class="row">
          <!-- begin col-2 -->
          <div class="col-md-2">
            <h4 >Dados do Contrato / Cliente</h4>
            <div><b class="text-inverse">Cliente</b></div>
            <p>
              @email_cli</br>
              @nome_cli,</br>
              @cpf_cli,</br>
              @cidade_cli,</br>
              @endereco_cli,</br>
              @numero_casa_cli,</br>
              @bairro_cli,</br>
              @telefone_cli,</br>
              @falta_qnt_dias
            </p>

            <div><b class="text-inverse">Empreendimento</b></div>
            <p>
              @nome_empreendimento 
            </p>
            <div><b class="text-inverse">Data</b></div>
            <p>
              @data_vencimento
              @data_venda
            </p>

          </div>
          <!-- end col-2 -->

          

          <!-- <div class="col-md-10">
            <div class="panel panel-inverse m-b-0">
              <div class="panel-heading">
                <h4 class="panel-title"> E-mail:  <?php  echo isset($aux)? $aux : '' ;?> </h4>
              </div>
            </div>
          </div> -->

          <!-- begin col-10 -->
          <div class="col-md-10" style="margin-top: -50px">
            <div class="panel panel-inverse m-b-0">
              <div class="panel-heading">
                <h4 class="panel-title">Editar E-mail: <?php  echo isset($aux)? $aux : '' ;?></h4>
              </div>
              <div class="panel-body p-0">
                <form action="#" method="POST" name="formulario" style="padding: 10px">
                  <textarea class="conteudo" name="conteudo"> <?php  echo isset($email_html)? $email_html: ''; ?></textarea>


                              <h3 class="title"> Dados do servidor do E-mail</h3>
                <p></p>
          
           <!-- <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                    <label for="host_geral">URL Do Cliente</label>
                    <input type="text" class="form-control" name="host_geral" placeholder="http://hrsantos.com.br" value="<?php echo $mostra_url ?>" required>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                    <label for="host_email">Host do E-mail</label>
                    <input type="text" class="form-control" name="host_email" placeholder="br808.hostgator.com.br" value="<?php echo $mostra_host ?>" required>
                </div>
              </div>
            </div>
            -->
            
            <!--
            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                    <label for="email_rem">E-mail do Usuário</label>
                    <input type="email" class="form-control" name="email_rem" placeholder="exemplo@exeplo.com" value="<?php echo $mostra_username ?>" required>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                    <label for="senha">Senha do Usuário</label>
                    <input type="password" class="form-control senha" name="senha" placeholder="Senha do Usuário" value="<?php echo $mostra_password ?>" required>
                </div>
              </div>
            </div>
            -->
            
            <div class="row">
            
             <!--
              <div class="col-md-6">
                <div class="form-group">
                    <label for="port">Porta do E-mail</label>
                    <input type="text" class="form-control" name="port" placeholder="Porta do E-mail" value="<?php echo $mostra_port ?>" required>
                </div>
              </div>
             -->
             
              <div class="col-md-6">
                <div class="form-group">
                    <label for="port">Remetente</label>
                    <input type="text" class="form-control" name="nome_remetente" placeholder="Informe o nome da empresa Remetente" value="<?php echo $mostra_nome_remetente ?>" required>
                </div>
              </div>
                       
              <div class="col-md-6">
                <div class="form-group">
                    <label for="email_desc">Assunto do E-mail</label> 
                    <input type="text" class="form-control" name="email_desc" placeholder="Descrição do E-mail do Remetente" value="<?php echo $mostra_remetente_desc ?>" required>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="col-md-6">
                <div class="form-group">
                    <label for="email_resp">E-mail de Resposta</label>
                    <input type="email" class="form-control" name="email_resp" placeholder="exemplo@exeplo.com required" value="<?php echo $mostra_email_resposta ?>" required>
                </div>
              </div>
              <div class="col-md-6">
                <div class="form-group">
                    <label for="email_desc_resp">Assunto do E-mail de resposta</label>
                    <input type="text" class="form-control" name="email_desc_resp" placeholder="Descrição E-mail de Resposta" value="<?php echo $mostra_desc_resposta ?>" required>
                </div>
              </div>
            </div>
          
                  <input type="submit" name="cadastrar" value="Cadastrar" class="btn btn-success"/>
                </form>
              </div>
            </div>
          </div>
          <!-- end col-10 -->
        </div>
        <!-- end row -->
      </div>


    <div class="script">
      <!-- ================== BEGIN BASE JS ================== -->
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-1.9.1.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery/jquery-migrate-1.1.0.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-ui/ui/minified/jquery-ui.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/bootstrap/js/bootstrap.min.js"></script>
      <!--[if lt IE 9]>
        <script src="https://immobilebusiness.com.br/admin/assets/crossbrowserjs/html5shiv.js"></script>
        <script src="https://immobilebusiness.com.br/admin/assets/crossbrowserjs/respond.min.js"></script>
        <script src="https://immobilebusiness.com.br/admin/assets/crossbrowserjs/excanvas.min.js"></script>
      <![endif]-->
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/slimscroll/jquery.slimscroll.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/jquery-cookie/jquery.cookie.js"></script>
      <!-- ================== END BASE JS ================== -->
      
      <!-- ================== BEGIN PAGE LEVEL JS ================== -->
      <script src="https://immobilebusiness.com.br/admin/assets/plugins/summernote/summernote.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/js/form-summernote.demo.min.js"></script>
      <script src="https://immobilebusiness.com.br/admin/assets/js/apps.min.js"></script>
      <!-- ================== END PAGE LEVEL JS ================== -->
    </div>

    <script>
      $(document).ready(function() {
        App.init();
        FormSummernote.init();
      });
    </script>
  </body>
</html>
