<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Energy Meters</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f4f4f4;
        }
        h1 {
            text-align: center;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            padding: 10px;
            border: 1px solid #ccc;
            text-align: left;
        }
        th {
            background-color: #007BFF;
            color: white;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
        .filter-container {
            margin-top: 20px;
            text-align: center;
        }
        .password-container {
            text-align: center;
            margin-top: 20%;
        }
        #mainContent {
            display: none; /* Hide main content initially */
        }
        #errorMessage {
            color: red;
            display: none;
        }
    </style>
</head>
<body>

    <div class="password-container" id="passwordContainer">
        <h2>Enter Password to Access the Page</h2>
        <input type="password" id="passwordInput" placeholder="Enter Password">
        <button onclick="checkPassword()">Submit</button>
        <p id="errorMessage">Incorrect password. Try again.</p>
    </div>

    <div id="mainContent">
        <h1>Energy Meters</h1>

        <div class="filter-container">
            <label for="statusFilter">Filter by Status:</label>
            <select id="statusFilter">
                <option value="all">All</option>
                <option value="on">On</option>
                <option value="off">Off</option>
            </select>
            <label for="orgIdFilter">Filter by Organization ID:</label>
            <select id="orgIdFilter">
                <option value="all">All</option>
            </select>
        </div>

        <table id="meterTable">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>ID</th>
                    <th>ORG ID</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <!-- Meter data will be inserted here -->
            </tbody>
        </table>
    </div>

    <script>
        const correctPassword = "securepassword123"; // Set your desired password

        function checkPassword() {
            const passwordInput = document.getElementById('passwordInput').value;
            const errorMessage = document.getElementById('errorMessage');
            if (passwordInput === correctPassword) {
                document.getElementById("passwordContainer").style.display = "none";
                document.getElementById("mainContent").style.display = "block";
            } else {
                errorMessage.style.display = "block";
            }
        }

        const url = 'https://iotapi.mautoiot.com/api/User/token';
        const emURL = 'https://iotapi.mautoiot.com/api/Meters';

        const data = {
            "email": "info@mauto.co",
            "password": "IneedkS@100%"
        };

        fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
        .then(response => {
            if (!response.ok) throw new Error('Error creating user token');
            return response.json();
        })
        .then(data => {
            const token = data.token;
            return fetch(emURL, {
                method: 'GET',
                headers: {
                    'Authorization': `Bearer ${token}`,
                    'Content-Type': 'application/json'
                }
            });
        })
        .then(response => {
            if (!response.ok) throw new Error('Error retrieving meters data');
            return response.json();
        })
        .then(meters => {
            const tableBody = document.getElementById('meterTable').querySelector('tbody');
            const orgIdFilter = document.getElementById('orgIdFilter');
            const uniqueOrgIds = new Set();

            meters.forEach(meter => {
                const row = document.createElement('tr');
                row.innerHTML = `<td>${meter.name}</td><td>${meter.id}</td><td>${meter.organizationId}</td><td>${meter.status}</td>`;
                row.dataset.status = meter.status.toLowerCase();
                row.dataset.orgId = meter.organizationId;
                tableBody.appendChild(row);

                uniqueOrgIds.add(meter.organizationId);
            });

            uniqueOrgIds.forEach(orgId => {
                const option = document.createElement('option');
                option.value = orgId;
                option.textContent = orgId;
                orgIdFilter.appendChild(option);
            });

            const statusFilter = document.getElementById('statusFilter');
            statusFilter.addEventListener('change', () => {
                filterTable(statusFilter.value, orgIdFilter.value);
            });

            orgIdFilter.addEventListener('change', () => {
                filterTable(statusFilter.value, orgIdFilter.value);
            });
        })
        .catch(error => {
            console.error(error);
            alert(error.message);
        });

        function filterTable(status, orgId) {
            const rows = document.querySelectorAll('#meterTable tbody tr');
            rows.forEach(row => {
                const matchesStatus = status === 'all' || row.dataset.status === status;
                const matchesOrgId = orgId === 'all' || row.dataset.orgId === orgId;

                if (matchesStatus && matchesOrgId) {
                    row.style.display = '';
                } else {
                    row.style.display = 'none';
                }
            });
        }
    </script>

</body>
</html>
