/*
 * @Author: HuXiangYu 
 * @Date: 2022-12-01 11:36:01 
 * @Last Modified by:   HuXiangYu 
 * @Last Modified time: 2022-12-01 11:36:01 
 */


<template>
    <div style="padding-top:10px">
        <v-form-render :form-data="formConfig"
                       ref="vFormRef">
        </v-form-render>
    </div>

</template>

<script>
import main from '../main'
export default {
    name: 'AceFlowFormRender',
    inject: ['processShell'],
    //混入main
    mixins: [main],
    data() {
        return {}
    },
    computed: {
        flowFormCode() {
            return this.processShell.nodeInfo.flow.dynamicFormCode
        },
        flowFormId() {
            return this.processShell.nodeInfo.flow.businessKey
        },
    },
    created() {
        this.getModelInfo(this.flowFormCode, () => {
            //不是新建模式，需要获取之前表单的数据
            if (!this.processShell.isCreateMode) {
                this.getFormData(this.flowFormId)
            }
        })

        this.processShell.$on('beforeSave', this.flowSubmitForm)
    },
    mounted() {},
    methods: {
        async flowSubmitForm(next) {
            this.$refs.vFormRef
                .getFormData(true)
                .then((formData) => {
                    let submitFormData = []
                    // Form Validation OK
                    this.formJson.boundWidgetList.forEach((widget) => {
                        for (let key in formData) {
                            if (key === widget.name) {
                                submitFormData.push({
                                    colName: widget.field.tabColName, //列名
                                    subName: widget.field.objColName, //字段名
                                    value: formData[key], //值
                                    type: widget.field.dataType, //数据类型
                                })
                            }
                        }
                    })
                    this.formData.cols = submitFormData //提交的字段数据
                    this.formData.flowCode = this.processShell.flowCode
                    this.$http({
                        method: 'post',
                        url: this.$http.adornWorkFlowUrl(
                            `modelData/insertDataAndCreateFlow`
                        ),
                        // url:'/modelData/insertData',
                        data: this.formData,
                    }).then(({ data }) => {
                        if (data.code === 40000) {
                            this.show = false
                            next({
                                flowCode: this.processShell.flowCode,
                                formId: data.idValue,
                            })

                            this.$notify({
                                title: '成功',
                                message: '保存成功',
                                type: 'success',
                            })
                        }
                    })
                })
                .catch((error) => {
                    this.$message.error(error)
                })
        },
    },
}
</script>

<style lang="scss" scoped>
</style>