Spaces:
Running
Running
Update templates/knowledgebase.html
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761<!DOCTYPE html>
<html>
<head>
<title>Knowledge Base</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Include AdminLTE CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/admin-lte@3.1/dist/css/adminlte.min.css">
<!-- Include DataTables CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap4.min.css">
<!-- Include AdminLTE JS -->
<script src="https://cdn.jsdelivr.net/npm/admin-lte@3.1/dist/js/adminlte.min.js"></script>
<!-- Include DataTables JS -->
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />
<style>
body {
background-color: transparent !important;
}
.content-wrapper{
flex: 1;
overflow-y: auto;
}
.wrapper {
display: flex;
flex-direction: column;
height: 100vh;
}
.modal-content {
background-color: #fff;
}
.content-header {
padding: 1rem;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.card {
padding: 0;
margin-top: -15px;
margin-left: -40px;
}
.table-responsive {
width: 100%;
overflow-x: auto;
/*overflow-y: scroll;*/
/* Allows horizontal scrolling if necessary */
}
.table,
.table-bordered {
width: 100%;
border-collapse: collapse;
margin-right: 7.5px;
}
.text-wrap {
white-space: normal !important;
word-break: break-word;
}
div.dataTables_wrapper div.dataTables_length select{
width: 60px;
display: inline-block;
}
th {
white-space: nowrap;
}
th:nth-child(1),
td:nth-child(1) {
/* Sno column */
width: 2%;
}
th:nth-child(2),
td:nth-child(2) {
/* Document Name column */
width: auto;
}
th:nth-child(3),
td:nth-child(3) {
/* Document Description column */
width: auto;
}
th:nth-child(4),
td:nth-child(4) {
/* Document Version column */
width: 20%;
}
th:nth-child(5),
td:nth-child(5) {
/* VectorDB Flag column */
width: auto;
}
th:nth-child(6),
td:nth-child(6) {
/* View column */
width: 10%;
}
th:nth-child(7),
td:nth-child(7) {
/* Edit column */
width: 10%;
}
th:nth-child(8),
td:nth-child(8) {
/* Delete column */
width: auto;
}
.alert {
position: relative;
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
}
.alert-success {
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}
.alert-danger {
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
}
.close {
position: absolute;
bottom: 10px;
right: 10px;
border: none;
background: none;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
{% include 'sidepane.html' %}
<div class="wrapper">
<div class="main-header" style="border-bottom: none;">
<div class="container mt-2">
<div class="form-group left-align" >
<label for="company" class="mr-1" style="margin-left:-40px; margin-top: 12px;">Company Name <span class="text-danger">*</span></label>
<select type="text" id="company" name="company" class="form-control" style="width:20%;margin-left:-40px;"required>
<option value="" selected>Select</option>
</select>
<div class="col-12 d-flex justify-content-end mb-3">
<button class="btn btn-primary" style="margin-top:-40px;margin-right: -10px;position: fixed;"
id="add" data-action="add">Add</button>
</div>
</div>
</div>
<div class="row">
<div id="message-container" style="margin-left: 200px;width:600px;"></div>
</div>
<section class="content" id="contentSection" style="display: none;">
<div class="container-fluid">
<div>
<div class="form-group">
<!-- <label for="company_id">company_id</label>--->
<input type="hidden" id="company_id" name="company_id" class="form-control" required>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div id="message-container">
<div class="card-body table-container">
<table id="knowledgeTable" class="table table-bordered table-striped">
<thead>
<tr>
<th>Sno</th>
<th>Document Name</th>
<th>Document Description</th>
<th>Document Version</th>
<th>VectorDB Flag</th>
<th>View</th>
<th>Edit</th>
<th>Delete</th>
<!-- <th style="display:none;">Company ID</th> -->
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addModalLabel">Add Document Details</h5>
<button type="button" class="btn-close" data-dismiss="modal" >
<span aria-hidden="true" data-backdrop="static" data-keyboard="false"></span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<!-- <label for="company_id">company_id</label>--->
<input type="hidden" id="company_id" name="company_id" class="form-control" required>
</div>
<form id="documentForm">
<div class="form-group">
<label for="uploadFile">Upload File<span class="text-danger">*</span></label>
<span id="file-name"></span>
<input type="hidden" id="file_name" name="file_name" class="form-control" required>
<input type="file" class="form-control-file" id="uploadFile" name="uploadFile" required
accept=".pdf,.doc,.docx">
<small class="form-text text-muted">Accepted formats: .pdf, .doc, .docx</small>
</div>
<div class="form-group">
<label for="documentName">Document Name<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="documentName" name="documentName" required>
</div>
<div class="form-group">
<label for="documentDescription">Document Description<span
class="text-danger">*</span></label>
<textarea class="form-control" id="documentDescription" name="documentDescription"
rows="3" required></textarea>
</div>
<div class="form-group">
<label for="department">Department<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="department" name="department" required>
</div>
<div class="form-group">
<label for="version">Version<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="version" name="version" required>
</div>
<div class="form-group">
<label for="lastUpdated">Last Updated<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="lastUpdated" name="lastUpdated" required>
</div>
</form>
</div>
<div class="modal-footer">
<!-- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> -->
<button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button>
<button type="button" id="save" onclick="save_file()" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</div>
{%include 'footer.html'%}
<!-- JavaScript to handle modal display -->
<script>
document.addEventListener("DOMContentLoaded", function () {
now = new Date().toISOString().replace('T', ' ').substr(0, 19);
document.getElementById('lastUpdated').value = now;
const inputs = document.querySelectorAll("#documentForm input, textarea");
inputs.forEach((input, index) => {
input.addEventListener("keydown", function (event) {
if (event.key === "Enter") {
event.preventDefault();
moveToNextInput(inputs, index);
}
});
});
function moveToNextInput(inputs, currentIndex) {
const nextInput = inputs[currentIndex + 1];
if (nextInput) {
nextInput.focus();
} else {
// Optionally, submit the form or trigger the save button
document.getElementById("save").focus();
}
}
// $(document).on('click', '.btn-view', function () {
// const companyId = $(this).data('id');
// console.log("company_id in view",companyId);
// viewCompany(companyId);
// });
// // Edit company details
// $(document).on('click', '.btn-edit', function () {
// const companyId = $(this).data('id');
// console.log("company_id in edit",companyId);
// editCompany(companyId);
// });
// // Delete company
// $(document).on('click', '.btn-delete', function () {
// const kid = $(this).data('id'); // Get the company ID from the da
// console.log("company_id in delete",kid);
// if (confirm("Are you sure you want to delete this company?")) {
// deleteCompany(kid); // Call the delete function with the company ID
// }
// });
fetchCompanies();
const companySelect = document.getElementById('company');
const documentForm = document.getElementById('documentForm');
const table = $('#knowledgeTable').DataTable(); // Initialize DataTable at the start
companySelect.addEventListener('change', async function () {
$("#message-container").empty();
const selectedCompanyName = companySelect.options[companySelect.selectedIndex].text;
console.log(`Selected Company Name: ${selectedCompanyName}`);
if (selectedCompanyName !== "Select") {
try {
const response = await fetch(`/api/company_id?company_name=${encodeURIComponent(selectedCompanyName)}`);
if (!response.ok) {
contentSection.style.display = 'none';
displayErrorMessage("Document details do not exist for this company. Please fill in the details.");
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log('data returned', data);
const companyId = data.company_id;
document.getElementById('company_id').value = companyId;
if (companyId) {
try {
const Response = await fetch(`/api/document_upload?company_id=${companyId}&company_name=${selectedCompanyName}`);
const connectorsResponse = await Response.json();
console.log("knowledge data table after connecting to table", connectorsResponse);
table.clear();
if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) {
//displayEmptyTable();
displayErrorMessage("Document details do not exist for this company. Please fill in the details.");
contentSection.style.display = 'none';
} else {
connectorsResponse.forEach((company, index) => {
table.row.add([
index + 1,
company.document_name,
company.document_desc,
company.version,
company.vectorDBflag,
`<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`,
`<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`,
//`<a href='#' class='btn btn-danger btn-sm' onclick='deleteCompany(${company.company_id})'><i class='fas fa-trash'></i></a>`,
`<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`,
company.row_id
// "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>",
// "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>",
// "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>"
]).draw(false);
});
contentSection.style.display = 'block';
}
} catch (error) {
console.error('Error fetching company documents:', error);
//displayEmptyTable();
displayErrorMessage("Document details1 do not exist for this company. Please fill in the details.");
contentSection.style.display = 'none';
}
} else {
// displayEmptyTable();
displayErrorMessage("Document details2 for knowledge do not exist for this company. Please fill in the details.");
}
} catch (error) {
console.error('No details for this company ID or data documents:', error);
// displayEmptyTable();
displayErrorMessage("Document details3 do not exist for this company. Please fill in the details.");
contentSection.style.display = 'none';
}
} else {
table.clear().draw();
document.getElementById('contentSection').style.display = 'none';
clearFormFields();
}
});
function displayErrorMessage(message) {
const messageContainer = document.getElementById('message-container');
if (messageContainer) {
messageContainer.innerHTML = `<div class='alert alert-danger'>${message}</div>`;
}
}
});
function clearFormFields() {
document.getElementById('file-name').textContent=""
document.getElementById('uploadFile').value = "";
document.getElementById('documentName').value = "";
document.getElementById('documentDescription').value = "";
document.getElementById('department').value = "";
document.getElementById('version').value = "";
//document.getElementById('lastUpdated').value = "";
}
$(document).ready(function () {
// Show modal function
$('#add').on('click', function () {
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'Add Document Details';
$('#uploadFile').show();
$('#saveupdate').hide();
clearFormFields();
$('#message-container').empty(); // Clear the message container
$('#addModal').modal('show');
});
$('.modal-footer .btn-secondary, .modal-header .btn-close').on('click', function () {
$('#addModal').modal('hide');
});
$('#KnowledgeTable').DataTable({
autoWidth: false
});
});
function viewCompany(button) {
var companyId = $(button).data('kid-id');
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'View Document Details';
$.getJSON(`/api/getknowledgebase/${companyId}`, function (company) {
$('#uploadFile').hide();
$('#file-name').text(company.file_path).attr('readonly','readonly');
$('#documentName').val(company.document_name).attr('readonly', 'readonly');
$('#documentDescription').val(company.document_desc).attr('readonly', 'readonly');
$('#department').val(company.department).attr('readonly', 'readonly');
$('#version').val(company.version).attr('readonly', 'readonly');
//$('#lastUpdated').val(company.last_updated).attr('readonly', 'readonly');
var lastUpdated = company.last_updated;
$('#lastUpdated').val(lastUpdated).attr('readonly', 'readonly');
$('#save').hide();// Disable the "Save" button when the modal opens
$('#addModal').modal('show');
}).fail(function () {
alert("Error retrieving company details.");
});
}
// Edit company details
function editCompany(button) {
var companyId = $(button).data('kid-id');
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'Edit Document Details';
// Fetch company details by ID
$.getJSON(`/api/getknowledgebase/${companyId}`, function (company) {
$('#uploadFile').hide();
$('#file-name').text(company.file_path).attr('readonly','readonly');
$('#documentName').removeAttr('readonly');
$('#documentDescription').removeAttr('readonly');
$('#department').removeAttr('readonly');
$('#version').removeAttr('readonly');
$('#lastUpdated').removeAttr('readonly');
// $('#vectorDBflag').removeAttr('readonly');
$('#company_id').removeAttr('readonly');
$('#save').hide();
$('#saveupdate').show();
// Populate the form with the company's details
$('#uploadFile').hide();
$('#file_name').val(company.file_path);
$('#file-name').val(company.file_path);
$('#documentName').val(company.document_name);
$('#documentDescription').val(company.document_desc);
$('#department').val(company.department);
$('#version').val(company.version);
$('#lastUpdated').val(company.last_updated);
$('#company_id').val(company.company_id);
$('#addModal').modal('show');
$('saveupdate').show();
// Update the save button to save the edited company
$('#saveupdate').off('click').on('click', function () {
saveupdate(companyId); // Pass the company ID for updating
});
}).fail(function () {
alert("Error retrieving company details.");
});
}
// Delete company
function deleteCompany(button) {
var companyId = $(button).data('kid-id');
console.log("k_id deleted",companyId);
$.ajax({
type: "DELETE",
url: `/api/delknowledgebase/${companyId}`,
success: function () {
var table = $('#knowledgeTable').DataTable();
table.row($(button).closest('tr')).remove().draw();
alert("Company deleted successfully.");
fetchCompanies(); // Refresh the table
},
error: function (xhr) {
alert("Error deleting company: " + xhr.responseJSON.detail);
}
});
}
function saveupdate(companyId) {
console.log("k_id", companyId);
var fileInput = document.getElementById('uploadFile'); // Assuming an input element for file upload
// var filePath = $('#file-name').text();
// console.log("k_id path", filePath);
var formData = new FormData();
formData.append('company_id', $('#company_id').val());
formData.append('file_name', $('#file_name').val());
formData.append('uploadFile', fileInput.files[0]); // Assuming a file input for upload
formData.append('documentName', $('#documentName').val());
formData.append('documentDescription', $('#documentDescription').val());
formData.append('department', $('#department').val());
formData.append('version', $('#version').val());
formData.append('vectorDBFlag', "NO");
formData.append('lastUpdated', $('#lastUpdated').val());
$.ajax({
url: `/api/putknowledgebase/${companyId}`,
type: 'PUT',
data: formData,
processData: false, // Required for FormData
contentType: false, // Required for FormData
success: function (response) {
alert('Knowledgebase details updated successfully.');
$('#addModal').modal('hide'); // Close the modal after saving
fetchCompanies();
},
error: function () {
alert('Failed to update company details.');
}
});
}
function save_file() {
const form = document.getElementById('documentForm');
// Check if the form is valid
if (!form.checkValidity()) {
// If the form is invalid, show validation messages and stop the submission
form.reportValidity();
return;
}
const uploadFile = document.getElementById("uploadFile").files[0];
const documentName = document.getElementById("documentName").value;
const documentDescription = document.getElementById("documentDescription").value;
const department = document.getElementById("department").value;
const version = document.getElementById("version").value;
const lastUpdated = document.getElementById("lastUpdated").value;
const company_id = document.getElementById("company_id").value;
//const formData = new FormData();
var formData = new FormData($('#documentForm')[0]);
const vectorDBFlag = "NO"; // Example value
const view = "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>";
const edit = "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>";
const dele= "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></a>";
formData.append("uploadFile", uploadFile);
formData.append("documentName", documentName);
formData.append("documentDescription", documentDescription);
formData.append("department", department);
formData.append("version", version);
formData.append("lastUpdated", lastUpdated);
formData.append("vectorDBflag", vectorDBFlag);
formData.append("company_id", company_id);
fetch('/upload_document', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
var table = $('#knowledgeTable').DataTable();
var rowCount = table.rows().count();
table.row.add([
rowCount + 1,
documentName,
documentDescription,
version,
vectorDBFlag,
view,
edit,
dele
]).draw(false);
const messageContainer = document.getElementById('message-container');
if(messageContainer)
messageContainer.innerHTML = `
<div class='alert alert-success'>
Data saved successfully
<button class='close' onclick='dismissMessage()'>OK</button>
</div>`;
// alert('Document saved successfully');
$('#addModal').modal('hide');
document.getElementById('contentSection').style.display = 'block'; // Show the table section
})
.catch(error => console.error('Error:', error));
}
function dismissMessage() {
const messageContainer = document.getElementById('message-container');
if (messageContainer) {
messageContainer.innerHTML = '';
}
}
async function fetchCompanies() {
try {
const response = await fetch('/api/companies');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
displayCompanies(data.companies);
} catch (error) {
console.error('Error fetching companies:', error);
}
}
function displayCompanies(companies) {
const companySelect = document.getElementById('company');
companySelect.innerHTML = '<option value="" selected>Select</option>'; // Reset the dropdown
companies.forEach(company => {
const option = document.createElement('option');
option.value = company.name;
option.textContent = company.name;
companySelect.appendChild(option);
});
}
</script>
</body>
<!DOCTYPE html><html>
<head> <title>Knowledge Base</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <!-- Include AdminLTE CSS --> <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/admin-lte@3.1/dist/css/adminlte.min.css"> <!-- Include DataTables CSS --> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/dataTables.bootstrap4.min.css"> <!-- Include AdminLTE JS --> <script src="https://cdn.jsdelivr.net/npm/admin-lte@3.1/dist/js/adminlte.min.js"></script> <!-- Include DataTables JS --> <script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap4.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" /> <style> body { background-color: transparent !important; } .content-wrapper{ flex: 1; overflow-y: auto; }
.wrapper { display: flex; flex-direction: column; height: 100vh; }
.modal-content { background-color: #fff; }
.content-header { padding: 1rem; } h1 { text-align: center; margin-bottom: 30px; }
.card { padding: 0; margin-top: -15px; margin-left: -40px; }
.table-responsive { width: 100%; overflow-x: auto; /*overflow-y: scroll;*/ /* Allows horizontal scrolling if necessary */ }
.table, .table-bordered { width: 100%; border-collapse: collapse; margin-right: 7.5px; }
.text-wrap { white-space: normal !important; word-break: break-word; } div.dataTables_wrapper div.dataTables_length select{ width: 60px; display: inline-block; } th { white-space: nowrap; }
th:nth-child(1), td:nth-child(1) { /* Sno column */ width: 2%; }
th:nth-child(2), td:nth-child(2) { /* Document Name column */ width: auto; }
th:nth-child(3), td:nth-child(3) { /* Document Description column */ width: auto; }
th:nth-child(4), td:nth-child(4) { /* Document Version column */ width: 20%; }
th:nth-child(5), td:nth-child(5) { /* VectorDB Flag column */ width: auto; }
th:nth-child(6), td:nth-child(6) { /* View column */ width: 10%; }
th:nth-child(7), td:nth-child(7) { /* Edit column */ width: 10%; }
th:nth-child(8), td:nth-child(8) { /* Delete column */ width: auto; } .alert { position: relative; padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px;}
.alert-success { color: #3c763d; background-color: #dff0d8; border-color: #d6e9c6;}
.alert-danger { color: #a94442; background-color: #f2dede; border-color: #ebccd1;}
.close { position: absolute; bottom: 10px; right: 10px; border: none; background: none; font-size: 16px; cursor: pointer;}
</style></head>
<body> {% include 'sidepane.html' %} <div class="wrapper"> <div class="main-header" style="border-bottom: none;"> <div class="container mt-2"> <div class="form-group left-align" > <label for="company" class="mr-1" style="margin-left:-40px; margin-top: 12px;">Company Name <span class="text-danger">*</span></label> <select type="text" id="company" name="company" class="form-control" style="width:20%;margin-left:-40px;"required> <option value="" selected>Select</option> </select> <div class="col-12 d-flex justify-content-end mb-3"> <button class="btn btn-primary" style="margin-top:-40px;margin-right: -10px;position: fixed;" id="add" data-action="add">Add</button> </div> </div> </div> <div class="row"> <div id="message-container" style="margin-left: 200px;width:600px;"></div> </div>
<section class="content" id="contentSection" style="display: none;"> <div class="container-fluid"> <div> <div class="form-group"> <!-- <label for="company_id">company_id</label>---> <input type="hidden" id="company_id" name="company_id" class="form-control" required> </div> <div class="row"> <div class="col-12"> <div class="card"> <div id="message-container"> <div class="card-body table-container"> <table id="knowledgeTable" class="table table-bordered table-striped"> <thead> <tr> <th>Sno</th> <th>Document Name</th> <th>Document Description</th> <th>Document Version</th> <th>VectorDB Flag</th> <th>View</th> <th>Edit</th> <th>Delete</th> <!-- <th style="display:none;">Company ID</th> --> </tr> </thead> <tbody>
</tbody> </table> </div> </div> </div> </div> </div> </section> </div> <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="addModalLabel">Add Document Details</h5> <button type="button" class="btn-close" data-dismiss="modal" > <span aria-hidden="true" data-backdrop="static" data-keyboard="false"></span> </button> </div> <div class="modal-body"> <div class="form-group"> <!-- <label for="company_id">company_id</label>---> <input type="hidden" id="company_id" name="company_id" class="form-control" required> </div> <form id="documentForm"> <div class="form-group"> <label for="uploadFile">Upload File<span class="text-danger">*</span></label> <span id="file-name"></span> <input type="hidden" id="file_name" name="file_name" class="form-control" required> <input type="file" class="form-control-file" id="uploadFile" name="uploadFile" required accept=".pdf,.doc,.docx"> <small class="form-text text-muted">Accepted formats: .pdf, .doc, .docx</small> </div> <div class="form-group"> <label for="documentName">Document Name<span class="text-danger">*</span></label> <input type="text" class="form-control" id="documentName" name="documentName" required> </div> <div class="form-group"> <label for="documentDescription">Document Description<span class="text-danger">*</span></label> <textarea class="form-control" id="documentDescription" name="documentDescription" rows="3" required></textarea> </div> <div class="form-group"> <label for="department">Department<span class="text-danger">*</span></label> <input type="text" class="form-control" id="department" name="department" required> </div> <div class="form-group"> <label for="version">Version<span class="text-danger">*</span></label> <input type="text" class="form-control" id="version" name="version" required> </div> <div class="form-group"> <label for="lastUpdated">Last Updated<span class="text-danger">*</span></label> <input type="text" class="form-control" id="lastUpdated" name="lastUpdated" required> </div> </form> </div> <div class="modal-footer"> <!-- <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> --> <button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button> <button type="button" id="save" onclick="save_file()" class="btn btn-primary">Save</button> </div> </div> </div> </div> </div>
{%include 'footer.html'%} <!-- JavaScript to handle modal display --> <script> document.addEventListener("DOMContentLoaded", function () { now = new Date().toISOString().replace('T', ' ').substr(0, 19); document.getElementById('lastUpdated').value = now; const inputs = document.querySelectorAll("#documentForm input, textarea");
inputs.forEach((input, index) => { input.addEventListener("keydown", function (event) { if (event.key === "Enter") { event.preventDefault(); moveToNextInput(inputs, index); } }); });
function moveToNextInput(inputs, currentIndex) { const nextInput = inputs[currentIndex + 1]; if (nextInput) { nextInput.focus(); } else { // Optionally, submit the form or trigger the save button document.getElementById("save").focus(); } }// $(document).on('click', '.btn-view', function () {// const companyId = $(this).data('id');// console.log("company_id in view",companyId);// viewCompany(companyId);// });
// // Edit knowledge base details// $(document).on('click', '.btn-edit', function () {// const companyId = $(this).data('id');// console.log("company_id in edit",companyId);// editCompany(companyId);// });
// // Delete company// $(document).on('click', '.btn-delete', function () {// const kid = $(this).data('id'); // Get the company ID from the da // console.log("company_id in delete",kid);// if (confirm("Are you sure you want to delete this company?")) {// deleteCompany(kid); // Call the delete function with the company ID// }// }); fetchCompanies();
const companySelect = document.getElementById('company'); const documentForm = document.getElementById('documentForm'); const table = $('#knowledgeTable').DataTable(); // Initialize DataTable at the start
companySelect.addEventListener('change', async function () { $("#message-container").empty(); const selectedCompanyName = companySelect.options[companySelect.selectedIndex].text; console.log(`Selected Company Name: ${selectedCompanyName}`);
if (selectedCompanyName !== "Select") { try { const response = await fetch(`/api/company_id?company_name=${encodeURIComponent(selectedCompanyName)}`); if (!response.ok) { contentSection.style.display = 'none'; displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); throw new Error('Network response was not ok'); } const data = await response.json(); console.log('data returned', data); const companyId = data.company_id; document.getElementById('company_id').value = companyId;
if (companyId) { try { const Response = await fetch(`/api/document_upload?company_id=${companyId}&company_name=${selectedCompanyName}`); const connectorsResponse = await Response.json(); console.log("knowledge data table after connecting to table", connectorsResponse); table.clear(); if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) { //displayEmptyTable();
displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } else {
connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.document_name, company.document_desc, company.version, company.vectorDBflag, `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, company.row_id // "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>", // "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>", // "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>" ]).draw(false); }); contentSection.style.display = 'block'; } } catch (error) { console.error('Error fetching company documents:', error); //displayEmptyTable();
displayErrorMessage("Knowledgebase details1 do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } } else { // displayEmptyTable();
displayErrorMessage("Details for knowledgebase do not exist for this company. Please fill in the details."); } } catch (error) { console.error('No details for this company ID or data documents:', error); // displayEmptyTable();
displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } } else { table.clear().draw(); document.getElementById('contentSection').style.display = 'none'; clearFormFields(); } });
function displayErrorMessage(message) { const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = `<div class='alert alert-danger'>${message}</div>`; } } });
function clearFormFields() { document.getElementById('file-name').textContent="" document.getElementById('uploadFile').value = ""; document.getElementById('documentName').value = ""; document.getElementById('documentDescription').value = ""; document.getElementById('department').value = ""; document.getElementById('version').value = ""; //document.getElementById('lastUpdated').value = ""; }
$(document).ready(function () { // Show modal function $('#add').on('click', function () { $('#documentName').removeAttr('readonly'); $('#documentDescription').removeAttr('readonly'); $('#department').removeAttr('readonly'); $('#version').removeAttr('readonly'); $('#lastUpdated').removeAttr('readonly'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Add Knowledgebase Details'; $('#uploadFile').show(); $('#saveupdate').hide(); $('#save').show(); clearFormFields(); $('#message-container').empty(); // Clear the message container $('#addModal').modal('show'); });
$('.modal-footer .btn-secondary, .modal-header .btn-close').on('click', function () { $('#addModal').modal('hide'); }); // $('#knowledgeTable').DataTable({ // autoWidth: false // });
}); function viewCompany(button) { var companyId = $(button).data('kid-id'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'View Knowledgebase Details'; $.getJSON(`/api/getknowledgebase/${companyId}`, function (company) { $('#uploadFile').hide(); $('#file-name').text(company.file_path).attr('readonly','readonly'); $('#documentName').val(company.document_name).attr('readonly', 'readonly'); $('#documentDescription').val(company.document_desc).attr('readonly', 'readonly'); $('#department').val(company.department).attr('readonly', 'readonly'); $('#version').val(company.version).attr('readonly', 'readonly'); //$('#lastUpdated').val(company.last_updated).attr('readonly', 'readonly'); var lastUpdated = company.last_updated; $('#lastUpdated').val(lastUpdated).attr('readonly', 'readonly'); $('#save').hide(); $('#saveupdate').hide(); $('#addModal').modal('show'); }).fail(function () { alert("Error retrieving knowledge base details."); }); }
// Edit knowledge base details function editCompany(button) { var companyId = $(button).data('kid-id'); const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Edit Knowledgebase Details'; // Fetch company details by ID $.getJSON(`/api/getknowledgebase/${companyId}`, function (company) { const fileName = company.file_path.split('/').pop(); const fileContent = new Blob([""], { type: 'text/plain' }); const file = new File([fileContent], fileName, { type: fileContent.type });const dataTransfer = new DataTransfer();dataTransfer.items.add(file);
// Set the file input's files property to the created fileconst fileInput = document.getElementById('uploadFile');fileInput.files = dataTransfer.files;
// Check if the input element is of type 'file'if (fileInput.type === 'file') { console.log("The input is of type 'file'.");} else { console.log("The input is NOT of type 'file'.");}
// Check if the value is of type 'File'const files = fileInput.files;if (files.length > 0 && files[0] instanceof File) { console.log("The value is of type 'File':", files[0].name);} else { console.log("No file is selected or the value is not of type 'File'.");} $('#uploadFileLabel').text(files[0].name); $('#uploadFile').text(files[0].name) $('#documentName').val(company.document_name); $('#documentDescription').val(company.document_desc); $('#department').val(company.department); $('#version').val(company.version); $('#lastUpdated').val(company.last_updated); $('#company_id').val(company.company_id); // Show/hide appropriate elements $('#uploadFile').show(); // Hide the file upload field $('#save').hide(); // Hide the default save button $('#saveupdate').show(); // Show the update button // Ensure fields are editable $('#documentName').removeAttr('readonly'); $('#documentDescription').removeAttr('readonly'); $('#department').removeAttr('readonly'); $('#version').removeAttr('readonly'); $('#lastUpdated').removeAttr('readonly'); $('#company_id').val(company.company_id); // Ensure company_id is included if necessary // Show the modal $('#addModal').modal('show'); // Set up the click event for the update button $('#saveupdate').off('click').on('click', function () { saveupdate(companyId,files[0].name); // Pass the company ID for updating }); }).fail(function () { alert("Error retrieving knowledge base details."); });}
function saveupdate(companyId, filename) { const formData = new FormData(); const fileInput = document.getElementById("uploadFile"); const uploadFile = fileInput.files[0]; // Get the file object const company_id=document.getElementById("company_id").value; formData.append("company_id", $('#company_id').val()); formData.append("documentName", $('#documentName').val()); formData.append("documentDescription", $('#documentDescription').val()); formData.append("department", $('#department').val()); formData.append("version", $('#version').val()); formData.append("vectorDBFlag", 'NO'); formData.append("lastUpdated", $('#lastUpdated').val());console.log("company_id to table refresh",company_id) // Append the file only if it exists, otherwise, append the filename if (uploadFile) { formData.append("file_path", uploadFile); // Append the file object } else { formData.append("file_path", filename); // Append the existing filename }
$.ajax({ url: `/api/putknowledgebase/${companyId}`, type: 'PUT', data: formData, processData: false, // Required for FormData contentType: false, // Required for FormData success: function (response) { // alert('Knowledgebase details updated successfully.'); const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-success'> Knowledgebasse Data updated successfully <button class='close' onclick='dismissMessage()'>OK</button> </div>`; $('#addModal').modal('hide'); // Close the modal after saving fetchUpdatedDocuments(company_id); }, error: function () { const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-danger'> No changes have been made to update knowledgebase details. <button class='close' onclick='dismissMessage()'>OK</button> </div>`; $('#addModal').modal('hide'); // alert('Form has no changes to update knowledge base details.'); } });}
async function fetchUpdatedDocuments(company_id) { console.log("company_id",company_id); var table = $('#knowledgeTable').DataTable(); const Response = await fetch(`/api/document_update?company_id=${company_id}`); const connectorsResponse = await Response.json(); console.log("knowledge data table after connecting to table", connectorsResponse); table.clear(); if (!Array.isArray(connectorsResponse) || connectorsResponse.length === 0) { //displayEmptyTable();
displayErrorMessage("Knowledgebase details do not exist for this company. Please fill in the details."); contentSection.style.display = 'none'; } else {
connectorsResponse.forEach((company, index) => { table.row.add([ index + 1, company.document_name, company.document_desc, company.version, company.vectorDBflag, `<a href='#' class='btn btn-info btn-sm'data-kid-id='${company.row_id}' data-action="view" onclick='viewCompany(this)''><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm'data-kid-id='${company.row_id}' data-action="edit" onclick='editCompany(this)'><i class='fas fa-edit'></i></a>`, //`<a href='#' class='btn btn-danger btn-sm' onclick='deleteCompany(${company.company_id})'><i class='fas fa-trash'></i></a>`, `<a href='#' class='btn btn-danger btn-sm' data-kid-id='${company.row_id}' onclick='deleteCompany(this)'><i class='fas fa-trash'></i></a>`, company.row_id // "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>", // "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>", // "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></button>" ]).draw(false); }); contentSection.style.display = 'block'; } } // Delete company function deleteCompany(button) { var companyId = $(button).data('kid-id'); console.log("k_id deleted",companyId); $.ajax({ type: "DELETE", url: `/api/delknowledgebase/${companyId}`, success: function () { var table = $('#knowledgeTable').DataTable(); table.row($(button).closest('tr')).remove().draw(); const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-danger'> Knowledgebase data deleted successfully. <button class='close' onclick='dismissMessage()'>OK</button> </div>`; // alert("Knowledgebase data deleted successfully."); fetchCompanies(); // Refresh the table }, error: function (xhr) { alert("Error deleting company: " + xhr.responseJSON.detail); } }); } function save_file() { const form = document.getElementById('documentForm');
// Check if the form is valid if (!form.checkValidity()) { // If the form is invalid, show validation messages and stop the submission form.reportValidity(); return; } const uploadFile = document.getElementById("uploadFile").files[0]; const documentName = document.getElementById("documentName").value; const documentDescription = document.getElementById("documentDescription").value; const department = document.getElementById("department").value; const version = document.getElementById("version").value; const lastUpdated = document.getElementById("lastUpdated").value; const company_id = document.getElementById("company_id").value;
//const formData = new FormData(); var formData = new FormData($('#documentForm')[0]); const vectorDBFlag = "NO"; // Example value const view = "<a href='#' class='btn btn-info btn-sm'><i class='fas fa-eye'></i></a>"; const edit = "<a href='#' class='btn btn-warning btn-sm'><i class='fas fa-edit'></i></a>"; const dele= "<a href='#' class='btn btn-danger btn-sm'><i class='fas fa-trash'></i></a>";
formData.append("uploadFile", uploadFile); formData.append("documentName", documentName); formData.append("documentDescription", documentDescription); formData.append("department", department); formData.append("version", version); formData.append("lastUpdated", lastUpdated); formData.append("vectorDBflag", vectorDBFlag); formData.append("company_id", company_id);console.log("formdata",formData); fetch('/upload_document', { method: 'POST', body: formData }) .then(response => { if (!response.ok) { throw new Error('Network response was not ok'); } return response.text(); }) .then(data => { var table = $('#knowledgeTable').DataTable(); var rowCount = table.rows().count(); table.row.add([ rowCount + 1, documentName, documentDescription, version, vectorDBFlag, view, edit, dele ]).draw(false); const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-success'> Knowledgebase Data saved successfully <button class='close' onclick='dismissMessage()'>OK</button> </div>`; // alert('Document saved successfully'); $('#addModal').modal('hide'); document.getElementById('contentSection').style.display = 'block'; // Show the table section }) .catch(error => console.error('Error:', error)); } function dismissMessage() { const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = ''; }} async function fetchCompanies() { try { const response = await fetch('/api/companies'); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); displayCompanies(data.companies); } catch (error) { console.error('Error fetching companies:', error); } }
function displayCompanies(companies) { const companySelect = document.getElementById('company'); companySelect.innerHTML = '<option value="" selected>Select</option>'; // Reset the dropdown companies.forEach(company => { const option = document.createElement('option'); option.value = company.name; option.textContent = company.name; companySelect.appendChild(option); }); } </script></body>
</html>templates/knowledgebase.html+760-666