<template>
  <el-dialog
    title="编辑用户"
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    :close-on-click-modal="false"
    append-to-body
    destroy-on-close
    :align-center="true"
    :fullscreen="false"
    @close="handleClose"
    width="600px"
  >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
      <!-- 姓名和用户名 -->
      <el-form-item label="唯一标识" prop="useridentifier">
        <el-input v-model="form.useridentifier" placeholder="请输入真实姓名" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <!-- 备注 -->
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          placeholder="请输入备注"
        />
      </el-form-item>

      <!-- 是否明文和是否脱敏 -->
      <el-form-item>
        <el-checkbox v-model="form.isoriginal">是否明文</el-checkbox>
        <el-checkbox v-model="form.ismask" style="margin-left: 20px">是否脱敏</el-checkbox>
      </el-form-item>
      </el-form>
    <template #footer>
      <div class="dialog-footer">
        <div class="footer-buttons">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'AppUserEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      default: () => null
    }
  },
  emits: ['update:visible', 'success'],
  watch: {
    visible(val) {
      if (val && this.formData) {
        this.form = {
          ...this.formData,
          password: '',
          confirmPassword: ''
        }
      }
    }
  },
  data() {
    return {
      form: {
        useridentifier: '',
        username: '',
        remark: '',
        isoriginal: false,
        ismask: false
      },
      rules: {
        useridentifier: [
          { required: true, message: '请输入唯一标识', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('update:visible', false)
      this.$refs.formRef?.resetFields()
      this.form = {
        useridentifier: '',
        username: '',
        remark: '',
        isoriginal: false,
        ismask: false
      }
    },
    handleSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 提交表单逻辑
          const params = { ...this.form }
          // 如果是编辑模式且没有修改密码，则不提交密码字段
          if (this.formData && !params.password) {
            delete params.password
            delete params.confirmPassword
          }
          
          console.log('submit form', params)
          // 调用接口保存数据
          this.$emit('success')
          this.handleClose()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>


</style>
