<?php
require("config.php");
//var_dump($_SESSION);
require("includes/debug.php");
require("includes/functions.php");
require("includes/logincheck.php");
require("includes/settings.php");
$status = "";
if (isset($_GET["setting"]) and $_GET["setting"] == "editmodel") {
    if (isset($_POST["salvarModelo"])) {
        //var_dump($_POST);
        $uid = $_SESSION["adminid"];
        $assunto = trim($_POST["assunto"]);
        $corpo = trim(htmlentities($_POST["corpo"]));
        $tipomodel = $_POST["tipomodel"];
        $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or mysql_error();
        mysql_select_db($dbname);
        $sql = mysql_query("INSERT INTO cmum_mail_models (uid,assunto,corpo,tipomodel,data_att) 
                           VALUES('$uid','$assunto','$corpo','$tipomodel',NOW())");
        mysql_close();
        if ($sql) {
            $status = "Modelo Salvo Com Sucesso!";
        } else {
            $status = "Error ao Salvar Dados!";
        }
    }
    if (isset($_POST["attModelo"])) {
        $uid = $_SESSION["adminid"];
        $assunto = trim($_POST["assunto"]);
        $corpo = trim(htmlentities($_POST["corpo"]));
        $tipomodel = $_POST["tipomodel"];
        $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or mysql_error();
        mysql_select_db($dbname);
        $sql = mysql_query("UPDATE cmum_mail_models SET assunto='$assunto',corpo='$corpo',data_att=NOW() WHERE uid='$uid' and tipomodel='$tipomodel'");
        if ($sql) {
            $status = "Modelo Atualizado com Sucesso!";
        } else {
            $status = "Error ao Atualizar Dados!";
        }
    }
    echo "<script>
        alert('$status');
        window.close();
        </script>";

    exit();
}
// Dados Vindo do Formulario Selec Modelo
if (isset($_GET["tipomodel"]) and $_GET["tipomodel"] != ""):

    $assunto = "";
    $corpo = "";
    $tipomodel = $_GET["tipomodel"];
    $conn = @mysql_connect($dbhost, $dbuser, $dbpass) or mysql_error();
    mysql_select_db($dbname);
    $result = mysql_query("SELECT * FROM cmum_mail_models WHERE uid = '{$_SESSION["adminid"]}' and tipomodel = '{$tipomodel}'");
    $line = mysql_fetch_assoc($result);
    if (is_array($line)) {
        $assunto = html_entity_decode($line["assunto"]);
        $corpo = html_entity_decode($line["corpo"]);
        //echo "<h1>Sucesso</h1>";
        //include html.php;
    }
endif;
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta http-equiv="Content-Type" content="text/html; charset=<?php print($charset); ?>" />
            <title>Formulario Editar Modelo Email</title>
            <link rel="shortcut icon" href="favicon.ico">
                <link rel="apple-touch-icon" href="favicon.png">
                    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
                        <link href="css/main.css" rel="stylesheet" type="text/css">
                            <script src="js/jquery1.9.js"></script>
                            <script src="js/tinymce/tinymce.min.js"></script>
                            </script>
                            <script>
                                tinymce.init({
                                    selector: "textarea",
                                    plugins: [
                                        "advlist autolink lists link image charmap print preview anchor",
                                        "searchreplace visualblocks code fullscreen",
                                        "insertdatetime media table contextmenu paste moxiemanager"
                                    ],
                                    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
                                });
                            </script>
                            </head>
                            <body style="background-color: white">
                                <table id="tbFormEditModel" style="border: solid 0px;">
                                    <form method="post" name="formEditModel" id="formEditModel" action="?setting=editmodel" style="">
                                        <tr>
                                            <td colspan="2">                    
                                                <h4>Variaveis</h4>
                                                <ul style="list-style: none; font-size: 10pt">
                                                    <li><strong>[EMVENDEDOR]</strong> = Email do Vendedor - Seu email vai aparecer onde adicionar essa variável.</li>
                                                    <li><strong>[NMVENDEDOR]</strong> = Nome do Vendedor -  Seu nome vai aparecer onde adicionar essa variável.</li>
                                                    <li><strong>[LGCLIENTE]</strong> = Login do Cliente - Vai aparecer o login do cliente.</li>
                                                    <li><strong>[SNCLIENTE]</strong> = Senha do Cliente - Vai aparecer a senha do cliente.</li>
                                                    <li><strong>[NMCLIENTE]</strong> = Login do Cliente - Vai aparecer o nome do cliente.</li>                                                
                                                    <li><strong>[VCCLIENTE]</strong> = Vencimento do Cliente</li>
                                                    <li><strong>[DDSERVIDOR]</strong> = Dados Servidor</li>        
                                                </ul>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td></td><td><label>Assunto:&nbsp;</label><br/><input type="text" name="assunto" id="assunto" value="<?php echo $assunto; ?>" style="width: 640px;" class="form-control"/></td>
                                        </tr>
                                        <tr>
                                            <td></td><td><label>Corpo:</label><br/><textarea name="corpo" id="corpo" style="width: 640px; height: 300px;"><?php echo $corpo; ?></textarea></td>
                                        </tr>
                                        <tr>
                                            <td>&nbsp;</td>
                                        </tr>
                                        <tr>
                                            <td></td><td align="right">
                                                <input type="hidden" name="tipomodel" value="<?php echo $_GET["tipomodel"] ?>"/>
                                                <?php if (!is_array($line)): ?>                        
                                                    <input type="submit" value="Salvar Modelo" name="salvarModelo" class="btn btn-default"/>
                                                <?php else: ?>
                                                    <input type="submit" value="Atualizar Modelo" name="attModelo" class="btn btn-default"/>
                                                <?php endif; ?>
                                            </td>
                                        </tr>       
                                    </form>        
                                </table>
                                <script>
                                    $( document ).ready(function() {
                                        window.moveTo(0, 0);
                                        window.resizeTo(screen.availWidth, screen.availHeight);  
                                    });
                                </script>

                            </body>
                            </html>