<template>
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="120px"
      style="padding: 10px;" :disabled="isDetail">
      <el-row>
        <el-col :span="12">
          <el-form-item label="开户银行" prop="bankName">
            <el-input v-model="formData.bankName" placeholder="请输入开户银行" />
          </el-form-item>
          
        </el-col>
        <el-col :span="12">
          <el-form-item label="账户名称" prop="accountName">
            <el-input v-model="formData.accountName" placeholder="请输入账户名称" maxlength="11"/>
          </el-form-item>
          
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="账户号码" prop="accountNumber">
            <el-input v-model="formData.accountNumber" placeholder="请输入账户号码"  onkeyup="value=value.replace(/[^\d]/g,'')"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
</template>
<script lang="ts" setup>
import { ShopApi } from '@/api/system/sale/shop/index'
import { FormRules } from 'element-plus'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
defineOptions({ name: 'SystemUserForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: '',
  id: undefined,
  status: 1,
  merchantId: 0,
  logoUrl: '',
  website:'',
  accountName:undefined,
  accountNumber:undefined,
  bankName:undefined,
})
const formRules = reactive<FormRules>({
  accountName: [{ required: true, message: '账户名称不能为空', trigger: 'blur' }],
  accountNumber: [{ required: true, message: '账户号码不能为空', trigger: 'blur' }],
  bankName: [{ required: true, message: '开户银行不能为空', trigger: 'blur' }],
})
const props = defineProps({
  obj: {
    type: Object,
    default: () => ({})
  },
  isDetail: {
    type: Boolean,
    default: false
  }
})
const getShop = async () => {
  ShopApi.getShop(props.obj.id).then((res)=>{
    formData.value = res
  })
}


const formRef = ref() // 表单 Ref


/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ShopApi.UserVO
    if (formType.value === 'create') {
      await ShopApi.createShop(data)
      message.success(t('common.createSuccess'))
    } else {
      await ShopApi.updateShop(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
    return true;
  } finally {
    formLoading.value = false
  }
}
defineExpose({ submitForm }) 


/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: '',
    id: undefined,
    status: 1,
    merchantId: 0,
    logoUrl: ''
  }
  formRef.value?.resetFields()
}

onMounted(() => {
  formData.value.merchantId = props.obj.merchantId
  formData.value.id = props.obj.id
  resetForm()
  getShop()
})
</script>
<style src="@/styles/PcComponents.css"  lang="scss" scoped></style>
