
    <?php
    $thisid =  $_GET["id"];

   include 'DBConnection.php';



    $sql       = 'select * from records where emp_id = '.$thisid;

    $results   = mysqli_query($connection, $sql);


    $row_count = mysqli_num_rows($results);
    $id = 0;
  
    while ($row = mysqli_fetch_array($results)) {
      /*  echo '<tr><td><a href="OBApprove.php?id='.$row['obid'].'">'.$row['emp_number'].'</a></td></tr>';
        echo '<td>'.$row['emp_id'].'</td>';
        echo '<td>'.$row['dept_from'].'</td>';
        echo '<td>'.$row['dept_to'].'</td>';
        echo '<td>'.$row['purpose'].'</td>';
        echo '<td>'.$row['dept_time'].'</td>';
        echo '<td>'.$row['return_time'].'</td>';
        echo '<td>'.$row['status'].'</td>';*/

        $emp_name = $row['lname']." ".$row['fname'];
        $emp_id = $row['emp_id'];
        $joined_date = $row['jdate'];
        $department = $row['division'];
        //$job_title_code = $row['job_title_code'];

  

    }

 

$temp = 'DAILY';

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

$status = $_POST['duration'];

if ($status == 'daily') {
     $temp = "DAILY";
 }
 else if ($status == 'weekly')
 {
    $temp = "WEEKLY";
 }
  else if ($status == 'monthly')
 {
    $temp = "MONTHLY";
 }
  else if ($status == 'yearly')
 {
    $temp = "YEARLY";
 } else
 {
    $temp = "DAILY";
 } 
 
 }

    $selected_duration = $temp;

    ?>



    <!DOCTYPE html>
    <html>

    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Page Title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel='stylesheet prefetch' href='https://use.fontawesome.com/releases/v5.0.10/css/all.css'>
        <link rel="stylesheet" type="text/css" media="screen" href="main.css" />

        <style>
    input {
        background-color:white;
        border:none;
        color: #000000;
    }
    </style>

       </head>

    <body>
                  <div id="content">
                    <!-- Set of nested tabs -->
                    <div class="tabs" style="border: 0 !important;">  
                        <div class="tabs-nav">
 
                        </div>
                        <div class="tabs-content">
                            <div class="tabs-form is-active">
 
                      
                                <div id="logo" style="display: flex; justify-content: center; margin-bottom: 100px;"><img src="pdflogo.png"></div>

                                <div style="background-color: black; color: white; text-align: center;">
                                    <h2 style="padding: 10px 0px !important;">OFFICIAL DAILY TIME RECORD</h2>
                                </div>
                                
                                 
                                <table border="1" cellpadding="10px" style="width:100%;">

                                    <tbody>
                                    <tr>
                                    <td style="" colspan="6">Time Record</td>
                                    <td style="">Employee No.:</td>
                                    <td style=""><input type="text" name="try" value="<?php echo $emp_id ?>" disabled></td>
                                    </tr>
                                    <tr>
                                    <td style="" colspan="2">Employee Name:</td>
                                    <td style="" colspan="4" ><input type="text" name="" value="<?php echo $emp_name ?>" disabled></td>
                                    <td style="">Start Date:</td>
                                    <td style=""><input type="text" name="" value="<?php echo $joined_date ?>" disabled></td>
                                    </tr>
                                    <tr>
                                    <td style="" colspan="2">BU/Department:</td>
                                    <td style="" colspan="4"><?php echo $department ?></td>
                                    <td style="">End Date:</td>
                                    <td style=""></td>
                                    </tr>
                                    

                              <!-- looping table -->

                                   <?php
                // real time data
                              $conn = @mysqli_connect('192.168.2.117', 'webster', 'webster');
                              if (!$conn)
                              {
                                die("Database Connection Failed" . mysqli_error($conn));
                              }

                              

                              $select_db = mysqli_select_db($conn,'realtimedata');
                              if (!$select_db)
                              {
                                die("Database Selection Failed" . mysqli_error($conn));
                              }
                              
                            
                              $conn1 = @mysqli_connect('localhost', 'root', '');
                              if (!$conn1)
                              {
                                die("Database Connection Failed" . mysqli_error($conn1));
                              }

                              $select_db1 = mysqli_select_db($conn1,'db');
                              if (!$select_db1)
                              {
                                die("Database Selection Failed" . mysqli_error($conn1));
                              }
                              // OT
                           
                              //session_start();
                                //$emp_id=$_SESSION['emp_id'];//null;
                              //if(isset($_POST['submit']))
                              //{
                                //  $emp_id=$_POST['emp_id'];
                              //}
                              
                              //$emp_id=$_SESSION['emp_id'];//null;
                       
                       
                                 
                        ?>


                                   
                                    <br> </br>
                                     <tr>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Date</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Day</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>IN</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>OUT</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Tardy</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>OT Hours (subject to filling &amp; approval)</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Undertime</strong></td>
                                        <td style="background-color: rgb(199, 199, 199);"><strong>Remarks</strong></td>
                                     </tr>

                                    <?php


                                    $connectionrtd = mysqli_connect("192.168.2.117","webster","webster","realtimedata");

                                    $attsql = "SELECT * from attendanceformatted WHERE userid =".$thisid;
                                        $query11=mysqli_query($connectionrtd, $attsql)or die(mysqli_error($connectionrtd));
                      

                                              //die("SELECT * from employee WHERE emp_id=$emp_id");
                                              while($row=mysqli_fetch_array($query11))
                                              {
                                            
                                               $att_in =  $row['att_in'];
                                               $att_out =  $row['att_out'];


                                               

                                               $s = strtotime($att_in);
                                               $s1 = strtotime($att_out);
                                               $set = "09:00:00";
                                               $cset = strtotime($set);

                                               $date =  date('Y-m-d', $s);
                                               $day = date('D', strtotime($att_in));
                                               $diff = $s - $s1;

                                               $timeIn = date('H:i:s', $s);
                                               $timeOut = date('H:i:s', $s1);

                                               $ctimein = strtotime($timeIn);
                                               $ctimeout = strtotime($timeOut);

                                               $hours = $diff / (60 * 60);
                                               $fhours = abs($hours);
                                              

                                             //  if ($ctimein < $cset){
                                               // $finalhours = 0;
                                               //} else {
                                                if($ctimein > $cset) {

                                                  $t =   $cset - $ctimein;
                                                 $t1 = $t / (3600);
                                                 $t2 = round(abs($cset - $ctimein) / 3600,2);
                                                  $finalhours = $t2;

                                                } else {
                                                  $finalhours = 0;
                                                }
                                               
                                               //  $finalhours = number_format((float)strtotime($set), 2, '.', '');


?>
                              
     <tr>
                                             <td align="center"><?php echo $date ?></a></td>
                                             <td align="center"><?php echo $day ?></a></td>
                                             <td align="center"><?php echo $timeIn ?></a></td>
                                               <td align="center"><?php echo $timeOut ?></a></td>
                                              <td align="center"><?php echo $finalhours ?></a></td>




<?php 

                                        $att2 = "SELECT * from ot WHERE `userid`=".$thisid." AND `time_out` = '".$timeOut."'";
                                        $att3 = "SELECT * from  obform WHERE `emp_id`=".$thisid;

                                        
                                        $ut_time =  round(abs($ctimein - $ctimeout) / 3600,2);




                                        $query1=mysqli_query($conn1,$att2)or die(mysqli_error($conn1));
                                
                                       $query2=mysqli_query($conn1,$att3)or die(mysqli_error($conn1));


                                  

                                         if (mysqli_num_rows($query1)==0) {

                                              $remarks = "None";
                                              $finalut = $ut_time;
                                              $ot_time1 = 0 ; 

                                             if(($ut_time < 8) && ($ut_time >4) && (!($ctimeout >= "17:30:00"))){
                                                $finalut = $ut_time;
                                              }            else {
                                                $finalut = "No Undertime Record";
                                              }

                                           
?>
                                               <td align="center"><?php echo $ot_time1 ?></a></td>            
                                              <td align="center"><?php echo $finalut ?></a></td>
                                              <td align="center"><?php echo $remarks ?></a></td>

                                         </tr>
<?php
               
                                         }    

                                         else {
 
                                       while($row = mysqli_fetch_array($query1))
                                          {
                                            
                                              $ot_time_in = $row['time_in'];
                                              $ot_time_out = $row['time_out'];
                                              $ctin = strtotime($ot_time_in);
                                              $ctout = strtotime($ot_time_out); 
                                              $date_from = $row['date_from'];
                                              $userid = $row['userid'];

                                              echo $ot_time_out;

                                              //Ob

                                           /*   $ob_emp_id = $row['emp_id'];
                                              $dept_time = $row['dept_time'];
                                              $return_time = $row['return_time'];
                                              $d_time = strtotime($dept_time);
                                              $r_time = strtotime($return_time);

                                              $date1 =  date('Y-m-d', $d_time);
                                              $date2 = date('Y-m-d', $r_time);

                                              $stat = $row['Status'];

                                              echo $date;
                                              echo $date1;

                                              if (($ob_emp_id == $thisid) && ($date == $date1))  {
                                               $remarks = "OB".$stat;

                                           
                                              }*/

                                            if (($userid == $thisid) && ($timeIn == $ot_time_in) && ($timeOut == $ot_time_out) && ($date_from == $date))
                                            
                                            {

                                           
                                            $status = $row['Status'];
                                            $otset = "17:30:00";            
                                            $oset = strtotime($otset);
                                            $otimeout = strtotime($ot_time_out);
                                          
                                                  if(($ut_time < 8) && ($ut_time >4) && (!($ctimeout > "17:30:00"))){
                                                $finalut = $ut_time;
                                              }            else {
                                                $finalut = "No Undertime Record";
                                              }
                                   
                                            $remarks = "Overtime";

 
                                            $temp1 = round(abs($oset - $ctout) / 3600,2);

                                            if ($status == 1)
                                            { 
                                                $str = " [approved] ";
                                                $ot_time1 = $temp1." ".$str;
                                            } 

                                            else
                                             {
                                                $str = " [reject] ";
                                                $ot_time1 = $temp1." ".$str;
                                             }


                                          } else {

                                                   if(($ut_time < 8) && ($ut_time >4) && (!($ctimeout > "17:30:00"))){
                                                $finalut = $ut_time;
                                              }            else {
                                                $finalut = "No Undertime Record";
                                              }
                                           
                                             
                                              $remarks = "None1";
                                           
                                              $ot_time1 = 0;
                                                       
                                            }

                                     ?>

                                      <td align="center"><?php echo $ot_time1 ?></a></td>       
                                      <td align="center"><?php echo $finalut ?></a></td>
                                      <td align="center"><?php echo $remarks ?></a></td>
                                      </tr>

                                        <?php 
                                            } //end of while loop
                                        }//end of else
                                      }//end of another while loop
      
                                  ?>

                                    <tr>
                                    <td style="">Leave Credits:</td>
                                    <td style="">{{leave_credits}}</td>
                                    <td style="" rowspan="2">&nbsp;</td>
                                    <td style="">Noted by:</td>
                                    <td style="" colspan="2">{{noted_by}}</td>
                                    <td style="">Approved by:</td>
                                    <td style="">{{approved_by}}</td>
                                    </tr>
                                    <tr>
                                    <td style="">As of</td>
                                    <td style="">{{date_of_request}}</td>
                                    <td style="" colspan="3">&nbsp;</td>
                                    <td style="" colspan="2">&nbsp;</td>
                                    </tr>
                                    </tbody>

                                </table>


                            </div>
                          </div>
                        </div>
                      </div>
                    
                        <div id="editor"></div>
                         <center> <button id="cmd">Generate PDF</button> </center>


                        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
                        <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.61/jspdf.min.js"></script>  
                        <script src="pdf.js"></script>
                    </body>
                  </html>