<?php
// include Database connection file
include("db_connection.php");

// check request
if(isset($_POST))
{
    // get values
    $id_histo = $_POST['id'];
    $first_name = $_POST['first_name'];
    $pieza = $_POST['last_name'];
    $dni = $_POST['dni'];
    $email = $_POST['email'];
    $predet = $_POST['predet'];
    $vesti = $_POST['vesti'];
    $doc = $_POST['profesional'];
    
		$query = "SELECT * FROM historia WHERE id_histo LIKE '$id_histo' LIMIT 1";
		if (!$result = mysqli_query($con, $query)) {
	        exit(mysqli_error($con));
	    }
		$row = mysqli_fetch_assoc($result);
	
		$clinica=$row['clinica'];
		$id_pac=$row['id_pac'];
		
		include("../../../tabulator_tabla2.php");		
			
					$trozos = explode ("/", $first_name);
					$vemos=array();
					$vemos['0']=$trozos['2'];
					$vemos['1']=$trozos['1'];
					$vemos['2']=$trozos['0'];
					$first_name = implode('-', $vemos);

    // Updaste User details
    $query = "UPDATE historia SET fecha='$first_name',pieza='$pieza',tratamiento='$dni',predet='$predet',observaciones='$email',vesti='$vesti',profesional='$doc' WHERE id_histo = '$id_histo'";
    if (!$result = mysqli_query($con, $query)) {
        exit(mysqli_error($con));
    }
	
	switch ($predet) 
    {
    case "extra":
        $query = "UPDATE $tabla2 SET extra='1',perno='0',circu='0',tc='0',implante='0',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";
        break;
    case "perno":
        $query = "UPDATE $tabla2 SET extra='0',perno='1',circu='0',tc='0',implante='0',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";        break;
    case "circu":
        $query = "UPDATE $tabla2 SET extra='0',perno='0',circu='1',tc='0',implante='0',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";
        break;
    case "tc":
        $query = "UPDATE $tabla2 SET extra='0',perno='0',circu='0',tc='1',implante='0',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";
        break;
    case "implante":
        $query = "UPDATE $tabla2 SET extra='0',perno='0',circu='0',tc='0',implante='1',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";
        break;
    case "inc":
        $query = "UPDATE $tabla2 SET extra='0',perno='0',circu='0',tc='0',implante='0',inc='1' WHERE id_pac = '$id_pac' AND pieza = $pieza";
        break;
    default:
        $query = "UPDATE $tabla2 SET extra='0',perno='0',circu='0',tc='0',implante='0',inc='0' WHERE id_pac = '$id_pac' AND pieza = $pieza";
    }
	
    if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
	
if($vesti=='si'){
if($pieza<29 || $pieza<66 && $pieza>50){	
$query = "UPDATE `$tabla2` SET `a1` = '1' WHERE `$tabla2`.`id_pac` = '$id_pac' AND `$tabla2`.`pieza` = '$pieza';";}
if($pieza>70 || $pieza<49 && $pieza>30){
$query = "UPDATE `$tabla2` SET `a3` = '1' WHERE `$tabla2`.`id_pac` = '$id_pac' AND `$tabla2`.`pieza` = '$pieza';";}
if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
}
else{
if($pieza<29 || $pieza<66 && $pieza>50){	
$query = "UPDATE `$tabla2` SET `a1` = '0' WHERE `$tabla2`.`id_pac` = '$id_pac' AND `$tabla2`.`pieza` = '$pieza';";}
if($pieza>70 || $pieza<49 && $pieza>30){
$query = "UPDATE `$tabla2` SET `a3` = '0' WHERE `$tabla2`.`id_pac` = '$id_pac' AND `$tabla2`.`pieza` = '$pieza';";}
if (!$result = mysqli_query($con, $query)) {exit(mysqli_error($con));}
}


}