Spaces:
Runtime error
Runtime error
| <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/[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 ; | |
| } | |
| .card { | |
| padding: 0; | |
| margin-top: 10px; | |
| margin-left: -40px; | |
| } | |
| .content-wrapper { | |
| background-color: transparent ; | |
| text-align: center; | |
| } | |
| .wrapper { | |
| background-color: transparent ; | |
| } | |
| .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 ; | |
| 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/[email protected]/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> | |
| </html> |