<template>
  <loading />
</template>

<script>
import axios from '@/common/services/axios-instance'
import loading from '@/common/pages/sd-loading.vue'
import components from './_import-components/sd-create-webflow-import'
import flowService from './flow-service'

export default {
  name: 'SdCreateWebflow',
  components: {
    ...components,
    loading,
  },
  data() {
    return {}
  },
  created() {
    const query = this.$route.query
    if (query.flowId) {
      let extParams = query.extParams
      // @ts-ignore
      extParams = JSON.parse(decodeURIComponent(extParams))
      let tempField = query.tempField
      // @ts-ignore
      tempField = JSON.parse(decodeURIComponent(tempField || '{}'))
      extParams = Object.assign(extParams, tempField)
      const params = []
      // @ts-ignore
      for (const key in extParams) {
        params.push({
          name: key,
          value: extParams[key] + '',
        })
      }
      return axios
        .get(`api/framework/v1/flow-manage/flow-definition/${query.flowId}`)
        .then((res) => {
          return flowService.save(
            { processId: query.flowId, processFormData: { formId: res.data.formId } },
            params,
            res.data.actionId
          )
        })
        .then((res) => {
          const instId = res.activeStepInfo[0].instId
          this.$router.replace(`/sd-webflow/pages/draft/${instId}`)
        })
    }
  },
  methods: {},
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
