<?php
include "include/db.php";

include "include/adminauth.php";
$itemno = ''; 

if(isset($_REQUEST['item_no']) && isset($_REQUEST['type'])){
    if(strlen($_REQUEST['item_no']) > 0 && strlen($_REQUEST['type']) > 0){
        $url_item_no = $_REQUEST['item_no'];
        $item_type = $_REQUEST['type'];

        if($item_type == 'bid'){
            $wo_no = $url_item_no;
            $query_check_wo = "SELECT * FROM bid WHERE bid_id = $wo_no AND is_delete IS NULL";
            $run_check_wo = mysqli_query($conn, $query_check_wo);
    
            if(mysqli_num_rows($run_check_wo) ==1){
                while($row_check_wo = mysqli_fetch_array($run_check_wo)){
                    $itemno = $row_check_wo['bid_wo_no'];
                    $client_id = $row_check_wo['client_id'];                   
                    $type = $row_check_wo['bid_type'];
                    $staff_name = ''; 
                    if(strlen($row_check_wo['admin_id']) > 0){
                        $admin_id = $row_check_wo['admin_id'];
                        $admin_details_query = "SELECT * FROM admin WHERE admin_id = $admin_id AND status = 'active'";
                        $admin_details_run = mysqli_query($conn, $admin_details_query);
                        $admin_details_row = mysqli_fetch_array($admin_details_run);
                        $staff_name = $admin_details_row['admin_fname'] . ' ' . $admin_details_row['admin_lname'];
                    }elseif(strlen($row_check_wo['employee_id']) > 0){
                        $staff_id = $row_check_wo['employee_id'];
                        $emp_details_query = "SELECT * FROM employee WHERE employee_id = $staff_id AND status = 'active'";
                        $emp_details_run = mysqli_query($conn, $emp_details_query);
                        $emp_details_row = mysqli_fetch_array($emp_details_run);
                        $staff_name = $emp_details_row['employee_fname'] . ' '. $emp_details_row['employee_lname'];            
                    }
                    $create_time = $row_check_wo['create_time'];
                        
                    $query_client_detail = "SELECT * FROM client WHERE client_id = $client_id";
                    $run_client_detail = mysqli_query($conn, $query_client_detail);
    
                    if(mysqli_num_rows($run_client_detail) ==1){
                        while($row_client_detail = mysqli_fetch_array($run_client_detail)){
                            $client_name = $row_client_detail['client_name'];
                        }
                    }
                }            
            }else{
                header("Location:index.php");  
            }
        }elseif($item_type == 'inv'){
            $invoice_id = $url_item_no;
            $query_check_wo = "SELECT * FROM invoice WHERE invoice_id = $invoice_id AND is_delete IS NULL";
            $run_check_wo = mysqli_query($conn, $query_check_wo);

            if(mysqli_num_rows($run_check_wo) ==1){
                while($row_check_wo = mysqli_fetch_array($run_check_wo)){
                    $itemno = $row_check_wo['invoice_no'];
                    $client_id = $row_check_wo['client_id'];
                    $type = $row_check_wo['invoice_type'];  
                    $staff_name = ''; 
                    if(strlen($row_check_wo['admin_id']) > 0){
                        $admin_id = $row_check_wo['admin_id'];
                        $admin_details_query = "SELECT * FROM admin WHERE admin_id = $admin_id AND status = 'active'";
                        $admin_details_run = mysqli_query($conn, $admin_details_query);
                        $admin_details_row = mysqli_fetch_array($admin_details_run);
                        $staff_name = $admin_details_row['admin_fname'] . ' ' . $admin_details_row['admin_lname'];
                    }elseif(strlen($row_check_wo['employee_id']) > 0){
                        $staff_id = $row_check_wo['employee_id'];
                        $emp_details_query = "SELECT * FROM employee WHERE employee_id = $staff_id AND status = 'active'";
                        $emp_details_run = mysqli_query($conn, $emp_details_query);
                        $emp_details_row = mysqli_fetch_array($emp_details_run);
                        $staff_name = $emp_details_row['employee_fname'] . ' '. $emp_details_row['employee_lname'];            
                    }             
                    $create_time = $row_check_wo['create_time'];
                    $query_client_detail = "SELECT * FROM client WHERE client_id = $client_id";
                    $run_client_detail = mysqli_query($conn, $query_client_detail);

                    if(mysqli_num_rows($run_client_detail) ==1){
                        while($row_client_detail = mysqli_fetch_array($run_client_detail)){
                            $client_name = $row_client_detail['client_name'];
                        
                        }
                    }
                }            
            }
        }  
            
    }else{
        header("Location:index.php");
        exit;
    }    
}else{
    header("Location:index.php");
    exit;
}
if(strlen($itemno) == 0){
    header("Location:index.php");
    exit;
}

