<?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";
     }      
     

  }
}
$cl_set = '';
$client_search = '';
         
$cl_id = 0;
if($user_type == 'admin'){   
  if(isset($_REQUEST['cl_id'])){
    if(strlen($_REQUEST['cl_id']) > 0){
      $cl_id = $_REQUEST['cl_id'];
      $query_fetch_client = "SELECT * FROM client WHERE client_is_delete IS NULL AND is_approve = 1 AND client_id = $cl_id";
      $run_fetch_client = mysqli_query($conn, $query_fetch_client);
      if(mysqli_num_rows($run_fetch_client)>0){
          while($row_fetch_client = mysqli_fetch_array($run_fetch_client)){
              $client_name = $row_fetch_client['client_name'];
              if(strlen($client_name) > 0){
                $cl_set = 'set';
                $client_search = "AND client_id = $cl_id";
              }
          }
      }
    }
  }
  $current_year = date('Y', time());
  $year_selected = $current_year;
  $option_selected = 'y';
  $value_selected =  'none';
  $select_string = "$year_selected";
  if(isset($_REQUEST['year']) && isset($_REQUEST['option']) && isset($_REQUEST['value'])){
    if(strlen($_REQUEST['year']) > 0 && strlen($_REQUEST['option']) > 0 && strlen($_REQUEST['value']) > 0){
      if($_REQUEST['year'] > 1990 && $_REQUEST['year'] < 2100){
        $year_selected =  $_REQUEST['year'];
      }
      if($_REQUEST['option'] == 'y' || $_REQUEST['option'] == 'q' || $_REQUEST['option'] == 'm'){
          $option_selected =  $_REQUEST['option'];
          if($option_selected == 'm'){
              if($_REQUEST['value'] >=0 && $_REQUEST['value'] <=11){
                $value_selected =  $_REQUEST['value'];
              }else{
                $value_selected = 0;
              }

              $m_names = ['January', 'February', 'March', 
                  'April', 'May', 'June', 'July', 
                  'August', 'September', 'October', 'November', 'December'];
                    
              $dash_month = $m_names[$value_selected];
              $select_string = "$dash_month $year_selected";
              $value_selected = $value_selected + 1;
          }
          if($option_selected == 'q'){
              if($_REQUEST['value'] >= 1 && $_REQUEST['value'] <= 4){
                $value_selected = $_REQUEST['value'];
              }else{
                $value_selected = 1;
              }
              $select_string = "Quarter $value_selected $year_selected";
          }
          if($option_selected == 'y'){
            
            $select_string = "$year_selected";
        }
      }

    }
  }
}
$month_timestamp = strtotime("-1 month", time());
$month_time = date("Y-m-d h:i:s", $month_timestamp);

$amount = 0;

if($option_selected == 'y'){
  $estimate_query_unsettled = "AND YEAR(create_time) = $year_selected";
}elseif($option_selected == 'm'){
  $estimate_query_unsettled = "AND YEAR(create_time) = $year_selected AND MONTH(create_time) = $value_selected";
  $value_selected = $value_selected - 1; 
}elseif($option_selected == 'q'){
  $estimate_query_unsettled = "AND YEAR(create_time) = $year_selected AND QUARTER(create_time) = $value_selected";
}else{
  $estimate_query_unsettled = "AND YEAR(create_time) = $year_selected";
}
if($user_type == 'admin'){   
  $query_bid_item_total = "SELECT * FROM bid WHERE bid_type = 2 AND is_approve = 1 $estimate_query_unsettled AND is_delete IS NULL $client_search ORDER BY create_time DESC";
  $run_bid_item_total1 = mysqli_query($conn, $query_bid_item_total);
  if(mysqli_num_rows($run_bid_item_total1) >0){
    while($row_bid_item_total = mysqli_fetch_array($run_bid_item_total1)){
      $row_bid_amount = $row_bid_item_total['bid_amount'];
      $amount = $amount + $row_bid_amount;
    }
  }
}
include "header.php";

