<script setup lang="ts">
import { computed, ref, watch } from 'vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '查看',
  },
  students: {
    type: Array,
    default: () => [],
  },
})

const emit = defineEmits(['update:modelValue'])

const visible = ref<boolean>(props.modelValue)
watch(
  () => props.modelValue,
  (v) => {
    visible.value = v
  },
)
watch(
  () => visible.value,
  (v) => {
    emit('update:modelValue', v)
  },
)

const items = computed(() => {
  const list = Array.isArray(props.students) ? props.students : []
  return list
    .map((i: any) => {
      if (typeof i === 'string') return { name: i }
      return {
        name:
          i?.name ??
          i?.userName ??
          i?.realName ??
          i?.nickName ??
          i?.studentName ??
          '',
      }
    })
    .filter((i) => i.name)
})
</script>

<template>
  <el-dialog v-model="visible" :title="title" width="600px" align-center>
    <div class="student-list" v-if="items.length">
      <div class="grid">
        <div v-for="item in items" :key="item.name" class="chip">
          <span class="avatar">{{ item.name.slice(0, 1) }}</span>
          <span class="name">{{ item.name }}</span>
        </div>
      </div>
    </div>
    <div v-else class="empty">暂无数据</div>
  </el-dialog>
</template>

<style scoped>
.student-list {
  padding: 4px 2px;
}

.grid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  display: flex;
  align-items: center;
  padding: 6px 10px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 8px;
}

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  margin-right: 8px;
  font-weight: 600;
  color: #fff;
  background: #409eff;
  border-radius: 50%;
}

.name {
  font-size: 14px;
  color: #606266;
}

.empty {
  padding: 16px 0;
  color: #909399;
  text-align: center;
}
</style>
