<?php
include("../../check.php");
session_start();
 
 
 if(isset($_GET['del'])){
     $del = $_GET['del'];
     $getall = $getData->runQuery("SELECT * FROM `master` where id='$del'");
     $filename = "img/".$getall[0]['img'];
      unlink($filename);
     $getData->runQuery("DELETE FROM `master` 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'];
     $bodies = $_POST['bodies'];
     
     $bodies = implode(',',$bodies);
     
     $show_pass = rand();
     $user_pw = md5($show_pass);
     
     $getall = $getData->numRows("SELECT * FROM `master` where user_id='$email'");
  
    
     if(empty($_POST['dataid'])){  // numRows
             
             
             $sattus = $getData->runQuery("INSERT INTO `master`(`user_id`, `user_pw`,`show_pass`, `user_name`, `email`,mobile, `access`,`type`, `address`,bodies) VALUES ('$email','$user_pw','$show_pass','$name','$email','$mobile','Yes','0','$address','$bodies')");
             if($getall == 0){
                 $_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'];
              //$getall = $getData->runQuery("SELECT * FROM `master` where id='$id'");
              //$filename = "img/".$getall[0]['img'];
              //unlink($filename);
              $update = $getData->runQuery("UPDATE `master` SET `user_id`='$email',`user_name`='$name',`email`='$email',mobile='$mobile',address='$address',bodies='$bodies' 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 `master` where id='$id'");
     if($getall[0]['status'] == "A"){
         $getData->runQuery("UPDATE `master` SET `status`='I' WHERE id='$id'");
         echo '<span class="badge badge-danger">Inactive</span>';
         
     }else{
         $getData->runQuery("UPDATE `master` SET `status`='A' WHERE id='$id'");
         echo '<span class="badge badge-success">Active</span>';
     }
 }
?>