<?php
	if (empty($_POST['mod_id'])) {
           $errors[] = "ID Ticket vacío";
        }else if (empty($_POST['incidencia'])) {
           $errors[] = "Incidencia vacío";
        } else if (
			!empty($_POST['incidencia'])
		){

		include "../config/config.php";//Contiene funcion que conecta a la base de datos
		include "../lib/email.php";

		$incidencia=mysqli_real_escape_string($con,(strip_tags($_POST["incidencia"],ENT_QUOTES)));
		$id=intval($_POST['mod_id']);

		$sql="UPDATE tickets SET sistema=\"$incidencia\", status_id=3 WHERE id=$id";
		$query_update = mysqli_query($con,$sql);
			if ($query_update){
				$messages[] = "La incidencía se ha agregado satisfactoriamente.";
				//email mensaje

				$tickets=mysqli_query($con, "select * from tickets where id=$id");
				
				$row=mysqli_fetch_array($tickets);
					$email_client=$row['email'];
					$number_ticket=$row['number_ticket'];
					$user_id=$row['asigned_id'];
					$kind_id=$row['kind_id'];
					$comment=$row['comment'];
				

				$users=mysqli_query($con, "select * from user where id=$user_id");
				$row=mysqli_fetch_array($users);
					$name=$row['name'];
					$lastname=$row['lastname'];
					$email_user=$row['email'];
				
				$name_user=$name." ".$lastname;

				$kinds=mysqli_query($con, "select * from kind where id=$kind_id");
				$row=mysqli_fetch_array($kinds);
					$name_kind=$row['name'];
				

				$date=date("Y-m-d H:i:s");
				emailIncidencia($email_client,$number_ticket, $date, $incidencia, $name_user, $email_user, $comment, $name_kind);

				//sleep(1);
				print("<script>window.location='./cancelados.php'</script>");
			} else{
				$errors []= "Lo siento algo ha salido mal intenta nuevamente.".mysqli_error($con);
			}
		} else {
			$errors []= "Error desconocido.";
		}
		
		if (isset($errors)){
			
			?>
			<div class="alert alert-danger" role="alert">
				<button type="button" class="close" data-dismiss="alert">&times;</button>
					<strong>Error!</strong> 
					<?php
						foreach ($errors as $error) {
								echo $error;
							}
						?>
			</div>
			<?php
			}
			if (isset($messages)){
				
				?>
				<div class="alert alert-success" role="alert">
						<button type="button" class="close" data-dismiss="alert">&times;</button>
						<strong>¡Bien hecho!</strong>
						<?php
							foreach ($messages as $message) {
									echo $message;
								}
							?>
				</div>
				<?php
			}

?>