<template>
  <sd-webflow
    ref="webflow"
    :class="$style.webflow"
    :validate-form="validForm"
    @actionBtnClick="actionBtnClick"
  >
    <template v-slot:form="{ model, FlowData }">
      <sd-form-by-builder
        :form-data="FlowData.processFormData"
        :class="{
          [$style.viewForm]:
            FlowData.processFormData.processFormPropertyValues.find((item) => {
              return item.name === 'CPM_IMPROVE_DETAIL'
            }).readonly === true,
        }"
      />
    </template>
  </sd-webflow>
</template>

<script>
import { message } from '@/common/one-ui'
import axios from '@/common/services/axios-instance'
import components from './_import-components/audit-cpm-improve-require-form-import'

export default {
  name: 'AuditCpmImproveRequireForm',
  metaInfo: {
    title: '问题整改要求',
  },
  components,
  data() {
    return {}
  },
  methods: {
    validForm() {
      // return true
      const child = this.$refs.webflow.getFieldValue('CPM_IMPROVE_DETAIL')
      let flag = true
      child.forEach((i) => {
        if (child.filter((u) => u.PROBLEM_ID === i.PROBLEM_ID).length > 1) {
          flag = false
        }
      })
      if (flag) {
        return Promise.resolve(true)
      } else {
        message.warning('请检查需要整改问题中重复数据!', 3)
        return Promise.resolve(false)
      }
    },
    // 提交校验
    actionBtnClick(evt, { button, FlowData }) {
      if (button.fakeId === 'workflow-push') {
        // 提交前校验子表数据是否已经有结束的数据，有的话则提示
        evt.waitUntil(
          new Promise((resolve, reject) => {
            // 不需要判断直接返回
            if (FlowData.attrs.cpm$checkRequire !== true || button.buttonName.includes('不通过')) {
              resolve()
              return
            }
            // 获取子表数据
            const dataList = this.$refs.webflow.getFieldValue('CPM_IMPROVE_DETAIL')
            if (dataList === undefined || dataList.length === 0) {
              message.warning('请增加至少一条需要整改问题!', 3)
              evt.preventDefault()
              resolve()
              return
            }
            // 获取子表ID
            let ids = []
            if (dataList.length > 0) {
              ids = dataList.map((item) => item.PROBLEM_ID)
            }
            axios({
              url: `api/xcoa-mobile/v1/cpm-improve-require/checkProblems?problemIds=${ids.join(
                ','
              )}`,
              methods: 'get',
            }).then((res) => {
              if (res.data.data?.length > 0) {
                message.warning(
                  `问题整改清单"${res.data.data
                    .map((item) => item.FIND_TITLE)
                    .join('，')}"已被审批通过,请重新选择！`,
                  3
                )
                evt.preventDefault()
              }
              resolve()
            })
          })
        )
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.webflow {
  :global(.ant-space-horizontal) {
    :global(.ant-space-item):nth-child(4) {
      display: none;
    }
  }
  :global(.sd-form-table) {
    tr:last-child {
      :global(.ant-form-item-control-wrapper) {
        :global(.selector-wapper_sd-form-item-td-input_common) {
          margin-top: -45px;
        }
      }
    }
  }
  .view-form {
    tr:last-child {
      :global(.ant-form-item-control-wrapper) {
        :global(.selector-wapper_sd-form-item-td-input_common) {
          margin-top: 0px !important;
        }
      }
    }
  }
}
</style>
