<template>
    <div style="min-height: 400px">
        <BasicForm @register="registerForm"></BasicForm>
    </div>
</template>

<script lang="ts">
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {computed, defineComponent,ref} from 'vue';
    import { propTypes } from '/@/utils/propTypes';
    import { VxeTablePropTypes,VxeUI,VxeTableEvents } from 'vxe-table'; 
    import {getBpmFormSchema,formSchema} from '../data/AuditCheckMethod.data';
    import {saveOrUpdate} from '../api/AuditCheckMethod.api';
    
    export default defineComponent({
        name: "AuditCheckMethodForm",
        components:{
            BasicForm
        },
        props:{
            formData: propTypes.object.def({}),
            formBpm: propTypes.bool.def(true),
            id:propTypes.number
        },
        setup(props){
            const [registerForm, { setFieldsValue, setProps, getFieldsValue }] = useForm({
                labelWidth: 150,
                schemas: formSchema,
                showActionButtonGroup: false,
                baseColProps: {span: 24}
            });
            const formDisabled = computed(()=>{
                if(props.formData.disabled === false){
                    return false;
                }
                return true;
            });

            let formData = {};

            const newMethod = ref({})
          
            async function initFormData(data){
                if(!data.id) return;
                formData = {...data}
                await setActData(formData)
                await setFieldsValue(formData);
            }

            async function submitForm() {
                try {  
                    let data = getFieldsValue();
                    let params = Object.assign({}, formData, data);
                    let isUpdate = params.id?true:false
                    newMethod.value = await saveOrUpdate(params, isUpdate)
                    VxeUI.modal.message({ content: '保存成功', status: 'success' });
                } catch(e) {
                    VxeUI.modal.message({ content: '保存失败', status: 'warning' });
                } 
            }

            
            async function setActData(newObj) {
                const ids = newObj.actIds;
                const caNames= newObj.caName
                if (!ids) return;
                const actIds = ids.split(",")
                const actNames = caNames.split(",")
                newObj['actIds'] = [actNames, actIds];
            }     

            async function getNewMethodId() {
               if(newMethod.value?.id) {
               } else {
                 await submitForm();
               }
               return newMethod.value.id
            }
            
            return {
                registerForm,
                formDisabled,
                submitForm,
                initFormData,
                getNewMethodId
            }
        }
    });
</script>