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

if(isset($_REQUEST['delete'])){
  if(strlen($_REQUEST['delete'])> 0){
     $client_id = $_REQUEST['delete'];
     $query_client_insert = "UPDATE bid SET $active_user_id is_delete = 1 WHERE bid_id = $client_id";  
  
     $run_client_insert = mysqli_query($conn, $query_client_insert);
     if(mysqli_affected_rows($conn) > 0){
         $successmessage = "Bid has been deleted.";   
     }else{
         $message = "Something went wrong. Try Again";
     }      
     

  }
}

include "header.php";
?>
<div id="page-wrapper">
<div id="page-wrapper">

<h3>Incomplete Bids<a class='btn btn-warning' style = 'float:right;' href='index.php' id = ''>Cancel</a></h3>

<?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>';
                    }
                }
                    ?> 
                    <style>
   th { font-size: 12px; padding:0px;}
td { font-size: 12px;padding:0px; }
   </style>  
<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-bidwiz12333">
                                    <thead>
                                        <tr><th>
                                                  Date
                                                </th>
                                                <th>
                                                   WO#
                                                </th>
                                                <th>
                                                  Client Name
                                                </th>
                                                <th>
                                                    Case#                                               </th>
                                                    <th>
                                                    Property                                               </th>
                                                    <th>
                                                City
                                                 </th> 
                                                 <th>
                                                State
                                                 </th> 
                                                 <th>
                                                Zip
                                                 </th>        
                                                <th>
                                               County
                                                </th>
                                                    <th>
                                              Amount
                                                </th>
                                                <th>
                                              Staff
                                                </th>
                                                <th>
                                              Delete
                                                </th>
                                                <th>
                                              Complete
                                                </th>
                                              
                                               
                                            </tr>
                                        </thead>
                                        <tbody>
<?php
$time = date('Y-m-d H:i:s', strtotime('-7 days'));

$query_bid_item12 = "UPDATE bid SET is_delete = 1 WHERE bid_type = 1 AND approve_time < '$time'";


$run_bid_item12 = mysqli_query($conn, $query_bid_item12); 

$query_bid_item = "SELECT * FROM bid WHERE bid_type = 1 AND is_delete IS NULL ORDER BY create_time DESC";
$run_bid_item = mysqli_query($conn, $query_bid_item);
if(mysqli_num_rows($run_bid_item) >0){
    

    while($row_bid_item = mysqli_fetch_array($run_bid_item)){
        $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'];
        $emp_id = $row_bid_item['employee_id'];
        $adm_id = $row_bid_item['admin_id'];
        $staff_name = '';
        if(isset($adm_id)){
          if(strlen($adm_id)>0){
          $query_adm = "SELECT * FROM admin WHERE admin_id = $adm_id";
          $run_adm = mysqli_query($conn, $query_adm);
          $row_adm = mysqli_fetch_array($run_adm);
          $staff_name = $row_adm['admin_fname']. ' ' . $row_adm['admin_lname'];
          }
      }
        if(isset($emp_id)){
          if(strlen($emp_id)>0){
          $query_emp = "SELECT * FROM employee WHERE employee_id = $emp_id";
          $run_emp = mysqli_query($conn, $query_emp);
          $row_emp = mysqli_fetch_array($run_emp);
          $staff_name = $row_emp['employee_fname'] . ' ' . $row_emp['employee_lname'];
          }
      }
        $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'];

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

        $query_bid_1item = "SELECT * FROM bid_item WHERE bid_id = $wo_no AND is_delete IS NULL";
    $run_bid_1item = mysqli_query($conn, $query_bid_1item);
    if(mysqli_num_rows($run_bid_1item) >0){
       
        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>' .$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>' .$bid_amount. '</td>';
            echo '<td>';
              if(isset($staff_name)){
                echo $staff_name;
              }
            if($user_type == 'admin'){  
              echo '<br>';
              echo "<a href='item_change_log.php?item_no=$wo_no&type=bid'>Change History</a>";
            }
            echo '</td>';
            echo "<td><a class='btn btn-danger tbl_e_narrow' href='incomplete-bids.php?delete=$wo_no' id = ''>Delete</a></td>";
            echo "<td><a class='btn btn-success tbl_e_narrow' href='add-items.php?wo_no=$wo_no&state=1&bid_wo_no=$bid_wo_no&client_id=$client_id' id = ''>Continue</a></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"; ?>