<?php 
include('../dbconnect.php');
if(isset($_POST['indibutton'])){
     $empid= $_POST['empid'];
    $toolsname = $_POST['toolsname'];
    $toolstype = $_POST['toolstype'];
   $toolsusername = $_POST['toolusername'];
    $toolspassword = $_POST['toolpassword'];
    $toolsdesc = $_POST['toolsdesc'];
    $date = date('Y-m-d');

    $sql="INSERT INTO `emptools`( `description`, `username`, `password`, `empid`, `idtools`,`dateupdated`)
    VALUES ('$toolsdesc','$toolsusername','$toolspassword','$empid','$toolsname','$date')";
   ;

    if( $conn->query($sql)){
        header('location: addupdate.php?id='.$empid.'');
    }
     $conn->error;

}
if(isset($_POST['sharedbutton'])){
    $empid= $_POST['empid'];
   $toolsname = $_POST['sharedname'];
   $toolstype = $_POST['toolstype'];
  $toolsusername = $_POST['sharedusername'];
   $toolspassword = $_POST['sharedpassword'];
   $toolsdesc = $_POST['shareddesc'];
   $date = date('Y-m-d');

   $sql="INSERT INTO `emptools`( `description`, `username`, `password`, `empid`, `idtools`, `dateupdated`)
   VALUES ('$toolsdesc','$toolsusername','$toolspassword','$empid','$toolsname','$date')";
  ;

   if( $conn->query($sql)){
       header('location: addupdate.php?id='.$empid.'');
   }
    $conn->error;

}if(isset($_POST['updatetools'])){
    $empid= $_POST['empid'];
   $toolsusername = $_POST['utoolusername'];
    $toolspassword = $_POST['utoolpassword'];
    $toolsdesc = $_POST['utoolsdesc'];
    $id = $_POST['id'];
    $date = date('Y-m-d');
     $sql="UPDATE emptools SET username='$toolsusername', password ='$toolspassword', description='$toolsdesc', dateupdated = '$date' WHERE toolsid='$id'";
    if( $conn->query($sql)){
     header('location: addupdate.php?id='.$empid.'');
    }
    echo $conn->error;

}


if(isset($reportfilter)){

    $tools = $_POST['tools'];
     $empid = $_POST['emp'];

    if($tools == 'All' && $empid == 'All'){
        $sql= 'SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid JOIN emptools ON empinfo.empid = emptools.empid ';


    }else if($tools == 'All' && $empid != 'All'){
        $sql= "SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid JOIN emptools ON empinfo.empid = emptools.empid WHERE emptools.empid = '$empid' ";

    }else if($empid == 'All' && $tools !='All'){

        $sql= "SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid JOIN emptools ON empinfo.empid = emptools.empid WHERE  idtools = '$tools'";

    }else if($empid != 'All' && $tools !='All'){

        $sql= "SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid JOIN emptools ON empinfo.empid = emptools.empid WHERE  idtools = '$tools' AND emptools.empid = '$empid'";

    }else{

        $sql= 'SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid JOIN emptools ON empinfo.empid = emptools.empid ';



    }

   

    $res = $conn->query($sql);
    
    
    while($row = $res->fetch_assoc()){
        $idtools = $row['idtools'];
        $indishared = "SELECT * FROM individualshared WHERE id= '$idtools'";
        $resindi = $conn->query($indishared);
        $toolrow = $resindi->fetch_assoc();
        echo'<tr>
            <td>'.$row['empid'].'</td>
            <td>'.$row['firstname'].' '.$row['surname'].'</td>
             <td>'.$row['team'].' </td>
            <td>'.$toolrow['name'].' </td>
            <td>'.$toolrow['type'].' </td>
            <td>'.$row['username'].' </td>
            <td>'.$row['password'].' </td>
            <td>'.date('m/d/Y',strtotime($row['dateupdated'])).' </td>
            <td>'.$row['description'].' </td>
          
    
        
            </tr>
            
    
        ';
    
    }

}


?>