/* global Window, localStorage, axios */
import util from '@src/common/util.js'
import Privilege from '@src/common/privilege.js'
import ContentSaveButton from './ContentSaveButton.vue'
import ContentPublishButton from './ContentPublishButton.vue'
import ContentLockButton from './ContentLockButton.vue'
import ContentViewButton from './ContentViewButton.vue'
import ContentSetTopButton from './ContentSetTopButton.vue'
import ContentVersionButton from './ContentVersionButton.vue'
import ContentSimilarButton from './ContentSimilarButton.vue'
import ContentOperatlogButton from './ContentOperatLogButton.vue'
import ContentRecBlockButton from './ContentRecBlockButton.vue'
// import ContentWeiboSendButton from './ContentWeiboSendButton.vue'
import ImageUploadButton from './ImageUploadButton.vue'
import UploadVideoButton from './UploadVideoButton.vue'
import WorkflowButtons from '../../contentworkflow/components/ContentWorkflowCommonButton.vue'
import socialmediaPushButton from './SocialMediaPushButton.vue'
import emitter from '@src/mixins/emitter.js'
import ContentAddButton from './ContentAddButton.vue'
import neCornerButton from '@src/components/ne-corner-button.vue'

export default {
  mixins: [emitter],
  componentName: 'contentDetailToolBar',
  components: {
    'content-version-button': ContentVersionButton,
    'content-similar-button': ContentSimilarButton,
    'content-operatlog-button': ContentOperatlogButton,
    'content-rec-block-button': ContentRecBlockButton,
    // 'content-weibo-send-button':ContentWeiboSendButton,
    'content-set-top-button': ContentSetTopButton,
    'content-publish-button': ContentPublishButton,
    'content-lock-button': ContentLockButton,
    'content-view-button': ContentViewButton,
    'content-save-button': ContentSaveButton,
    'image-upload-button': ImageUploadButton,
    'workflow-buttons': WorkflowButtons,
    'upload-video-button': UploadVideoButton,
    socialmediaPushButton,
    'content-add-button': ContentAddButton,
    'ne-corner-button': neCornerButton
  },
  props: {
    content: {
      type: Object,
      required: true
    },
    showDetail: {
      type: Boolean
    },
    contentFormRules: {
      type: Object
    },
    contentId: {
      type: [String, Number]
    },
    catalogId: {
      type: [String, Number]
    },
    isShowBottomBtn: {
      type: Boolean,
      default: false
    },
    align: {
      type: String,
      default: 'space-around'
    }
  },
  inject: ['loadContent'],
  data () {
    return {
      file: {},
      button: '',
      status: {
        pub: 30
      },
      buttonType: 'text',
      showSaveButton: false,
      showPublishButton: false,
      userName: localStorage.userName,
      buttons: [{name: 'content-add-button', key: new Date().toString()}],
      detailContent: {
        show: false,
        ID: '',
        catalogID: this.catalogId,
        contentTypeID: ''
      }
    }
  },
  created () {
    this.$on('publishContent', val => {
      this.publish(val.data.contentID, val.data.type)
    })
  },
  computed: {
    isVisible: {
      set (val) {
        this.$parent.$emit('update:visible', val)
      },
      get () {
        return this.visible
      }
    }
  },
  watch: {
    content (val) {
      this.button = val.contentTypeID === 'Image'
    }
  },
  methods: {
    refreshWorkflowButton () {
      this.$refs.workflowButtons.loadButton()
    },
    workflowButtonsCallback (saveButton, publishButton) {
      this.showSaveButton = saveButton
      this.showPublishButton = publishButton
    },
    // 调用上层去刷新列表
    reflushList (val) {
      this.dispatch('ContentEditor', 'content-list', val)
      window.refreshOpenerContentList && window.refreshOpenerContentList()
    },

    back () {
      this.isVisible = false
      this.$router.go(-1)
    },
    async publish (ids, type) {
      let savePrivStr = this.content.ID === 0 ? `com.zving.cms.Catalog.Content.Add.${this.catalogId}` : `com.zving.cms.Catalog.Content.Edit.${this.catalogId}`
      if (this.content.contentTypeID === 'Article' && Privilege.hasPriv(savePrivStr)) {
        Window.startPublishing = true
        Window.saveContent().then(async (data) => {
          if (data === 'success') {
            let flag = !!ids
            if (!flag) {
              ids = Window.articleID
            }
            this.doPublish(ids, type)
          } else {
            util.showError('发布失败！')
          }
          Window.startPublishing = false
        }, function () {
          util.showError('发布失败！')
          Window.startPublishing = false
        })
      } else {
        this.doPublish(ids, type)
      }
    },

    async doPublish (ids, type) {
      if (this.content.contentTypeID === 'Video' && this.content.convertFlag === 'Y' && this.content.convertSuccess !== 'Y') {
        this.$confirm('视频转换以后才可以发布', '系统提示', {
          confirmButtonText: '确定',
          showCancelButton: false,
          type: 'error'
        })
        return
      }
      if (this.content.contentTypeID === 'Image' && this.content.images.length === 0) {
        this.$confirm('添加图片后才可以发布', '系统提示', {
          confirmButtonText: '确定',
          showCancelButton: false,
          type: 'error'
        })
        return
      }
      let resp = await axios.put(`/ui/contentcore/contentpublish/${ids}/${type}`, { catalogID: this.catalogId })
      util.showResponseMessage(resp.data)
      if (resp.data.status === 1) {
        this.loadContent()
        this.reflushList()
      }
    },
    // 内容编辑页面底部操作关闭按钮
    closePage(contentId) {
      window.close()
    }
  }
}
