<?php
include "scrinit.php";
include $menu;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<h3>ΔΗΜΙΟΥΡΓΙΑ ΝΕΑΣ ΔΡΑΣΤΗΡΙΟΤΗΤΑΣ ΜΕ ΑΝΤΙΓΡΑΦΗ ΑΠΟ ΥΦΙΣΤΑΜΕΝΗ</h3>
<?php
require("dbconnect.php");
$name=$_SESSION['n'];
$usr=$_SESSION['pmp'];
$dt=$_SESSION['dt'];
$code=$_GET['cod'];

$result=$dbcon->prepare("SELECT onoma,code,date,perapo,permexri,katddk FROM ekdilosi WHERE code=?");
$result->execute([$code]);
$row=$result->fetch(PDO::FETCH_ASSOC);
$dat1 = $row['date'];
$code1 =$row['code'];
$perapo1 = $row['perapo'];
$permexri1 = $row['permexri'];
$ekdilo1 = $row['onoma'];
$kat = $row['katddk'];

echo '<p style="color: red">
      Με την Αντιγραφή δημιουργείτε μια νέα δραστηριότητα που πρέπει οποωσδήποτε να έχει διαφορετικό όνομα.
	  Μπορείτε επίσης να αλλάξετε την Ημερομηνία και τις περιόδους της δραστηριότητας και να προσθαφαιρέσετε μαθητές στον κατάλογο!
      </p>';

?>
<br>
<form  method="post" name = "ekdilosichange" > 
<fieldset style="width: 750px; ">
<legend>ΣΤΟΙΧΕΙΑ ΔΡΑΣΤΗΡΙΟΤΗΤΑΣ:<?php echo "<b>".$ekdilo1."</b>";  ?> </legend>
<p> ΗΜΕΡΟΜΗΝΙΑ:<input type="date"  name = 'dat' value= <?php echo $dat1; ?> >  </p >
<p> ΔΡΑΣΤΗΡΙΟΤΗΤΑ  :<input type='text' name='ekdilo' size = '60' value="<?php echo $ekdilo1."x"; ?>"  /> 
 ΚΩΔΙΚΟΣ:<input type='text' name='code' size = '5' value="<?php echo " "; ?>" readonly /></p > 
<p > ΑΠΟ ΠΕΡ.  :<input type='text' name='perapo' size = '3' value ="<?php echo $perapo1; ?>"  /> 
ΜΕΧΡΙ ΠΕΡ.:<input type='text' name='permexri' size = '3' value = "<?php echo $permexri1; ?>" /> </p>   
<input type='hidden' name='kat' value = '<?php echo $kat; ?>' />
<input type='hidden' name='codex' value = '<?php echo $code1; ?>' />

<br ><br ><input type="Submit" name="Submit" value="Υποβολή Στοιχείων Δραστηριότητας" >
</fieldset>
</form>	

<?php
if (isset($_POST['Submit'])) 
{ 

$ekdilo = $_POST['ekdilo'];
$papo = $_POST['perapo'];
$pm = $_POST['permexri'];
$dat = $_POST['dat']; 
$kat=$_POST['kat'];
$code=$_POST['codex'];
 
$result = mysqli_query($con,"SELECT pmp,tmima,p1,p2,p3,p4,p5,p6,p7,p8,p9 FROM ekdilosistu WHERE code = '$code' ");
$rowcount=mysqli_num_rows($result);


if ($ekdilo==$ekdilo1){
	echo " names must not be the same";}
else
{

if ($ekdilo==$ekdilo1 and $papo==$perapo1 and $pm==$permexri1 and $dat==$dat1) {
	echo " no change";}
else
{
 
$sql = "CREATE TABLE temp SELECT * FROM ekdilosistu WHERE code = '$code'";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  }  

$sql="INSERT INTO ekdilosi (onoma,date,perapo,permexri,katddk,teacher) VALUES ('$ekdilo','$dat','$papo','$pm','$kat','$usr') ";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  } 

$sql=mysqli_query($con,"SELECT  code FROM ekdilosi WHERE  onoma='$ekdilo' AND date='$dat'" );	
$row=mysqli_fetch_array($sql);
$code=$row['code'];


$sql ="UPDATE temp SET code = '$code'";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  } 

$sqlt=mysqli_query($con,"SELECT * from temp");
while ($row=mysqli_fetch_array($sqlt))
{
	$pmp=$row['pmp'];
	$code=$row['code'];
	$tm=$row['tmima'];
	$p1=$row['p1'];$p2=$row['p2'];$p3=$row['p3'];$p4=$row['p4'];$p5=$row['p5'];
	$p6=$row['p6'];$p7=$row['p7'];$p8=$row['p8'];$p9=$row['p9'];
	mysqli_query($con,"insert into ekdilosistu (id,code,pmp,tmima,p1,p2,p3,p4,p5,p6,p7,p8,p9) values (NULL,'$code','$pmp','$tm','$p1','$p2','$p3','$p4','$p5','$p6','$p7','$p8','$p9')");
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  } 	
}

$sql="DROP TABLE temp";
if (!mysqli_query($con,$sql)) {die('Error: ' . mysqli_error($con));  }    
 
//..........................code only.............................
   
$result = mysqli_query($con,"SELECT pmp,tmima,p1,p2,p3,p4,p5,p6,p7,p8,p9 FROM ekdilosistu WHERE code = '$code' ");
$rowcount=mysqli_num_rows($result);

include "epmathnew.php";

}
}
}
?>
</body>
</html>