<?php
include("../../check.php");
session_start();
 
 
 if(isset($_GET['del'])){
     $del = $_GET['del'];
     $getall = $getData->runQuery("SELECT * FROM `standard_tbl` where id='$del'");
     $filename = "img/".$getall[0]['img'];
      unlink($filename);
     $getData->runQuery("DELETE FROM `standard_tbl` WHERE id='$del'");
     $_SESSION['message']="<span class='text-success'>Data deleted successfully</span>";
     header("Location: index.php");
 }
 if(isset($_POST['imgSubmit'])){
     
     $title = $_POST['title'];
     $standard = $_POST['standard'];
    
     $img_name = $_FILES['img']['name'];
     $img_temp = $_FILES['img']['tmp_name'];
     $img_type = $_FILES['img']['type'];
     $img_size = $_FILES['img']['size'];
     
     $target_dir = "img/";
     
     
     
    $imageFileType = strtolower(pathinfo($img_name,PATHINFO_EXTENSION));
    
    $target_files = date('Ymdhis').".".$imageFileType;
    
    $target_file = $target_dir . $target_files;
    
    if(empty($_POST['dataid'])){
        if (move_uploaded_file($img_temp, $target_file)) {
             
             $getData->runQuery("INSERT INTO `standard_tbl`(`title`, `standard`, `img`) VALUES ('$title','$standard','$target_files')");
             
             $_SESSION['message']="<span class='text-success'>Data inserted successfully</span>";
             header("Location: index.php");
             
         }else{
             $_SESSION['message']="<span class='text-danger'>Data insertion failed</span>";
             header("Location: index.php");
         }
     }else{
         $id = $_POST['dataid'];
          $status = $_POST['status'];
          if($img_name !=''){
              $getall = $getData->runQuery("SELECT * FROM `standard_tbl` where id='$id'");
              $filename = "img/".$getall[0]['img'];
              unlink($filename);
              /*Image upload */
              move_uploaded_file($img_temp, $target_file);
              
              $update = $getData->runQuery("UPDATE `standard_tbl` SET `title`='$title',`standard`='$standard',`img`='$target_files',`status`='$status' WHERE id='$id'");
                 $_SESSION['message']="<span class='text-success'>Data updated successfully</span>";
                 header("Location: index.php");
          }else{
              $update = $getData->runQuery("UPDATE `standard_tbl` SET `title`='$title',`standard`='$standard',`status`='$status' WHERE id='$id'");
                 $_SESSION['message']="<span class='text-success'>Data updated successfully</span>";
                 header("Location: index.php");
         }
     }
 }
 
  if($_POST['status']){
     $id = $_POST['status'];
     $getall = $getData->runQuery("SELECT * FROM `standard_tbl` where id='$id'");
     if($getall[0]['status'] == "A"){
         $getData->runQuery("UPDATE `standard_tbl` SET `status`='I' WHERE id='$id'");
         echo '<span class="badge badge-danger">Inactive</span>';
         
     }else{
         $getData->runQuery("UPDATE `standard_tbl` SET `status`='A' WHERE id='$id'");
         echo '<span class="badge badge-success">Active</span>';
     }
 }
?>