<script setup>
import { computed } from 'vue';

const props = defineProps({
  resource: { type: String, required: true },
  label: { type: String, default: '数据' },
  template: { type: Boolean, default: true },
  importable: { type: Boolean, default: true },
  examId: { type: String, default: '' },
  batchId: { type: String, default: '' }
});
const emit = defineEmits(['import']);
const query = computed(() => {
  const params = new URLSearchParams();
  if (props.examId) params.set('examId', props.examId);
  if (props.batchId) params.set('batchId', props.batchId);
  return params.toString();
});
function url(template = false) {
  const params = new URLSearchParams(query.value);
  if (template) params.set('template', '1');
  return `/api/admin/excel/${props.resource}${params.size ? `?${params}` : ''}`;
}
function selectFile(event) {
  const file = event.target.files?.[0];
  if (file) emit('import', { file, input: event.target });
}
</script>

<template>
  <div class="excel-action-bar excel-action-bar--descriptive">
    <span><strong>{{ label }} Excel</strong><small>{{ importable ? '使用系统模板可获得逐行校验' : '按当前账号数据范围导出' }}</small></span>
    <div>
      <a v-if="template" :href="url(true)">下载模板</a>
      <a :href="url(false)">导出当前数据</a>
      <label v-if="importable">导入 Excel<input type="file" accept=".xlsx" hidden @change="selectFile"></label>
    </div>
  </div>
</template>
