<?php 
	if(isset($_POST['first_name']) && isset($_POST['last_name']) && isset($_POST['email']))
	{
		// include Database connection file 
		include("db_connection.php");

		// get values
		$id_pac= $_POST['id_pac'];		
		$fecha = date("Y-m-d");
		$pieza = $_POST['last_name'];
		$tratamiento = $_POST['dni'];
		$observaciones = $_POST['email'];
		$predet = $_POST['doc'];
		if($predet=='------')
			$predet='------';
		$vesti = $_POST['vesti'];
		$profesional = $_POST['profesional'];

		$query = "SELECT * FROM tabla1 WHERE id_pac LIKE '$id_pac' LIMIT 1";
		if (!$result1 = mysqli_query($con, $query)) {exit(mysqli_error($con));}
		$row1 = mysqli_fetch_assoc($result1);
		$clinica=$row1['clinica'];
		
		include("../../../tabulator_tabla2.php");		
		
		//$query = "INSERT INTO users(paciente, nombre, email) VALUES('$first_name', '$last_name', '$email')";
		
		
		$query = "INSERT INTO `historia` (`id_histo`, `id_pac`, `fecha`, `mes`, `anio`, `tratamiento`, `pieza`, `predet`, `vesti`, `oclu`, `palat`, `mesial`, `distal`, `observaciones`, `fact`, `MATERIAL_1`, `MATERIAL_2`, `MATERIAL_3`, `MATERIAL_4`, `MATERIAL_5`, `MATERIAL_6`, `MATERIAL_7`, `MATERIAL_8`, `MATERIAL_9`, `MATERIAL_10`, `MATERIAL_11`, `MATERIAL_12`, `MATERIAL_13`, `MATERIAL_14`, `MATERIAL_15`, `paciente`, `obra`, `profesional`, `clinica`, `costo`, `pago`, `saldo`, `egreso`, `resto`, `proveedor`, `proveedor_insumo`) VALUES (NULL, '$id_pac', '$fecha', 'dic', '2017', '$tratamiento', '$pieza', '$predet', '$vesti', '', '', '', '', '$observaciones', '', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'Zarate-Celia Aldana', 'Sancor Salud', '$profesional', '$clinica', '0', '0', '0', '0', '0', '', '');";
		
		if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
	    echo "1 Paciente Agregado!";
		 	
			
if($vesti=='si'){
if($pieza<29 || $pieza<66 && $pieza>50){	
$query = "UPDATE $tabla2 SET a1=\"1\" WHERE id_pac=$id_pac AND pieza=$pieza";}
if($pieza>70 || $pieza<49 && $pieza>30){
$query = "UPDATE $tabla2 SET a3=\"1\" WHERE id_pac=$id_pac AND pieza=$pieza";}
if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
}

	$query = "UPDATE $tabla2 SET $predet=\"1\" WHERE id_pac=$id_pac AND pieza=$pieza";
		
	if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
	
}
?>
