

import re

file_path = 'includes/layout/footer.php'

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

# 1. Update Add Patient Modal
# Look for the name input in addPatientModal
pattern_add = r'(<div class="modal fade" id="addPatientModal".*?name="name".*?</div>)'
replacement_add = r'\1\n' + \
r'''                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label"><?php echo __('civil_id'); ?></label>
                            <input type="text" name="civil_id" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label"><?php echo __('nationality'); ?></label>
                            <select name="nationality" class="form-select select2-modal">
                                <option value=""><?php echo __('search'); ?>...</option>
                                <?php foreach ($all_countries as $c): ?>
                                <option value="<?php echo htmlspecialchars($c); ?>"><?php echo htmlspecialchars($c); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?php echo __('city'); ?></label>
                        <input type="text" name="city" class="form-control">
                    </div>'''

# We need to be careful not to match too much.
# Let's find the specific div for name inside addPatientModal
# It's better to find the index.

add_modal_start = content.find('id="addPatientModal"')
if add_modal_start != -1:
    # Find the name input div AFTER this start
    name_div_end = content.find('</div>', content.find('name="name"', add_modal_start)) + 6
    
    # Check if we already added it (idempotency)
    if 'name="civil_id"' not in content[name_div_end:name_div_end+500]:
        content = content[:name_div_end] + replacement_add + content[name_div_end:]

# 2. Update Edit Patient Modal
edit_modal_start = content.find('id="editPatientModal"')
if edit_modal_start != -1:
    # Find the name input div AFTER this start
    name_div_end = content.find('</div>', content.find('name="name"', edit_modal_start)) + 6
    
    replacement_edit = r'''
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label"><?php echo __('civil_id'); ?></label>
                            <input type="text" name="civil_id" id="edit_patient_civil_id" class="form-control">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label"><?php echo __('nationality'); ?></label>
                            <select name="nationality" id="edit_patient_nationality" class="form-select select2-modal">
                                <option value=""><?php echo __('search'); ?>...</option>
                                <?php foreach ($all_countries as $c): ?>
                                <option value="<?php echo htmlspecialchars($c); ?>"><?php echo htmlspecialchars($c); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?php echo __('city'); ?></label>
                        <input type="text" name="city" id="edit_patient_city" class="form-control">
                    </div>'''
    
    if 'name="civil_id"' not in content[name_div_end:name_div_end+500]:
        content = content[:name_div_end] + replacement_edit + content[name_div_end:]

# 3. Update JS showEditPatientModal
js_func = 'function showEditPatientModal(patient) {'
js_start = content.find(js_func)
if js_start != -1:
    # Find the fields object
    fields_start = content.find("var fields = {", js_start)
    if fields_start != -1:
        # Insert new fields
        insert_pos = content.find("'edit_patient_name': patient.name,", fields_start) + len("'edit_patient_name': patient.name,")
        
        new_js_fields = r'''
                'edit_patient_civil_id': patient.civil_id || '',
                'edit_patient_nationality': patient.nationality || '',
                'edit_patient_city': patient.city || '',
'''''
        
        if "'edit_patient_civil_id'" not in content[fields_start:fields_start+500]:
             content = content[:insert_pos] + new_js_fields + content[insert_pos:]
    
    # Find the loop to add trigger
    loop_start = content.find("for (var id in fields) {", js_start)
    if loop_start != -1:
        # We want to add the trigger inside the loop
        # Find 'if (field) field.value = fields[id];'
        val_set = content.find("if (field) field.value = fields[id];", loop_start)
        if val_set != -1:
             replace_js = '''if (field) {
                    field.value = fields[id];
                    if (id === 'edit_patient_nationality') {
                        $(field).val(fields[id]).trigger('change');
                    }
                }'''
             content = content.replace("if (field) field.value = fields[id];", replace_js, 1)

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

print("Updated footer.php")
