<template>
  <el-dialog 
    title="批量设置" 
    v-model="visible" 
    :width="600" 
    :close-on-click-modal="false" 
    draggable>
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="100px" 
      v-loading="loading">
      <el-alert
        title="提示"
        type="info"
        :closable="false"
        style="margin-bottom: 20px;">
        <template #default>
          <div>已选择 {{ selectedCount }} 条教室记录，请选择要设置的二级学院</div>
        </template>
      </el-alert>
      <el-form-item label="二级学院" prop="deptCode">
        <el-select 
          v-model="form.deptCode" 
          placeholder="请选择二级学院" 
          clearable
          filterable
          style="width: 100%">
          <el-option
            v-for="item in deptList"
            :key="item.deptCode"
            :label="item.deptName"
            :value="item.deptCode">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="TeachClassroomBatchDialog">
import { ref, reactive, nextTick, onMounted, computed } from 'vue'
import { useMessage } from '/@/hooks/message'
import { batchSet } from '/@/api/stuwork/teachclassroom'
import { getDeptListByLevelTwo } from '/@/api/basic/basicdept'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const deptList = ref<any[]>([])
const selectedRows = ref<any[]>([])

// 提交表单数据
const form = reactive({
  deptCode: ''
})

// 选中数量
const selectedCount = computed(() => selectedRows.value.length)

// 定义校验规则
const dataRules = {
  deptCode: [
    { required: true, message: '请选择二级学院', trigger: 'change' }
  ]
}

// 打开弹窗
const openDialog = (rows: any[]) => {
  selectedRows.value = rows
  visible.value = true
  resetForm()
  nextTick(() => {
    dataFormRef.value?.resetFields()
  })
}

// 重置表单
const resetForm = () => {
  form.deptCode = ''
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    if (selectedRows.value.length === 0) {
      useMessage().warning('请先选择要设置的教室')
      return
    }
    
    loading.value = true
    try {
      // 根据接口文档，请求体格式为数组，每个元素包含 id 和 deptCode
      const requestData = selectedRows.value.map((row: any) => ({
        id: row.id,
        deptCode: form.deptCode
      }))
      await batchSet(requestData)
      useMessage().success('批量设置成功')
      visible.value = false
      selectedRows.value = []
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || '批量设置失败')
    } finally {
      loading.value = false
    }
  })
}

// 获取系部列表
const getDeptListData = async () => {
  try {
    const res = await getDeptListByLevelTwo()
    if (res.data) {
      deptList.value = Array.isArray(res.data) ? res.data : []
    }
  } catch (err) {
    deptList.value = []
  }
}

// 初始化
onMounted(() => {
  getDeptListData()
})

// 暴露方法
defineExpose({
  openDialog
})
</script>

<style scoped lang="scss">
</style>

