<script>
import moment from 'moment'
import { Modal } from 'ant-design-vue'
import flowService from './flow-service'
import components from './_import-components/sd-flow-design-mixin-import'

export default {
  name: 'SdFlowDesignMixin',
  components,
  props: {
    appId: {
      type: String,
      default: '',
    },
    appNodeId: {
      type: String,
      default: undefined,
    },
    idPrefix: {
      type: String,
      default: '',
    },
    editable: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      times: '',
    }
  },
  methods: {
    loadTreeData(parentCatId) {
      if (parentCatId) {
        return flowService.getFlowCateTree({ parentCatId }).then((res) =>
          res.data.map((d) => ({
            ...d,
            permission: {
              create: this.editable,
              update: this.editable,
              delete: this.editable,
            },
          }))
        )
      } else {
        // 获取流程根节点
        let p
        // 有appNodeId，说明是公共应用，且 可编辑时，获取个性流程根节点
        if (this.editable && this.appNodeId) {
          p = flowService.getNodeFlowTreeRoot({ appNodeId: this.appNodeId })
        } else {
          p = flowService.getFlowTreeRoot({ appId: this.appId })
        }
        return p.then((res) => {
          const id = res.data.id
          if (id) {
            return [
              {
                id: 'C' + id,
                text: res.data.name,
                leaf: false,
                permission: {
                  create: this.editable,
                },
              },
            ]
          } else {
            return Promise.resolve([])
          }
        })
      }
    },
    updateTimes() {
      this.times = moment().format('YYYYMMDD_HHmmss')
    },
    deleteRows(deleteFunc) {
      return new Promise((resolve) => {
        Modal.confirm({
          title: '您确定删除这项内容吗？',
          content: '删除这条数据后，就无法恢复初始的状态。',
          okText: '删除',
          okType: 'danger',
          onOk: () => {
            deleteFunc?.()
              .then((res) => {
                if (res.status === 200) {
                  resolve(true)
                } else {
                  resolve(false)
                }
              })
              .catch((e) => {
                resolve(false)
                throw e
              })
          },
          onCancel: () => {
            resolve(false)
          },
        })
      })
    },
  },
}
</script>

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