import { message } from '@/common/one-ui'
import crossWindowWatcher from '@/common/services/cross-window-watcher'
import FormService from '@/webform/form-service'

export default function({ button }) {
  return this.validateFields().then(() => {
    return FormService.save({
      eventId: 'save',
      inputs: this.$refs.form.getBackendValues(),
      pageFlowId: this.formData.attrs.pageflowId,
      pagePath: this.formData.attrs.pagePath,
    }).then((res) => {
      message.success('保存成功')
      const recordId = res.data.pageFormData.pageFieldInfos.find((_) => _.name === 'id')?.value
      crossWindowWatcher.notifyChange(res.data)
      // 新建时保存
      if (!this.recordId) {
        this.$router.replace({
          path: '/sd-webform',
          query: {
            pageId: res.data.pageFormData.pageId,
            id: recordId,
          },
        })
      }
      this.updateForm()
    })
  })
}
