<?php
require("../../check.php");
session_start();
 
 
 if(isset($_GET['del'])){
     $del = $_GET['del'];
     $getall = $getData->runQuery("SELECT * FROM `bodies_tbl` where id='$del'");
     $filename = "pdf/".$getall[0]['pdf'];
      unlink($filename);
     $getData->runQuery("DELETE FROM `bodies_tbl` WHERE id='$del'");
     $_SESSION['message']="<span class='text-success'>Data deleted successfully</span>";
     header("Location: index.php");
 }
 if(isset($_POST['imgSubmit'])){
     
     //$id = $_POST['id'];
     $title = $_POST['title'];
     $dataa = $_POST['standard'];
     
     $standard = implode(',',$dataa);
     
     
     $img_name = $_FILES['pdf']['name'];
     $img_temp = $_FILES['pdf']['tmp_name'];
     $img_type = $_FILES['pdf']['type'];
     $img_size = $_FILES['pdf']['size'];
     
     $target_dir = "pdf/";
     
     $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 `bodies_tbl`(`title`, `standard`, `pdf`) 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 !=''){
              if($imageFileType =='pdf'){
                  $getall = $getData->runQuery("SELECT * FROM `bodies_tbl` where id='$id'");
                  $filename = "pdf/".$getall[0]['pdf'];
                  unlink($filename);

                  move_uploaded_file($img_temp, $target_file);

                  $update = $getData->runQuery("UPDATE `bodies_tbl` SET `title`='$title',`standard`='".$standard."',`pdf`='$target_files',`status`='$status' WHERE id='$id'");
                  $_SESSION['message']="<span class='text-success'>Data updated successfully</span>";
                  header("Location: index.php");
              }else{
                $_SESSION['message']="<span class='text-danger'>Please upload pdf file</span>";
                header("Location: index.php");
              }
          }else{
              $update = $getData->runQuery("UPDATE `bodies_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 `bodies_tbl` where id='$id'");
     if($getall[0]['status'] == "A"){
         $getData->runQuery("UPDATE `bodies_tbl` SET `status`='I' WHERE id='$id'");
         echo '<span class="badge badge-danger">Inactive</span>';
         
     }else{
         $getData->runQuery("UPDATE `bodies_tbl` SET `status`='A' WHERE id='$id'");
         echo '<span class="badge badge-success">Active</span>';
     }
 }
?>