<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css"/>
  <title>Kolej Vokasional Sepang - E-Vote</title>
</head>



<body>
   
 <div class="container">
    <nav class="navbar navbar-default">
 <h1>KVS Digital - VOTING SYSTEM</h1>
    <ul class="nav navbar-nav">
      <li class="active"><a href="http://e-vote.kvsdigital.my/index.php">Home</a></li>
      <li><a href="login.html">Undian</a></li>
      <li><a href="admin.html">Admin</a></li>
    </ul>


  </nav>

<?php
include 'dbconn.php';
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

else
{
if (isset($_POST['submit'])){
$var=$_POST["var1"];
$candIC=$_POST["candIC"];
$candName=$_POST["candName"];
$candCourse = $_POST['candCourse'];
    $candPost = $_POST['candPost'];
    $candN = $_POST['candN'];

    $imageUpdateSql = "";
    if (isset($_FILES['image']) && isset($_FILES['image']['name']) && $_FILES['image']['name'] !== '') {
        $targetDir = "images/";
        if (!is_dir($targetDir)) {
            mkdir($targetDir, 0777, true);
        }
        $fileName = time() . "_" . basename($_FILES["image"]["name"]);
        $targetFilePath = $targetDir . $fileName;
        $fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
        $allowedTypes = array("jpg", "jpeg", "png", "gif", "webp");
        if (in_array($fileType, $allowedTypes) && move_uploaded_file($_FILES["image"]["tmp_name"], $targetFilePath)) {
            $imageUpdateSql = ", image='" . mysqli_real_escape_string($conn, $targetFilePath) . "'";
        }
    }

$update = "UPDATE tblcandidate SET candIC='$candIC',candName='$candName',candCourse='$candCourse', candPost='$candPost', candN='$candN' $imageUpdateSql
WHERE candIC='$var'";
$query1 = mysqli_query($conn,$update);

if($query1){
    echo '<script type="text/javascript">
      alert("Data calon berjaya dikemaskini");
      window.location = "admin.php";
    </script>';
    exit;
}

}


if (isset($_POST['delete'])){
    $var=$_POST["var1"];
    echo "Adakah anda pasti untuk memadam data pelajar ini?";
   
    ?>
    <form action="update4.php" method="post">
        <input type="hidden" name="var2" value="<?=$var?>"/>
    <input type="submit" name="delete1" value="DELETE">
    <input type="button" onclick="location.href='search.html';" value="CANCEL">
    </form>
    
    <?php
}
if (isset($_POST['delete1'])){
    
         $ic = $_POST["var2"];
       
        $delete = "DELETE FROM tblcandidate WHERE candIC='$ic'";
        $query2 = mysqli_query($conn,$delete);
        if($query2){
        echo '<script type="text/javascript">
          alert("Data calon telah dipadamkan");
          window.location = "admin.php";
        </script>';
        exit;
        }
        else
        echo "cuba lagi";
    }
   
}
 ?>
</div>
</body>
</html>