<template>
    <div>
      <label>
        上传 Weapon.xml：
        <input type="file" @change="onWeaponFileChange" />
      </label>
      <br />
      <label>
        上传单位 XML（如 AlliedAntiInfantryInfantry.xml）：
        <input type="file" @change="onUnitFileChange" />
      </label>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue'
  
  const emit = defineEmits(['updateWeaponTemplates', 'updateUnitDoc'])
  
  const onWeaponFileChange = (e) => {
    const file = e.target.files[0]
    if (!file) return
  
    const reader = new FileReader()
    reader.onload = (event) => {
      const parser = new DOMParser()
      const xml = parser.parseFromString(event.target.result, 'text/xml')
      const templates = xml.querySelectorAll('WeaponTemplate')
      const ids = new Set()
      templates.forEach((t) => {
        const id = t.getAttribute('id')
        if (id) ids.add(id)
      })
      emit('updateWeaponTemplates', ids)
    }
    reader.readAsText(file)
  }
  
  const onUnitFileChange = (e) => {
    const file = e.target.files[0]
    if (!file) return
  
    const reader = new FileReader()
    reader.onload = (event) => {
      const parser = new DOMParser()
      const xml = parser.parseFromString(event.target.result, 'text/xml')
      emit('updateUnitDoc', xml)
    }
    reader.readAsText(file)
  }
  </script>
  