<?php
session_start();
include('database/connection.php');
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
// echo "<pre>";
// print_r($_POST);
// die;
$id  = $_POST['id'];
$status  = $_POST['status'];
$note  = $_POST['note'];

// Attempt insert query execution
if($_POST['action'] && $_POST['action']=='update_status')
{ 
    //$sql = "UPDATE leads SET `status` = ".$status." Where id = ".$id;
    
    $sql = "UPDATE leads SET status = '".$status."' WHERE id = $id";

   
    if(mysqli_query($link, $sql))
        echo 'success';
    else
        echo "Error updating record: " . mysqli_error($link);
   
}

if($_POST['action'] && $_POST['action']=='add_note')
{ 
    
    // Prepare the SQL query
    $query = "SELECT * FROM leads WHERE id = $id";
    // Execute the query
    $result = mysqli_query($link, $query);
    // Fetch the row as an associative array
    $row = mysqli_fetch_assoc($result);
    $old_note = $row["note"];
    $new_note=$old_note."\n-----------------------------------------\nCreated at: ".date('Y-m-d H:i:s')."\n Created by: ".$_SESSION['user_name']."\n ".$note;
    
    
    $sql = "UPDATE leads SET note = '".$new_note."' WHERE id = $id";

   
    if(mysqli_query($link, $sql))
        echo $new_note;
        //echo json_encode(['status'=>'success', 'data'=>$new_note]);
    else
        echo 'error';
        //echo json_encode(['status'=>'error', 'message'=>mysqli_error($link)]);
        //echo "Error updating record: " . mysqli_error($link);
   
}

if($_POST['action'] && $_POST['action']=='delete_record')
{ 
    
    $id = $_POST["id"];

    $query="DELETE from leads WHERE id=$id";
    $exec= mysqli_query($link,$query);
    if($exec){
      echo "success";
    }else{
      echo "error";
    }
        
   
}

if($_POST['action'] && $_POST['action']=='add_user')
{ 
    
    $query="DELETE from leads WHERE id=$id";
    $exec= mysqli_query($link,$query);
    if($exec){
      echo "success";
    }else{
      echo "error";
    }
        
    
    // Sample data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $role = $_POST['role'];
    
    // Prepare and execute INSERT query
    $sql = "INSERT INTO users (name, email, password, role) VALUES ('$name', '$email', '$password', '$role')";
    
    if ($link->query($sql) === TRUE) {
        // Set a temporary notification message
        $_SESSION['success'] = 'Registration successfull';
        header("Location: user.php");
    } else {
        // Set a temporary notification message
        $_SESSION['error'] = 'some error occured';
        header("Location: add_user.php");
    }
}

if($_POST['action'] && $_POST['action']=='user_status_update')
{ 
    //$sql = "UPDATE leads SET `status` = ".$status." Where id = ".$id;
    
    $id = $_POST['id'];
    $status = $_POST['status'];
    
    $sql = "UPDATE users SET status = '".$status."' WHERE id = $id";

   
    if(mysqli_query($link, $sql))
        echo 'success';
    else
        echo "Error updating record: " . mysqli_error($link);
   
}

if($_POST['action'] && $_POST['action']=='delete_user_record')
{ 
    
    $id = $_POST["id"];

    $query="DELETE from users WHERE id=$id";
    $exec= mysqli_query($link,$query);
    if($exec){
      echo "success";
    }else{
      echo "error";
    }
        
   
}

//echo 'Error Occured!';
 
// Close connection
mysqli_close($link);
?>