
import re

file_path = 'includes/layout/footer.php'

with open(file_path, 'r') as f:
    content = f.read()

# 1. Update HTML for #ev-inquiries
# Search for the specific block
html_pattern = r'(<div class="tab-pane fade" id="ev-inquiries" role="tabpanel" aria-labelledby="ev-inquiries-tab">\s*<div class="alert alert-info py-3">.*?</div>\s*</div>)'

new_html = r"""<div class="tab-pane fade" id="ev-inquiries" role="tabpanel" aria-labelledby="ev-inquiries-tab">
                            <div class="d-flex justify-content-end gap-2 mb-3">
                                 <button type="button" class="btn btn-sm btn-info text-white" id="btn_add_lab_inquiry">
                                     <i class="bi bi-flask"></i> <?php echo __('add_inquiry'); ?>
                                 </button>
                                 <button type="button" class="btn btn-sm btn-dark" id="btn_add_xray_inquiry">
                                     <i class="bi bi-camera"></i> <?php echo __('add_xray_inquiry'); ?>
                                 </button>
                            </div>
                            <div id="edit_visit_inquiries_list" class="list-group">
                                <!-- Populated by JS -->
                            </div>
                        </div>"""

content = re.sub(html_pattern, new_html, content, flags=re.DOTALL)


# 2. Update JS showEditVisitModal
# We will inject the new logic before the last line of the function (bootstrap.Modal....)
js_pattern = r'(function showEditVisitModal\(visit\) \{.*?)(bootstrap\.Modal\.getOrCreateInstance\(el\)\.show\(\);)'

js_injection = r"""
            // Update Add Buttons
            const btnLab = document.getElementById('btn_add_lab_inquiry');
            const btnXray = document.getElementById('btn_add_xray_inquiry');
            if (btnLab) {
                btnLab.onclick = function() {
                    showLabInquiryModalFromVisit(visit.id, visit.patient_id, visit.patient_name);
                };
            }
            if (btnXray) {
                btnXray.onclick = function() {
                    showXrayInquiryModalFromVisit(visit.id, visit.patient_id, visit.patient_name);
                };
            }

            // Populate Inquiries List
            const inqContainer = document.getElementById('edit_visit_inquiries_list');
            if (inqContainer) {
                let html = '';
                
                // Lab Inquiries
                if (visit.lab_inquiries && visit.lab_inquiries.length > 0) {
                    visit.lab_inquiries.forEach(li => {
                        li.patient_name = visit.patient_name; // Ensure patient name is passed
                        // Escape single quotes in JSON
                        const jsonLi = JSON.stringify(li).replace(/'/g, "&#39;").replace(/"/g, "&quot;");
                        html += `
                            <div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                                <div>
                                    <span class="badge bg-info me-2">Lab</span>
                                    <span class="fw-bold">#${li.id}</span>
                                    <span class="text-muted small ms-2">${li.status}</span>
                                    ${li.notes ? `<div class="small text-muted mt-1"><em>${li.notes}</em></div>` : ''}
                                </div>
                                <button type="button" class="btn btn-sm btn-outline-secondary" onclick='showEditInquiryModal(${jsonLi})'>
                                    <i class="bi bi-pencil"></i>
                                </button>
                            </div>
                        `;
                    });
                }

                // Xray Inquiries
                if (visit.xray_inquiries && visit.xray_inquiries.length > 0) {
                    visit.xray_inquiries.forEach(xi => {
                        xi.patient_name = visit.patient_name; // Ensure patient name is passed
                        const jsonXi = JSON.stringify(xi).replace(/'/g, "&#39;").replace(/"/g, "&quot;");
                        html += `
                            <div class="list-group-item list-group-item-action d-flex justify-content-between align-items-center">
                                <div>
                                    <span class="badge bg-dark me-2">X-Ray</span>
                                    <span class="fw-bold">#${xi.id}</span>
                                    <span class="text-muted small ms-2">${xi.status}</span>
                                    ${xi.notes ? `<div class="small text-muted mt-1"><em>${xi.notes}</em></div>` : ''}
                                </div>
                                 <button type="button" class="btn btn-sm btn-outline-secondary" onclick='showEditXrayInquiryModal(${jsonXi})'>
                                    <i class="bi bi-pencil"></i>
                                </button>
                            </div>
                        `;
                    });
                }
                
                if (html === '') {
                    html = '<div class="text-center text-muted p-3"><?php echo __("no_data_found"); ?></div>';
                }
                inqContainer.innerHTML = html;
            }
            
            """

content = re.sub(js_pattern, r'\1' + js_injection + r'\2', content, flags=re.DOTALL)

with open(file_path, 'w') as f:
    f.write(content)

print("Updated includes/layout/footer.php successfully.")
