Redmind_GPT_API / templates /company_profile.html
Redmind's picture
Update templates/company_profile.html
47f2c80 verified
raw
history blame
24.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<title>Company Profile</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Include AdminLTE CSS -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/@ttskch/[email protected]/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: -50px;
}
.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%;
}
.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: 10%;
}
th:nth-child(4),
td:nth-child(4) {
width: 15%;
}
th:nth-child(5),
td:nth-child(5) {
width: 20%;
}
th:nth-child(6),
td:nth-child(6) {
width: 10%;
}
th:nth-child(7),
td:nth-child(7) {
width: 10%;
}
th:nth-child(8),
td:nth-child(8) {
width: 10%;
}
.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>
<input type="hidden" id="userRole" name="userRole" value={{role}}>
<div class="row">
<div id="message-container" style="margin-left: 200px;width:600px;"></div>
</div>
</div>
</div>
</div>
<!-- Main content -->
<section class="content" id="startTable" style="display: none;">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-body table-container">
<table id="companyTable" class=" 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">&times;</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>
<div class="form-group">
<label for="domain">UserName<span class="text-danger">*</span></label>
<input type="text" class="form-control" id="username" name="username" required>
</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/[email protected]/dist/js/select2.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const role = sessionStorage.getItem('userRole');
const company_id=sessionStorage.getItem('company_id');
console.log('Current role:', role); // Debug statement to check the role
document.getElementById('userRole').value = role;
$('#addModal').hide(); // Make sure this is called initially
if (role === 'superadmin') {
console.log("insidse compamy profile super admin");
$('#startTable').show();
$('#addModal').hide();
document.getElementById('add').style.display = 'block';
} else {
console.log("insidse company profile admin");
document.getElementById('add').style.display = 'none';
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'Company Details';
viewCompany(company_id);
}
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';
console.log('Modal title after setting:', modalTitle.textContent); // This should log the new title
$('#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);
if (companyId)
{
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'Edit Company Details';
console.log('Modal title:', modalTitle.textContent);
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 username = sessionStorage.getItem('username');
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'View Company Details';
$.getJSON(`/api/getcompanydetails/${companyId}`, function (company) {
// Set values and attributes for the form fields
$('#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');
// Clear existing options in the dropdown
$('#llm_tools').empty();
// Add the options that are present in company.llm_tools
const availableOptions = {
'Database': 'Database',
'Static Documents': 'Static Documents',
'API': 'API'
};
if (Array.isArray(company.llm_tools)) {
company.llm_tools.forEach(tool => {
if (availableOptions[tool]) {
$('#llm_tools').append(new Option(availableOptions[tool], tool, true, true));
}
});
}
$('#llm_tools').prop('disabled', true); // Disable the dropdown
$('#username').val(username).attr('readonly', 'readonly'); // Set username field
// Show the modal with view details only
$('#saveupdate').hide();
$('#save').hide();
$('#addModal').modal('show');
}).fail(function () {
alert("Error retrieving company details.");
});
}
// Edit company details
function Editcompany(companyId) {
const username=sessionStorage.getItem('username');
const modalTitle = document.getElementById('addModalLabel');
modalTitle.textContent = 'Edit Company Details';
console.log('Modal title after setting:', modalTitle.textContent); // Should log "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');
$('#username').val(username).attr('readonly', 'readonly');// Disable the select input
//sessionStorage.setItem('llmTools', company.llm_tools); // Store as a string
$('#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' style='font-size:medium;margin-top:6px;' 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(','),
username: $('#username').val() // 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' style='font-size:medium;margin-top:6px;' 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' style='font-size:medium;margin-top:6px;' 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();
const username=$('#username').val();
// Prepare FormData
let formData = new FormData();
const password="password";
const rolesave="admin";
formData.append("company_name", company_name);
formData.append("company_code", company_code);
formData.append("domain", domain);
formData.append("llm_tools", llm_tools.join(','));
formData.append("username",username);
formData.append("password",password);
formData.append("role",rolesave);
$.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' style='font-size:medium;margin-top:6px;' 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>