<template>
  <!-- 无流程表单详情 -->
  <sd-detail-modal ref="detail" :record-id="recordId" :page-id="pageId">
    <template v-slot="{ formData }">
      <sd-form-by-builder :form-data="formData.pageFormData" />
    </template>
  </sd-detail-modal>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import todoListService from '@/todo/todo-list-service'
import PageService from '@/common/services/page-service'
import components from './_import-components/sd-open-doc-handler-import'

export default {
  name: 'SdOpenDocHandler',
  components,
  data() {
    return {
      recordId: '',
      pageId: '',
    }
  },
  methods: {
    run({ button, eventScript, context }) {
      // 1. 根据目标表单id获取目标表单设计
      return axios
        .get('api/framework/v1/we-app-module/get-module-id?formId=' + eventScript.targetForm[0].id)
        .then((res) => {
          if (res?.data?.moduleId) {
            return axios
              .get('api/framework/v1/we-app-form-list/show?appModuleId=' + res.data.moduleId)
              .then((res) => {
                const formId = res.data.formId
                this.pageId = res.data.pageId
                // 2. 获取目标表单数据
                const { fieldInfos } = context
                const exps = this.toExps(eventScript.associateConditions, fieldInfos)
                return PageService.getList({
                  startPosition: 0,
                  maxResults: 2,
                  expressions: exps,
                  formId,
                }).then((res) => {
                  // 找到的数据不唯一（有多条）
                  if (res.data.data?.length > 1) {
                    message.error('找到的文件不唯一')
                  } else {
                    const record = res.data.data?.[0] || null
                    // 3. 找到了唯一对应的数据，打开
                    if (record) {
                      // 流程表单
                      if (eventScript.formType === 1) {
                        todoListService.openTaskItem(record)
                      } else {
                        // 无流程表单，Modal打开
                        this.recordId = record.id
                        this.$refs.detail.show()
                      }
                    } else {
                      // 4. 未找到对应的数据，提示
                      message.error('未找到文件')
                    }
                  }
                })
              })
          }
        })
    },
    toExps(associateConditions, fieldInfos) {
      const converByType = (type, value) => {
        let dataType = 'str'
        let valueKey = 'stringValue'
        let dataValue = value
        switch (type) {
          case 'user':
            dataValue = value ? JSON.parse(value)[0]?.code || '' : value
            break
          case 'date':
          case 'number':
            dataType = 'long'
            valueKey = 'longValue'
            break
          default:
            break
        }
        return [dataType, valueKey, dataValue]
      }
      const converOp = (op) => {
        let expsOp = op
        switch (op) {
          case '=':
            expsOp = 'eq'
            break
          default:
            break
        }
        return expsOp
      }
      return (associateConditions || [])
        .filter((c) => c.targetField[0] && c.curField[0])
        .map((c) => {
          const targetField = c.targetField[0]
          const curField = c.curField[0]
          const value = fieldInfos.find((f) => f.name === curField.id)?.value
          const [dataType, valueKey, dataValue] = converByType(targetField.type, value)
          return {
            dataType,
            name: targetField.id,
            op: converOp(c.op),
            [valueKey]: dataValue,
          }
        })
    },
  },
}
</script>

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