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

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

		$asigned_id=mysqli_real_escape_string($con,(strip_tags($_POST["asigned_id"],ENT_QUOTES)));
		$tipo_requerimiento=mysqli_real_escape_string($con,(strip_tags($_POST["tipo_requerimiento"],ENT_QUOTES)));
		$id=mysqli_real_escape_string($con,(strip_tags($_POST["ticked_id"],ENT_QUOTES)));
		$atendido=date("Y-m-d H:i:s");

		$sql="UPDATE tickets SET status_id=2, asigned_id=\"$asigned_id\", tipo_requerimiento=$tipo_requerimiento,date_asigned=\"$atendido\" WHERE id=$id";
		$query_update = mysqli_query($con,$sql);
			if ($query_update){
				$messages[] = "La asignación ha sido ingresada satisfactoriamente.";

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

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

				$cliente=mysqli_query($con, "select * from clientes where id=$client_id");
				$row=mysqli_fetch_array($cliente);
					$business=$row['business'];
				
				emailAsigned($email_client,$number_ticket,$atendido,$name_user,$email_user, $comment, $business);

				//sleep(1);
				print("<script>window.location='./registrados.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
			}

?>