let activeCard = null;
document.querySelectorAll('.card-container').forEach(cardContainer => {
    cardContainer.addEventListener('click', () => {
        const card = cardContainer.querySelector('.card');
        if (activeCard === card) return;
        if (activeCard) {
            activeCard.classList.remove('flip');
        }
            card.classList.add('flip');
            activeCard = card;
      });
});
// Sample patient data for testing
let patients = [
    { id: 1, name: 'Manjeet Raj', dob: '1985-01-01', ssn: '1234', address: 'Noida
Scetor 125' },
    { id: 2, name: 'Bishant', dob: '1990-02-15', ssn: '4321', address: 'Delhi
Sector 7' }
];
// Track the last used patient ID
let lastPatientId = patients.length;
// Session management: Check if user is logged in
function protectPage() {
    if (!sessionStorage.getItem('loggedIn')) {
        alert("You need to be logged in to access this page.");
        window.location.href = 'index.html';
    }
}
// Logout function
function logout() {
    sessionStorage.removeItem('loggedIn');
    window.location.href = 'index.html';
}
// New Patient Registration
const patientForm = document.getElementById('patientForm');
if (patientForm) {
    patientForm.addEventListener('submit', function (e) {
        e.preventDefault();
            const newPatient = {
                id: ++lastPatientId,
                name: document.getElementById('name').value,
                dob: document.getElementById('dob').value,
                ssn: document.getElementById('ssn').value,
                address: document.getElementById('address').value
            };
            // Push new patient to the array
            patients.push(newPatient);
            document.getElementById('responseMessage').textContent = "Patient
registered successfully!";
        patientForm.reset();
    });
}
// Update patient information
const editPatientForm = document.getElementById('editPatientForm');
if (editPatientForm) {
    editPatientForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const ssn = document.getElementById('editPatientSsn').value;
        const patient = patients.find(p => p.ssn === ssn);
        if (patient) {
             patient.name = document.getElementById('editName').value;
             patient.dob = document.getElementById('editDob').value;
             patient.address = document.getElementById('editAddress').value;
             document.getElementById('editResponseMessage').textContent = "Patient
information updated successfully!";
             editPatientForm.reset();
        } else {
             document.getElementById('editResponseMessage').textContent = "Patient
not found.";
        }
    });
}
// Fetch patient details for editing
function fetchPatient() {
    const ssn = document.getElementById('editPatientSsn').value;
    const patient = patients.find(p => p.ssn === ssn);
    if (patient) {
        document.getElementById('editName').value = patient.name;
        document.getElementById('editDob').value = patient.dob;
        document.getElementById('editAddress').value = patient.address;
        document.getElementById('editPatientDetails').style.display = 'block';
    } else {
        alert("Patient not found.");
    }
}
// Delete patient
const deletePatientForm = document.getElementById('deletePatientForm');
if (deletePatientForm) {
    deletePatientForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const ssn = document.getElementById('deletePatientSsn').value;
        const patientIndex = patients.findIndex(p => p.ssn === ssn);
        if (patientIndex > -1) {
            patients.splice(patientIndex, 1);
            document.getElementById('deleteResponseMessage').textContent = "Patient
deleted successfully!";
            deletePatientForm.reset();
            // Update lastPatientId if necessary
            lastPatientId = patients.length > 0 ? patients[patients.length -
1].id : 0;
        } else {
            document.getElementById('deleteResponseMessage').textContent = "Patient
not found.";
        }
    });
}
// View all active patients
function viewPatients() {
    const patientListDiv = document.getElementById('patientList');
    patientListDiv.innerHTML = '';
    patients.forEach(patient => {
        const patientDiv = document.createElement('div');
        patientDiv.textContent = `ID: ${patient.id}, Name: ${patient.name}, DOB: $
{patient.dob}, SSN: ${patient.ssn}, Address: ${patient.address}`;
        patientListDiv.appendChild(patientDiv);
    });
}
// Update patient search functionality
const searchForm = document.getElementById('searchForm');
if (searchForm) {
    searchForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const ssn = document.getElementById('searchPatientSsn').value;
        const patient = patients.find(p => p.ssn === ssn);
         if (patient) {
             document.getElementById('searchResult').textContent = `Patient found:
ID: ${patient.id}, Name: ${patient.name}`;
         } else {
             document.getElementById('searchResult').textContent = "Patient not
found.";
         }
    });
}
// Update billing functionality
const billingForm = document.getElementById('billingForm');
if (billingForm) {
    billingForm.addEventListener('submit', function (e) {
        e.preventDefault();
        const billingSsn = document.getElementById('billingPatientSsn').value;
        const roomBill = parseFloat(document.getElementById('roomBill').value);
        const pharmacyBill =
parseFloat(document.getElementById('pharmacyBill').value);
        const diagnosticsBill =
parseFloat(document.getElementById('diagnosticsBill').value);
         const patient = patients.find(p => p.ssn === billingSsn);
         if (patient) {
             const totalBill = roomBill + pharmacyBill + diagnosticsBill;
             document.getElementById('billingResult').textContent = `Total Bill for
${patient.name}: Rs ${totalBill.toFixed(2)}`;
         } else {
             document.getElementById('billingResult').textContent = "Patient not
found.";
         }
    });
}