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

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

       $maps = $_REQUEST['maps'];
       $cl_id = $_REQUEST['cl_id'];
       $invoice_id = $_REQUEST['in_no'];
       $invoice_no = $_REQUEST['invoice_no'];
       $state = $_REQUEST['state'];

       $query_check_bid = "SELECT * FROM invoice WHERE invoice_id = $invoice_id AND client_id = $cl_id AND invoice_type = $state AND invoice_no = '$invoice_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);
       $invoice_amount_prev = $row_check_bid['invoice_amount'];
       
       $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'];
                $invoice_item_amount = $row_fetch_maps_amount['maps_min_bid_price'];
                if(!isset($invoice_item_amount)){
                   $invoice_item_amount = 0.00;

                }               

            }   
                /* $bid_item_amount  */

       }else{
            http_response_code(400);
            exit;
       }       

       if(isset($invoice_item_amount)){
            $time = date("Y-m-d H:i:s");
            $query_check2_bid_item = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id 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) !=1 ){
                http_response_code(400);
                exit;

            }

            $row_check2_bid_item = mysqli_fetch_array($run_check2_bid_item);
            $invoice_item_total_actual = $row_check2_bid_item['invoice_item_total'];
/* continue */
            $invoice_amount_new = $invoice_amount_prev - $invoice_item_total_actual;

            $query_insert_bid_item = "DELETE FROM invoice_item WHERE invoice_id = $invoice_id AND maps_id = $maps"; 
            $run_insert_bid_item = mysqli_query($conn, $query_insert_bid_item);

            if(mysqli_affected_rows($conn) > 0){
                $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Removed item $invoice_maps_name', type = 'invoice', item_id = $invoice_id, time = '$time'"; 

                $run_insert_log = mysqli_query($conn, $query_insert_log);
                if($invoice_amount_new != $invoice_amount_prev){

                    $query_update_bid = "UPDATE invoice SET invoice_amount = $invoice_amount_new WHERE invoice_id = $invoice_id AND client_id = $cl_id AND invoice_type = $state AND invoice_no = '$invoice_no'";

                    $run_update_bid = mysqli_query($conn, $query_update_bid);  
                    if(mysqli_affected_rows($conn) > 0){
                        $invoice_amount_new = "$ ". $invoice_amount_new;
                        $arr = array('bid_amount' => $invoice_amount_new, 'status' => 'done');
                        echo json_encode($arr);
                        http_response_code(200);//success
                        exit;
                    }

                }     
                    $arr = array('status' => 'delete');
                    echo json_encode($arr);      
                    http_response_code(200);//success
                    exit;
            }else{ 
                http_response_code(400);
                exit;
            }
        }else{
            http_response_code(400);//fail
            exit; 
        }

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