?>
<style>
body {
  padding-top: 50px;
}

/*modal fullscreen */


.modal.modal-fullscreen .modal-dialog,
.modal.modal-fullscreen .modal-content {
  bottom: 0;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
}
.modal.modal-fullscreen .modal-dialog {
  margin: 0;
  width: 100%;
  animation-duration:0.6s;
}
.modal.modal-fullscreen .modal-content {
  border: none;
  -moz-border-radius: 0;
  border-radius: 0;
  -webkit-box-shadow: inherit;
  -moz-box-shadow: inherit;
  -o-box-shadow: inherit;
  box-shadow: inherit;
  /* change bg color below */
 /* background:#1abc9c; */
}

.modal.modal-fullscreen.force-fullscreen .modal-body {
  padding: 0;
}
.modal.modal-fullscreen.force-fullscreen .modal-header,
.modal.modal-fullscreen.force-fullscreen .modal-footer {
  position: relative;
  bottom: 0;
}
.modal.modal-fullscreen.force-fullscreen .modal-header {
  top: 0;
}

.modal-body {
  height: 100%;
}

.modal-footer {
  width: 100%;
  position: relative;
  bottom: 0;
  background-color: #FFF;
}
.autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; }
.autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; overflow: hidden; }
.autocomplete-selected { background: #F0F0F0; }
.autocomplete-suggestions strong { font-weight: normal; color: #3399FF; }
.autocomplete-group { padding: 2px 5px; }
.autocomplete-group strong { display: block; border-bottom: 1px solid #000; }
                    </style>
<div id="page-wrapper">
<div style = 'float:right;'>
<?php          
	if($user_type == 'admin'){     
?>
<button style='padding:2px;font-size:14px;margin-bottom:0px;margin-right:3px;' class= 'btn btn-success' id = 'addBidModal'>Import Bid Excel</button>
<?php          
  }     
?>
<a class='btn btn-warning' href='index.php' id = ''>Cancel</a></div>
<h3><?php if(strlen($cl_set) == 0){?>
  <?php echo $select_string; ?> Bids List
<?php }elseif($cl_set == 'set'){?>  
  <?php echo $select_string; ?> Bid list - <?php echo $client_name; ?>
<?php } ?></h3>

<?php          
	if($user_type == 'admin'){     
?>
<div class="row">
  
  <div class="col-md-3">

            <input type="text" class='form-control' name='new_bid_client_name1' id="new_bid_client_name1" autofocus required='required' placeholder="Choose Client to show Bid list">

            <input type="hidden" class='form-control' name='new_bid_client_name' id='new_bid_client_name'>
  </div>
  <div class="form-group col-md-1">
<select id = "year-selected" class = "form-control select_dash" name="year" onchange = "year_change()">    
<?php 
$prev_year = $current_year - 1;?>
<option class = "option_dash" value="<?php echo $current_year; ?>" <?php 
    if($current_year == $year_selected){
      echo 'selected';
    }
?>><?php echo $current_year; ?></option>
<?php
for($i = $prev_year; $i >= 2019; $i--){
?>
<option class = "option_dash" value="<?php echo $i; ?>"  <?php 
    if($i == $year_selected){
      echo 'selected';
    }
?>><?php echo $i; ?></option>
<?php
}
?>      

    </select>
    </div>
    <div class="form-group col-md-1">
    <select id = "filter-selected" class = "form-control  select_dash" name="year" onchange = "select_filter()">
    <option class = "option_dash" value="y" <?php if($option_selected == 'y'){echo 'selected';} ?>>Year</option>
    <option class = "option_dash" value="m" <?php if($option_selected == 'm'){echo 'selected';} ?>>Month</option>
    <option class = "option_dash" value="q" <?php if($option_selected == 'q'){echo 'selected';} ?>>Quarter</option>    
    </select>
    </div>
    <div class="form-group col-md-2">
    <?php if($option_selected == 'y'){ ?>  
    <select id = "value-selected" class = "form-control  select_dash" name="year" onchange = "set_new_values()" style = 'display:none'>
      
        </select>
    <?php }elseif($option_selected == 'm'){ ?>
      <select id = "value-selected" class = "form-control  select_dash" name="year" onchange = "set_new_values()">
      <option <?php if($value_selected == 0){echo 'selected = "true"';} ?> value="0">January</option>
      <option <?php if($value_selected == 1){echo 'selected = "true"';} ?> value="1">February</option>
      <option <?php if($value_selected == 2){echo 'selected = "true"';} ?> value="2">March</option>
      <option <?php if($value_selected == 3){echo 'selected = "true"';} ?> value="3">April</option>
      <option <?php if($value_selected == 4){echo 'selected = "true"';} ?> value="4">May</option>
      <option <?php if($value_selected == 5){echo 'selected = "true"';} ?> value="5">June</option>
      <option <?php if($value_selected == 6){echo 'selected = "true"';} ?> value="6">July</option>
      <option <?php if($value_selected == 7){echo 'selected = "true"';} ?> value="7">August</option>
      <option <?php if($value_selected == 8){echo 'selected = "true"';} ?> value="8">September</option>
      <option <?php if($value_selected == 9){echo 'selected = "true"';} ?> value="9">October</option>
      <option <?php if($value_selected == 10){echo 'selected = "true"';} ?> value="10">November</option>
      <option <?php if($value_selected == 11){echo 'selected = "true"';} ?> value="11">December</option>      
      </select>
      <?php }elseif($option_selected == 'q'){ ?>
        <select id = "value-selected" class = "form-control  select_dash" name="year" onchange = "set_new_values()" <?php if($value_selected == 'none'){echo 'style = "display:none"';} ?>>
        <option value="1" <?php if($value_selected == 1){echo 'selected = "true"';} ?>>Quarter 1</option>   
        <option value="2" <?php if($value_selected == 2){echo 'selected = "true"';} ?>>Quarter 2</option>   
        <option value="3" <?php if($value_selected == 3){echo 'selected = "true"';} ?>>Quarter 3</option>
        <option value="4" <?php if($value_selected == 4){echo 'selected = "true"';} ?>>Quarter 4</option>
      </select>
        <?php } ?>
    </div>
  <div class="col-md-5">
  </div>
 
</div><br>
<div class="form-group" style = 'font-size:16px'>
  Grand Total:
<b><?php echo $amount;?></b>
  </div> 

  
<?php          
	}   
?>

<?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>';
                    }
                }
                    ?>   
<!-- Modal -->
<div id="bidModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Upload Bid Excel</h4>
      </div>
      <div class="modal-body">
      <div class="alert alert-danger" role="alert" id = 'vendor-alert' style='display:none'>
    <p id='danger_text'></p> 
 </div>
 <div class="alert alert-info" role="alert" id = 'vendor-alert-warning' style='display:none'>
    <p id='danger_text_warning'>Excel is uploading.</p> 
 </div>

<div class="alert alert-success" role="alert" id = 'vendor-alert-success' style='display:none'>
 <p id='danger_text1'>
</p> </div>
<form id="bid_item_excel" enctype="multipart/form-data">
<div class = "row">
        
<div class = "form-group col-lg-6">
        <label for="fileToUpload_bid">Choose Excel to upload</label>
            <input type="file" class="upload-file" name="fileToUpload_bid" id="fileToUpload_bid">
        </div>
        <div class = "form-group col-lg-6">
            <a href="excels/bid.xlsx" download class="btn btn-danger" style = 'float:right;'>
              Bid Excel Format
            </a>
        </div>
        </div>
    </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>

  </div>
</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-bidwiz-view-bid">
                                    <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>
                                             Edit
                                                </th>
                                               
                                                <th>
                                              Download
                                                </th>
                                               
                                            </tr>
                                        </thead>
                                        <tbody>

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

</div> <!--  page wrapper -->

<?php include "footer3.php"; ?>
<script>
$('head').append('<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">');

</script>
</body>
</html>

