<?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['maps']) && isset($_REQUEST['wo_no'])){

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['wo_no']) >0){
       
       $maps_id = $_REQUEST['maps'];      
       $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 = $_REQUEST['wo_no'];    
       
       $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;        

       }

       $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 = $row_bid_item_check['bid_item_amount'];
        $bid_item_total_prev = $row_bid_item_check['bid_item_total'];

        $bid_item_quantity_prev = $row_bid_item_check['bid_item_quantity'];
    
       }else{
        
        http_response_code(400);
           
            exit;
       }
       if($bid_item_quantity_prev == 1){
            $bid_amount_new = $bid_amount_prev - $bid_item_amount;
            $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_d = "DELETE FROM bid_item WHERE bid_id = $wo_no AND maps_id = $maps_id";

            $run_bid_qty_update_d = mysqli_query($conn, $query_bid_qty_update_d);

            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, 'bid_item_total' => 0.00, 'bid_item_quantity' => 0);
                echo json_encode($arr);
                http_response_code(200);
                exit;
            }  


        }

       $bid_item_quantity_new = $bid_item_quantity_prev - 1;
       $bid_item_total_new = $bid_item_total_prev - $bid_item_amount;
       $bid_item_total_new = number_format($bid_item_total_new, 2, '.', '');
       $bid_amount_new = $bid_amount_prev - $bid_item_amount;
       $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_quantity = $bid_item_quantity_new 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{
            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name - quantity reduced to $bid_item_quantity_new', type = 'bid', item_id = $wo_no, time = '$time'"; 

            $run_insert_log = mysqli_query($conn, $query_insert_log);
            $bid_amount_new = "$ ". $bid_amount_new;
            $arr = array('bid_amount' => $bid_amount_new, 'bid_item_total' => $bid_item_total_new, 'bid_item_quantity' => $bid_item_quantity_new);
            echo json_encode($arr);
            http_response_code(200);
            exit;
        }  
    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

