<template>
  <el-dialog 
    title="教室公物编辑" 
    v-model="visible" 
    :width="800" 
    :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="12" class="mb20">
          <el-form-item label="楼号" prop="buildingNo">
            <el-input 
              v-model="form.buildingNo" 
              placeholder="楼号" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="学院" prop="deptName">
            <el-input 
              v-model="form.deptName" 
              placeholder="学院" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="班级" prop="classCode">
            <el-input 
              v-model="form.classNo" 
              placeholder="班级" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="教室位置" prop="position">
            <el-input 
              v-model="form.position" 
              placeholder="教室位置" 
              disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="讲台类型" prop="platformType">
            <el-select 
              v-model="form.platformType" 
              placeholder="请选择讲台类型" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in platformTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="投影类型" prop="tyType">
            <el-select 
              v-model="form.tyType" 
              placeholder="请选择投影类型" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in tyTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="电视机" prop="tvType">
            <el-select 
              v-model="form.tvType" 
              placeholder="请选择电视机" 
              clearable
              filterable
              style="width: 100%">
              <el-option
                v-for="item in tvTypeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="方凳数量" prop="chairCnt">
            <el-input-number 
              v-model="form.chairCnt" 
              :min="0"
              placeholder="请输入方凳数量" 
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="mb20">
          <el-form-item label="课桌数量" prop="tableCnt">
            <el-input-number 
              v-model="form.tableCnt" 
              :min="0"
              placeholder="请输入课桌数量" 
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remarks">
            <el-input 
              v-model="form.remarks" 
              type="textarea" 
              :rows="3"
              placeholder="请输入备注" />
          </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="ClassroomAssetsDialog">
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { editAssets } from '/@/api/stuwork/classassets'
import { getDicts } from '/@/api/admin/dict'

const emit = defineEmits(['refresh'])

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

// 提交表单数据
const form = reactive({
  id: '',
  buildingNo: '',
  deptName: '',
  deptCode: '',
  classCode: '',
  classNo: '',
  position: '',
  platformType: '',
  tyType: '',
  tvType: '',
  chairCnt: 0,
  tableCnt: 0,
  remarks: ''
})

// 定义校验规则
const dataRules = {
  platformType: [
    { required: true, message: '请选择讲台类型', trigger: 'change' }
  ],
  tyType: [
    { required: true, message: '请选择投影类型', trigger: 'change' }
  ],
  tvType: [
    { required: true, message: '请选择电视机', trigger: 'change' }
  ],
  chairCnt: [
    { required: true, message: '请填写方凳数量', trigger: 'blur' }
  ],
  tableCnt: [
    { 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.platformType = row.platformType || ''
    form.tyType = row.tyType || ''
    form.tvType = row.tvType || ''
    form.chairCnt = row.chairCnt || 0
    form.tableCnt = row.tableCnt || 0
    form.remarks = row.remarks || ''
  }
  
  nextTick(() => {
    dataFormRef.value?.clearValidate()
  })
}

// 重置表单
const resetForm = () => {
  form.id = ''
  form.buildingNo = ''
  form.deptName = ''
  form.deptCode = ''
  form.classCode = ''
  form.classNo = ''
  form.position = ''
  form.platformType = ''
  form.tyType = ''
  form.tvType = ''
  form.chairCnt = 0
  form.tableCnt = 0
  form.remarks = ''
}

// 提交表单
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,
        platformType: form.platformType,
        tyType: form.tyType,
        tvType: form.tvType,
        chairCnt: form.chairCnt,
        tableCnt: form.tableCnt,
        remarks: form.remarks
      }
      await editAssets(submitData)
      useMessage().success('保存成功')
      visible.value = false
      emit('refresh')
    } catch (_err) {
      // 错误由 request 拦截器统一提示
    } finally {
      loading.value = false
    }
  })
}

// 获取讲台类型字典
const getPlatformTypeDict = async () => {
  try {
    const res = await getDicts('platform_type')
    if (res.data) {
      platformTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    platformTypeList.value = []
  }
}

// 获取投影类型字典
const getTyTypeDict = async () => {
  try {
    const res = await getDicts('ty_type')
    if (res.data) {
      tyTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    tyTypeList.value = []
  }
}

// 获取电视机类型字典
const getTvTypeDict = async () => {
  try {
    const res = await getDicts('tv_type')
    if (res.data) {
      tvTypeList.value = Array.isArray(res.data) ? res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      })) : []
    }
  } catch (err) {
    tvTypeList.value = []
  }
}

// 初始化
onMounted(() => {
  getPlatformTypeDict()
  getTyTypeDict()
  getTvTypeDict()
})

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

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