<?php
include 'classes/config.php';
$hoje = date("Y-m-d");
$host = str_replace("www.", "", $_SERVER['HTTP_HOST']);

//sitemap($host);

$pattern = "/".VPS_DOMINIO."/i";
$sub = preg_match($pattern, $host);
if($sub == 1){
    $url_request = explode(".", $host);
    $subdominio = $url_request[0];
    $params = explode("/", $_SERVER["REQUEST_URI"]);
    $page = $params[1];
    $param0 = $params[0];
    $param1 = $params[1];
    $param2 = $params[2];
} else {
    $dominio = explode(".", $host);
    $params = explode("/", $_SERVER["REQUEST_URI"]);
    $param0 = $params[0];
    $param1 = $params[1];
    $param2 = $params[2];
}
//print 'Param 0 = '.$param0.'<br>';
//print 'Param 1 = '.$param1.'<br>';
//print 'Param 2 = '.$param2.'<br>';
$configuracoesHospedagem = ConfiguracoesHospedagem::find(0, array("subdominio LIKE '".$subdominio."'"));
//print_r($configuracoesHospedagem);
// Busca por assinaturas da Corretora
if(count($configuracoesHospedagem) >=1){
    $assinaturas = Assinatura::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."'"));
    //print_r($assinaturas);
    if(count($assinaturas) >=1){
        // Verifica se a assinatura está ativa
        if($assinaturas[0]->vencimento >= $hoje){
            //verifica se é domínio ou subdomínio
            if($sub == 0){ // domínio
                if($param1 != 'lp' && $param1 != 'card'){
                    $sites = Site::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo'"));
                    if(count($sites) >=1){
                        novavisita($sites[0]->id, "Site");
                        if(!empty($param1) && empty($param2)){
                            $site = $sites[0];
                            $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = '".strtolower($param1)."'"));
                            $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.'.html');
                            $html = preg_replace('/{{dominio}}/', 'https://'.$dominio.'/', $html);
                            $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                            print $html;
                        } elseif(!empty($param1) && !empty($param2)){
                            $site = $sites[0];
                            $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = '".strtolower($param1)."'"));
                            $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.".html");
                            $html = preg_replace('/{{dominio}}/', 'https://'.$dominio.'/', $html);
                            if($param1 == 'seguros'){
                                $siteSeguroAvulso = SiteSeguros::find(0, array("url = '".$param2."' AND corretora = '".$site->corretora."' AND status = 'Ativo'"));
                                if(count($siteSeguroAvulso) >=1){
                                    $html = populaSeguroAvulso($html, $siteSeguroAvulso[0]->id);
                                } else {
                                    redireciona('https://'.$dominio.'/');
                                }
                            } elseif($param1 == 'seguradoras'){
                                $siteSeguradoraAvulso = SiteSeguradoras::find(0, array("url = '".$param2."' AND corretora = '".$site->corretora."' AND status = 'Ativo'"));
                                if(count($siteSeguradoraAvulso) >=1){
                                    $html = populaSeguradoraAvulsa($html, $siteSeguradoraAvulso[0]->id);
                                } else {
                                    redireciona('https://'.$dominio.'/');
                                }
                            } else {
                                redireciona('https://'.$dominio.'/');
                            }
                            $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                            print $html;
                        } else {
                            $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = 'home'"));
                            $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.'.html');
                            $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                            $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                            $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                            print $html;
                        }
                 
                    } else {
                        redireciona('https://'.$dominio.'/');
                    }
                }
                if($param1 == 'lp'){ 
                    $lps = Landing::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo' AND route = '".$param2."'"));
                    if(count($lps) >=1){
                        $templateLanding = TemplateLandingpage::find($lps[0]->template);
                        novavisita($lps[0]->id, 'Landing');
                        $html = file_get_contents("https://templates.otimize.online/".$templateLanding->pasta."/".$templateLanding->referencia."/index.html");
                        $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                        $html = populaVariaveisLP($html, $lps[0]->id);
                        if($lps[0]->template == 20){
                            $html = populaSegurosLP($html, $lps[0]->id);
                        }
                        
                        if($lps[0]->template < 20){
                            $html = populaSeguradorasLP($html, $lps[0]->id);
                        } elseif($lps[0]->template == 33){
                            $html = populaSeguradorasLP7($html, $lps[0]->id);
                            $html = populaFaq($html, $lps[0]->id);
                            $html = populaDepoimentos($html, $lps[0]->id);
                        }
                        print $html;
                    }
                }
                if($param1 == 'card'){
                    $cds = Cartao::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo' AND route = '".$param2."'"));
                    if(count($cds) >=1){
                        $templateLanding = TemplateCartoes::find($cds[0]->template);
                        novavisita($cds[0]->id, 'Cartao');
                        $html = file_get_contents("https://templates.otimize.online/".$templateLanding->pasta."/".$templateLanding->referencia."/index.html");
                        $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                        $html = populaVariaveisCartao($html, $cds[0]->id);
                        print $html;
                    }
                }
            } else {  // subdomínio
                $sites = Site::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo'"));
                if(count($sites) >=1){
                    if(empty($param1)){
                        novavisita($sites[0]->id, 'Site');
                        $site = $sites[0];
                        if(empty($page)){
                            $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = 'home'"));
                        } else {
                            $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = '".$page."'"));
                        }
                        $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.'.html');
                        $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                        $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                        $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                        $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                        print $html;
                    } else {
                        if($param1 == 'lp'){ 
                            $lps = Landing::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo' AND route = '".$param2."'"));
                            if(count($lps) >=1){
                                $templateLanding = TemplateLandingpage::find($lps[0]->template);
                                novavisita($lps[0]->id, 'Landing');
                                $html = file_get_contents("https://templates.otimize.online/".$templateLanding->pasta."/".$templateLanding->referencia."/index.html");
                                $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                                $html = populaVariaveisLP($html, $lps[0]->id);
                                
                                if($lps[0]->template == 20){
                                    $html = populaSegurosLP($html, $lps[0]->id);
                                }
                                if($lps[0]->template < 20){
                                    $html = populaSeguradorasLP($html, $lps[0]->id);
                                } elseif($lps[0]->template == 33){
                                    $html = populaSeguradorasLP7($html, $lps[0]->id);
                                    $html = populaFaq($html, $lps[0]->id);
                                    $html = populaDepoimentos($html, $lps[0]->id);
                                }
                                print $html;
                            }
                        }
                        if($param1 == 'card'){
                            $cds = Cartao::find(0, array("corretora = '".$configuracoesHospedagem[0]->corretora."' AND status = 'Ativo' AND route = '".$param2."'"));
                            if(count($cds) >=1){
                                $templateLanding = TemplateCartoes::find($cds[0]->template);
                                novavisita($cds[0]->id, 'Cartao');
                                $html = file_get_contents("https://templates.otimize.online/".$templateLanding->pasta."/".$templateLanding->referencia."/index.html");
                                $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                                $html = populaVariaveisCartao($html, $cds[0]->id);
                                print $html;
                            }
                        }
                        if($param1 != 'lp' && $param1 != 'card'){
                            if(empty($param2)){
                                $site = $sites[0];
                                $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = '".strtolower($param1)."'"));
                                $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.'.html');
                                $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                                $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                                $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                                $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                                print $html;
                            } else {
                                $site = $sites[0];
                                $sitePagina = SitePagina::find(0, array("site = '".$sites[0]->id."' AND route = '".strtolower($param1)."'"));
                                $html = file_get_contents("https://templates.otimize.online/".$sites[0]->pasta."/".$sites[0]->referencia."/".$sitePagina[0]->route.".html");
                                $html = preg_replace('/{{dominio}}/', 'https://'.$subdominio.'.'.VPS_DOMINIO.'/', $html);
                                if($param1 == 'seguros'){
                                    $siteSeguroAvulso = SiteSeguros::find(0, array("url = '".$param2."' AND corretora = '".$site->corretora."' AND status = 'Ativo'"));
                                    if(count($siteSeguroAvulso) >=1){
                                        $html = populaSeguroAvulso($html, $siteSeguroAvulso[0]->id);
                                    } else {
                                        redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
                                    }
                                } elseif($param1 == 'seguradoras'){
                                    $siteSeguradoraAvulso = SiteSeguradoras::find(0, array("url = '".$param2."' AND corretora = '".$site->corretora."' AND status = 'Ativo'"));
                                    if(count($siteSeguradoraAvulso) >=1){
                                        $html = populaSeguradoraAvulsa($html, $siteSeguradoraAvulso[0]->id);
                                    } else {
                                        redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
                                    }
                                } else {
                                    redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
                                }
                                $html = populaVariaveis($html, $site->id, $sitePagina[0]->id);
                                $html = populaSeguros($html, $site->id, $sitePagina[0]->id);
                                $html = populaSeguradoras($html, $site->id, $sitePagina[0]->id);
                                print $html;
                            }
                        }
                    }
                } else {
                    redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
                }
            }
        } else {
            redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
        }
    } else {
        redireciona('https://'.$subdominio.'.'.VPS_DOMINIO.'/');
    }
} else {
    redireciona('https://'.VPS_DOMINIO.'/');
}

