Spaces:
Runtime error
Runtime error
Update templates/company_profile.html
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Company Profile</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">
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/@ttskch/select2-bootstrap4-theme@1.4.0/dist/select2-bootstrap4.min.css"
rel="stylesheet" />
<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;
}
.card {
padding: 0;
margin-top: 10px;
margin-left: -40px;
}
.content-wrapper {
background-color: transparent !important;
text-align: center;
}
.wrapper {
background-color: transparent !important;
}
.modal-content {
background-color: #fff;
}
h1 {
text-align: center;
margin-bottom: 30px;
}
.table-responsive {
width: 100%;
overflow-x: auto;
}
.table {
width: 100%;
}
.text-wrap {
white-space: normal !important;
word-break: break-word;
}
div.dataTables_wrapper div.dataTables_length select {
width: 60px;
display: inline-block;
}
.center-align {
padding-top: 20px;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.reduced-width {
width: 50%;
}
th,
td {
white-space: nowrap;
}
.select2-container--bootstrap4 {
z-index: 1050;
/* Higher than the modal's z-index */
}
.select2-container--open {
z-index: 1060;
/* Ensure the dropdown is above the modal */
}
th:nth-child(1),
td:nth-child(1) {
width: 5%;
}
th:nth-child(2),
td:nth-child(2) {
width: 20%;
}
th:nth-child(3),
td:nth-child(3) {
width: 20%;
}
th:nth-child(4),
td:nth-child(4) {
width: 20%;
}
th:nth-child(5),
td:nth-child(5) {
width: 20%;
}
th:nth-child(6),
td:nth-child(6) {
width: 15%;
}
th:nth-child(7),
td:nth-child(7) {
width: 20%;
}
.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;
}
.close2 {
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="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-12">
<div class="col-12 d-flex justify-content-end mb-3">
<button class="btn btn-primary" style="margin-bottom:-30px;position:fixed"
id="add">Add</button>
</div>
</div>
<div class="row">
<div id="message-container" style="margin-left: 200px;width:600px;"></div>
</div>
</div>
</div>
</div>
<!-- Main content -->
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body table-container">
<table id="companyTable"
class="custom-select-lm table table-bordered table-striped">
<thead>
<tr>
<th>Sno</th>
<th>Company Name</th>
<th>Company code</th>
<th>Domain</th>
<th>LLM Tools</th>
<th>View</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
</div>
</div>
<!-- Modal -->
<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 Company Profile</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" data-backdrop="static" data-keyboard="false">×</span>
</button>
</div>
<div class="modal-body">
<form id="companyForm">
<div class="form-group">
<label for="companyName">Company Name<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="company_name" name="company_name" required>
</div>
<div class="form-group">
<label for="companyCode">Company Code<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="company_code" name="company_code" required>
</div>
<div class="form-group">
<label for="domain">Domain<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="domain" name="domain" required>
</div>
<div class="form-group">
<label for="llm_tools">LLM Tools<span class="text-danger">*</span></label>
<select class="form-control" id="llm_tools" name="llm_tools" multiple required>
<option value="Database">Database</option>
<option value="Static Documents">Static Documents</option>
<option value="API">API</option>
</select>
<div class="invalid-feedback">
Please select at least one LLM tool.
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button>
<button type="button" id="save" onclick="saveProfile()" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
{%include 'footer.html'%}
<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>
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const role = sessionStorage.getItem('userRole');
console.log('Current role:', role); // Debug statement to check the role
if (role === 'admin') {
document.getElementById('add').style.display = 'block';
} else {
document.getElementById('add').style.display = 'none';
}
fetchCompanies();
});
window.addEventListener('pageshow', function (event) {
if (event.persisted || (window.performance && window.performance.navigation.type === 2)) {
window.location.reload();
}
});
$(document).ready(function () {
// Initialize DataTable
$('#companyTable').DataTable();
// Initialize Select2
$('#llm_tools').select2({
theme: 'bootstrap4',
placeholder: 'Select',
allowClear: true
});
// Show modal function
$('#add').on('click', function () {
$('#addModal').modal('show');
$('#save').show();
$('#saveupdate').hide();
clearmodal();
$('#save').prop('disabled', false); // Disable the "Save" button when the modal opens
});
$('.modal-footer .btn-secondary, .modal-header .close').on('click', function () {
$('#addModal').modal('hide');
});
$(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 companyId = $(this).data('id'); // Get the company ID from the da
console.log("company_id in delete", companyId);
if (confirm("Are you sure you want to delete this company?")) {
deleteCompany(companyId); // Call the delete function with the company ID
}
});
});
// View company details
function viewCompany(companyId) {
// Fetch company details by ID
$.getJSON(`/api/getcompanydetails/${companyId}`, function (company) {
// alert(`Company Name: ${company.company_name}\nCompany Code: ${company.company_code}\nDomain: ${company.domain}\nLLM Tools: ${company.llm_tools}`);
$('#company_name').val(company.company_name).attr('readonly', 'readonly');
$('#company_code').val(company.company_code).attr('readonly', 'readonly');
$('#domain').val(company.domain).attr('readonly', 'readonly');
$('#llm_tools').val(company.llm_tools.split(',')).trigger('change');
$('#llm_tools').prop('disabled', true); // Disable the select input
// Open the modal for editing
$('#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(companyId) {
// Fetch company details by ID
$.getJSON(`/api/getcompanydetails/${companyId}`, function (company) {
$('#company_name').removeAttr('readonly');
$('#company_code').removeAttr('readonly');
$('#domain').removeAttr('readonly');
$('#llm_tools').prop('disabled', false);
$('#save').hide();
$('#saveupdate').show();
// Populate the form with the company's details
$('#company_name').val(company.company_name);
$('#company_code').val(company.company_code);
$('#domain').val(company.domain);
$('#llm_tools').val(company.llm_tools.split(',')).trigger('change');
// Open the modal for editing
$('#addModal').modal('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(companyId) {
console.log("company_id deleted", companyId);
$.ajax({
type: "DELETE",
url: `/api/delcompanydetails/${companyId}`,
success: function () {
// alert("Company deleted successfully.");
const messageContainer = document.getElementById('message-container');
if (messageContainer)
messageContainer.innerHTML = `
<div class='alert alert-success'>
Company deleted successfully.
<button class='close' onclick='dismissMessage()'>OK</button>
</div>`;
fetchCompanies(); // Refresh the table
},
error: function (xhr) {
alert("Error deleting company: " + xhr.responseJSON.detail);
}
});
}
function saveupdate(companyId) {
console.log("company_id", companyId);
var companyData = {
company_name: $('#company_name').val(),
company_code: $('#company_code').val(),
domain: $('#domain').val(),
llm_tools: $('#llm_tools').val().join(',') // Assuming llm_tools is a multi-select
};
console.log("company_data", companyData);
$.ajax({
url: `/api/putcompanydetails/${companyId}`,
type: 'PUT',
data: companyData,
success: function (response) {
alert('Company details updated successfully.');
$('#addModal').modal('hide'); // Close the modal after saving
fetchCompanies();
},
error: function () {
alert('Failed to update company details.');
}
});
}
function saveProfile() {
const form = document.getElementById('companyForm');
// Check if the form is valid
if (!form.checkValidity()) {
// If the form is invalid, show validation messages and stop the submission
form.reportValidity();
return;
}
// Gather form data
const company_name = document.getElementById('company_name').value;
const company_code = document.getElementById('company_code').value;
const domain = document.getElementById('domain').value;
const llm_tools = $('#llm_tools').val(); // Get selected values from Select2
// Prepare FormData
let formData = new FormData();
formData.append("company_name", company_name);
formData.append("company_code", company_code);
formData.append("domain", domain);
formData.append("llm_tools", llm_tools.join(','));
$.ajax({
type: "POST",
url: "/submit_company_profile",
data: formData,
processData: false,
contentType: false,
success: function (response) {
// alert("Data saved successfully"); // Show success message
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>`;
fetchCompanies(); // Refresh table data
// Clear the form and close the modal
// $('#companyForm')[0].reset();
$('#addModal').modal('hide');
},
error: function (xhr) {
alert("An error occurred: " + xhr.responseJSON.detail); // Show error message
}
});
}
function dismissMessage() {
const messageContainer = document.getElementById('message-container');
if (messageContainer) {
messageContainer.innerHTML = '';
}
}
async function fetchCompanies() {
try {
console.log("Fetching companies...");
const response = await fetch(`/api/companydetails`);
const companies = await response.json();
console.log(companies); // Inspect the data here
if (!Array.isArray(companies)) {
throw new TypeError('Expected an array of companies');
}
const table = $('#companyTable').DataTable();
table.clear(); // Clear existing table data
companies.forEach((company, index) => {
table.row.add([
index + 1,
company.company_name,
company.company_code,
company.domain,
company.llm_tools, // Join array of tools into a string
`<a href='#' class='btn btn-info btn-sm'onclick='viewCompany(${company.company_id})'><i class='fas fa-eye'></i></a>`,
`<a href='#' class='btn btn-warning btn-sm'onclick='editCompany(${company.company_id})'><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>`
]).draw(false);
});
} catch (error) {
console.error('Error fetching companies:', error);
}
}
function clearmodal() {
$('#companyForm')[0].reset(); // Reset all form fields
$('#llm_tools').val(null).trigger('change'); // Clear and reset the Select2 field
$('#llm_tools').select2({
theme: 'bootstrap4',
placeholder: 'Select',
allowClear: true
}); // Reinitialize Select2
}
</script>
</body>
<!DOCTYPE html><html lang="en">
<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Company Profile</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"> <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> <link href="https://cdn.jsdelivr.net/npm/@ttskch/select2-bootstrap4-theme@1.4.0/dist/select2-bootstrap4.min.css" rel="stylesheet" /> <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; }
.card { padding: 0; margin-top: 10px; margin-left: -40px; }
.content-wrapper { background-color: transparent !important; text-align: center; }
.wrapper { background-color: transparent !important; }
.modal-content { background-color: #fff; }
h1 { text-align: center; margin-bottom: 30px; }
.table-responsive { width: 100%; overflow-x: auto; }
.table { width: 100%; }
.text-wrap { white-space: normal !important; word-break: break-word; }
div.dataTables_wrapper div.dataTables_length select { width: 60px; display: inline-block; }
.center-align { padding-top: 20px; display: flex; justify-content: center; align-items: center; width: 100%; }
.reduced-width { width: 50%; }
th, td { white-space: nowrap; }
.select2-container--bootstrap4 { z-index: 1050; /* Higher than the modal's z-index */ }
.select2-container--open { z-index: 1060; /* Ensure the dropdown is above the modal */ }
th:nth-child(1), td:nth-child(1) { width: 5%; }
th:nth-child(2), td:nth-child(2) { width: 20%; }
th:nth-child(3), td:nth-child(3) { width: 20%; }
th:nth-child(4), td:nth-child(4) { width: 20%; }
th:nth-child(5), td:nth-child(5) { width: 20%; }
th:nth-child(6), td:nth-child(6) { width: 15%; }
th:nth-child(7), td:nth-child(7) { width: 20%; }
.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; }
.close2 { 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="content-wrapper"> <!-- Content Header (Page header) --> <div class="content-header"> <div class="container-fluid"> <div class="row mb-2"> <div class="col-12"> <div class="col-12 d-flex justify-content-end mb-3"> <button class="btn btn-primary" style="margin-bottom:-30px;position:fixed" id="add">Add</button> </div> </div> <div class="row"> <div id="message-container" style="margin-left: 200px;width:600px;"></div> </div> </div> </div> </div> <!-- Main content --> <section class="content"> <div class="container-fluid"> <div class="row"> <div class="col-12"> <div class="card"> <div class="card-body table-container"> <table id="companyTable" class="custom-select-lm table table-bordered table-striped"> <thead> <tr> <th>Sno</th> <th>Company Name</th> <th>Company code</th> <th>Domain</th> <th>LLM Tools</th> <th>View</th> <th>Edit</th> <th>Delete</th> </tr> </thead> <tbody> </tbody> </table> </div> </div> </div> </div> </div> </section> </div> </div> <!-- Modal --> <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 Company Profile</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true" data-backdrop="static" data-keyboard="false">×</span> </button> </div> <div class="modal-body"> <form id="companyForm"> <div class="form-group"> <label for="companyName">Company Name<span class="text-danger">*</span></label> <input type="text" class="form-control" id="company_name" name="company_name" required> </div> <div class="form-group"> <label for="companyCode">Company Code<span class="text-danger">*</span></label> <input type="text" class="form-control" id="company_code" name="company_code" required> </div> <div class="form-group"> <label for="domain">Domain<span class="text-danger">*</span></label> <input type="text" class="form-control" id="domain" name="domain" required> </div> <div class="form-group"> <label for="llm_tools">LLM Tools<span class="text-danger">*</span></label> <select class="form-control" id="llm_tools" name="llm_tools" multiple required> <option value="Database">Database</option> <option value="Static Documents">Static Documents</option> <option value="API">API</option> </select> <div class="invalid-feedback"> Please select at least one LLM tool. </div> </div> </form> </div> <div class="modal-footer"> <button type="button" id="saveupdate" class="btn btn-primary" style="display: none;">Update</button> <button type="button" id="save" onclick="saveProfile()" class="btn btn-primary">Save</button> </div> </div> </div> </div> {%include 'footer.html'%} <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> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script> <script> document.addEventListener('DOMContentLoaded', function () {
const role = sessionStorage.getItem('userRole'); console.log('Current role:', role); // Debug statement to check the role
if (role === 'admin') { document.getElementById('add').style.display = 'block'; } else { document.getElementById('add').style.display = 'none'; } fetchCompanies(); }); window.addEventListener('pageshow', function (event) { if (event.persisted || (window.performance && window.performance.navigation.type === 2)) { window.location.reload(); } });
$(document).ready(function () { // Initialize DataTable $('#companyTable').DataTable(); // Initialize Select2 $('#llm_tools').select2({ theme: 'bootstrap4', placeholder: 'Select', allowClear: true }); // Show modal function $('#add').on('click', function () { const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Add Company Details'; $('#addModal').modal('show'); $('#save').show(); $('#saveupdate').hide(); clearmodal(); $('#save').prop('disabled', false); // Disable the "Save" button when the modal opens }); $('.modal-footer .btn-secondary, .modal-header .close').on('click', function () { $('#addModal').modal('hide'); });
$(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 companyId = $(this).data('id'); // Get the company ID from the da console.log("company_id in delete", companyId); if (confirm("Are you sure you want to delete this company?")) { deleteCompany(companyId); // Call the delete function with the company ID } });
}); // View company details function viewCompany(companyId) { // Fetch company details by ID const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'View Company Details'; $.getJSON(`/api/getcompanydetails/${companyId}`, function (company) { // alert(`Company Name: ${company.company_name}\nCompany Code: ${company.company_code}\nDomain: ${company.domain}\nLLM Tools: ${company.llm_tools}`); $('#company_name').val(company.company_name).attr('readonly', 'readonly'); $('#company_code').val(company.company_code).attr('readonly', 'readonly'); $('#domain').val(company.domain).attr('readonly', 'readonly'); $('#llm_tools').val(company.llm_tools.split(',')).trigger('change'); $('#llm_tools').prop('disabled', true); // Disable the select input
// Open the modal for editing $('#saveupdate').hide(); $('#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(companyId) { const modalTitle = document.getElementById('addModalLabel'); modalTitle.textContent = 'Edit Company Details'; // Fetch company details by ID $.getJSON(`/api/getcompanydetails/${companyId}`, function (company) { $('#company_name').removeAttr('readonly'); $('#company_code').removeAttr('readonly'); $('#domain').removeAttr('readonly'); $('#llm_tools').prop('disabled', false); $('#save').hide(); $('#saveupdate').show(); // Populate the form with the company's details $('#company_name').val(company.company_name); $('#company_code').val(company.company_code); $('#domain').val(company.domain); $('#llm_tools').val(company.llm_tools.split(',')).trigger('change');
// Open the modal for editing $('#addModal').modal('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(companyId) { console.log("company_id deleted", companyId); $.ajax({ type: "DELETE", url: `/api/delcompanydetails/${companyId}`, success: function () { // alert("Company deleted successfully."); const messageContainer = document.getElementById('message-container'); if (messageContainer) messageContainer.innerHTML = ` <div class='alert alert-danger'> Company details deleted successfully. <button class='close' onclick='dismissMessage()'>OK</button> </div>`; fetchCompanies(); // Refresh the table }, error: function (xhr) { alert("Error deleting company: " + xhr.responseJSON.detail); } }); } function saveupdate(companyId) { console.log("company_id", companyId); var companyData = { company_name: $('#company_name').val(), company_code: $('#company_code').val(), domain: $('#domain').val(), llm_tools: $('#llm_tools').val().join(',') // Assuming llm_tools is a multi-select }; console.log("company_data", companyData);
$.ajax({ url: `/api/putcompanydetails/${companyId}`, type: 'PUT', data: companyData, success: function (response) { // alert('Company details updated successfully.'); const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-success'> Company Data Updated successfully <button class='close' onclick='dismissMessage()'>OK</button> </div>`; $('#addModal').modal('hide'); // Close the modal after saving fetchCompanies(); }, error: function () { const messageContainer = document.getElementById('message-container'); if(messageContainer) messageContainer.innerHTML = ` <div class='alert alert-danger'> No changes have been made to update company details. <button class='close' onclick='dismissMessage()'>OK</button> </div>`; $('#addModal').modal('hide'); //alert('No changes have been made to update company details.'); } }); } function saveProfile() { const form = document.getElementById('companyForm');
// Check if the form is valid if (!form.checkValidity()) { // If the form is invalid, show validation messages and stop the submission form.reportValidity(); return; } // Gather form data const company_name = document.getElementById('company_name').value; const company_code = document.getElementById('company_code').value; const domain = document.getElementById('domain').value; const llm_tools = $('#llm_tools').val(); // Get selected values from Select2
// Prepare FormData let formData = new FormData(); formData.append("company_name", company_name); formData.append("company_code", company_code); formData.append("domain", domain); formData.append("llm_tools", llm_tools.join(','));
$.ajax({ type: "POST", url: "/submit_company_profile", data: formData, processData: false, contentType: false, success: function (response) { // alert("Data saved successfully"); // Show success message 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>`; fetchCompanies(); // Refresh table data
// Clear the form and close the modal // $('#companyForm')[0].reset(); $('#addModal').modal('hide'); }, error: function (xhr) { alert("An error occurred: " + xhr.responseJSON.detail); // Show error message } }); } function dismissMessage() { const messageContainer = document.getElementById('message-container'); if (messageContainer) { messageContainer.innerHTML = ''; } } async function fetchCompanies() { try { console.log("Fetching companies..."); const response = await fetch(`/api/companydetails`); const companies = await response.json(); console.log(companies); // Inspect the data here
if (!Array.isArray(companies)) { throw new TypeError('Expected an array of companies'); }
const table = $('#companyTable').DataTable(); table.clear(); // Clear existing table data
companies.forEach((company, index) => { table.row.add([ index + 1, company.company_name, company.company_code, company.domain, company.llm_tools, // Join array of tools into a string `<a href='#' class='btn btn-info btn-sm'onclick='viewCompany(${company.company_id})'><i class='fas fa-eye'></i></a>`, `<a href='#' class='btn btn-warning btn-sm'onclick='editCompany(${company.company_id})'><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>`
]).draw(false); }); } catch (error) { console.error('Error fetching companies:', error); } } function clearmodal() { $('#companyForm')[0].reset(); // Reset all form fields $('#llm_tools').val(null).trigger('change'); // Clear and reset the Select2 field $('#llm_tools').select2({ theme: 'bootstrap4', placeholder: 'Select', allowClear: true }); // Reinitialize Select2 }
</script></body>
</html>templates/company_profile.html+542-520