<template>
  <el-dialog 
    title="门锁密码" 
    v-model="visible" 
    :width="500" 
    :close-on-click-modal="false" 
    draggable>
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="120px" 
      v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="楼号">
            <el-input 
              v-model="form.buildingNo" 
              placeholder="楼号" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="教室位置">
            <el-input 
              v-model="form.position" 
              placeholder="教室位置" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="门锁密码" prop="password">
            <el-input 
              v-model="form.password" 
              type="password"
              show-password
              placeholder="请输入门锁密码" 
              clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </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="ClassroomPasswordDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { editAssets } from '/@/api/stuwork/classassets'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)

// 提交表单数据
const form = reactive({
  id: '',
  buildingNo: '',
  deptName: '',
  deptCode: '',
  classCode: '',
  classNo: '',
  position: '',
  password: ''
})

// 定义校验规则
const dataRules = {
  password: [
    { required: true, message: '请输入门锁密码', trigger: 'blur' }
  ]
}

// 打开弹窗
const openDialog = async (row: any) => {
  visible.value = true
  resetForm()
  
  // 填充现有数据
  if (row) {
    form.id = row.id || ''
    form.buildingNo = row.buildingNo || ''
    form.deptName = row.deptName || ''
    form.deptCode = row.deptCode || ''
    form.classCode = row.classCode || ''
    form.classNo = row.classNo || ''
    form.position = row.position || ''
    form.password = row.password || ''
  }
  
  nextTick(() => {
    dataFormRef.value?.clearValidate()
  })
}

// 重置表单
const resetForm = () => {
  form.id = ''
  form.buildingNo = ''
  form.deptName = ''
  form.deptCode = ''
  form.classCode = ''
  form.classNo = ''
  form.position = ''
  form.password = ''
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      // 只提交必要的字段用于修改门锁密码
      const submitData = {
        id: form.id,
        buildingNo: form.buildingNo,
        deptName: form.deptName,
        deptCode: form.deptCode,
        classCode: form.classCode,
        classNo: form.classNo,
        position: form.position,
        password: form.password
      }
      await editAssets(submitData)
      useMessage().success('保存成功')
      visible.value = false
      emit('refresh')
    } catch (_err) {
      // 错误由 request 拦截器统一提示
    } finally {
      loading.value = false
    }
  })
}

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

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