<template>
  <el-form
    :label-position="'right'"
    label-width="120px"
    :model="systemForm"
    :rules="formRules"
    ref="systemRef"
    style="max-width: 66%"
  >
    <el-form-item label="所属机构" prop="org">
      <el-input v-model="systemForm.org" />
    </el-form-item>
    <el-form-item label="业务系统编码" prop="code">
      <el-input v-model="systemForm.code" />
    </el-form-item>
    <el-form-item label="业务系统名称" prop="name">
      <el-input v-model="systemForm.name" />
    </el-form-item>
    <el-form-item label="系统LOGO" prop="logo">
       <bg-upload-image v-model="systemForm.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="desc">
      <el-input type="textarea" :rows="3" v-model="systemForm.desc" />
    </el-form-item>
     <el-form-item label="角色" prop="role">
      <el-input v-model="systemForm.role" />
    </el-form-item>
     <el-form-item label="账号" prop="account">
      <el-input v-model="systemForm.account" />
    </el-form-item>
     <el-form-item label="密码" prop="password">
      <el-input type="password" v-model="systemForm.password" />
    </el-form-item>
     <el-form-item label="确认密码" prop="confirm_password">
      <el-input type="password" v-model="systemForm.confirm_password" />
    </el-form-item>
     <el-form-item label="访问地址" prop="url">
      <el-input v-model="systemForm.url" />
    </el-form-item>
    <el-form-item label="开发厂商名称" prop="firm_name">
      <el-input v-model="systemForm.firm_name" />
    </el-form-item>
    <el-form-item label="是否启用" prop="is_use">
      <bg-switch :labels="['否','是']" :values="[0,1]" v-model="systemForm.is_use"></bg-switch>
    </el-form-item>
  </el-form>
</template>

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

const systemForm = reactive({
  org: '',
  code: '',
  name: '',
  desc: '',
  logo: '',
  role: '',
  account: '',
  password: '',
  confirm_password: '',
  url: '',
  firm_name: '',
  is_use: 1,
 
})

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

const formRules = reactive({
  org: [
    { required: true, message: '请选择组织机构', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请输入业务系统名称', trigger: 'blur' },
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'blur' },
  ],
  account: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  confirm_password: [
      { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validatePass, trigger: 'blur' }
  ],
})



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

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

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

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

onMounted(() => {
    
})


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