<?php
	ini_set("display_errors", 1);
    error_reporting(1);
    ini_set("track_errors","1");
	if(isset ($_POST['save'])){
		
	$customer = $_POST['customer'];
	$subject = $_POST['subject'];
	$msg = $_POST['msg'];
	$type = '2';
	$date = date('Y-m-d');
	$data_email = date('d/m/Y');
	$dataen_email = date('m/d/Y');
	$hour = date('H:i:s');
	$tpk = rand(111111,999999);
	$department = $_POST['department'];
	$user = $logged['id'];
	$priority = $_POST['priority'];
	
	$crud = new crud('tickets');  // table
    $crud->inserir("customer,user,date,hour,department,priority,subject,msg,tpk,status", "'$customer','$user','$date','$hour','$department','$priority','$subject','$msg','$tpk','1'");
    
    $query_cc = mysql_query("SELECT * FROM customers WHERE id = '$customer'");
    $customer = mysql_fetch_array($query_cc);
    $customer_email = $customer['email'];
    $customer_name = $customer['name'];
    
    // SEND MAIL 
	require_once("config/mail/class.phpmailer.php");
	$query_tpl = mysql_query("SELECT * FROM email_template WHERE id = '4'");
    $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=Tickets&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=Tickets&y=1');
	</script>";
    }
	// END SEND 
    
	
	}
	
	
    $cmd1 = mysql_query("SELECT * FROM tickets WHERE status = '1'");
	$ticketsopen = mysql_num_rows($cmd1);
    $cmd2 = mysql_query("SELECT * FROM tickets WHERE status = '2'");
	$ticketsanswered = mysql_num_rows($cmd2);
	$cmd3 = mysql_query("SELECT * FROM tickets WHERE status = '3'");
	$ticketsprocess = mysql_num_rows($cmd3);
	$cmd4 = mysql_query("SELECT * FROM tickets WHERE status = '4'");
	$ticketsclosed = mysql_num_rows($cmd4);
	
?>

 <script>
     $().ready(function() {

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

	});
	$(function () {
        //Initialize Select2 Elements
        $(".select2").select2();
        });
	</script>

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

        <!-- Main content -->
        <section class="content">
          <div class="row">
			  
            <div class="col-md-3">
              <a href="?mod=Tickets" class="btn btn-primary btn-block margin-bottom"><?php echo $lang['TITLE_BACK_NEW_TICKET']; ?></a>
              <div class="box box-solid">
                <div class="box-header with-border">
                  <h3 class="box-title">Tickets</h3>
                  <div class="box-tools">
                    <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
                  </div>
                </div>
                <div class="box-body no-padding">
                  <ul class="nav nav-pills nav-stacked">
                    <li><a href="?mod=Tickets"><i class="fa fa-circle-o text-green"></i> <?php echo $lang['TICKET_OPEN']; ?> <span class="label label-primary pull-right"><?php echo $ticketsopen; ?></span></a></li>
                    <li><a href="?mod=TicketsAnswered"><i class="fa fa-circle-o text-light-blue"></i> <?php echo $lang['TICKET_ANSWERED']; ?> <span class="label label-primary pull-right"><?php echo $ticketsanswered; ?></span></a></li>
                    <li><a href="?mod=TicketsProcess"><i class="fa fa-circle-o text-yellow"></i> <?php echo $lang['TICKET_PROCESS']; ?> <span class="label label-primary pull-right"><?php echo $ticketsprocess; ?></span></a></li>
                    <li><a href="?mod=TicketsClosed"><i class="fa fa-circle-o text-red"></i> <?php echo $lang['TICKET_CLOSED']; ?> <span class="label label-primary pull-right"><?php echo $ticketsclosed; ?></span></a></li>
                  </ul>
                </div><!-- /.box-body -->
              </div><!-- /. box -->
              
            </div><!-- /.col -->
            
            
            <div class="col-md-9">
              <div class="box box-primary">
                <div class="box-header with-border">
                  <h3 class="box-title"><?php echo $lang['TITLE_NEW_TICKET']; ?></h3>
                </div><!-- /.box-header -->
                
                <form id="newticket" name="newticket" method="post" enctype="multipart/form-data">
                <div class="box-body">
					
					<div class="form-group">
                      <label for="Input"><?php echo $lang['DEPARTMENT']; ?></label>
                      <select id="department" name="department" class="form-control" style="width: 100%;">
					
                    <?php
					$cdecc = mysql_query("SELECT * FROM departments order by name_department ASC");
					while($depart = mysql_fetch_array($cdecc)){ 
					?>
                    <option value="<?php echo $depart['id']; ?>" <?php if (@$result['department'] == $depart['id']) { echo "selected"; } ?>><?php echo $depart['name_department']; ?></option>
				    <?php } ?> 
				    </select>
                    </div>
                  
                  <div class="form-group">
                      <label for="Input"><?php echo $lang['CUSTOMER']; ?></label>
                      <select id="customer" name="customer" class="form-control select2" style="width: 100%;" required>
						  <option value="">Select</option>
                    <?php
					$catcc = mysql_query("SELECT * FROM customers order by name ASC");
					while($custm = mysql_fetch_array($catcc)){ 
					?>
                    <option value="<?php echo $custm['id']; ?>" <?php if (@$result['customer'] == $custm['id']) { echo "selected"; } ?>><?php echo $custm['name']; ?> - <?php echo $custm['email']; ?></option>
				    <?php } ?> 
				    </select>
                    </div>
                  
                  <div class="form-group">
                    <input class="form-control" id="subject" name="subject" placeholder="<?php echo $lang['TICKET_FORM_SUBJECT']; ?>:" required>
                  </div>
                  <div class="form-group">
                    <textarea id="msg" name="msg" class="form-control" style="height: 300px" placeholder="<?php echo $lang['TICKET_FORM_MSG']; ?>" required></textarea>
                  </div>
                  <div class="form-group">
                  
                  <label for="A"><input type="radio" class="btn btn-danger" name="priority" value="A" id="A"> <?php echo $lang['HIGH']; ?></label>  
                  <label for="M"><input type="radio" class="btn btn-warning" name="priority" value="M" id="M"> <?php echo $lang['MEDIUM']; ?></label> 
                  <label for="B"><input type="radio" class="btn btn-default" name="priority" value="B" id="B" checked> <?php echo $lang['LOW']; ?></label> 
                </div><!-- /.box-body -->
                
                <div class="box-footer">
                  <div class="pull-right">
            
                    <input type="submit" class="btn btn-primary" name="save" value="<?php echo $lang['SEND']; ?>">
                  </div>
                 
                </div><!-- /.box-footer -->
                
              </div><!-- /. box -->
              </form>
            </div><!-- /.col -->
          </div><!-- /.row -->
        </section><!-- /.content -->
      </div><!-- /.content-wrapper -->
