<?php
include("../../check.php");
session_start();
 
 if(isset($_GET['del'])){
     $del = $_GET['del'];
     $getall = $getData->runQuery("SELECT * FROM `employee` where id='$del'");
     $filename = "img/".$getall[0]['img'];
      unlink($filename);
     $getData->runQuery("DELETE FROM `employee` WHERE id='$del'");
     $_SESSION['message']="<span class='text-success'>Data deleted successfully</span>";
     header("Location: employ-list.php");
 }
 if(isset($_POST['imgSubmit'])){
     
     
     $name = $_POST['user_name'];
     $mobile = $_POST['mobile'];
     $email = $_POST['email'];
     $address = $_POST['address'];

    $standard = $_POST['standard'];
    $bodies = $_POST['bodies'];
     
     $sess_id = $_SESSION['user_id'];
     
     
    $standard = implode(',',$standard);
    $bodies = implode(',',$bodies);

     
     $show_pass = rand();
     $user_pw = md5($show_pass);
     
     $getall = $getData->numRows("SELECT * FROM `employee` where user_id='$email'");
  
    
     if(empty($_POST['dataid'])){  // numRows
             
             
        $sattus = $getData->runQuery("INSERT INTO `employee`(`master_id`,`user_id`, `user_pw`,`show_pass`, `user_name`, `email`,mobile, `access`,`type`, `address`,`standard`, `bodies`) VALUES ('$sess_id','$email','$user_pw','$show_pass','$name','$email','$mobile','Yes','2','$address','$standard', '$bodies')");

             if($getall == 0){
                 $_SESSION['message']="<span class='text-success'>Data inserted successfully</span>";
                 header("Location: employ-list.php");
             }else{
                 $_SESSION['message']="<span class='text-danger'>Data insertion failed ! try another email id</span>";
                 header("Location: employ-list.php");     
            }
     }else{
         $id = $_POST['dataid'];
              //$getall = $getData->runQuery("SELECT * FROM `employee` where id='$id'");
              //$filename = "img/".$getall[0]['img'];
              //unlink($filename);
              $update = $getData->runQuery("UPDATE `employee` SET `user_id`='$email',`user_name`='$name',`email`='$email',mobile='$mobile',address='$address',standard='$standard', bodies='$bodies' WHERE id='$id'");
                 $_SESSION['message']="<span class='text-success'>Data updated successfully</span>";
                 header("Location: employ-list.php");
     }
 }
   
 if($_POST['status']){
     $id = $_POST['status'];
     $getall = $getData->runQuery("SELECT * FROM `employee` where id='$id'");
     if($getall[0]['status'] == "A"){
         $getData->runQuery("UPDATE `employee` SET `status`='I' WHERE id='$id'");
         echo '<span class="badge badge-danger">Inactive</span>';
         
     }else{
         $getData->runQuery("UPDATE `employee` SET `status`='A' WHERE id='$id'");
         echo '<span class="badge badge-success">Active</span>';
     }
 }
?>