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

if(isset($_REQUEST['maps']) && isset($_REQUEST['wo_no']) && isset($_REQUEST['new_price'])){

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['wo_no'] && strlen($_REQUEST['new_price'])) >0){

       $maps_id = $_REQUEST['maps'];      
       $wo_no = $_REQUEST['wo_no'];        
       $bid_item_amount_new = $_REQUEST['new_price'];
       $bid_item_amount_new = number_format($bid_item_amount_new, 2, '.', '');

       if(is_numeric($bid_item_amount_new)){            
          $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']; 
                $work_order_number = $row_bid_check['bid_wo_no'];

                if($bid_amount_prev == NULL){
                    $bid_amount_prev = 0;
                }               
            }else{           
                http_response_code(400);                    
                exit;        
            }
            $query_bid_item_check = "SELECT * FROM bid_item WHERE bid_id = $wo_no AND maps_id = $maps_id AND is_delete IS NULL";

            $run_bid_item_check = mysqli_query($conn, $query_bid_item_check);

            if(mysqli_num_rows($run_bid_item_check) ==1){
                $row_bid_item_check = mysqli_fetch_array($run_bid_item_check);
                $bid_item_amount_prev = $row_bid_item_check['bid_item_amount'];
                if($bid_item_amount_prev  == NULL){
                    $bid_item_amount_prev  = 0;
                }
                $bid_item_total_prev = $row_bid_item_check['bid_item_total'];
                if($bid_item_total_prev == NULL){
                    $bid_item_total_prev = 0;
                }
                $bid_item_quantity = $row_bid_item_check['bid_item_quantity'];
                if($bid_item_quantity == NULL){
                    $bid_item_quantity = 1;
                }

            }else{
                http_response_code(400);                
                exit;
            }

            if($bid_item_amount_new == $bid_item_amount_prev){
                $bid_amount_prev = "$ ". $bid_amount_prev;
                $arr = array('bid_amount' => $bid_amount_prev, 'bid_item_total' => $bid_item_total_prev, 'price' => $bid_item_amount_new);


                echo json_encode($arr);
            
                http_response_code(200);
                exit;
            }

            $bid_item_total_new = $bid_item_amount_new * $bid_item_quantity;
            $bid_item_total_new = number_format($bid_item_total_new, 2, '.', '');

            $query_bid_item_update = "UPDATE bid_item SET bid_item_total = $bid_item_total_new, bid_item_amount = $bid_item_amount_new WHERE bid_id = $wo_no AND maps_id = $maps_id AND is_delete IS NULL";

            $run_bid_item_update = mysqli_query($conn, $query_bid_item_update);

            if(mysqli_affected_rows($conn) !=1){                
                http_response_code(400);                
                exit;
            }


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

            $query_bid_update = "UPDATE bid SET bid_amount = $bid_amount_new WHERE bid_id = $wo_no AND is_delete IS NULL";
            $run_bid_update = mysqli_query($conn, $query_bid_update);
            if(mysqli_affected_rows($conn) !=1){
               
                http_response_code(400);                    
                exit;        
            }
            $time = date("Y-m-d H:i:s");
            $bid_amount_new = "$ ". $bid_amount_new;
            $arr = array('bid_amount' => $bid_amount_new, 'bid_item_total' => $bid_item_total_new, 'price' => $bid_item_amount_new);
            if($user_type == 'staff'){
                $query_fetch_maps_name = "SELECT * FROM maps WHERE maps_id = $maps_id";
                $run_fetch_maps_name = mysqli_query($conn, $query_fetch_maps_name);
                $row_fetch_maps_name = mysqli_fetch_array($run_fetch_maps_name);
                $item_name = $row_fetch_maps_name['maps_name'];
                $time = date("Y-m-d H:i:s");
            
                $alert_string = "has changed price of $item_name to $$bid_item_amount_new in Bid# $work_order_number.";    
                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL,admin_alert_time = '$time', admin_alert_link = 'estimate_finalize.php?wo_no=$wo_no',employee_id= $employee_id, admin_alert_activity = '$alert_string'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);

            }

            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Changed price of $item_name to $$bid_item_amount_new', 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;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

