<?php
	session_start();
	if($_SESSION['unohs'] == null){
		header("location:index.php?msg=unauthorized");
	}
?>
<?php
	include ("conn.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_FILES["image"])) {
    $target_dir = "../images_usdt/"; // Directory to store uploaded files
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false) {
            $uploadOk = 1;
        } else {
            echo '<script type="text/JavaScript"> alert("File is not an image."); </script>';
            $uploadOk = 0;
        }
    }
   
    if (file_exists($target_file)) {
        echo '<script type="text/JavaScript"> alert("Sorry, file already exists."); </script>';
        $uploadOk = 0;
    }

    if ($_FILES["image"]["size"] > 500000) {
        echo '<script type="text/JavaScript"> alert("Sorry, your file is too large."); </script>';
        $uploadOk = 0;
    }

    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
        echo '<script type="text/JavaScript"> alert("Sorry, only JPG, JPEG, PNG files are allowed."); </script>';
        $uploadOk = 0;
    }

    if ($uploadOk == 0) {
        //echo '<script type="text/JavaScript"> alert("Sorry, your file was not uploaded."); </script>';
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
         
            $filename = basename($_FILES["image"]["name"]);
            $sql = "INSERT INTO images_usdt (filename) VALUES ('$filename')";

            if ($conn->query($sql) === TRUE) {
                echo '<script type="text/JavaScript"> alert("Record added to database successfully"); </script>';
            } else {
                echo '<script type="text/JavaScript"> alert("Error adding record"); </script>';
            }
        } else {
            echo '<script type="text/JavaScript"> alert("Sorry, there was an error uploading your file."); </script>';
        }
      }
  }
  if(isset($_POST['upiid'])){
		$a_id = $_POST['upiid'];
		$sql_s = "SELECT * FROM images_usdt WHERE filename = '".$a_id."'";
		$run = mysqli_query($conn, $sql_s);
		//Set status to 0
		$sql_d = "SELECT * FROM images_usdt WHERE status = '1'";		
		$run_d = mysqli_query($conn, $sql_d);
		$rund_f = mysqli_fetch_array($run_d);
		$ch_s0 = "UPDATE images_usdt SET status='0' WHERE id='".$rund_f['id']."'";
		$exe_ch_s0 = mysqli_query($conn, $ch_s0);
		//Set status to 1
		$run_f = mysqli_fetch_array($run);
		$ch_s1 = "UPDATE images_usdt SET status='1' WHERE id='".$run_f['id']."'";
		$exe_ch_s1 = mysqli_query($conn, $ch_s1);
	}

?>

        <?php include 'header.php';?>
        

      <div class="main-panel">
        <div class="content-wrapper">
          <div class="row">
            <div class="col-sm-12 mb-4 mb-xl-0">
              <h4 class="font-weight-bold text-dark">Add USDT Image</h4>
            </div>
          </div> 
		  <div class="row">
			<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data" style="margin:5px; padding:8px;">
				<h4>Select image to upload:</h4>
				<div class="d-flex align-items-center">	
					<input type="file" name="image" id="image" class="flex-grow-1 cool-input" style="height: 40px;">
				</div>
				<div class="d-flex align-items-center mt-3">
					<input type="submit" value="Upload Image" name="submit" class="btn btn-primary cool-button mr-2">
				</div>
			</form>
          </div>
		  <div class="row">
            <?php
				$sel_upi = "SELECT * FROM images_usdt WHERE status='0' OR status='1'";
				$upi_r = mysqli_query($conn, $sel_upi);
			?>
				<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" id="upisave" method="post" autocomplete="off">
			<?php	while ($row = mysqli_fetch_array($upi_r)) {
			?>
				<input name="upiid" type="radio" value="<?php echo $row['filename']; ?>" <?php if($row['status']==1){echo "checked";} ?> style="margin-top: 10px; margin-left:12px;" />
				<label for="<?php echo $row['filename']; ?>"><?php echo $row['filename']; ?></label>
				</br>
			<?php	}
			?>
				<button type="submit" class="btn btn-primary cool-button mr-2" style="margin-top: 10px; margin-left:12px;">Save</button>
				</form>
          </div>		  					  		  		  
        </div>
        <footer class="footer">
          <div class="d-sm-flex justify-content-center justify-content-sm-between">
            <span class="text-muted d-block text-center text-sm-left d-sm-inline-block">Copyright © dream77.site 2024</span>
          </div>
        </footer>
      </div>
    </div>
  </div>
  
  <script>	
	if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
  </script>
</body>

</html>