<?php
include "include/db.php";
include "include/auth.php";
function clean_input_data($cellVal){	
    include "include/db.php";
	$cellVal = trim($cellVal);
	$cellVal = mysqli_real_escape_string($conn, $cellVal);
	$cellVal = strtolower($cellVal);
    $cellVal = str_replace('*', '', $cellVal);
	return $cellVal;
}

$message = '';
$search_result_no = 0;

$prop_results = 0;
$user_results = 0;
$order_results = 0;
$pay_results = 0;
$job_results = 0;
$inv_results = 0;

$search_property = '';
$search_users = '';
$search_jobs = '';

$search_orders = '';
$search_payments = '';
$search_invoices = '';
$search_all = '';

if(isset($_REQUEST['search_text'])){
    if(strlen($_REQUEST['search_text']) > 0){
        $search_text =  clean_input_data($_REQUEST['search_text']);
        if(strlen($search_text) > 0){            
            if(isset($_REQUEST['property'])){
                if(strlen($_REQUEST['property']) > 0){
                    $search_property = clean_input_data($_REQUEST['property']);
                }
            }
            if(isset($_REQUEST['users'])){
                if(strlen($_REQUEST['users']) > 0){
                    $search_users = clean_input_data($_REQUEST['users']);
                }
            }

            if(isset($_REQUEST['jobs'])){
                if(strlen($_REQUEST['jobs']) > 0){
                    $search_jobs = clean_input_data($_REQUEST['jobs']);
                }
            }

            if(isset($_REQUEST['orders'])){
                if(strlen($_REQUEST['orders']) > 0){
                    $search_orders = clean_input_data($_REQUEST['orders']);
                }
            }

            if(isset($_REQUEST['payments'])){
                if(strlen($_REQUEST['payments']) > 0){
                    $search_payments = clean_input_data($_REQUEST['payments']);
                }
            }

            if(isset($_REQUEST['invoices'])){
                if(strlen($_REQUEST['invoices']) > 0){
                    $search_invoices = clean_input_data($_REQUEST['invoices']);
                }
            }

            if(isset($_REQUEST['all'])){
                if(strlen($_REQUEST['all']) > 0){
                    $search_all = clean_input_data($_REQUEST['all']);
                    if(strlen($search_all) > 0){
                        $search_property = 'property';
                        $search_users = 'users';
                        $search_jobs = 'jobs'; 
                        $search_orders = 'orders';
                        $search_invoices = 'invoices'; 
                        $search_payments = 'payments';
                    }
                }
            }
            

            if(strlen($search_property) == 0 && strlen($search_users) == 0 && strlen($search_jobs) == 0 && strlen($search_orders) == 0 && strlen($search_invoices) == 0 && strlen($search_payments) == 0){
                $search_invoices = 'invoices';
            }

            if(strlen($search_property) > 0){
                $search_prop_query = "SELECT * FROM property WHERE prop_case_num LIKE '%$search_text%' OR prop_loan_num LIKE '%$search_text%' OR prop_full_add LIKE '%$search_text%'";
                $search_prop_run = mysqli_query($conn, $search_prop_query); 
                $prop_results = mysqli_num_rows($search_prop_run);  
            }

            if($user_type == 'admin'){     
                if(strlen($search_users) > 0){
                    $search_users_query = "SELECT * FROM employee
                    WHERE status = 'active' AND (employee_fname LIKE '%$search_text%' OR employee_lname LIKE '%$search_text%' OR employee_email LIKE '%$search_text%')";
                    $search_users_run = mysqli_query($conn, $search_users_query); 
                    $user_results = mysqli_num_rows($search_users_run);  
                }
            }

            if(strlen($search_jobs) > 0){
                $search_jobs_query = "SELECT * FROM maps LEFT JOIN category
                ON maps.cat_id = category.cat_id  WHERE maps.maps_is_delete IS NULL AND maps.maps_approved = 1 AND (maps_name LIKE '%$search_text%' OR maps_desc LIKE '%$search_text%')";
                $search_jobs_run = mysqli_query($conn, $search_jobs_query); 
                $job_results = mysqli_num_rows($search_jobs_run);  
            }

            if(strlen($search_orders) > 0){
                $search_orders_query = "SELECT * FROM bid
                WHERE is_delete IS NULL AND bid_wo_no LIKE '%$search_text%'";
                $search_orders_run = mysqli_query($conn, $search_orders_query); 
                $order_results = mysqli_num_rows($search_orders_run);  
            }

            if(strlen($search_invoices) > 0){
                $search_invoices_query = "SELECT * FROM invoice WHERE is_delete IS NULL AND (bid_wo_no LIKE '%$search_text%' OR invoice_no LIKE '%$search_text%' OR vendor_pay_ref LIKE '%$search_text%' OR client_pay_ref LIKE '%$search_text%')";
                $search_invoices_run = mysqli_query($conn, $search_invoices_query); 
                $inv_results = mysqli_num_rows($search_invoices_run);  
            }

            if(strlen($search_payments) > 0){
                $search_payments_query = "SELECT * FROM invoice WHERE is_delete IS NULL AND (bid_wo_no LIKE '%$search_text%' OR invoice_no LIKE '%$search_text%' OR vendor_pay_ref LIKE '%$search_text%' OR client_pay_ref LIKE '%$search_text%')";
                $search_payments_run = mysqli_query($conn, $search_payments_query); 
                $pay_results = mysqli_num_rows($search_payments_run);  
            }

            $search_result_no = $prop_results + $user_results + $order_results + $pay_results + $job_results + $inv_results;
        }else{
            $message = 'Please Enter a search term.';
        }    
    }else{
        $message = 'Please Enter a search term.';
    }    
}else{
    $message = 'Please Enter a search term.';
}

