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

		include "../config/config.php";//Contiene funcion que conecta a la base de datos
		include "../lib/email.php";
		
		$comentario=mysqli_real_escape_string($con,(strip_tags($_POST["comentario"],ENT_QUOTES)));
		$id=intval($_POST['mod_id']);
		$atendido=date("Y-m-d H:i:s");

		$sql="UPDATE tickets SET status_id=4, finalizado=\"$comentario\", date_atendid=\"$atendido\" WHERE id=$id";
		$query_update = mysqli_query($con,$sql);
			if ($query_update){
				$messages[] = "El ticket ha sido atendido 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'];
					$client_id=$row['client_id'];
				

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

				$areas=mysqli_query($con, "select * from area where id=$id");
				$row=mysqli_fetch_array($areas);
					$name_area=$row['name'];
				

				$clientes=mysqli_query($con, "select * from clientes where id=$client_id");
				$row=mysqli_fetch_array($clientes);
					$business=$row['business'];
				


				$date=date("Y-m-d H:i:s");
				emailFinish($number_ticket, $atendido, $email_client, $business, $name_area, $comment, $comentario);

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

?>