<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="systemAccountFormRef"
    :rules="systemAccountFormRules"
    ref="platformAccountRef"
    style="max-width: 66%"
  >
    <el-form-item label="所属组织机构" prop="org">
      <el-input v-model="systemAccountFormRef.org" />
    </el-form-item>
    <el-form-item label="角色" prop="role">
      <el-input v-model="systemAccountFormRef.role" />
    </el-form-item>
    <el-form-item label="头像" prop="logo">
       <bg-upload-image v-model="systemAccountFormRef.logo" :showTips="true" :limit="1" :fileSize="500" :fileSizeUnit="'KB'" listType="picture-card" :accept="['.jpg','.jpeg','.png']" customTips="请选择图片上传：大小120 * 120像素支持jpg、png等格式，图片需小于500KB"></bg-upload-image>
    </el-form-item>
     <el-form-item label="账号" prop="account">
      <el-input v-model="systemAccountFormRef.account" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="systemAccountFormRef.phone" />
    </el-form-item>
     <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="systemAccountFormRef.password" />
    </el-form-item>
     <el-form-item label="确认密码" prop="confirm_password">
      <el-input type="password" v-model="systemAccountFormRef.confirm_password" />
    </el-form-item>
  
    <el-form-item label="是否启用" prop="is_use">
      <bg-switch :labels="['否','是']" :values="[0,1]" v-model="systemAccountFormRef.is_use"></bg-switch>
    </el-form-item>
    
  </el-form>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'

const systemAccountFormRef = reactive({
  org: '',
  role: '',
  logo: '',
  account: '',
  phone: '',
  password: '',
  confirm_password: '',
  is_use: 1,
 
})

const validatePass = (rule, value, callback) => {
  if (value !== systemAccountFormRef.password) {
    callback(new Error("密码输入不一致"))
  } else {
    callback()
  }
}

const systemAccountFormRules = reactive({
  org: [
    { required: true, message: '请选择组织', trigger: 'blur' },
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'blur' },
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
  ],
 
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  confirm_password: [
      { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
})



const platformAccountRef = ref(null)
const emit = defineEmits(['action'])

const submitForm = async () => {
  if (!platformAccountRef) return
  await platformAccountRef.value.validate((valid, fields) => {
    if (valid) {
      emit('action', systemAccountFormRef)
    } else {
      emit('action',null)
    }
  })
}

const clearForm = () => {
  if (!platformAccountRef) return
  platformAccountRef.value.resetFields()
} 

const setForm = (data) => {
  systemAccountFormRef.value = Object.assign(systemAccountFormRef, data);
}

onMounted(() => {
    
})


defineExpose({ submitForm, clearForm, setForm })
</script>