include "header.php";
?>

<div id="page-wrapper">
<?php                                     
                if(isset($successmessage)){
                    if(strlen($successmessage) > 0){

                    echo '<div class="alert alert-success">
                    <strong>'. $successmessage .'</strong></div>';
                     }
                } 

                if(isset($message)){
                    if(strlen($message) > 0){
                    echo '<div class="alert alert-danger">
                    <strong>'. $message .'</strong>
                  </div>';
                    }
                }
                    ?>  
<?php 
if(isset($search_text)){
?>
<h3><?php if(strlen($search_result_no) > 0){ echo $search_result_no; }?> Search result<?php if($search_result_no > 1 || $search_result_no == 0){ ?>s<?php } ?> for "<?php echo $search_text; ?>"</h3>
<?php
}else{?>
<h3>Search</h3>
<?php
}
?>
<div class="row">
 
 <div class="col-md-4">
 <form action="search_results.php" method="post">
    <div class="input-group">
    <div class="input-group-btn">
        <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-gear fa-fw"></i></button>
        <ul class="dropdown-menu dropdown-menu-left">       
          <li> <input type="checkbox" name="property" value="property" id="property" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_property) > 0){ echo "checked";} ?>><label for="property">Property</label>
          </li>
          <li> <input type="checkbox" name="users" value="users" id="users" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_users) > 0){ echo "checked";} ?>><label for="users" >Users</label></li>
          <li> <input type="checkbox" name="jobs" value="jobs" id="jobs" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_jobs) > 0){ echo "checked";} ?>><label for="jobs">Jobs</label></li>
          <li> <input type="checkbox" name="orders" value="orders" id="orders" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_orders) > 0){ echo "checked";} ?>><label for="orders">Orders</label></li>
          <li> <input type="checkbox" name="payments" value="payments" id="payments" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_payments) > 0){ echo "checked";} ?>><label for="payments">Payments</label></li>
          <li> <input type="checkbox" name="invoices" id="invoices" value="invoices" style='margin-left:5px;margin-right:5px'  <?php if(strlen($search_invoices) > 0){ echo "checked";}elseif(strlen($search_property) == 0 && strlen($search_users) == 0 && strlen($search_jobs) == 0 && strlen($search_orders) == 0 && strlen($search_invoices) == 0 && strlen($search_payments) == 0){echo "checked";} ?>><label for="invoices">Invoices</label></li>
          <li> <input type="checkbox" name="all" value="all" id="all" style='margin-left:5px;margin-right:5px' <?php if(strlen($search_all) > 0){ echo "checked";} ?>><label for="all">All</label>
          </li>
        </ul>
      </div><!-- /btn-group -->
      <input type="text" class="form-control" value='<?php 
if(isset($search_text)){
?>
<?php echo $search_text; ?>
<?php
}
?>' name="search_text" autofocus  onfocus="var temp_value=this.value; this.value=''; this.value=temp_value" id="search_text" required minlength='2' placeholder='Search'>
      <span class="input-group-btn">
        <button class="btn btn-info" type="submit"><i class="fa fa-search fa-fw"></i>Search</button>
      </span>
    </div><!-- /input-group -->
</form>
    </div>

</div><!-- /.row -->  
<br>