include "header.php";

?>
<div id="page-wrapper">
<h3><?php 
$item_name = "";
if($item_type == 'bid'){
    echo 'Bid#';
    $item_name = "bid";
}elseif($item_type == 'inv'){
    echo 'Invoice#';
    $item_name = "invoice";
}
echo " ". $itemno;
 ?> Change Details<a class='btn btn-warning' style = 'float:right;' href='<?php

if(isset($_REQUEST['state'])){
    if(strlen($_REQUEST['state']) > 0){
        if($_REQUEST['state'] == "report"){
            $item_type = 'report';
        }
    }
}
if($item_type == 'report'){
    echo 'pay-report.php';
}elseif($item_type == 'bid'){
    if($type == '1'){
        echo 'incomplete-bids.php';
    }elseif($type == '2'){
    echo 'view-bids.php';
    }
}elseif($item_type == 'inv'){
    if($type == '1'){
        echo 'incomplete-invoices.php';
    }elseif($type == '2'){
        echo 'view-invoices.php';
    }
}
?>' id = ''>Back</a></h3>
<h4>Client Name- <?php echo $client_name; ?></h4>

              
    
<div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-dash">
                                    <thead>
                                        <tr>
                                                <th>
                                                  Time
                                                </th>
                                                <th>
                                                Action
                                                </th>
                                                <th>
                                                  User
                                                </th>                                     
                                         
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <tr class="odd gradeX">
 <td>
<?php
   
    $create_time = strtotime($create_time);
    $create_time = date('m-d-Y h:i A', $create_time);  
    echo $create_time;
?>
</td>
<td>
<?php
    echo "Created the $item_name";
?>
</td>
<td>
<?php
    echo $staff_name;
?>
</td>
</tr>
<?php
    $query_log = "SELECT * FROM change_log WHERE type = '$item_name' AND item_id = $url_item_no";
    $run_log = mysqli_query($conn, $query_log);
    if(mysqli_num_rows($run_log) >0){
        while($row_log = mysqli_fetch_array($run_log)){
            $log_user_name = '';
            if(strlen($row_log['employee_id']) > 0){
                $log_staff_id = $row_log['employee_id'];
                $log_emp_details_query = "SELECT * FROM employee WHERE employee_id = $log_staff_id";
                $log_emp_details_run = mysqli_query($conn, $log_emp_details_query);
                $log_emp_details_row = mysqli_fetch_array($log_emp_details_run);
                $log_user_name = $log_emp_details_row['employee_fname'] . ' '. $log_emp_details_row['employee_lname'];            
            }elseif(strlen($row_log['admin_id']) > 0){
                $log_admin_id = $row_log['admin_id'];
                $log_admin_details_query = "SELECT * FROM admin WHERE admin_id = $log_admin_id AND status = 'active'";
                $log_admin_details_run = mysqli_query($conn, $log_admin_details_query);
                $log_admin_details_row = mysqli_fetch_array($log_admin_details_run);
                $log_user_name = $log_admin_details_row['admin_fname'] . ' ' . $log_admin_details_row['admin_lname'];
            } 
            $log_change_text = $row_log['change_text'];
            $log_create_time = $row_log['time'];
            $log_create_time = strtotime($log_create_time);
            $log_create_time = date('m-d-Y h:i A', $log_create_time);  
            echo '<tr class="odd gradeX">';
            echo '<td>';
            echo $log_create_time;
            $log_create_time = '';
            echo '</td>';
            echo '<td>';
            echo $log_change_text;
            $log_change_text = '';
            echo '</td>';
            echo '<td>';
            echo $log_user_name;
            $log_user_name = '';
            echo '</td>';
            echo '</tr>';

        }
    }
?>
</tbody>
                         
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->


</div> <!--  page wrapper -->                             
       
       
<?php  include "footer.php";   ?>