<?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_id']) && isset($_REQUEST['check']) && isset($_REQUEST['vendor_id'])){
    if(strlen($_REQUEST['bid_id']) >0 && strlen($_REQUEST['check']) >0 && strlen($_REQUEST['vendor_id']) >0){
        $bid_id = $_REQUEST['bid_id'];
        $bid_item_vendor_id = $_REQUEST['vendor_id'];

        $set_vendor_name_queryx = "SELECT * FROM bid_item WHERE bid_item_id = $bid_item_vendor_id AND is_delete IS NULL";
        $set_vendor_name_runx = mysqli_query($conn, $set_vendor_name_queryx);
        if(mysqli_num_rows($set_vendor_name_runx) == 1){
            $set_vendor_name_rowx = mysqli_fetch_array($set_vendor_name_runx);
            $vendor_id = $set_vendor_name_rowx['vendor_id']; 
        }

        $check = $_REQUEST['check'];   
        
        $count_rem = 0;
        $count_add = 0;
        $fetch_bid_item_query = "SELECT * FROM bid_item WHERE bid_id = $bid_id AND is_delete IS NULL";
        $fetch_bid_item_run = mysqli_query($conn, $fetch_bid_item_query);
        if(mysqli_num_rows($fetch_bid_item_run) > 0){
            $number_items = mysqli_num_rows($fetch_bid_item_run);
            
            while($fetch_bid_item_row = mysqli_fetch_array($fetch_bid_item_run)){
                $bid_item_id = $fetch_bid_item_row['bid_item_id'];
                if($check == 'yes'){
                    if(isset($vendor_id)){
                        if(strlen($vendor_id) > 0){
                            $fetch_vendor_query = "SELECT * FROM vendor WHERE vendor_id = $vendor_id AND vendor_is_delete IS NULL";
                            $fetch_vendor_run = mysqli_query($conn, $fetch_vendor_query);
                            if(mysqli_num_rows($fetch_vendor_run) == 1){
                                $fetch_vendor_row = mysqli_fetch_array($fetch_vendor_run);
                                $vendor_name = $fetch_vendor_row['vendor_name'];
                                if(isset($vendor_name)){
                                    if(strlen($vendor_name) > 0){
                                        $set_vendor_name_query = "UPDATE bid_item SET vendor_id = $vendor_id WHERE bid_item_id = $bid_item_id AND is_delete IS NULL";
                                        $set_vendor_name_run = mysqli_query($conn, $set_vendor_name_query);
                                        if($set_vendor_name_run){
                                            $count_add++;
                                           
                                        }

                                        if($count_add == $number_items){
                                            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added $vendor_name to all items.', type = 'bid', item_id = $bid_id, time = '$time'"; 
            
                                            $run_insert_log = mysqli_query($conn, $query_insert_log);
                                            echo $vendor_name;
                                            http_response_code(200);
                                            exit;
                                        }
                                    }
                                }
                               
                            }
                        }
                    }  
                        
                }elseif($check == 'no'){
                    $set_vendor_name_query = "UPDATE bid_item SET vendor_id = NULL WHERE bid_item_id = $bid_item_id AND is_delete IS NULL";
                    $set_vendor_name_run = mysqli_query($conn, $set_vendor_name_query);
                    if($set_vendor_name_run){
                        $count_rem++;                        
                    }
                    if($count_rem == $number_items){
                        $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Removed all vendors', type = 'bid', item_id = $bid_id, time = '$time'"; 
            
                        $run_insert_log = mysqli_query($conn, $query_insert_log);
                        echo 'removed';
                        http_response_code(200);
                        exit;
                    }
                }else{
                    http_response_code(400);
                    exit;   
                }
            }
            
        }else{
            http_response_code(400);
            exit;             
        }

    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

