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

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['cl_id']) >0 && strlen($_REQUEST['state']) >0 && strlen($_REQUEST['wo_no']) >0 && strlen($_REQUEST['bid_wo_no']) >0){

        $maps_added = $_REQUEST['maps'];
        $cl_id = $_REQUEST['cl_id'];
        $wo_no = $_REQUEST['wo_no'];
        $bid_wo_no = $_REQUEST['bid_wo_no'];
        $state = $_REQUEST['state'];
        $suc_count = 0;

        $query_check_bid = "SELECT * FROM bid WHERE bid_id = $wo_no AND client_id = $cl_id AND bid_type = $state AND bid_wo_no = '$bid_wo_no'";

        $run_check_bid = mysqli_query($conn, $query_check_bid);

        if(mysqli_num_rows($run_check_bid) !=1){
            http_response_code(400);
            exit;
        }
        $row_check_bid = mysqli_fetch_array($run_check_bid);
        $prop_id = $row_check_bid['prop_id'];
        $bid_amount_prev = $row_check_bid['bid_amount'];
        $bid_amount_prev = number_format($bid_amount_prev, 2, '.', '');
        $maps_arr = array();
        $query_fetch_maps_added = "SELECT * FROM maps WHERE maps_id = $maps_added";
        $run_fetch_maps_added = mysqli_query($conn, $query_fetch_maps_added);

        if(mysqli_num_rows($run_fetch_maps_added) ==1){
            array_push($maps_arr, $maps_added); 
            $check_maps_group_query = "SELECT * FROM maps_group WHERE parent_id = $maps_added AND is_delete IS NULL AND is_approve = 1";   
            $check_maps_group_run = mysqli_query($conn, $check_maps_group_query); 
            if(mysqli_num_rows($check_maps_group_run) > 0){
                while($check_maps_group_row = mysqli_fetch_array($check_maps_group_run)){
                    $sub_id = $check_maps_group_row['sub_id'];
                    array_push($maps_arr, $sub_id);
                }
            }                   
        }      
        
        $item_count = count($maps_arr);
        foreach($maps_arr as $maps){  
            $query_fetch_maps_amount = "SELECT * FROM maps WHERE maps_id = $maps";
            $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'];
                        $bid_item_amount = $row_fetch_maps_amount['maps_min_bid_price'];
                        $bid_item_uom = $row_fetch_maps_amount['maps_crew_unit_uom_id'];
                        if(!isset($bid_item_uom) || strlen($bid_item_uom) == 0){
                            $bid_item_uom = 0;
                        }       
                        if(!isset($bid_item_amount)){
                        $bid_item_amount = 0.00;

                        }
                        if($bid_item_amount == NULL){
                            $bid_item_amount = 0.00;  
                        }
                        $bid_item_amount = number_format($bid_item_amount, 2, '.', '');
                    }   
                    
            }else{
                continue;           
            } 
            if(isset($bid_item_amount)){
                    $time = date("Y-m-d H:i:s");
                    $query_check2_bid_item = "SELECT * FROM bid_item WHERE bid_id = $wo_no AND maps_id = $maps AND is_delete IS NULL"; 
                    $run_check2_bid_item = mysqli_query($conn, $query_check2_bid_item);
                    if(mysqli_num_rows($run_check2_bid_item)>0){
                        continue;               
                    }
                    $bid_amount_new = $bid_amount_prev + $bid_item_amount;
                    $bid_amount_new = number_format($bid_amount_new, 2, '.', '');
                    $qty = 1;
                    if($bid_item_uom == 1){
                        $qty = 0;
                        $bid_item_amount = 0;
                    }

                    $query_insert_bid_item = "INSERT INTO bid_item SET bid_id = $wo_no, maps_id = $maps, bid_item_amount = $bid_item_amount, bid_item_uom = $bid_item_uom, bid_item_quantity = $qty, bid_item_total = $bid_item_amount, create_time = '$time'"; 

                    $run_insert_bid_item = mysqli_query($conn, $query_insert_bid_item);
                    $bid_item_id_u = mysqli_insert_id($conn);
                    if(mysqli_affected_rows($conn) > 0){  
                        $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added item $invoice_maps_name', type = 'bid', item_id = $wo_no, time = '$time'"; 

                        $run_insert_log = mysqli_query($conn, $query_insert_log);
                        if($bid_amount_new != $bid_amount_prev){    
                            $query_update_bid = "UPDATE bid SET bid_amount = $bid_amount_new WHERE bid_id = $wo_no AND client_id = $cl_id AND bid_type = $state AND bid_wo_no = '$bid_wo_no'"; 
                            $run_update_bid = mysqli_query($conn, $query_update_bid);
                            if(mysqli_affected_rows($conn) > 0){         
                                $suc_count++;
                                if($item_count > 1){
                                    $bid_amount_prev = $bid_amount_new;
                                }
                            }
                        }else{
                            $suc_count++;
                        }

                    }else{ 
                        continue;
                    }

                    if($bid_item_uom == 4){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }

                        }
                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        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'];
                                
                                $query_insert_bid_item_uom_value = "INSERT INTO bid_item_uom_value SET bid_item_id = $bid_item_id_u, bid_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);

                            }
                                    
                        } 
                        
                        if($p_lot_size_set == 'yes'){
                            $query_uom_field2 = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom AND uom_field_type = 'bidin' AND uom_field_name = 'bidin_uom_val1'"; 
                            $run_uom_field2 = mysqli_query($conn, $query_uom_field2);

                            if(mysqli_num_rows($run_uom_field2) == 1){
                                $row_uom_field2 = mysqli_fetch_array($run_uom_field2);
                                $uom2_field_id = $row_uom_field2['uom_field_id'];

                                $update_uom2_query = "UPDATE bid_item_uom_value SET 	bid_item_uom_value_amount = '$p_lot_size' WHERE bid_item_id = $bid_item_id_u AND bid_item_uom_field_id = $uom2_field_id";
                                $update_uom2_run = mysqli_query($conn, $update_uom2_query);

                            }else{
                                continue;
                            }
                        }
                        
                    }

                    if($bid_item_uom == 1 || $bid_item_uom == 5 || $bid_item_uom == 6 || $bid_item_uom == 7 || $bid_item_uom == 8){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }
                        }

                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        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'];
                                
                                $query_insert_bid_item_uom_value = "INSERT INTO bid_item_uom_value SET bid_item_id = $bid_item_id_u, bid_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);

                            }
                                    
                        } 
                        
                        $query_fetch_maps_uom_field = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom 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";
                
                            $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;  
                            }
                
                        }
                       
                        $query_insert_bid_item = "UPDATE bid_item SET bid_item_amount = $maps_uom_val1, bid_item_total = 0.00 WHERE bid_item_id = $bid_item_id_u"; 

                        $run_insert_bid_item = mysqli_query($conn, $query_insert_bid_item);
                        $bid_amount_new_adjust = $bid_amount_new - $bid_item_amount;
                        $query_update_bid = "UPDATE bid SET bid_amount = $bid_amount_new_adjust WHERE bid_id = $wo_no AND client_id = $cl_id AND bid_type = $state AND bid_wo_no = '$bid_wo_no'"; 
                        $run_update_bid = mysqli_query($conn, $query_update_bid);
                    }


                    if($bid_item_uom == 2){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }

                        }

                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        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'];
                                $query_insert_bid_item_uom_value = "INSERT INTO bid_item_uom_value SET bid_item_id = $bid_item_id_u, bid_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);
                            }                                 
                        }
                        if($p_lot_size_set == 'yes'){
                            $query_uom_field2 = "SELECT * FROM uom_field WHERE uom_id = $bid_item_uom AND uom_field_type = 'bidin' AND uom_field_name = 'bidin_uom_val1'"; 
                            $run_uom_field2 = mysqli_query($conn, $query_uom_field2);

                            if(mysqli_num_rows($run_uom_field2) == 1){
                                $row_uom_field2 = mysqli_fetch_array($run_uom_field2);
                                $uom2_field_id = $row_uom_field2['uom_field_id'];

                                $update_uom2_query = "UPDATE bid_item_uom_value SET 	bid_item_uom_value_amount = '$p_lot_size' WHERE bid_item_id = $bid_item_id_u AND bid_item_uom_field_id = $uom2_field_id";
                                $update_uom2_run = mysqli_query($conn, $update_uom2_query);

                            }else{
                                continue;
                            }
                        }        
                    }
                }else{
                    continue; 
                }
        } 
        if($suc_count > 0){
            http_response_code(200);
            exit;
        }else{
            http_response_code(400);
            exit;            
        }    
    }else{
        http_response_code(400);
        exit;
    }    
}else{   
    http_response_code(400);
    exit;
}
?>

