<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}elseif($user_type == 'staff'){
    $active_user_id = "employee_id = $employee_id,";
}
$time = date("Y-m-d H:i:s");

if(isset($_REQUEST['bid_i']) && isset($_REQUEST['uom_type']) && isset($_REQUEST['lot_size']) && isset($_REQUEST['grass_height'])){

    if(strlen($_REQUEST['bid_i']) >0 && strlen($_REQUEST['uom_type']) && strlen($_REQUEST['lot_size']) >0 && strlen($_REQUEST['grass_height']) >0){

       $bid_i = $_REQUEST['bid_i'];      
       $uom_type = $_REQUEST['uom_type'];        
       $lot_size = $_REQUEST['lot_size'];
       $grass_height = $_REQUEST['grass_height'];       
       
       $query_fetch_maps_id = "SELECT * FROM bid_item WHERE bid_item_id = $bid_i AND is_delete IS NULL";
       
       $run_fetch_maps_id = mysqli_query($conn, $query_fetch_maps_id);      

       if(mysqli_num_rows($run_fetch_maps_id) ==1){
        $row_bid_item_check = mysqli_fetch_array($run_fetch_maps_id);
        $bid_item_amount = $row_bid_item_check['bid_item_amount']; 
        $bid_item_total_prev = $row_bid_item_check['bid_item_total']; 
        $maps_id = $row_bid_item_check['maps_id'];
        $query_fetch_maps_amount = "SELECT * FROM maps WHERE maps_id = $maps_id";
        $run_fetch_maps_amount = mysqli_query($conn, $query_fetch_maps_amount);

        if(mysqli_num_rows($run_fetch_maps_amount) ==1){
            while($row_fetch_maps_amount = mysqli_fetch_array($run_fetch_maps_amount)){
                $invoice_maps_name = $row_fetch_maps_amount['maps_name'];
            }   
        }   
        $wo_no = $row_bid_item_check['bid_id'];
  
       }else{
        
        http_response_code(400);
           
        exit;
       }
       $query_bid_check = "SELECT * FROM bid WHERE bid_id = $wo_no AND is_delete IS NULL";

       $run_bid_check = mysqli_query($conn, $query_bid_check);

       if(mysqli_num_rows($run_bid_check) ==1){
            $row_bid_check = mysqli_fetch_array($run_bid_check);

            $bid_amount_prev = $row_bid_check['bid_amount']; 
           
       }else{
       
        http_response_code(400);
           
        exit;        

       }
        

       include "include/uomFormulaLib.php";

       if($uom_type == 2){

       $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $uom_type AND uom_field_type = 'bidin'"; 

       $run_uom_field = mysqli_query($conn, $query_uom_field);
       $checker = 0;
       if(mysqli_num_rows($run_uom_field)>0){
           while($row_uom_field = mysqli_fetch_array($run_uom_field)){
              $uom_field_id = $row_uom_field['uom_field_id'];
              $uom_field_name = $row_uom_field['uom_field_name'];              
               if($uom_field_name == 'bidin_uom_val1'){
                    $query_select_bid_item_uom_value = "SELECT * FROM bid_item_uom_value WHERE bid_item_id = $bid_i AND bid_item_uom_field_id = $uom_field_id"; 

                    $run_select_bid_item_uom_value = mysqli_query($conn, $query_select_bid_item_uom_value);

                    $row_select_bid_item_uom_value = mysqli_fetch_array($run_select_bid_item_uom_value);

                    $bid_item_uom_value = $row_select_bid_item_uom_value['bid_item_uom_value_amount'];
                    if($lot_size == $bid_item_uom_value){
                        $checker = $checker + 1;
                    }
                    $query_insert_bid_item_uom_value = "UPDATE bid_item_uom_value SET 	bid_item_uom_value_amount = $lot_size WHERE bid_item_id = $bid_i AND bid_item_uom_field_id = $uom_field_id"; 
                    $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);                    
               }

               if($uom_field_name == 'bidin_uom_val2'){ 
                    $query_select_bid_item_uom_value = "SELECT * FROM bid_item_uom_value WHERE bid_item_id = $bid_i AND bid_item_uom_field_id = $uom_field_id"; 

                    $run_select_bid_item_uom_value = mysqli_query($conn, $query_select_bid_item_uom_value);

                    $row_select_bid_item_uom_value = mysqli_fetch_array($run_select_bid_item_uom_value);

                    $bid_item_uom_value = $row_select_bid_item_uom_value['bid_item_uom_value_amount'];
                    if($grass_height == $bid_item_uom_value){
                        $checker = $checker + 1;
                    }

                    $query_insert_bid_item_uom_value = "UPDATE bid_item_uom_value SET 	bid_item_uom_value_amount = $grass_height WHERE bid_item_id = $bid_i AND bid_item_uom_field_id = $uom_field_id";
                    $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);
               }    
              

           }
                         
       }
       if($checker == 2){
           echo "same";
        http_response_code(200);
        exit;
       }
        $formulaType ="GF";

        $bidin_uom_val1 = $lot_size;
        $bidin_uom_val2 = $grass_height;
        

        $query_fetch_maps_uom_field = "SELECT * FROM uom_field WHERE uom_id = $uom_type AND uom_field_type = 'maps'"; 
        $run_fetch_maps_uom_field = mysqli_query($conn, $query_fetch_maps_uom_field);

        while($row_fetch_maps_uom_field = mysqli_fetch_array($run_fetch_maps_uom_field)){
            $uom_field_name = $row_fetch_maps_uom_field['uom_field_name'];

            $uom_field_id = $row_fetch_maps_uom_field['uom_field_id'];

            $query_fetch_maps_uom_value = "SELECT * FROM maps_uom_value WHERE uom_field_id = $uom_field_id AND maps_id = $maps_id";

            $run_fetch_maps_uom_value = mysqli_query($conn, $query_fetch_maps_uom_value);

            $row_fetch_maps_uom_value = mysqli_fetch_array($run_fetch_maps_uom_value);

            $maps_uom_value_amount = $row_fetch_maps_uom_value['maps_uom_value_amount'];

            if($uom_field_name == 'maps_uom_val1'){
                $maps_uom_val1 = $maps_uom_value_amount; //fixed base price  
            }elseif($uom_field_name == 'maps_uom_val2'){
                $maps_uom_val2 = $maps_uom_value_amount;  // certain grass height
            }elseif($uom_field_name == 'maps_uom_val3'){
                $maps_uom_val3 = $maps_uom_value_amount;  // fixed price over a certain grass height
            }elseif($uom_field_name == 'maps_uom_val4'){
                $maps_uom_val4 = $maps_uom_value_amount;  //equipment costs
            }

        }

    }

    if($formulaType = "GF"){    
        $gc_formula_res = gcFormula($bidin_uom_val1, $bidin_uom_val2, $maps_uom_val1, $maps_uom_val2, $maps_uom_val3, $maps_uom_val4);  

        $bid_item_total_new = $gc_formula_res['bid_item_total'];
        $bid_item_gc_total = $gc_formula_res['gc_cost'];  
        $bid_item_gc_total = number_format($bid_item_gc_total, 2, '.', '');      
    }

     /*  */ 
     
     $bid_item_total_new = number_format($bid_item_total_new, 2, '.', '');
     $bid_amount_temp = $bid_amount_prev - $bid_item_total_prev;
     $bid_amount_new = $bid_amount_temp + $bid_item_total_new;
     $bid_amount_new = number_format($bid_amount_new, 2, '.', '');

     if($bid_amount_prev != $bid_amount_new){
          $query_bid_amount_update = "UPDATE bid SET bid_amount = $bid_amount_new WHERE bid_id = $wo_no";

          $run_bid_amount_update = mysqli_query($conn, $query_bid_amount_update);
  
              if(mysqli_affected_rows($conn) !=1){
                  http_response_code(400);                       
                  exit;
              }
     }
   
     $query_bid_qty_update = "UPDATE bid_item SET bid_item_total = $bid_item_total_new, bid_item_amount = $bid_item_gc_total WHERE bid_id = $wo_no AND maps_id = $maps_id";

      $run_bid_qty_update = mysqli_query($conn, $query_bid_qty_update);

      if(mysqli_affected_rows($conn) !=1){
              http_response_code(400);
              exit;
      }else{
          $bid_amount_new = "$ ". $bid_amount_new;
          $arr = array('bid_amount' => $bid_amount_new, 'm_id' => $maps_id);
          $arr =array_merge($arr, $gc_formula_res);
          $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name- GC dimensions changed to Grass Height- $grass_height, Lot Size- $lot_size', type = 'bid', item_id = $wo_no, time = '$time'"; 
          $run_insert_log = mysqli_query($conn, $query_insert_log);
          echo json_encode($arr);
          http_response_code(200);
          exit;
      }
       
    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

