<template>
  <div class="w-full h-full flex flex-col">
    <div class="flex-1 overflow-y-auto">
      <CustomerForm ref="formFormRef" :formJson="formJson" :formValue="formVal"></CustomerForm>
    </div>
    <div class="mar-20px ">
      <van-button type="primary" @click="onSubmit" class="w-full">
        {{ Object.keys(formVal).length > 0 ? '确定修改' : '确定添加' }}
      </van-button>
    </div>
  </div>
</template>

<script setup>
import { ref, onActivated } from 'vue';
import { useLifecycle } from '@hooks/useCommon.js';
import CustomerForm from '@components/common/formForm/formView.vue'
import requests from "@common/requests";
import useToast from "@hooks/useToast"
import { CUSTOMER_ADDED_EDITOR } from "@hooks/useApi"
import useRouterStore from "@store/useRouterStore.js";

const router = useRouterStore()
const { toastText, toastSuccess, toastFail, toastLoading } = useToast()

const props = defineProps({
  formJson: { required: true },
  formValue: { required: true },
});

const formFormRef = ref(null)
const formVal = ref({})

function onSubmit() {
  formFormRef.value.getJsonData().then((res) => {
    if(!res.data) {
      return
    }
    toastLoading('保存中', 0)
    requests.post(CUSTOMER_ADDED_EDITOR, { ...props.formValue, ...res.data }).then(() => {
      toastSuccess('保存成功')
      setTimeout(() => {
        router.navigateBack({
          success: () => {
            router.eventEmit('moduleListRefreshData', {})
          }
        })
      }, 2000)
    }).catch(() => {
      toastFail('保存失败')
    })
  })
}

useLifecycle({
  load: () => {
    formVal.value = props.formValue
  },
  init: () => {
    formVal.value = props.formValue
  }
});

onActivated(() => {
  
})
</script>

<style lang='scss' scoped>
/* 样式代码 */
</style>