<?php 
include('../dbconnect.php');

if(isset($all)){
    $sql= 'SELECT * from empinfo JOIN empdetails ON empinfo.empid = empdetails.detailid';
    $res = $conn->query($sql);
    
    
    while($row = $res->fetch_assoc()){
        echo'<tr>
            <td>'.$row['empid'].'</td>
            <td>'.$row['firstname'].' '.$row['surname'].'</td>
            <td>'.$row['team'].' </td>
            <td>'.$row['status'].' </td>
            <td><a href="addupdate.php?id='.$row['empid'].'" class="btn btn-sm btn-success"> Add/Update </a> 
    
            </td>
            </tr>
            
    
        ';
    
    }
    
    
}

if(isset($toolslist)){
$empid= $_GET['id'];
      $sql= "SELECT *, emptools.description AS 'emptooldesc' from emptools JOIN individualshared ON emptools.idtools = individualshared.id WHERE empid='$empid' ";
    $res = $conn->query($sql);
    
    
    while($row = $res->fetch_assoc()){
        echo'<tr>
            <td>'.$row['name'].'</td>
      
            <td>'.$row['username'].' </td>
            <td>'.$row['password'].' </td>
            <td>'.$row['description'].' </td>
            <td>'.$row['type'].' </td>
            <td><a href="#edit'.$row['id'].'" data-toggle="modal">
            <button type="button" class="btn btn-primary btn-sm"> <i class="fas fa-edit"></i>  Edit</button> 
            </a>
    
            </td>
            </tr>
            <div id="edit'.$row['id'].'" class="modal fade" role="dialog">
                <form method="post" action="form.php" class="form-horizontal" role="form">
                    <div class="modal-dialog modal-md modal-dialog-centered">
                        <!-- Modal content-->
                        <div class="modal-content">
                            <div class="modal-header">
                            <h4 class="modal-title">Edit Individual Tools</h4>
                                <button type="button" class="close" data-dismiss="modal">&times;</button>
                    
                            </div>
                            <div class="modal-body">
                                <div class="row">
                                    <div class="col-sm-6">
                                        <label for="toolsusername" >Tools</label>
                                        <input type="text" class="form-control" name="utoolsname" id="utoolsname" value="'.$row['name'].'" placeholder="Tools" readonly>
                                        <input type="hidden" value="'.$row['toolsid'].'" name="id">
                                        <input type="hidden" value="'.$row['empid'].'" name="empid">
                                    </div>
                                    <div class="col-sm-6">
                                        <label for="toolsusername" >Type</label>
                                        <input type="text" class="form-control" name="utoolstype" value="'.$row['type'].'" placeholder="Password" readonly>
                                    </div>
                                    <div class="col-sm-6">
                                        <label for="toolsusername" >Username</label>
                                        <input type="text" class="form-control" name="utoolusername" value="'.$row['username'].'" placeholder="Username">
                                    </div>
                                    <div class="col-sm-6">
                                        <label for="toolpassword" >Password</label>
                                        <input type="text" class="form-control" name="utoolpassword"  value="'.$row['password'].'" placeholder="Password">
                                    </div>
                                    <div class="col-sm-12">
                                        <label for="toolsdesc" >Description</label>
                                        <textarea name="utoolsdesc" class="form-control"   cols="30" rows="5" placeholder="Description">'.$row['description'].'</textarea>
                                    </div>
                                </div>



                            </div>


                            <div class="modal-footer">
                                <button type="submit" class="btn btn-success" name="updatetools"><i class="fas fa-check"> </i> Save</button>
                                <button type="button" class="btn btn-danger" data-dismiss="modal"><i class="fas fa-close"> </i> Cancel</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            
    
        ';
    
    }
    

}
if(isset($report) || isset($_POST['filterReport'])){

    if(isset($_POST['filterReport'])){

        $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 ';


        
        }
        
    }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>
            
    
        ';
    
    }

}
?>