<?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['in_i']) && isset($_REQUEST['i_no'])){
    if(strlen($_REQUEST['in_i']) >0 && strlen($_REQUEST['i_no']) >0){
        $invoice_item_id = $_REQUEST['in_i'];
        $lin = $_REQUEST['i_no'];
        $execute = "yes";

        $lin_del = $lin - 1;

        $query_bid_check = "SELECT * FROM invoice_item WHERE invoice_item_id = $invoice_item_id AND is_delete IS NULL";

        $run_bid_check = mysqli_query($conn, $query_bid_check);

        if(mysqli_num_rows($run_bid_check) ==1){
            $fetch_bid_details_row12 = mysqli_fetch_array($run_bid_check);
            $maps_id = $fetch_bid_details_row12['maps_id'];
            $invoice_id = $fetch_bid_details_row12['invoice_id'];
            $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'];
                }
            } 
           $bid_item_image = "SELECT * FROM invoice_actual_image WHERE item_id = $invoice_item_id ORDER BY image_id ASC";
            $bid_item_image_run = mysqli_query($conn, $bid_item_image);
    
            $cnt_id = mysqli_num_rows($bid_item_image_run); 
            if($cnt_id > 0){
                $image_array = array("set"=>"");
                $i = 0;
                while($bid_item_image_row = mysqli_fetch_array($bid_item_image_run)){
                    $image_id = $bid_item_image_row['image_id'];
                    $item_id = $bid_item_image_row['item_id'];
                    $image_name = $bid_item_image_row['image_name'];
                    $image_array[$i] = $image_id;                  
                    $i++;
                } 
                
                $image_id = $image_array[$lin_del];                 
                $query_bid_check1 = "DELETE FROM invoice_actual_image WHERE image_id = $image_id";
                $run_bid_check1 = mysqli_query($conn, $query_bid_check1);
                if(mysqli_affected_rows($conn) ==1){
                    $cnt_id--;
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '1 image removed from $invoice_maps_name.', type = 'invoice', item_id = $invoice_id, time = '$time'"; 
                    $run_insert_log = mysqli_query($conn, $query_insert_log);
                    echo $cnt_id;
                    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;            
    }
}else{
    http_response_code(400);           
    exit;        
}