<?php
include("../../check.php");
session_start();
 if(isset($_GET['del'])){
     $del = $_GET['del'];
     $getall = $getData->runQuery("SELECT * FROM `draft` where id='$del'");
     $filename = "pdf/".$getall[0]['pdf'];
      unlink($filename);
     $getData->runQuery("DELETE FROM `draft` WHERE id='$del'");
     $_SESSION['message']="<span class='text-success'>Data deleted successfully</span>";
     header("Location: index.php");
 }
 if(isset($_POST['imgSubmit'])){
     
     
     $name = $_POST['name'];
     $mobile = $_POST['mobile'];
     $email = $_POST['email'];
     $address = $_POST['address'];
     $user = $_POST['user'];
     $password = $_POST['password'];
     $bodies = $_POST['bodies'];
     $standard = $_POST['standard'];
     
     $standard = implode(',',$standard);
     
    /* $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));
     
     if($imageFileType !='pdf'){
         $_SESSION['message']="<span class='text-danger'>FIle is not pdf</span>";
         header("Location: index.php");
     }
     
     $target_files = date('Ymdhis').".".$imageFileType;
     
    $target_file = $target_dir . $target_files;*/
    
    // print_r($_FILES);
    // print_r($_POST); die;
     
    // $getall = $getData->numRows("SELECT * FROM `draft` where user_id='$email'");
  
     if(empty($_POST['dataid'])){  // numRows
             
        if (move_uploaded_file($img_temp, $target_file)) {
             $sattus = $getData->runQuery("INSERT INTO `draft`(`user`, `pass`,  `bodies`,`standard`, `company_name`, `mobile`, `email`, `address`) VALUES ('$user','$password','$bodies','$standard','$name','$mobile','$email','$address')");
                 $_SESSION['message']="<span class='text-success'>Data inserted successfully</span>";
                 header("Location: index.php");
                 
             }else{
                 $_SESSION['message']="<span class='text-danger'>Data insertion failed ! try another email id</span>";
                 header("Location: index.php");
                 
            }
     }else{
         $id = $_POST['dataid'];
         /*if($img_name !=''){
              $getall = $getData->runQuery("SELECT * FROM `draft` where id='$id'");
              $filename = "pdf/".$getall[0]['img'];
              unlink($filename);
              //`pdf`='$target_files',
              $update = $getData->runQuery("UPDATE `draft` SET `user`='$user',`pass`='$password',`bodies`='$bodies',`standard`='$standard',`company_name`='$name',`mobile`='$mobile',`email`='$email',`address`='$address' WHERE id='$id'");
                 $_SESSION['message']="<span class='text-success'>Data updated successfully</span>";
                 header("Location: index.php");
         }else{*/
             $update = $getData->runQuery("UPDATE `draft` SET `user`='$user',`pass`='$password',`bodies`='$bodies',`standard`='$standard',`company_name`='$name',`mobile`='$mobile',`email`='$email',`address`='$address' 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 `draft` where id='$id'");
     if($getall[0]['status'] == "A"){
         $getData->runQuery("UPDATE `draft` SET `status`='I' WHERE id='$id'");
         echo '<span class="badge badge-danger">Inactive</span>';
         
     }else{
         $getData->runQuery("UPDATE `draft` SET `status`='A' WHERE id='$id'");
         echo '<span class="badge badge-success">Active</span>';
     }
 }
?>