<?php 
    include("header.php"); 
    
    $edit_ename = '';
    $edit_comp  = '';
    $edit_img   = '';
    $edit_uname = '';
    $edit_pwd   = '';
    $edit_email = '';
    $edit_phone = '';
    $edit_desc  = '';
    $emp_id     = '';


    if(isset($_POST['action']) && $_POST['action'] == 'edit_profile' && $_POST['edit_id']){

        $single_emp = mysqli_fetch_assoc(mysqli_query($conn, "select * from db_users where id=".$_POST['edit_id']));

        $edit_ename = $single_emp['full_name'];
        $edit_comp  = $single_emp['company_name'];
        $edit_img   = $single_emp['profile_img'];
        $edit_uname = $single_emp['username'];
        $edit_email = $single_emp['email'];
        $edit_phone = $single_emp['phone'];
        $edit_desc  = $single_emp['description'];
        $emp_id     = $_POST['edit_id'];
    }
    if(isset($_POST['action']) && $_POST['action'] == 'del_profile' && $_POST['del_id']){
        mysqli_query($conn, "delete from db_users where id=".$_POST['del_id']);
    }
    if(isset($_POST['update_emp']) && !empty($_POST['emp_id'])){
        $query = "update db_users set username='".$_POST['username']."', email='".$_POST['email']."', phone='".$_POST['phone']."', full_name='".$_POST['ename']."' ,company_name='".$_POST['comp']."' ,description='".$_POST['desc']."' ,type='".$_POST['user_type']."' where id=".$_POST['emp_id'];
        $sql=mysqli_query($conn,$query);
    }
    if (isset($_POST['save_company']))
    {
          
            $username   =   $_POST['username'];
            $password   =   md5($_POST['password']);
            $email      =   $_POST['email'];
            $phone      =   $_POST['phone'];
            $ename      =   $_POST['ename'];
            $comp       =   $_POST['comp'];
            $desc       =   $_POST['desc'];
            $type       =   $_POST['user_type'];
            $fname      =   "";
            $time       =   date('Y-m-d h:i:s');
            
            if(isset($_FILES['eimage']['name']) && !empty($_FILES['eimage']['name'])){

                $rawBaseName = pathinfo($_FILES['eimage']['name'], PATHINFO_FILENAME );
                $extension = pathinfo($_FILES['eimage']['name'], PATHINFO_EXTENSION );
                $counter = 0;
                $fname = basename($_FILES["eimage"]["name"]);

                while(file_exists($_SERVER['DOCUMENT_ROOT']."/timesheet_new/dist/assets/profile/".$fname)) {
                    $fname = $rawBaseName . $counter . '.' . $extension;
                    $counter++;
                }
                move_uploaded_file($_FILES['eimage']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/timesheet_new/dist/assets/profile/".$fname);

            }

            $query = "insert into db_users(username, password, email,phone,full_name,company_name,description,profile_img,type,date_time) values ('$username', '$password', '$email', '$phone','$ename', '$comp', '$desc', '$fname', '$type', '$time' )";
            $sql=mysqli_query($conn,$query);
            
    }

     
   /* if(!$_SESSION['email'])  
    {  
      
        header("Location: index.php");
    }  */
  
?>

            <!-- Body: Body -->       
            <div class="body d-flex py-lg-3 py-md-2">
                <div class="container-xxl">
                    <div class="row align-items-center">
                        <div class="border-0 mb-4">
                            <div class="card-header py-3 no-bg bg-transparent d-flex align-items-center px-0 justify-content-between border-bottom flex-wrap">
                                <h3 class="h4 mb-0">Company List</h3>
                                <div class="col-auto d-flex w-sm-100 mt-2 mt-sm-0">
                                    <button type="button" class="btn btn-dark btn-set-task w-sm-100" data-bs-toggle="modal" data-bs-target="#addemp"><i class="icofont-plus-circle me-2 fs-6"></i>Add Company</button>
                                </div>
                            </div>
                        </div>
                    </div> <!-- Row end  -->
                    <div class="row clearfix g-3">
                    <div class="col-sm-12">
                        <div class="card mb-3">
                            <div class="card-body">
                                <table id="myProjectTable" class="table table-hover align-middle mb-0" style="width:100%">
                                    <thead>
                                        <tr>
                                            <th>Company Name</th>
                                            <th>Project</th>
                                            <th>Total Employes</th>
                                            <th>Email</th>
                                            <th>Created On</th>
                                           
                                            <th>Actions</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php 
                                            $emp_list = mysqli_query($conn, "select * from db_users where type='company' ");
                                            if(!empty($emp_list)){
                                                while($res = mysqli_fetch_array($emp_list)):
                                                    $pro_img = '';
                                                    $show_img = !empty($res['profile_img']) ? $site_url.'dist/assets/profile/'.$res['profile_img'] : 'assets/images/xs/avatar1.svg';
                                                echo '<tr>
                                                   <td>
                                                       <img class="avatar rounded-circle" src="'.$show_img.'" alt="">
                                                       <span class="fw-bold ms-1">'.$res['full_name'].'</span>
                                                   </td>
                                                   <td>
                                                       -
                                                   </td>
                                                   <td>
                                                        0
                                                   </td>
                                                   <td>
                                                        <a href="mailto:'.$res['email'].'">'.$res['email'].'</a>
                                                   </td>
                                                   <td>
                                                        '.date('d/m/y', strtotime($res['date_time'])).'
                                                   </td>
                                                   
                                                    <td>
                                                        <div class="btn-group" role="group" aria-label="Basic outlined example">
                                                            <button type="button" class="btn btn-outline-secondary edit-btn" data-id="'.$res['id'].'"><i class="icofont-edit text-success"></i></button>
                                                            <button type="button" class="btn btn-outline-secondary deleterow" data-id="'.$res['id'].'"><i class="icofont-ui-delete text-danger"></i></button>
                                                        </div>
                                                    </td>
                                                </tr>';
                                            endwhile;
                                            }
                                        ?>
                                        
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    </div><!-- Row End -->
                </div>
            </div>
            <!-- Create Employee-->
           <div class="modal fade" id="addemp" tabindex="-1"  aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-lg modal-dialog-scrollable">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title  fw-bold" id="createprojectlLabel"> Add Company</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body"> 
                        
                             <form name="f1" method="POST" enctype="multipart/form-data">
                            <div class="mb-3">
                                <label for="exampleFormControlInput877" class="form-label">Company Name</label>
                                <input type="text" name="ename" class="form-control" id="exampleFormControlInput877" placeholder="Explain what the Project Name" required="">
                            </div>
                            
                            <div class="mb-3">
                                <label for="formFileMultipleoneone" class="form-label">Company Profile</label>
                                <input class="form-control" name="eimage" type="file" id="formFileMultipleoneone" >
                            </div>
                          <div class="deadline-form">
                               
                            
                            <div class="row g-3 mb-3">
                                <div class="col">
                                    <label for="exampleFormControlInput177" class="form-label">User Name</label>
                                    <input type="text" name="username" class="form-control" id="exampleFormControlInput177" placeholder="User Name" required="" autocomplete="off">
                                </div>
                                <div class="col">
                                    <label for="exampleFormControlInput277" class="form-label">Password</label>
                                    <input type="Password" name="password" class="form-control" id="exampleFormControlInput277" placeholder="Password" required="" autocomplete="off">
                                </div>
                            </div> 
                            <div class="row g-3 mb-3">
                                <div class="col">
                                    <label for="exampleFormControlInput477" class="form-label">Email ID</label>
                                    <input type="text" name="email" class="form-control" id="exampleFormControlInput477" placeholder="Email Address" required="">
                                </div>
                            <div class="col">
                                    <label for="exampleFormControlInput777" class="form-label">Phone</label>
                                    <input type="text" name="phone" class="form-control" id="exampleFormControlInput777" placeholder="Phone Number" required="">
                                </div>
                            </div>
                              
                                
                        </div> 
                          <div class="mb-3">          
                                <label for="exampleFormControlTextarea78" class="form-label">Description (optional)</label>
                                <textarea class="form-control" name="desc" id="exampleFormControlTextarea78" rows="3" placeholder="Add any extra details about employee"></textarea>
                            </div> 
                            <input type="hidden" name="user_type" value="company">
                            <input type="submit" name="save_company" class="save_company" style="display: none;">
                           
                            </form>
                        </div>
                     <div class="modal-footer">
                            <!-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Done</button> -->
                            <button type="submit" class="btn btn-primary" name="submit" value="submit" onclick="$('.save_company').click();">Create</button>
                        </div> 
                    </div>  
                </div>
            </div>

             <!-- Edit Employee-->
           <div class="modal fade" id="editemp" tabindex="-1"  aria-hidden="true">
                <div class="modal-dialog modal-dialog-centered modal-lg modal-dialog-scrollable">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title  fw-bold" id="createprojectlLabel"> Edit Company</h5>
                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body"> 
                        
                             <form name="f1" method="POST" enctype="multipart/form-data">
                            <div class="mb-3">
                                <label for="exampleFormControlInput877" class="form-label">Company Name</label>
                                <input type="text" name="ename" class="form-control" id="exampleFormControlInput877" placeholder="Explain what the Project Name" required="" value="<?= $edit_ename; ?>">
                            </div>
                    
                            <div class="mb-3">
                                <label for="formFileMultipleoneone" class="form-label">Company Profile</label>
                                <input class="form-control" name="eimage" type="file" id="formFileMultipleoneone" >
                                <input type="hidden" name="old_img_name" value="<?= $edit_img; ?>">
                            </div>
                          <div class="deadline-form">
                               
                            
                            <div class="row g-3 mb-3">
                                <div class="col">
                                    <label for="exampleFormControlInput177" class="form-label">User Name</label>
                                    <input type="text" name="username" class="form-control" id="exampleFormControlInput177" placeholder="User Name" required="" autocomplete="off" value="<?= $edit_uname; ?>">
                                </div>
                                <!-- <div class="col">
                                    <label for="exampleFormControlInput277" class="form-label">Password</label>
                                    <input type="Password" name="password" class="form-control" id="exampleFormControlInput277" placeholder="Password" required="" autocomplete="off">
                                </div> -->
                            </div> 
                            <div class="row g-3 mb-3">
                                <div class="col">
                                    <label for="exampleFormControlInput477" class="form-label">Email ID</label>
                                    <input type="text" name="email" class="form-control" id="exampleFormControlInput477" placeholder="Email Address" required="" value="<?= $edit_email; ?>">
                                </div>
                            <div class="col">
                                    <label for="exampleFormControlInput777" class="form-label">Phone</label>
                                    <input type="text" name="phone" class="form-control" id="exampleFormControlInput777" placeholder="Phone Number" required="" value="<?= $edit_phone; ?>">
                                </div>
                            </div>
                              
                                
                        </div> 
                          <div class="mb-3">          
                                <label for="exampleFormControlTextarea78" class="form-label">Description (optional)</label>
                                <textarea class="form-control" name="desc" id="exampleFormControlTextarea78" rows="3" placeholder="Add any extra details about employee"></textarea>
                            </div> 
                            <input type="hidden" name="user_type" value="company">
                            <input type="hidden" name="emp_id" value="<?= $emp_id; ?>">
                            <input type="submit" name="update_emp" class="update_emp" style="display: none;">
                           
                            </form>
                        </div>
                        <div class="modal-footer">
                            <!-- <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Done</button> -->
                            <button type="submit" class="btn btn-primary" name="update_profile" value="Update" onclick="$('.update_emp').click();">Update</button>
                        </div> 
                    </div>  
                </div>
            </div>
            <style type="text/css">
            .modal-body {
                padding-top: 25px;
            }</style>

<?php include("footer.php"); ?>
<script type="text/javascript">
        jQuery(document).on("click",".edit-btn", function(){
            jQuery("#editemp").modal('show');
            var edit_id = jQuery(this).attr("data-id");
            jQuery.ajax({
                type:"post",
                url:"",
                data:{action:"edit_profile",edit_id:edit_id},
                success:function(res){
                    jQuery('#editemp').html(jQuery(res).find('#editemp').html());
                }
            });
        });
        jQuery(document).on("click", '.deleterow',function(){
            var tablename = $(this).closest('table').DataTable();  
            var del_id = jQuery(this).attr("data-id");
            if(confirm("Are you sure?")){
                 jQuery.ajax({
                    type:"post",
                    url:"",
                    data:{action:"del_profile",del_id:del_id},
                    success:function(res){
                        jQuery('#editemp').html(jQuery(res).find('#editemp').html());
                    }
                });

                tablename.row( $(this).parents('tr') ).remove().draw();
            }
            

            } );
</script>