<html>
<head>
<link rel="stylesheet" type="text/css" href="../../styles/Style.css">
<script language="JavaScript" type="text/javascript" src="../ajax.js"></script>
</head>
<body>
<?php
//header("Cache-Control: no-store, no-cache, must-revalidate");
include("../../bd/sql_variables.php");
$conex= mysqli_connect("$sql_host", "$sql_usuario", "$sql_pass","$sql_db") or die(header ("No Disponible Servidor de Base de Datos"));



$idj=$_POST['idj'];
$ids=$_POST['ids'];
$anno=$_POST['anno'];
$mes=$_POST['mes'];
$idtp=$_POST['idtp'];
$ugid=$_POST['ugid'];

$arrcodper = unserialize(stripslashes($_POST['arrcodper'])); 
$arrdia = unserialize(stripslashes($_POST['arrdia'])); 
$arrcodprog = unserialize(stripslashes($_POST['arrcodprog'])); 
$arrhorario = unserialize(stripslashes($_POST['arrhorario'])); 
$arrarea = unserialize(stripslashes($_POST['arrarea'])); 
$arrgarea = unserialize(stripslashes($_POST['arrgarea'])); 
$arrtiporeg = unserialize(stripslashes($_POST['arrtiporeg'])); 
$arrformareg = unserialize(stripslashes($_POST['arrformareg'])); 

//print_r($arrformareg);
//echo "<br>";
//print_r($arrtiporeg);
//echo print_r($arrcodper)."<br>";
//echo $ids."<br>";
//echo $idtp."<br>";


echo "<input type='hidden' id='idj' name='idj' value='".$idj."'>";
echo "<input type='hidden' id='ids' name='ids' value='".$ids."'>";
echo "<input type='hidden' id='anno' name='anno' value='".$anno."'>";
echo "<input type='hidden' id='mes' name='mes' value='".$mes."'>";
echo "<input type='hidden' id='idtp' name='idtp' value='".$idtp."'>";
echo "<input type='hidden' id='ugid' name='ugid' value='".$ugid."'>";

for ($i=0;$i<count($arrcodper);$i++){
//busca codigo del personal y el servicio
$sqlask ="SELECT ug_id as cod, s_id as ids FROM tb_usutrab where ut_codprog='$arrcodprog[$i]' and  ug_id='$arrcodper[$i]' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mes' and j_id='$idj' and ut_idserva='$ids'";
//echo $sqlask."<br>"; 
$resultask = mysqli_query($conex,$sqlask);
if(mysqli_num_rows($resultask)!=0){
while($rowask = mysqli_fetch_array($resultask)){
$idso = $rowask['ids'];
}}
if($arrformareg[$i]==1){
$sqlrepro = "select * from tb_reprohrs where rh_dia='$arrdia[$i]' and h_id='$arrhorario[$i]' and a_id='$arrarea[$i]' and ga_id='$arrgarea[$i]' and tp_id='$idtp'"; 
$sqlrepro = $sqlrepro." and ug_id='$arrcodper[$i]' and uap_anno='$anno' and uap_mes='$mes' and s_id='$idso' and rh_idserva='$ids' and j_id='$idj' and rh_tipo='$arrtiporeg[$i]'";
//echo $sqlrepro."<br>";
$resultrepro = mysqli_query($conex,$sqlrepro);
if(mysqli_num_rows($resultrepro)==0){
$sqlinsert = "insert into tb_reprohrs values ('$arrdia[$i]','$arrcodprog[$i]','$arrhorario[$i]','$arrarea[$i]','$arrgarea[$i]','$idtp','$arrcodper[$i]','$anno','$mes','$idso','$idj','$ids','$arrtiporeg[$i]','$ugid',now())";
//echo $sqlinsert."<br>";
$resultinsert = mysqli_query($conex,$sqlinsert);
}
}else{
$sqldel= "delete from tb_reprohrs where h_id='$arrhorario[$i]' and rh_dia='$arrdia[$i]' and a_id='$arrarea[$i]' and ga_id='$arrgarea[$i]' and tp_id='$idtp'"; 
$sqldel= $sqldel." and ug_id='$arrcodper[$i]' and uap_anno='$anno' and uap_mes='$mes' and s_id='$idso' and rh_idserva='$ids' and j_id='$idj' and rh_tipo='$arrtiporeg[$i]'";
$resultdel = mysqli_query($conex,$sqldel);
}}


$arrcodpertxt = unserialize(stripslashes($_POST['arrcodpertxt'])); 
$arrtxtobs = unserialize(stripslashes($_POST['arrtxtobs'])); 
//print_r ($arrcodpertxt);
//print_r ($arrtxtobs);
for ($j=0;$j<count($arrcodpertxt);$j++){

//busca codigo del personal y el servicio
$sqlask ="SELECT ug_id as cod, s_id as ids FROM tb_usutrab where ug_id='$arrcodpertxt[$j]' and tp_id='$idtp' and uap_anno='$anno' and uap_mes='$mes' and j_id='$idj' and ut_idserva='$ids'";
//echo $sqlask."-".$arrtxtobs[$j]."<br>"; 
$resultask = mysqli_query($conex,$sqlask);
if(mysqli_num_rows($resultask)!=0){
while($rowask = mysqli_fetch_array($resultask)){
$idso = $rowask['ids'];
}}
//echo "<br>".$arrtxtobs[$j]."<br>";
if($arrtxtobs[$j] != "$"){
$sqlask2 ="select * from tb_obsreprog where j_id='$idj' and ug_id='$arrcodpertxt[$j]' and s_id='$idso' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
$resultask2 = mysqli_query($conex,$sqlask2);
//echo $sqlask2."<br>";
if(mysqli_num_rows($resultask2)==0){
$sqlinserttxt = "insert into tb_obsreprog values ('$idj','$idso','$arrcodpertxt[$j]','$anno','$mes','$ids',upper('".$arrtxtobs[$j]."'))";
$resultinsert = mysqli_query($conex,$sqlinserttxt);
//echo $sqlinserttxt."<br>";
}else{
$sqlup="update tb_obsreprog set or_obs=upper('$arrtxtobs[$j]') where ug_id='$arrcodpertxt[$j]' and j_id='$idj' and s_id='$idso' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
//echo $sqlup."<br>";
$resultup = mysqli_query($conex,$sqlup);
}
}else{
$sqldel="delete from tb_obsreprog where j_id='$idj' and s_id='$idso' and ug_id='$arrcodpertxt[$j]' and uap_anno='$anno' and uap_mes='$mes' and or_idserva='$ids'";
//echo $sqldel."<br>";
$resultdel = mysqli_query($conex,$sqldel);
}
}
//echo "<meta http-equiv='refresh' content='60'>";
$anno= $anno;
$mes=$mes;
$idj=$idj;
$ids=$ids;
$idtp=$idtp;
$ugid=$ugid;
include("reprogramacionper.php");

?>
