<template>
  <AFormPanel
    ref="formPanel"
    v-bind="formPanelProps">
    <template #default="formData">
      <div class="form-items">
        <ElFormItem label="Id" prop="id">
          <ElInput
            v-model="formData.id"
            placeholder="Id"/>
        </ElFormItem>
        <ElFormItem label="账户编号" prop="sn">
          <ElInput
            v-model="formData.sn"
            placeholder="账户编号"/>
        </ElFormItem>
        <ElFormItem label="账户类型；account_type，GeRen-->个人、QiYe-->企业" prop="accountType">
          <ElInput
            v-model="formData.accountType"
            placeholder="账户类型；account_type，GeRen-->个人、QiYe-->企业"/>
        </ElFormItem>
        <ElFormItem label="用户 Id" prop="userId">
          <ElInput
            v-model="formData.userId"
            placeholder="用户 Id"/>
        </ElFormItem>
        <ElFormItem label="客户 Id" prop="customerId">
          <ElInput
            v-model="formData.customerId"
            placeholder="客户 Id"/>
        </ElFormItem>
        <ElFormItem label="组织 Id" prop="orgId">
          <ElInput
            v-model="formData.orgId"
            placeholder="组织 Id"/>
        </ElFormItem>
        <ElFormItem label="营收余额" prop="revenue">
          <ElInput
            v-model="formData.revenue"
            placeholder="营收余额"/>
        </ElFormItem>
        <ElFormItem label="充值余额" prop="recharge">
          <ElInput
            v-model="formData.recharge"
            placeholder="充值余额"/>
        </ElFormItem>
        <ElFormItem label="创建人 Id；sys_user.id" prop="creatorId">
          <ElInput
            v-model="formData.creatorId"
            placeholder="创建人 Id；sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="修改人 Id； sys_user.id" prop="modifierId">
          <ElInput
            v-model="formData.modifierId"
            placeholder="修改人 Id； sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="创建时间" prop="createTime">
          <ElInput
            v-model="formData.createTime"
            placeholder="创建时间"/>
        </ElFormItem>
        <ElFormItem label="修改时间" prop="modifyTime">
          <ElInput
            v-model="formData.modifyTime"
            placeholder="修改时间"/>
        </ElFormItem>
        <ElFormItem label="是否删除； 0-->未删除、1-->已删除" prop="deleted">
          <ElInput
            v-model="formData.deleted"
            placeholder="是否删除； 0-->未删除、1-->已删除"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import MoneyAccountApi from '@/pages/fin/money-account/money-account-api.ts'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import Strings from '@/common/utils/strings.ts'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<MoneyAccountTypes.SearchMoneyAccountResult>({
  title: status.value === 'add' ? '新建资金账户表' : '修改资金账户表信息',
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve()
    } else {
      status.value = 'modify'
      return MoneyAccountApi
        .detail(id!)
        .then(res => res.data)
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return MoneyAccountApi
        .add(data)
        .then(props.research)
    } else {
      return MoneyAccountApi
        .modify(data)
        .then(props.research)
    }
  },
  rules: {
    id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
    sn: [ {required: true, message: '请填写账户编号', trigger: 'blur'} ],
    accountType: [ {required: true, message: '请填写账户类型；account_type，GeRen-->个人、QiYe-->企业', trigger: 'blur'} ],
    userId: [ {required: true, message: '请填写用户 Id', trigger: 'blur'} ],
    customerId: [ {required: true, message: '请填写客户 Id', trigger: 'blur'} ],
    orgId: [ {required: true, message: '请填写组织 Id', trigger: 'blur'} ],
    revenue: [ {required: true, message: '请填写营收余额', trigger: 'blur'} ],
    recharge: [ {required: true, message: '请填写充值余额', trigger: 'blur'} ],
    creatorId: [ {required: true, message: '请填写创建人 Id；sys_user.id', trigger: 'blur'} ],
    modifierId: [ {required: true, message: '请填写修改人 Id； sys_user.id', trigger: 'blur'} ],
    createTime: [ {required: true, message: '请填写创建时间', trigger: 'blur'} ],
    modifyTime: [ {required: true, message: '请填写修改时间', trigger: 'blur'} ],
    deleted: [ {required: true, message: '请填写是否删除； 0-->未删除、1-->已删除', trigger: 'blur'} ],
  },
})

defineExpose({
  open(data?: MoneyAccountTypes.SearchMoneyAccountResult) {
    formPanelIns.value?.open(data?.id)
  },
})

</script>

<style lang="stylus" scoped>
.form-items {
  grid-template-columns: 1fr 1fr;
}
</style>
