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

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['in_no']) && strlen($_REQUEST['new_name']) >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'];
            }   
       }     
       $invoice_id = $_REQUEST['in_no'];        
       $new_name = $_REQUEST['new_name'];

       $query_bid_item_check = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id 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){
        $query = "UPDATE invoice_item SET invoice_item_desc = '$new_name' WHERE invoice_id = $invoice_id AND maps_id = $maps_id AND is_delete IS NULL";
        $run = mysqli_query($conn, $query);
        if($run){
            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name description changed to $new_name', type = 'invoice', item_id = $invoice_id, time = '$time'"; 
                $run_insert_log = mysqli_query($conn, $query_insert_log);
                $arr = array('name' => $new_name);
                echo json_encode($arr);
                http_response_code(200);
                exit;
        }


       }else{
        
        http_response_code(400);
           
            exit;
       }

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