function populaSeguros($html, $site_id, $pagina_id){
    $site = Site::find($site_id);
    $seguros = SiteSeguros::find(0, array("corretora = '".$site->corretora."'"));
    $div = '';
    foreach($seguros as $seguro){
        if($site->template == 1){
            if($seguro->status == 'Ativo'){
                $div .= '<div class="fe07_card">';
                $div .= '<div class="fe07_card_seguro" style="background: '.$site->cor_icones.'"><img src="https://templates.otimize.online/seguros/'.$seguro->icone.'" width="125" sizes="(max-width: 479px) 12vw, 55px" class="fe07_images"></div>';
                $div .= '<h4 class="fe07_h4">'.$seguro->nome.'</h4>';
                $div .= '<p class="fe07_text">'.$seguro->subtitulo.'</p>';
                $div .= '<a href="seguros/'.$seguro->url.'" class="fe07_button w-button" style="background: '.$site->cor_primaria.'">SAIBA MAIS</a>';
                $div .= '</div>';
            }
        } elseif($site->template == 2){
            if($seguro->status == 'Ativo'){
                $div .= '<a href="seguros/'.$seguro->url.'" class="link-block w-inline-block">';
                $div .= '<div class="fe02_card"><div class="fe02_card_seguro" style="background: '.$site->cor_icones.'"><img src="https://templates.otimize.online/seguros/'.$seguro->icone.'" width="55" sizes="(max-width: 991px) 48px, 50px" class="fe02_icon"></div>';
                $div .= '<div class="fe02_card_titles">';
                $div .= '<h5 class="fe02_h5">'.$seguro->nome.'</h5>';
                $div .= '<div class="fe02_text">SAIBA MAIS</div>';
                $div .= '</div>';
                $div .= '</div>';
                $div .= '</a>';
            }
        } elseif($site->template == 3){
            if($seguro->status == 'Ativo'){
                $div .= '<div class="fe04_card">';
                $div .= '<div class="fe04_icon" style="background: '.$site->cor_icones.'"><img src="https://templates.otimize.online/seguros/'.$seguro->icone.'" width="30" sizes="30px" class="fe04_icon_img white"></div>';
                $div .= '<div class="fe04_icon_text">'.$seguro->nome.'<br></div>';
                $div .= '<a href="seguros/'.$seguro->url.'" class="fe04_link_cover w-inline-block"></a>';
                $div .= '</div>';
            }
        }
    }
    $html = preg_replace('/{{seguros}}/', $div, $html);
    return $html;
}    
function populaSeguradoras($html, $site_id, $pagina_id){
    $site = Site::find($site_id);
    $seguradoras = SiteSeguradoras::find(0, array("corretora = '".$site->corretora."'"));
    $div = '';
    foreach($seguradoras as $seguradora){
        if($site->template == 1){
            if($seguradora->status == 'Ativo'){
                $div .= '<div class="cl04_card">';
                $div .= '<a href="seguradoras/'.$seguradora->url.'" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img">';
                $div .= '</a>';
                $div .= '</div>';
            }
        } elseif($site->template == 2){
            if($seguradora->status == 'Ativo'){
                $div .= '<div class="cl04_card more">';
                $div .= '<a href="seguradoras/'.$seguradora->url.'" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img"></a>';
                $div .= '</div>';
            }
        } elseif($site->template == 3){
            if($seguradora->status == 'Ativo'){
                $div .= '<div class="cl04_card more">';
                $div .= '<a href="seguradoras/'.$seguradora->url.'" class="w-inline-block"><img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" alt="" class="cl04_logo_img"></a>';
                $div .= '</div>';
            }
        }
    }
    $html = preg_replace('/{{seguradoras}}/', $div, $html);
    return $html;
}    
function populaVariaveis($html, $site_id, $pagina_id){
    $site = Site::find($site_id);
    $variaveis = SiteVariaveis::find(0, array("site = '".$site_id."' AND pagina = '".$pagina_id."'"));
    
    $whatsapp = str_replace(array(" ", "(", ")", "-", ".", "[", "]", "_"), "", $site->whatsapp);
    
    
    $html = preg_replace('/{{tagHead}}/', $site->tagsHead, $html);
    $html = preg_replace('/{{tagFooter}}/', $site->tagsBody, $html);
    $html = preg_replace('/{{site_id}}/', $site_id, $html);
    $html = preg_replace('/{{site_id}}/', $site_id, $html);
    $html = preg_replace('/{{cor_primaria}}/', $site->cor_primaria, $html);
    $html = preg_replace('/{{cor_secundaria}}/', $site->cor_secundaria, $html);
    $html = preg_replace('/{{telefone}}/', $site->telefone, $html);
    $html = preg_replace('/{{endereco}}/', $site->endereco, $html);
    $html = preg_replace('/{{whatsapp}}/', $whatsapp, $html);
    $html = preg_replace('/{{titulo}}/', $site->titulo, $html);
    $html = preg_replace('/{{logotipo}}/', $site->logotipo, $html);
    $html = preg_replace('/{{favicon}}/', $site->favicon, $html);
    $html = preg_replace('/{{email}}/', $site->email, $html);
    $html = preg_replace('/{{facebook}}/', $site->facebook, $html);
    $html = preg_replace('/{{instagram}}/', $site->instagram, $html);
    $html = preg_replace('/{{linkedin}}/', $site->linkedin, $html);
    $html = preg_replace('/{{twitter}}/', $site->twitter, $html);
    $html = preg_replace('/{{fraseFooter}}/', $site->fraseFooter, $html);
    $html = preg_replace('/{{fraseFooter2}}/', $site->fraseFooter2, $html);
    foreach($variaveis as $v){
        if(!empty($v->conteudo)){
            $html = preg_replace('/'.$v->referencia.'/', $v->conteudo, $html);
        } else {
            $html = preg_replace('/'.$v->referencia.'/', $v->imagem, $html);
        }
    }
    return $html;
}   
function populaVariaveisPRO($html, $site_id){
    $site = Site::find($site_id);
    $html = preg_replace('/{{cor_primaria}}/', $site->cor_primaria, $html);
    $html = preg_replace('/{{cor_secundaria}}/', $site->cor_secundaria, $html);
    $html = preg_replace('/{{telefone}}/', $site->telefone, $html);
    $html = preg_replace('/{{whatsapp}}/', $site->whatsapp, $html);
    $html = preg_replace('/{{titulo}}/', $site->titulo, $html);
    $html = preg_replace('/{{logotipo}}/', $site->logotipo, $html);
    $html = preg_replace('/{{favicon}}/', $site->favicon, $html);
    $html = preg_replace('/{{email}}/', $site->email, $html);
    $html = preg_replace('/{{facebook}}/', $site->facebook, $html);
    $html = preg_replace('/{{instagram}}/', $site->instagram, $html);
    $html = preg_replace('/{{linkedin}}/', $site->linkedin, $html);
    $html = preg_replace('/{{twitter}}/', $site->twitter, $html);
    $html = preg_replace('/{{fraseFooter}}/', $site->fraseFooter, $html);
    return $html;
}       
function populaSeguroAvulso($html, $id){
    $seguro = SiteSeguros::find($id);
    $html = preg_replace('/{{seguroNome}}/', $seguro->nome, $html);
    $html = preg_replace('/{{seguroTitulo}}/', $seguro->titulo, $html);
    $html = preg_replace('/{{seguroSubtitulo}}/', $seguro->subtitulo, $html);
    $html = preg_replace('/{{imagemSeguro}}/', $seguro->imagem, $html);
    $html = preg_replace('/{{detalhesSeguro}}/', $seguro->detalhes, $html);
    return $html;
}
function populaSeguradoraAvulsa($html, $id){
    $seguro = SiteSeguradoras::find($id);
    $html = preg_replace('/{{nomeSeguradora}}/', $seguro->nome, $html);
    $html = preg_replace('/{{sobreSeguradora}}/', $seguro->sobre, $html);
    $html = preg_replace('/{{logotipoSeguradora}}/', $seguro->logotipo, $html);
    $html = preg_replace('/{{telefoneSeguradora}}/', $seguro->telefone, $html);
    return $html;
}
function novaVisita($id, $tipo){
    $visita = new Visita();
    if($tipo == 'Site'){
        $visita->site = $id;
    } elseif($tipo == 'Landing'){
        $visita->landing = $id;
    } elseif($tipo == 'Cartao'){
        $visita->cartao = $id;
    }
    $visita->save();
}
function redireciona($redir){
    return header("Location: ".$redir."405.html");
}
function populaVariaveisCartao($html, $lp_id){
    $cartao = Cartao::find($lp_id);
    $whatsapp = str_replace(array(" ", "(", ")", "-", ".", "[", "]", "_"), "", $cartao->whatsapp);
    $html = preg_replace('/{{cor_primaria}}/', $cartao->cor_primaria, $html);
    $html = preg_replace('/{{cor_secundaria}}/', $cartao->cor_secundaria, $html);
    $html = preg_replace('/{{descricao}}/', $cartao->descricao, $html);
    $html = preg_replace('/{{telefone}}/', $cartao->telefone, $html);
    $html = preg_replace('/{{endereco}}/', $cartao->endereco, $html);
    $html = preg_replace('/{{whatsapp}}/', $whatsapp, $html);
    $html = preg_replace('/{{titulo}}/', $cartao->titulo, $html);
    $html = preg_replace('/{{logotipo}}/', $cartao->logotipo, $html);
    $html = preg_replace('/{{favicon}}/', $cartao->favicon, $html);
    $html = preg_replace('/{{email}}/', $cartao->email, $html);
    if(!empty($cartao->facebook)){
            $facebook = '<a href="'.$cartao->facebook.'" target="_blank" class="social-link w-inline-block" style="opacity:1 !important;">';
            $facebook .= '<img src="https://templates.otimize.online/kit1/cartao1/images/facebook.png" alt="" class="social-icon">';
            $facebook .= '</a>';
            $html = preg_replace('/{{facebook}}/', $facebook, $html);
    } else {
         $html = preg_replace('/{{facebook}}/', '', $html);
    }
    if(!empty($cartao->twitter)){
            $twitter = '<a href="'.$cartao->twitter.'" target="_blank" class="social-link w-inline-block">';
            $twitter .= '<img src="https://templates.otimize.online/kit1/cartao1/images/Twitter.svg" alt="" class="social-icon">';
            $twitter .= '</a>';
            $html = preg_replace('/{{twitter}}/', $twitter, $html);
    } else {
         $html = preg_replace('/{{twitter}}/', '', $html);
    }
    if(!empty($cartao->instagram)){
          $instagram = '<a href="'.$cartao->instagram.'" target="_blank" class="social-link w-inline-block">';
          $instagram .= '<img src="https://templates.otimize.online/kit1/cartao1/images/Instagram.svg" alt="" class="social-icon">';
          $instagram .= '</a>';
          $html = preg_replace('/{{instagram}}/', $instagram, $html);
    } else {
         $html = preg_replace('/{{instagram}}/', '', $html);
    }
    if(!empty($cartao->linkedin)){
          $linkedin = '<a href="'.$cartao->linkedin.'" target="_blank" class="social-link w-inline-block">';
          $linkedin .= '<img src="https://templates.otimize.online/kit1/cartao1/images/linkedin.svg" alt="" class="social-icon">';
          $linkedin .= '</a>';
          $html = preg_replace('/{{linkedin}}/', $linkedin, $html);
    } else {
         $html = preg_replace('/{{linkedin}}/', '', $html);
    }
    return $html;
}  
function populaVariaveisLP($html, $lp_id){
    $landing = Landing::find($lp_id);
    $variaveis = LandingVariaveis::find(0, array("landing = '".$lp_id."'"));
    $whatsapp = str_replace(array(" ", "(", ")", "-", ".", "[", "]", "_"), "", $landing->whatsapp);
    
    $html = preg_replace('/{{tagHead}}/', $landing->tagsHead, $html);
    $html = preg_replace('/{{tagFooter}}/', $landing->tagsBody, $html);
    $html = preg_replace('/{{cor_primaria}}/', $landing->cor_primaria, $html);
    $html = preg_replace('/{{cor_secundaria}}/', $landing->cor_secundaria, $html);
    $html = preg_replace('/{{cor_terciaria}}/', $landing->cor_terciaria, $html);
    $html = preg_replace('/{{cor_quaternaria}}/', $landing->cor_quaternaria, $html);
    $html = preg_replace('/{{cor_quinaria}}/', $landing->cor_quinaria, $html);
    $html = preg_replace('/{{cor_senaria}}/', $landing->cor_senaria, $html);
    $html = preg_replace('/{{telefone}}/', $landing->telefone, $html);
    $html = preg_replace('/{{lp_id}}/', $lp_id, $html);
    $html = preg_replace('/{{endereco}}/', $landing->endereco, $html);
    $html = preg_replace('/{{whatsapp}}/', $whatsapp, $html);
    $html = preg_replace('/{{titulo}}/', $landing->titulo, $html);
    $html = preg_replace('/{{logotipo}}/', $landing->logotipo, $html);
    $html = preg_replace('/{{favicon}}/', $landing->favicon, $html);
    $html = preg_replace('/{{email}}/', $landing->email, $html);
    $html = preg_replace('/{{facebook}}/', $landing->facebook, $html);
    $html = preg_replace('/{{instagram}}/', $landing->instagram, $html);
    $html = preg_replace('/{{linkedin}}/', $landing->linkedin, $html);
    $html = preg_replace('/{{twitter}}/', $landing->twitter, $html);
    if(empty($landing->logotipo_footer)){
        $html = preg_replace('/{{logotipo_footer}}/', $landing->logotipo, $html);
    } else {
        $html = preg_replace('/{{logotipo_footer}}/', $landing->logotipo_footer, $html);
    }
    if(empty($landing->redirecionamento)){
        $redirect = 'setTimeout(function(){ location.reload(); }, 1000);';
    } else {
        $redirect = 'setTimeout(function(){ location.href="'.$landing->redirecionamento.'"; }, 1000);';
    }
    $html = preg_replace('/{{redirecionamento}}/', $redirect, $html);
    foreach($variaveis as $v){
        if(!empty($v->conteudo)){
            $html = preg_replace('/'.$v->referencia.'/', $v->conteudo, $html);
        } else {
            $html = preg_replace('/'.$v->referencia.'/', $v->imagem, $html);
        }
    }
    return $html;
}  



