<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="platformAccountForm"
    :rules="platformAccountFormRules"
    ref="platformAccountRef"
    style="max-width: 66%"
  >
    <el-form-item label="头像" prop="logo">
       <bg-upload-image v-model="platformAccountForm.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="platformAccountForm.account" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="platformAccountForm.phone" />
    </el-form-item>
     <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="platformAccountForm.password" />
    </el-form-item>
     <el-form-item label="确认密码" prop="confirm_password">
      <el-input type="password" v-model="platformAccountForm.confirm_password" />
    </el-form-item>
    <el-form-item label="角色授权" prop="role">
      <el-input v-model="platformAccountForm.role" />
    </el-form-item>
    <el-form-item label="是否启用" prop="is_use">
      <bg-switch :labels="['否','是']" :values="[0,1]" v-model="platformAccountForm.is_use"></bg-switch>
    </el-form-item>
    
  </el-form>
</template>

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

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

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

const platformAccountFormRules = reactive({
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
  ],
  role: [
    { 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', platformAccountForm)
    } else {
      emit('action',null)
    }
  })
}

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

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

onMounted(() => {
    
})


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