<?php if($prop_results > 0){  ?>


<div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Property</h4>
            </div>    

            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwizzz-search1">
                                    <thead>
                                        <tr>                                        
                                                <th>
                                                Case/Loan Number
                                                </th>
                                                <th>
                                                Address
                                                 </th> 
                                                 <th>
                                                City
                                                 </th> 
                                                 <th>
                                                State
                                                 </th> 
                                                 <th>
                                                Zip
                                                 </th>                                          
                                                <th>
                                               County
                                                </th>                                         
                                                <th>
                                                 View Details
                                                </th>                                              
                                                <th>
                                                 Edit
                                                </th>
                                            <th>Disable / Enable</th>
                                            <th>
                                                Client Assignments
                                                </th>
                                           
                                        </tr>
                                        </thead>
                                        <tbody>
<?php

      
            while($row = mysqli_fetch_array($search_prop_run)){
                $prop_id = $row['prop_id'];
                $prop_full_add = $row['prop_full_add']; 
                $prop_city = $row['prop_city']; 
                $prop_state = $row['prop_state']; 
                $prop_zip = $row['prop_zip']; 

                $prop_case_num = $row['prop_case_num'];       

               
                $prop_county = $row['prop_county'];       
                
                $is_delete = $row['is_delete'];  
    
                echo '<tr class="odd gradeX">';
                echo '<td>' .$prop_case_num. '</td>';
                echo '<td>' .$prop_full_add.'</td>';
                echo '<td>' .$prop_city. '</td>';    
                echo '<td>' .$prop_state. '</td>';    
                echo '<td>' .$prop_zip. '</td>';    
                echo '<td>' .$prop_county. '</td>';              
                


                echo "<td><a href='property.php?view=$prop_id'>View</a> </td>";
                echo "<td><a href='property.php?edit=$prop_id'>Edit</a> </td>";              
                if($is_delete == NULL){
                echo "<td><a href='property.php?delete=$prop_id&prop_case_num=$prop_case_num' class=\"delete\" data-confirm=\"Are you sure to disable $prop_case_num?\">Deactivate</a> </td>";
                }else{
                    echo "<td><a href='property.php?active=$prop_id&prop_case_num=$prop_case_num' class=\"delete\" data-confirm=\"Are you sure to activate $prop_case_num?\">Activate</a> </td>";
                }
                echo "<td><a href='property.php?assign=$prop_id'>Check</a> </td>"; 
                echo '</tr>';
            }
        

   

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



        <?php 
        if($user_type == 'admin'){
        if($user_results > 0){  ?>

            <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Users</h4>
            </div>  
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-search2">
                                    <thead>
                                        <tr>
                                                <th>
                                                    ID
                                                </th>
                                                <th>
                                                First Name
                                                </th>
                                                <th>
                                                Last Name

                                                </th><th>
                                                Email
                                                </th>
                                                <th>
                                               Status
                                                </th>
                                               
                                            <th>Edit</th>
                                            <th>Change Password</th>
                                            <th>Deactivate</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
  
    while($row = mysqli_fetch_array($search_users_run)){
        $id = $row['employee_id'];
        $name = $row['employee_fname']; 
        $code = $row['employee_lname'];  
        $email = $row['employee_email'];       
        $status = $row['status'];  

        echo '<tr class="odd gradeX">';
        echo '<td>' .$id. '</td>'; 
        echo '<td>' .$name. '</td>';
        echo '<td>' .$code. '</td>';
        echo '<td>' .$email. '</td>';
        echo '<td>' .$status. '</td>';
        echo "<td><a href='employee.php?spe=$id'>Edit</a> </td>";
        echo "<td><a href='employee.php?pass=$id'>Change</a> </td>";
        if($status =='active'){
            echo "<td><a href='employee.php?delete=$id' class=\"delete22\" data-confirm=\"Are you sure to deactivate $name $code?\">Deactivate</a> </td>";
        }else{
            echo "<td><a href='employee.php?active=$id' class=\"delete22\" data-confirm=\"Are you sure to activate $name $code?\">Activate</a> </td>";
        }
        echo '</tr>';
    }

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



        <?php }
        }
        
        ?>
        <?php if($order_results > 0){  ?>
            <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Estimate</h4>
            </div>  
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-search4">
                                    <thead>
                                        <tr>
                                        <th>
                                                  Date
                                                </th>
                                                <th>
                                                   WO#
                                                </th>
                                                <th>
                                                  Client Name
                                                </th>
                                                <th>
                                                    Case#                                               </th>
                                                <th>
                                                    Property                                               </th>
                                                    <th>
                                              Amount
                                                </th>
                                                <th>
                                              Staff
                                                </th>
                                                
                                                <th>
                                             Edit
                                                </th>
                                                
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
<?php

    while($row_bid_item = mysqli_fetch_array($search_orders_run)){
        $wo_no =  $row_bid_item['bid_id'];
        $bid_wo_no = $row_bid_item['bid_wo_no'];
        $prop_id = $row_bid_item['prop_id'];
        $bid_amount = $row_bid_item['bid_amount'];
        $client_id = $row_bid_item['client_id'];
        $employee_id = $row_bid_item['employee_id'];
        $date = $row_bid_item['create_time'];
				$timestamp = strtotime($date);
				$date= date('m-d-Y', $timestamp);
        $query_maps = "SELECT * FROM client WHERE client_id = $client_id";
        $run_maps = mysqli_query($conn, $query_maps);
        $row_maps = mysqli_fetch_array($run_maps);
        $client_name = $row_maps['client_name'];

        if(isset($employee_id)){
          $query_emp = "SELECT * FROM employee WHERE employee_id = $employee_id";
          $run_emp = mysqli_query($conn, $query_emp);
          $row_emp = mysqli_fetch_array($run_emp);
          $staff_name = $row_emp['employee_fname'];
      }
        $query_prop = "SELECT * FROM property WHERE prop_id = $prop_id";
        $run_prop = mysqli_query($conn, $query_prop);
        $row_prop = mysqli_fetch_array($run_prop);
        $prop_case_num = $row_prop['prop_case_num'];
        $prop_full_add = $row_prop['prop_full_add'];
        $prop_county = $row_prop['prop_county'];
        $prop_state = $row_prop['prop_state'];
        $prop_city = $row_prop['prop_city'];
        $prop_zip = $row_prop['prop_zip'];
        
        $property_details = $prop_full_add ." ". $prop_county. " ". $prop_city. " ". $prop_state." ". $prop_zip;
        echo '<tr class="odd gradeX">';
        echo '<td>' .$date. '</td>';
            echo '<td>' .$bid_wo_no. '</td>';
            echo '<td>' .$client_name. '</td>';
            if(isset($prop_case_num)){
              echo '<td>' .$prop_case_num. '</td>';
            }else{
              echo '<td></td>';
            }
            echo '<td>' .$property_details. '</td>';
            echo '<td>' .$bid_amount. '</td>';
            if(isset($staff_name)){
              echo '<td>' .$staff_name. '</td>';
            }else{
              echo '<td></td>';
            }
           
            echo "<td><a class='btn btn-default' href='estimate_finalize.php?wo_no=$wo_no' id = ''>Edit</a></td>";
           
        echo '</tr>';
        
    }

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

            <?php if($pay_results > 0){  ?>
                <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Payments</h4>
            </div>  
            <div class="panel-body">

                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-search6">
                                    <thead>
                                        <tr>
                                        <th>
                                                  Date
                                                </th>
                                                <th>
                                                   Invoice#
                                                </th>
                                                <th>
                                                   Pay Reference#
                                                </th>
                                                <th>
                                                  Client Name
                                                </th>
                                                <th>
                                                    Case#                                               </th>
                                                <th>
                                                    Property                                               </th>
                                                    <th>
                                                    Work Order#                                               </th>
                                                    <th>
                                              Amount
                                                </th>
                                                <th>
                                              Staff
                                                </th>
                                                
                                                <th>
                                             Edit
                                                </th>
                                                
                                               
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
<?php

    while($row_bid_item = mysqli_fetch_array($search_payments_run)){
        $invoice_id =  $row_bid_item['invoice_id'];
        $invoice_no = $row_bid_item['invoice_no'];
        $prop_id = $row_bid_item['prop_id'];
        $invoice_amount = $row_bid_item['invoice_amount'];
        $client_id = $row_bid_item['client_id'];
        $employee_id = $row_bid_item['employee_id'];
        $bid_wo_no = $row_bid_item['bid_wo_no'];
        $client_pay_no = $row_bid_item['client_pay_ref'];
        $date = $row_bid_item['create_time'];
				$timestamp = strtotime($date);
				$date= date('m-d-Y', $timestamp);
        $query_maps = "SELECT * FROM client WHERE client_id = $client_id";
        $run_maps = mysqli_query($conn, $query_maps);
        $row_maps = mysqli_fetch_array($run_maps);
        $client_name = $row_maps['client_name'];
        
        if(isset($employee_id)){
          $query_emp = "SELECT * FROM employee WHERE employee_id = $employee_id";
          $run_emp = mysqli_query($conn, $query_emp);
          $row_emp = mysqli_fetch_array($run_emp);
          $staff_name = $row_emp['employee_fname'];
        }
        $query_prop = "SELECT * FROM property WHERE prop_id = $prop_id";
        $run_prop = mysqli_query($conn, $query_prop);
        $row_prop = mysqli_fetch_array($run_prop);
        $prop_case_num = $row_prop['prop_case_num'];
        $prop_full_add = $row_prop['prop_full_add'];
        $prop_county = $row_prop['prop_county'];
        $prop_state = $row_prop['prop_state'];
        $prop_city = $row_prop['prop_city'];
        $prop_zip = $row_prop['prop_zip'];
        
        $property_details =$prop_full_add ." ". $prop_county. " ". $prop_city. " ". $prop_state." ". $prop_zip;
        echo '<tr class="odd gradeX">';
        echo '<td>' .$date. '</td>';
            echo '<td>' .$invoice_no. '</td>';
            echo '<td>' .$client_pay_no. '</td>';
            echo '<td>' .$client_name. '</td>';
            if(isset($prop_case_num)){
              echo '<td>' .$prop_case_num. '</td>';
            }else{
              echo '<td></td>';
            }
            echo '<td>' .$property_details. '</td>';
            if(isset($bid_wo_no)){
              echo '<td>' .$bid_wo_no. '</td>';
            }else{
              echo '<td></td>';
            }
            echo '<td>' .$invoice_amount. '</td>';
            if(isset($staff_name)){
              echo '<td>' .$staff_name. '</td>';
            }else{
              echo '<td></td>';
            }
            echo "<td><a class='btn btn-default' href='invoice_finalize.php?in_no=$invoice_id' id = ''>Edit</a></td>";
           
        echo '</tr>';
        
    }

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

            <?php if($job_results > 0){  ?>
                <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Items</h4>
            </div>  
            <div class="panel-body">
            
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-search3">
                                    <thead>
                                        <tr>
                                        <th>
                                                   Edit / Delete Row
                                                </th>
                                                <th>
                                                Price List    
                                                </th>
                                                <th>
                                                Job Task     
                                                </th>
                                                <th>
                                                Reference Photo
                                                </th>
                                                <th>
                                                Category

                                                </th>
                                               
                                                <th>
                                                Description
                                                </th>
                                                <th>
Client Allowable Price                                                </th>
                                               
                                            <th>Minimum Bid Price</th>
                                            <th>Crew Pay</th>
                                            <th>UOM</th>
                                            <th>Photo Requirements</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
    
            while($row = mysqli_fetch_array($search_jobs_run)){  
                $maps_id = $row['maps_id'];
                $ref_photo = $row['maps_ref_pic'];
                $cat_name = $row['cat_name'];
                $client_id = $row['client_id'];
                $price_list = '';
                if(!isset($client_id)){
                    $client_id = 0;
                }
                if($client_id == 0){
                    $price_list = 'MAPS';
                }else{
                    $get_client_query = "SELECT * FROM client WHERE client_id = $client_id";
                    $get_client_run = mysqli_query($conn, $get_client_query);
                    $get_client_row = mysqli_fetch_array($get_client_run);
                    $price_list = $get_client_row['client_name'];
                }
                $maps_name = $row['maps_name'];
                $description = $row['maps_desc'];
                $client_allow_price = $row['maps_client_allow_price'];
                $min_bid_price = $row['maps_min_bid_price'];
                $crew_pay = $row['maps_crew_pay'];
                $photo_req = $row['maps_photo_req'];
                $maps_uom =$row['maps_crew_unit_uom_id'];
if($maps_uom == NULL){

}else{
    $query22 = "SELECT uom_name FROM uom WHERE uom_id = $maps_uom";
                
    $run22 = mysqli_query($conn, $query22);
if(mysqli_num_rows($run22)==1){
$row22 = mysqli_fetch_array($run22);

$maps_uom = $row22['uom_name'];
}
}        

                echo '<tr class="odd gradeX">';
                echo '<td><a href="MAPS-sheet.php?edit='.$maps_id.'&cl_id='.$client_id.'">Edit</a><br><a href="MAPS-sheet.php?delete='.$maps_id.'&cl_id='.$client_id.'">Delete</a></td>'; 
                echo '<td>' .$price_list. '</td>';
                echo '<td>' .$maps_name. '</td>';
                echo '<td><img src = "maps_ref_image.php?i='.$ref_photo.'" height = "80px" width = "120px">'. '</td>';
                echo '<td>' .$cat_name. '</td>';
             
                echo '<td>' .$description. '</td>';
                echo '<td>' .$client_allow_price. '</td>';
                echo '<td>' .$min_bid_price. '</td>';
                echo '<td>' .$crew_pay. '</td>';

                echo '<td>' .$maps_uom. '</td>';
                 echo '<td>' .$photo_req. '</td>';             
                echo '</tr>';
            }
        
?>
                                            
                                            </tbody>
                                    
                                    </table>
                                    
                                    </div><!-- panel-body  -->
                 </div> <!-- panel panel-default -->
            </div> <!--  col lg 12-->
        </div> <!-- row -->
            <?php }  ?>

            <?php if($inv_results > 0){  ?>
                <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
            <div class="panel-heading">
                <h4>Invoice</h4>
            </div> 
            <div class="panel-body">
            
                            <table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-bidwiz-search5">
                                    <thead>
                                        <tr>
                                        <th>
                                                  Date
                                                </th>
                                                <th>
                                                   Invoice#
                                                </th>
                                                <th>
                                                  Client Name
                                                </th>
                                                <th>
                                                    Case#                                               </th>
                                                <th>
                                                    Property                                               </th>
                                                    <th>
                                                    Work Order#                                               </th>
                                                    <th>
                                              Amount
                                                </th>
                                                <th>
                                              Staff
                                                </th>
                                                
                                                <th>
                                             Edit
                                                </th>
                                                
                                               
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
<?php

    while($row_bid_item = mysqli_fetch_array($search_invoices_run)){
        $invoice_id =  $row_bid_item['invoice_id'];
        $invoice_no = $row_bid_item['invoice_no'];
        $prop_id = $row_bid_item['prop_id'];
        $invoice_amount = $row_bid_item['invoice_amount'];
        $client_id = $row_bid_item['client_id'];
        $employee_id = $row_bid_item['employee_id'];
        $bid_wo_no = $row_bid_item['bid_wo_no'];
        $date = $row_bid_item['create_time'];
				$timestamp = strtotime($date);
				$date= date('m-d-Y', $timestamp);
        $query_maps = "SELECT * FROM client WHERE client_id = $client_id";
        $run_maps = mysqli_query($conn, $query_maps);
        $row_maps = mysqli_fetch_array($run_maps);
        $client_name = $row_maps['client_name'];
        
        if(isset($employee_id)){
          $query_emp = "SELECT * FROM employee WHERE employee_id = $employee_id";
          $run_emp = mysqli_query($conn, $query_emp);
          $row_emp = mysqli_fetch_array($run_emp);
          $staff_name = $row_emp['employee_fname'];
        }
        $query_prop = "SELECT * FROM property WHERE prop_id = $prop_id";
        $run_prop = mysqli_query($conn, $query_prop);
        $row_prop = mysqli_fetch_array($run_prop);
        $prop_case_num = $row_prop['prop_case_num'];
        $prop_full_add = $row_prop['prop_full_add'];
        $prop_county = $row_prop['prop_county'];
        $prop_state = $row_prop['prop_state'];
        $prop_city = $row_prop['prop_city'];
        $prop_zip = $row_prop['prop_zip'];
        
        $property_details =$prop_full_add ." ". $prop_county. " ". $prop_city. " ". $prop_state." ". $prop_zip;
        echo '<tr class="odd gradeX">';
        echo '<td>' .$date. '</td>';
            echo '<td>' .$invoice_no. '</td>';
            echo '<td>' .$client_name. '</td>';
            if(isset($prop_case_num)){
              echo '<td>' .$prop_case_num. '</td>';
            }else{
              echo '<td></td>';
            }
            echo '<td>' .$property_details. '</td>';
            if(isset($bid_wo_no)){
              echo '<td>' .$bid_wo_no. '</td>';
            }else{
              echo '<td></td>';
            }
            echo '<td>' .$invoice_amount. '</td>';
            if(isset($staff_name)){
              echo '<td>' .$staff_name. '</td>';
            }else{
              echo '<td></td>';
            }
            echo "<td><a class='btn btn-default' href='invoice_finalize.php?in_no=$invoice_id' id = ''>Edit</a></td>";
           
        echo '</tr>';
        
    }

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

            <?php }  ?>

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