function populaSegurosLP($html, $lp_id){
    $landing = Landing::find($lp_id);
    $landingSeguros = LandingSeguros::find(0, array("landing = '".$landing->id."' AND status = 'Ativo'"));
    if(count($landingSeguros) >=1){
        $seguros = LandingSeguros::find(0, array("landing = '".$landing->id."'"));
        foreach($seguros as $seguro){
            if($seguro->status == 'Ativo'){
                $div = '<div class="fe04_card" >';
                $div .= '<div class="fe04_icon" style="background: '.$landing->cor_primaria.';"><img src="https://templates.otimize.online/seguros/'.$seguro->icone.'" width="30" sizes="30px" class="fe04_icon_img white"></div>';
                $div .= '<div class="fe04_icon_text">'.$seguro->nome.'<br></div>';
                $div .= '<a href="javascript:void(0);" class="fe04_link_cover w-inline-block"></a>';
                $div .= '</div>';
                $html = preg_replace('/'.$seguro->variavel.'/', $div, $html);
            } else {
                $html = preg_replace('/'.$seguro->variavel.'/', '', $html);
            }
        }
        $css = '<style>.seguros{ display:block !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    } else {
        $css = '<style>.seguros{ display:none !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    }
    return $html;
}  
function populaSeguradorasLP($html, $lp_id){
    $landing = Landing::find($lp_id);
    $landingSeguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."' AND status = 'Ativo'"));
    if(count($landingSeguradoras) ==1 && $landing->template != 20){
        $logotipoSeguradora = '<a href="javascript:void(0)" class="nav07_logo cia w-nav-brand"><img src="https://templates.otimize.online/seguradoras/'.$landingSeguradoras[0]->logotipo.'" width="120" sizes="120px" class="nav07_logo_image"></a>';
        $html = preg_replace('/{{logotipoSeguradora}}/', $logotipoSeguradora, $html);
        $html = preg_replace('/{{subtituloSeguradora}}/', $landingSeguradoras[0]->subtitulo, $html);
        $html = preg_replace('/{{sobreSeguradora}}/', $landingSeguradoras[0]->sobre, $html);
        $css = '<style>.seguradoras{ display:none !important; } .seguradora{ display:block !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    } elseif(count($landingSeguradoras) >=2 && $landing->template != 20){
        $html = preg_replace('/{{logotipoSeguradora}}/', ' ', $html);
        $html = preg_replace('/{{subtituloSeguradora}}/', ' ', $html);
        $html = preg_replace('/{{sobreSeguradora}}/', ' ', $html);
        $seguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."'"));
        foreach($seguradoras as $seguradora){
            if($seguradora->status == 'Ativo'){
                $div = '<div class="cl04_card">';
                $div .= '<a href="javascript:void(0);" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img">';
                $div .= '</a>';
                $div .= '</div>';
                $html = preg_replace('/'.$seguradora->variavel.'/', $div, $html);
            } else {
                $html = preg_replace('/'.$seguradora->variavel.'/', '', $html);
            }
        }
        $css = '<style>.seguradoras{ display:block !important; } .seguradora{ display:none !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    } elseif(count($landingSeguradoras) ==0 && $landing->template != 20){
        $html = preg_replace('/{{logotipoSeguradora}}/', ' ', $html);
        $html = preg_replace('/{{subtituloSeguradora}}/', ' ', $html);
        $html = preg_replace('/{{sobreSeguradora}}/', ' ', $html);
        $seguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."'"));
        foreach($seguradoras as $seguradora){
            if($seguradora->status == 'Ativo'){
                $div = '<div class="cl04_card">';
                $div .= '<a href="javascript:void(0);" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img">';
                $div .= '</a>';
                $div .= '</div>';
                $html = preg_replace('/'.$seguradora->variavel.'/', $div, $html);
            } else {
                $html = preg_replace('/'.$seguradora->variavel.'/', '', $html);
            }
        }
        $css = '<style>.seguradoras, .seguradora{ display:none !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    }
    if(count($landingSeguradoras) >=1 && $landing->template == 20){
        $logotipoSeguradora = '<a href="javascript:void(0);" class="nav07_logo cia w-nav-brand"><img src="https://templates.otimize.online/seguradoras/'.$landingSeguradoras[0]->logotipo.'" width="120" sizes="120px" class="nav07_logo_image"></a>';
        $html = preg_replace('/{{logotipoSeguradora}}/', $logotipoSeguradora, $html);
        $html = preg_replace('/{{subtituloSeguradora}}/', $landingSeguradoras[0]->subtitulo, $html);
        $html = preg_replace('/{{sobreSeguradora}}/', $landingSeguradoras[0]->sobre, $html);
        $seguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."'"));
        foreach($seguradoras as $seguradora){
            if($seguradora->status == 'Ativo'){
                $div = '<div class="cl04_card">';
                $div .= '<a href="javascript:void(0);" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img">';
                $div .= '</a>';
                $div .= '</div>';
                $html = preg_replace('/'.$seguradora->variavel.'/', $div, $html);
            } else {
                $html = preg_replace('/'.$seguradora->variavel.'/', '', $html);
            }
        }
        $css = '<style>.seguradoras{ display:block !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    } elseif(count($landingSeguradoras) ==0 && $landing->template == 20){
        $logotipoSeguradora = '<a href="javascript:void(0)" class="nav07_logo cia w-nav-brand"><img src="https://templates.otimize.online/seguradoras/'.$landingSeguradoras[0]->logotipo.'" width="120" sizes="120px" class="nav07_logo_image"></a>';
        $html = preg_replace('/{{logotipoSeguradora}}/', $logotipoSeguradora, $html);
        $html = preg_replace('/{{subtituloSeguradora}}/', $landingSeguradoras[0]->subtitulo, $html);
        $html = preg_replace('/{{sobreSeguradora}}/', $landingSeguradoras[0]->sobre, $html);
        $seguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."'"));
        foreach($seguradoras as $seguradora){
            if($seguradora->status == 'Ativo'){
                $div = '<div class="cl04_card">';
                $div .= '<a href="javascript:void(0);" class="w-inline-block">';
                $div .= '<img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'" sizes="100px" class="cl04_logo_img">';
                $div .= '</a>';
                $div .= '</div>';
                $html = preg_replace('/'.$seguradora->variavel.'/', $div, $html);
            } else {
                $html = preg_replace('/'.$seguradora->variavel.'/', '', $html);
            }
        }
        $css = '<style>.seguradoras, .seguradora{ display:none !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    }
    return $html;
}    
function populaSeguradorasLP7($html, $lp_id){
    $landing = Landing::find($lp_id);
    $landingSeguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."' AND status = 'Ativo'"));
    if(count($landingSeguradoras) >=1){
        $seguradoras = LandingSeguradoras::find(0, array("landing = '".$landing->id."'"));
        foreach($seguradoras as $seguradora){
            if($seguradora->status == 'Ativo'){
                $div .= '<div class="seguradoras2-logo"><img src="https://templates.otimize.online/seguradoras/'.$seguradora->logotipo.'"></div>';
            } else {
 
            }
        }
        $html = preg_replace('/{{seguradoras}}/', $div, $html);
        $css = '<style>.seguradoras{ display:block !important; } </style> </body>';
        $html = str_replace('</body>', $css, $html);
    } 
    return $html;
}    

