<template>
  <el-dialog v-model="visible" title="添加重点关注对象" width="500px">
    <el-form label-width="80px">
      <el-form-item label="选择学生">
        <el-select 
          v-model="form.studentId" 
          filterable 
          remote 
          :remote-method="remoteMethod" 
          placeholder="搜索学生"
          style="width: 100%"
        >
          <el-option v-for="item in studentOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="关注标签">
        <el-select v-model="form.tags" multiple placeholder="请选择标签">
          <el-option 
            v-for="type in typeOptions" 
            :key="type.value" 
            :label="type.label" 
            :value="type.value" 
          />
        </el-select>
      </el-form-item>
      <el-form-item label="关注等级">
        <el-rate v-model="form.level" :max="3" :texts="['一般', '重要', '紧急']" show-text />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="confirm">确认添加</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  typeOptions: {
    type: Array,
    default: () => []
  }
})

const emit = defineEmits(['success'])

const visible = ref(false)
const studentOptions = ref([])

const form = reactive({
  studentId: '',
  tags: [],
  level: 1
})

const open = () => {
  form.studentId = ''
  form.tags = []
  form.level = 1
  visible.value = true
}

const remoteMethod = (query) => {
  if (query) {
    setTimeout(() => {
      studentOptions.value = [
        { value: '31901004', label: '杨勇捷 (31901004)' },
        { value: '31901005', label: '苑国波 (31901005)' },
      ]
    }, 200)
  } else {
    studentOptions.value = []
  }
}

const confirm = () => {
  if (!form.studentId) return
  visible.value = false
  ElMessage.success('已添加至重点关注名单')
  emit('success')
}

defineExpose({
  open
})
</script>
