@endsection
@section('script')
// Function to generate room HTML and append it to the modal content
function generateHiddenInputs(meter, room) {
//console.log(meter);
//console.log('Power supply: ' + meter.is_power_supply_on);
return `
`;
}
function generateRoomHTML(room, id_house) {
console.log('Render room HTML', room);
const tableBody = document.querySelector('#room-details-table tbody');
if (!tableBody) {
console.error('Table body not found for id_house:', id_house);
return;
}
const meter = room.meter || {};
const isRemoteReady = meter.is_remote_ready || false;
const isRemoteLive = meter.is_remote_live || false;
const isConfigured = true; // Assuming remote control is always configured for this example
const hiddenInputsHTML = generateHiddenInputs(meter, room);
const roomRowHTML = `
';
});
});
});
});
function activate_swith(room_id)
{
switch_status = $('#meter_switch_status_'+room_id).val() == 0 || $('#meter_switch_status_'+room_id).val() == false ? false : true;
$('#meter_switch_'+room_id).toggles({
drag: true, // allow dragging the toggle between positions
click: true, // allow clicking on the toggle
text: {
on: 'ON', // text for the ON position
off: 'OFF' // and off
},
on: switch_status, // is the toggle ON on init
animate: 250, // animation time (ms)
easing: 'swing', // animation transition easing function
checkbox: null, // the checkbox to toggle (for use in forms)
clicker: null, // element that can be clicked on to toggle. removes binding from the toggle itself (use nesting)
type: 'select' // if this is set to 'select' then the select style toggle will be used
});
console.log('Activate switch : ' + room_id);
}
function toggleHouse(houseId) {
const panel = document.getElementById(`accordion-${houseId}`);
if (!panel) {
console.error(`Panel with id panel-${houseId} not found.`);
return;
}
const isPanelOpen = panel.style.display === 'block';
if (isPanelOpen) {
// If the panel is open, hide it and remove dynamically generated content
panel.style.display = 'none';
// Remove dynamically generated elements
const generatedElements = panel.querySelectorAll('[id^="room-button-"], [id^="meter_switch_"], [id^="meter_id_"], [id^="meter_switch_status_"]');
generatedElements.forEach(element => {
element.remove();
});
console.log(`Closed panel and removed dynamically generated elements for houseId: ${houseId}`);
} else {
// If the panel is closed, show it
panel.style.display = 'block';
console.log(`Opened panel for houseId: ${houseId}`);
}
}
document.addEventListener('DOMContentLoaded', function () {
const closeButton = document.querySelector('[data-bs-dismiss="modal"]');
closeButton.addEventListener('click', function () {
const modal = document.getElementById('roomDetailsModal');
modal.classList.remove('show');
modal.setAttribute('aria-hidden', 'true');
modal.style.display = 'none';
// Reset the body's padding to avoid screen content shifting
document.body.classList.remove('modal-open');
document.body.style.paddingRight = '';
// Remove the modal backdrop
const backdrop = document.querySelector('.modal-backdrop');
if (backdrop) backdrop.remove();
});
});
$(document).ready(function () {
// Initialize DataTable on page load
var table = $('#power_meter_table').DataTable({
"order": [[0, 'asc']], // Order by the first column
"paging": true, // Enable pagination
"pageLength": 50, // Set the default number of rows per page
"searching": true, // Enable search functionality
"ordering": true, // Enable column ordering
"info": true, // Display table information (e.g., showing 1 to 10 of 100 entries)
"autoWidth": false, // Disable auto-width for columns
"fixedHeader": true, // Keep the table header fixed on scroll
"bProcessing": true, // Enable processing indicator during AJAX load
"lengthChange": false, // Disable "Show entries" dropdown
});
// Handle show-details button click to toggle details row
$('.show-details-btn').on('click', function () {
const houseId = $(this).data('house-id');
const detailsRow = $(`#details-row-${houseId}`);
const accordionBody = $(`#collapse-${houseId} .accordion-body`);
// Toggle the details row visibility
detailsRow.toggle();
// Fetch data only if the accordion is empty
if (accordionBody.text().trim() === 'Loading details...') {
const url = `${getHouseMeterDataByHouseIdUrl}?house_id=${houseId}`;
fetch(url)
.then(response => response.json())
.then(data => {
if (data.status_code) {
accordionBody.empty(); // Clear the loading message
const rooms = data.rooms;
rooms.forEach(room => {
const roomHtml = `
Room ID: ${room.id_house_room}
Details: ${room.details}
`;
accordionBody.append(roomHtml);
});
// Remove the loading image after loading content
detailsRow.find('img').remove();
// Redraw the DataTable to handle dynamic rows
table.draw();
} else {
console.error('Error:', data.message);
accordionBody.html('