function populaFaq($html, $lp_id){
    $landing = Landing::find($lp_id);
    $landingFaqs = LandingFaq::find(0, array("landing = '".$landing->id."'"));
    if(count($landingFaqs) >=1){
        foreach($landingFaqs as $faq){
            $div .= '<div class="accordion-item">';
            $div .= '<h2 class="accordion-header">';
            $div .= '<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#flush-collapse'.$faq->id.'" aria-expanded="false" aria-controls="flush-collapse'.$faq->id.'">'.$faq->pergunta.'</button>';
            $div .= '</h2>';
            $div .= '<div id="flush-collapse'.$faq->id.'" class="accordion-collapse collapse" data-bs-parent="#accordionFlushExample">';
            $div .= '<div class="accordion-body">'.$faq->resposta.'</div>';
            $div .= ' </div>';
            $div .= '</div>';
        }
        $html = preg_replace('/{{perguntas}}/', $div, $html);
    } 
    return $html;
}    
function populaDepoimentos($html, $lp_id){
    $landing = Landing::find($lp_id);
    $landingDepos = LandingDepoimentos::paginate(0, 4, "landing = '".$landing->id."'", 'RAND()');
    if(count($landingDepos) >=1){
        foreach($landingDepos as $depo){
            $div .= '<div class="depoimento">';
            $div .= '<img src="https://templates.otimize.online/kit1/lp7/imagens/1260503_1_17376694646792bb58b7fba586168002.png">';
            $div .= '<h3>'.$depo->titulo.'</h3>';
            $div .= '<p>'.$depo->texto.'</p>';
            $div .= '</div>';
        }
        $html = preg_replace('/{{depoimentos}}/', $div, $html);
    } 
    return $html;
}    



function sitemap($domain){
    
    $host = 'https://'.$domain.'/';
    $xml = simplexml_load_file('sitemap.xml');

    $xml->url[0]->loc = $host;
    $xml->url[1]->loc = $host.'seguros';
    $xml->url[2]->loc = $host.'seguradoras';
    $xml->url[3]->loc = $host.'contato';
    $xml->url[4]->loc = $host.'sobre';
    
    $xml->asXML('sitemap.xml');
}
?>