<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="500px"
    destroy-on-close
    :close-on-click-modal="false"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" :disabled="isEdit" />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="form.realName" placeholder="请输入真实姓名" />
      </el-form-item>
      <el-form-item label="角色" prop="roles">
        <el-select v-model="form.roles" multiple placeholder="请选择角色" style="width: 100%">
          <el-option label="系统管理员" value="admin" />
          <el-option label="宿管主管" value="manager" />
          <el-option label="宿管员" value="staff" />
          <el-option label="维修人员" value="repair" />
          <el-option label="学生" value="student" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属部门" prop="department">
        <el-select v-model="form.department" placeholder="请选择部门" style="width: 100%">
          <el-option label="公寓管理中心" value="center" />
          <el-option label="后勤保障部" value="logistics" />
          <el-option label="学工处" value="student_affairs" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.remark" type="textarea" rows="3" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="handleSave">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['success'])

const visible = ref(false)
const formRef = ref()
const isEdit = ref(false)

const form = reactive({
  id: '',
  username: '',
  realName: '',
  roles: [],
  department: '',
  status: 1,
  remark: ''
})

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  roles: [{ required: true, message: '请选择角色', trigger: 'change' }],
  department: [{ required: true, message: '请选择部门', trigger: 'change' }]
}

const title = computed(() => (isEdit.value ? '编辑用户' : '新增用户'))

const open = (row?: any) => {
  visible.value = true
  if (row) {
    isEdit.value = true
    Object.assign(form, row)
  } else {
    isEdit.value = false
    Object.assign(form, {
      id: '',
      username: '',
      realName: '',
      roles: [],
      department: '',
      status: 1,
      remark: ''
    })
  }
}

const handleSave = async () => {
  if (!formRef.value) return
  await formRef.value.validate((valid: boolean) => {
    if (valid) {
      ElMessage.success(isEdit.value ? '更新成功' : '添加成功')
      emit('success', { ...form })
      visible.value = false
    }
  })
}

defineExpose({ open })
</script>
