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