   <?php
   // CRUD 
   @$getId = base64_decode(anti_injection($_GET['id']));  
    if(@$getId){ 
        $query = mysql_query("SELECT pl.*, cl.name AS name, cl.email AS email FROM tickets AS pl INNER JOIN customers AS cl ON pl.customer = cl.id WHERE pl.id = + $getId");
        $result = mysql_fetch_array($query);
    }
   
    
    
    if(isset ($_POST['send'])){
	
	$id = $_POST['ticketID'];
	$tpk = $_POST['tpk'];
	$customer = $_POST['customer'];
	$customer_name = $_POST['customer_name'];
	$customer_email = $_POST['customer_email'];
	$user = $_POST['user'];
	$type = '2';
	$date = date('Y-m-d');
	$data_email = date('d/m/Y');
	$dataen_email = date('m/d/Y');
	$hour = date('H:i:s');
	$subject = $_POST['subject'];
	$msg = $_POST['msg'];
	$idT = base64_encode($id);
	
	$crud = new crud('messages');  // table
    $crud->inserir("ticket,customer,user,date,hour,subject,msg,type,status", "'$id','$customer','$user','$date','$hour','$subject','$msg','$type','S'");
	
	$crud = new crud('tickets'); // Table
    $crud->atualizar("status='2',date='$date',hour='$hour'", "id='$id'");
	
	// SEND MAIL 
	require_once("config/mail/class.phpmailer.php");
	$query_tpl = mysql_query("SELECT * FROM email_template WHERE id = '2'");
    $template_mail = mysql_fetch_array($query_tpl);
	
	$mail = new PHPMailer();
	$mail->IsSMTP();		
	$mail->SMTPDebug = 0;		
	$mail->SMTPAuth = true;		
	$mail->IsHTML(true);
	$mail->Host = $config['smtp_host'];	
	$mail->Port = $config['smtp_port'];		
	$mail->Username = $config['smtp_user'];	
	$mail->Password = $config['smtp_pass'];	

	$mail->From = $template_mail['from_mail'];  
	$mail->FromName = $template_mail['name_from']; 

	$mail->AddAddress($customer_email);
	$mail->IsHTML(true); 
	$mail->CharSet = 'iso-8859-1'; 
	$mail->Subject  = $template_mail['subject']; 
	
	$corpoDoEmail = $template_mail['msg']; 
	$signature = $config['signature'];
	$logo = $config['logo'];
	
	// MAIL Variables
    $corpoDoEmail = str_replace( '%NOME%', $customer_name, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%TICKET%', $tpk, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%DATABR%', $data_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%DATAEN%', $dataen_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%ASSUNTO%', $subject, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%MENSAGEM%', $msg, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%EMAIL%',  $customer_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%SIGNATURE%',  $signature, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%LOGO%',  $logo, $corpoDoEmail );
    
    // END Variables
	$mail->Body = $corpoDoEmail;
	$SendOK = $mail->Send();
	$mail->ClearAllRecipients();
	$mail->ClearAttachments();
	if ($SendOK) {
	header("Location: index.php?mod=Ticket&id=$idT&y=1"); // redirect send mail ok
    } else {
	echo "<script>
        alert ('Error sending email, please check the e-mail settings!');
        document.location.href = ('index.php?mod=Ticket&id=$idT&y=1');
	</script>";
    }
	// END SEND 
	
	
		
    }
    if ((isset($_GET["action"])) && ($_GET["action"] == "delete")) {
    $id = base64_decode($_GET['msg']); 
    $crud = new crud('messages'); // Table
    $crud->excluir("id = $id"); // ID
    $idT = $_GET['id'];
    header("Location: index.php?mod=Ticket&id=$idT&y=3");
    }
    if ((isset($_GET["action"])) && ($_GET["action"] == "close")) {
    $id = base64_decode($_GET['msg']); 
    
    $crud = new crud('tickets'); // Table
    $crud->atualizar("status='4'", "id='$id'");
    
    // SEND MAIL 
	require_once("config/mail/class.phpmailer.php");
	$query_tpl = mysql_query("SELECT * FROM email_template WHERE id = '3'");
    $template_mail = mysql_fetch_array($query_tpl);
    $data_email = date('d/m/Y');
	$dataen_email = date('m/d/Y');
	$hour = date('H:i:s');
	$customer_name = base64_decode($_GET['nm']);
	$customer_email = base64_decode($_GET['em']);
	
	$mail = new PHPMailer();
	$mail->IsSMTP();		
	$mail->SMTPDebug = 0;		
	$mail->SMTPAuth = true;		
	$mail->IsHTML(true);
	$mail->Host = $config['smtp_host'];	
	$mail->Port = $config['smtp_port'];		
	$mail->Username = $config['smtp_user'];	
	$mail->Password = $config['smtp_pass'];	

	$mail->From = $template_mail['from_mail'];  
	$mail->FromName = $template_mail['name_from']; 

	$mail->AddAddress($customer_email);
	$mail->IsHTML(true); 
	$mail->CharSet = 'iso-8859-1'; 
	$mail->Subject  = $template_mail['subject']; 
	
	$corpoDoEmail = $template_mail['msg']; 
	$signature = $config['signature'];
	$logo = $config['logo'];
	
	// MAIL Variables
    $corpoDoEmail = str_replace( '%NOME%', $customer_name, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%DATABR%', $data_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%DATAEN%', $dataen_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%EMAIL%',  $customer_email, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%SIGNATURE%',  $signature, $corpoDoEmail );
    $corpoDoEmail = str_replace( '%LOGO%',  $logo, $corpoDoEmail );
    
    // END Variables
	$mail->Body = $corpoDoEmail;
	$SendOK = $mail->Send();
	$mail->ClearAllRecipients();
	$mail->ClearAttachments();
	if ($SendOK) {
	header("Location: index.php?mod=TicketsClosed&y=4"); // redirect send mail ok
    } else {
	echo "<script>
        alert ('Error sending email, please check the e-mail settings!');
        document.location.href = ('index.php?mod=TicketsClosed&y=4');
	</script>";
    }
	// END SEND
	
    
    }
    ?>
    
    <!-- JAVASCRIPT -->
     <script>
     $().ready(function() {

		$("#newticket").validate({
			rules: {
				
				subject: "required",
				msg: {
					required: true,
					minlength: 10
				}
			},
			messages: {
				subject: "<?php echo $lang['VALIDATE_TICKET_SUBJECT']; ?>",
				msg: {
					required: "<?php echo $lang['VALIDATE_TICKET_MSG']; ?>",
					minlength: "<?php echo $lang['VALIDATE_TICKET_MSG_MIN']; ?>"
				}
			}
		});

	});
	</script>
    
    <!-- Content Wrapper. Contains page content -->
      <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Timeline - <?php echo $lang['TICKET_VIEW']; ?>
   
          </h1>
         
        </section>

        <!-- Main content -->
        <section class="content">

          <!-- row -->
          <div class="row">
            <div class="col-md-12">
              <!-- The time line -->
              <ul class="timeline">
                
                <li class="time-label">
                  <span class="bg-blue">
                 TICKET ID #<?php echo $result['tpk']; ?> - <?php if($lang['DATE_FORMAT'] == 'PT') { ?><?php echo data_br($result['date']); ?><?php } ?><?php if($lang['DATE_FORMAT'] == 'EN') { ?><?php echo data_en($result['date']); ?><?php } ?>
                 </span>
                </li>
                <li><a  href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_CLOSE_TICKET']; ?>')) { window.location.href='?mod=Ticket&id=<?php echo $_GET['id']; ?>&msg=<?php echo base64_encode($result['id']); ?>&action=close&nm=<?php echo base64_encode($result['name']); ?>&em=<?php echo base64_encode($result['email']); ?>' } else { void('') };" class="btn btn-warning btn-sm"><i class="fa fa-remove"></i> <?php echo $lang['ALERT_BTN_CLOSE_TICKET']; ?></a></li>
              
                <?php
 		  		$query_msg = mysql_query("SELECT p.*, c.name AS name, c.email AS email, d.name_user AS name_user FROM messages AS p INNER JOIN customers AS c ON p.customer = c.id INNER JOIN users AS d ON p.user = d.id WHERE p.ticket = '$getId' order by p.date DESC");
 		  		while($msg = mysql_fetch_array($query_msg)){ 
				?>
                <li>
                  <i class="<?php if($msg['type'] == '1') { ?>fa fa-user bg-green <? } else { ?>fa fa-envelope bg-blue<?php } ?>"></i>
                  <div class="timeline-item">
                    <span class="time"><i class="fa fa-clock-o"></i> <?php if($lang['DATE_FORMAT'] == 'PT') { ?><?php echo data_br($msg['date']); ?><?php } ?><?php if($lang['DATE_FORMAT'] == 'EN') { ?><?php echo data_en($msg['date']); ?><?php } ?> <?php echo $msg['hour']; ?></span>
                    <h3 class="timeline-header"><a href="javascript:void(0);"><?php if($msg['type'] == '1') { ?><?php echo $msg['name']; ?><?php } ?><?php if($msg['type'] == '2') { ?><?php echo $msg['name_user']; ?><?php } ?></a> <?php echo $msg['subject']; ?></h3>
                    <div class="timeline-body">
                      <?php echo $msg['msg']; ?>
                    </div>
                    <div class="timeline-footer">
                      <a class="btn btn-danger btn-xs" href="javascript:void(0);" onclick="javascript: if (confirm('<?php echo $lang['ALERT_BTN_DELETE']; ?>')) { window.location.href='?mod=Ticket&id=<?php echo $_GET['id']; ?>&msg=<?php echo base64_encode($msg['id']); ?>&action=delete' } else { void('') };">Delete</a>
                    </div>
                  </div>
                </li>
                <li><a  href="?mod=TicketsAnswered" class="btn btn-info btn-sm"><i class="fa fa-back"></i> <?php echo $lang['TICKET_BTN_BACK_TICKET']; ?></a></li>
              
                <?php } ?>
                
                
               
                
                 <!-- quick email widget -->
              <div class="box box-info">
                <div class="box-header">
                  <i class="fa fa-envelope"></i>
                  <h3 class="box-title">Quick Email</h3>
                  
                </div>
                <div class="box-body">
                  <form id="newticket" name="newticket" method="post">
                    
                    <div class="form-group">
                      <input type="text" class="form-control" id="subject" name="subject" placeholder="<?php echo $lang['TICKET_FORM_SUBJECT']; ?>">
                    </div>
                    <div>
                      <textarea class="textarea" name="msg" id="msg" placeholder="<?php echo $lang['TICKET_FORM_MSG']; ?>" style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
                    </div>
                 
                </div>
                <div class="box-footer clearfix">
                <input type="submit" value="<?php echo $lang['TICKET_BTN']; ?>" name="send" class="btn btn-default">
                <input type="hidden" name="ticketID" value="<?php echo base64_decode($_GET['id']); ?>">
                <input type="hidden" name="customer" value="<?php echo $result['customer']; ?>">
                <input type="hidden" name="user" value="<?php echo $result['user']; ?>">
                <input type="hidden" name="tpk" value="<?php echo $result['tpk']; ?>">
                <input type="hidden" name="customer_name" value="<?php echo $result['name']; ?>">
                <input type="hidden" name="customer_email" value="<?php echo $result['email']; ?>">
                </div>
                 </form>
              </div>

                
              </ul>
            </div><!-- /.col -->
          </div><!-- /.row -->

          

        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
