<template>
  <div>
    <!-- 收藏弹出框 -->
    <a-modal
      ref="favoritesModal"
      :class="$style.candidate"
      title="请选择收藏夹"
      :destroy-on-close="true"
      :body-style="{ overflow: 'auto' }"
      :width="600"
      :visible="favoritiesShow"
      :confirm-loading="confirmLoading"
      @cancel="onCancel"
    >
      <sd-favorites-tree
        ref="tree"
        :type="1"
        :hideleftbtn="true"
        @treeSelectd="treeSelectd"
      ></sd-favorites-tree>
      <template slot="footer">
        <a-button key="create" @click="createFavorites"> 新建收藏夹 </a-button>
        <a-button key="cancel" @click="onCancel"> 取消 </a-button>
        <a-button key="submit" type="primary" @click="onFavorities">
          确定
        </a-button>
      </template>
    </a-modal>
    <!-- 新建收藏夹弹窗框 -->
    <sd-detail-modal
      ref="createFavoritesModal"
      :record-id="docId"
      page-id="comp/bookmark/oaBookmarkFolder"
      :modal-props="{ width: 450 }"
      @saved="fwSaved"
    >
      <template v-slot="{ model, fields }">
        <sd-create-modal
          :model="model"
          :fields="fields"
          @modalTreeSelectd="modalTreeSelectd"
        ></sd-create-modal>
      </template>
    </sd-detail-modal>
  </div>
</template>

<script>
import { Message } from 'ant-design-vue'
import sdFavoritesTree from '@/favorites/sd-favorites-tree'
import sdCreateModal from '@/favorites/sd-create-modal'
import FlowService from '../flow-service'
import components from './_import-components/sd-workflow-my-collection-import'
const duration = 1
let resolve1
export default {
  name: 'SdWorkflowMyCollection',
  metaInfo: {
    title: 'SdWorkflowMyCollection',
  },
  components: {
    ...components,
    sdFavoritesTree,
    sdCreateModal,
  },
  data() {
    return {
      docId: null,
      favoritiesShow: false, // 收藏弹框
      confirmLoading: false,
      modelSelectedKey: null,
    }
  },
  methods: {
    run({ button }) {
      this.favoritiesShow = true
      return new Promise((resolve, reject) => {
        resolve1 = resolve
      })
    },
    // 收藏 保存
    onFavorities() {
      const key = Symbol.for()
      Message.loading({ content: '处理中...', key })
      this.confirmLoading = true
      const url = window.location.hash
      const list = this.webflow.FlowData.processFormData.processFormPropertyValues
      const params = {
        eventId: 'save',
        inputs: [
          {
            name: 'topping',
            value: 1, // 0置顶， 1不置顶，默认不置顶
          },
          {
            name: 'url',
            value: url.split('#')[1],
          },
          {
            name: 'formId',
            value: this.webflow.FlowData.processFormData.formId,
          },
          {
            name: 'beanType',
            value: this.webflow.FlowData.processFormData.beanId,
          },
          {
            name: 'folderId',
            value: this.modelSelectedKey,
          },
        ],
        pageFlowId: 'string',
        pagePath: 'comp/bookmark/oaBookmarkContent',
      }
      list.forEach((k, index) => {
        if (k.name === 'title' && k.value) {
          params.inputs.push({
            name: 'title',
            value: k.value,
          })
        }
        if (k.name === 'businessTypeName' && k.value) {
          params.inputs.push({
            name: 'businessTypeName',
            value: k.value,
          })
        }
        if (k.name === 'businessTypeId' && k.value) {
          params.inputs.push({
            name: 'businessTypeId',
            value: k.value,
          })
        }
      })

      if (this.webflow.FlowData.instId) {
        params.inputs.push({
          name: 'instId',
          value: this.webflow.FlowData.instId,
        })
      }
      FlowService.saveFavorities(params).then(
        () => {
          Message.success({ content: '收藏成功', key, duration }).then(() => {
            this.confirmLoading = true
            this.webflow.updateFlow()
            resolve1(true)
          })
        },
        (_) => {
          Message.error({ content: '收藏失败', key, duration })
          resolve1()
        }
      )
      this.favoritiesShow = false
    },
    // 收藏 取消
    onCancel() {
      this.confirmLoading = false
      this.favoritiesShow = false
      resolve1()
    },
    // 选择收藏夹
    treeSelectd(id) {
      if (id.length < 1) return
      this.modelSelectedKey = parseInt(id)
    },
    // 新建收藏夹
    createFavorites(e) {
      this.docId = undefined
      this.$refs.createFavoritesModal.show()
    },
    // 新建收藏夹 保存
    fwSaved() {
      const key = Symbol.for()
      Message.success({ content: '已保存', key, duration })
      this.$refs.tree.refresh(1)
    },
    modalTreeSelectd(id) {
      this.modelSelectedKey = id
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.candidate {
  :global(.ant-modal-body) {
    padding: 12px;
  }
}
</style>
