
import util from '@src/common/util.js'
import TreeSelect from '@src/components/TreeSelect.vue'
import PathPicker from './components/PathPicker.vue'
import LogoPath from './components/LogoPath.vue'
import CatalogPCTemplate from './components/CatalogDefaultTemplate.vue'
import ContentSelector from '@src/components/contentComponent/RecommendedContentSelector.vue'
import CatalogSelector from './components/CatalogSelector.vue'
import InputExternalLink from './components/inputExternalLink.vue'
import CatalogPrivilegeDropDown from './components/CatalogPrivilegeDropDown.vue'
import CatalogBasicExtends from '../contentextend/components/CatalogInfoExtendAction.vue'
import CatalogPublishDropDown from './components/CatalogPublishDropDown.vue'
import CatalogCleanButton from './components/CatalogCleanButton.vue'
import CatalogScanPreviewDropDown from './components/CatalogScanPreviewDropDown.vue'
import CatalogImportButton from './components/CatalogImportButton.vue'
import CatalogDeleteButton from './components/CatalogDeleteButton.vue'
import CatalogExportStructureButton from './components/CatalogExportStructureButton.vue'
import CatalogMoveButton from './components/CatalogMoveButton.vue'
import CatalogSortButton from './components/CatalogSortButton.vue'

export default {
  components: {
    'path-picker': PathPicker,
    'logo-path': LogoPath,
    'tree-select': TreeSelect,
    'pc-template': CatalogPCTemplate,
    'c-publish-dropdown': CatalogPublishDropDown,
    'link-content': ContentSelector,
    'link-catalog': CatalogSelector,
    'input-link': InputExternalLink,
    'c-privilege-dropdown': CatalogPrivilegeDropDown,
    'catalog-basic-extends': CatalogBasicExtends,
    'c-scan-preview-dropdown': CatalogScanPreviewDropDown,
    'c-clean-button': CatalogCleanButton,
    'c-import-button': CatalogImportButton,
    'c-del-button': CatalogDeleteButton,
    'c-export-structure-button': CatalogExportStructureButton,
    'c-move-button': CatalogMoveButton,
    'c-sort-button': CatalogSortButton
  },
  props: {
    catalogId: [Number, String],
    type: String,
    oldType: String,
    contentType: String,
    contentTypes: Array,
    types: Array,
    branchTree: Array,
    treeItemsOptions: Object,
    tabName: String,
    dimensionId: {
      type: [Number, String],
      default: 1
    }
  },
  data () {
    return {
      submitLoading: false,
      catalogLinkInfo: {
        logoFile: ''
      }, /** 基本信息 */
      dataLoading: false, /** 数据加载 */
      resourceDialogProps: {},
      linkContentChoice: {
        /** 选择链接文章子组件 */
        isShowModal: false,
        title: ''
      },
      linkURL: {
        isShowModal: false
      }, /** 外部地址 */
      linkCatalogChoice: {
        /** 选择链接栏目子组件 */
        isShowModal: false,
        title: '',
        ID: '',
        inputType: ''
      }
    }
  },
  watch: {
    tabName (val) {
      if (val === 'info') {
        this.loadCatalogLinkInfo()
      }
    },
    catalogId: function (val) {
      if (val && this.tabName === 'info') {
        this.loadCatalogLinkInfo()
      }
    }
  },
  created () {
    this.loadCatalogLinkInfo()
  },
  methods: {
    loadCatalogTree (val) {
      if (val === 'del') {
        this.$refs['catalog-link-info'].resetFields()
      }
      this.$emit('loadCatalogTree', val)
    },
    async visible () {
      if (!this.catalogId) {
        return
      }
      let changeVisible = async () => {
        let params = {
          catalogID: this.catalogId,
          visibleFlag: this.catalogLinkInfo.visibleFlag
        }
        let res = await axios.put(
          `/ui/catalogs/changevisible`,
          params
        )
        if (res.data.status === 1) {
          this.$emit('loadCatalogTree')
        }
        util.showResponseMessage(res.data)
      }
      if (!this.catalogLinkInfo.isLeaf) {
        try {
          await this.$confirm('修改本栏目的状态，子栏目的状态也会被改变，确认要执行此操作吗？', '系统提示', {
            confirmButtonText: '确定',
            showCancelButton: false,
            type: 'warning'
          })
          await changeVisible()
        } catch (e) {
          return
        }
      } else {
        await changeVisible()
      }
      this.loadCatalogLinkInfo()
    },
    /** 链接到栏目内容 */
    selectContentClickHandler () {
      this.linkContentChoice.isShowModal = true
      this.linkContentChoice.title = '选择内容记录'
    },
    async catalogImportCallBack (res) {
      await util.showProgress(res.data.taskID, `正在导入到[${this.catalogLinkInfo.name}]栏目`)
    },
    /** 得到栏目内容 */
    getChoiceLinkContent (data) {
      this.catalogLinkInfo.cName = data.title
      this.catalogLinkInfo.URL = data.link
    },
    /** 链接到栏目 */
    selectCatalogClickHandler () {
      this.linkCatalogChoice.isShowModal = true
      this.linkCatalogChoice.title = '选择栏目'
      this.linkCatalogChoice.ID = this.catalogId
      this.linkCatalogChoice.inputType = 'radio'
    },
    /** 得到栏目 */
    getChoiceLinkCatalog (data) {
      this.catalogLinkInfo.cName = `栏目：${data.name}`
      this.catalogLinkInfo.URL = data.link
    },
    /** 外部地址 */
    inputUrlClickHandler () {
      this.linkURL.isShowModal = true
    },
    /** 外部地址弹框回调 */
    getInputURL (URL) {
      this.catalogLinkInfo.URL = URL
      this.catalogLinkInfo.cName = URL
    },
    /** 发布栏目弹框回调 */
    async afterPublishCallBack (taskID) {
      await util.showProgress(taskID, `正在发布[${this.catalogLinkInfo.name}]栏目`)
    },
    /** 清空栏目弹框回调 */
    async cleanCallBack (res) {
      await util.showProgress(res.data.taskID, `正在清空[${this.catalogLinkInfo.name}]栏目`)
    },
    /** 栏目移动 */
    catalogMoveClickHandler (data) {
      this.move.isShowModal = true
      this.move.innerCode = this.catalogLinkInfo.innerCode
    },
    /** 保存栏目基本信息 */
    async catalogSaveClickHandler () {
      if (!this.catalogLinkInfo.name ||
        !this.catalogLinkInfo.alias ||
        !this.catalogLinkInfo.path
      ) {
        this.dialog('不能为空')
        return
      }
      this.catalogLinkInfo.name = this.catalogLinkInfo.name.replace('\t', '  ')
      this.catalogLinkInfo.name = this.catalogLinkInfo.name.replace(
        '/\r\b\f/g',
        ''
      )
      let saveCatalog = async () => {
        this.submitLoading = true
        await util.validateForm(this.$refs['catalog-link-info'])
        let res = await axios.put(
          `/ui/catalogs/${this.catalogId}`,
          this.catalogLinkInfo
        )
        this.submitLoading = false
        util.showResponseMessage(res.data)
        if (res.data.status === 1) {
          if (this.type !== this.oldType) { // 栏目类型改变则向上层传递刷新栏目树
            let val = {
              ID: this.catalogLinkInfo.ID,
              contentType: this.catalogLinkInfo.contentType,
              parentID: this.catalogLinkInfo.parentID,
              type: this.catalogLinkInfo.type
            }
            this.$emit('loadCatalogTree', val)
          } else {
            this.$emit('loadCatalogTree')
          }
        }
        this.loadCatalogLinkInfo()
      }
      if (this.oldType !== this.catalogLinkInfo.type) {
        try {
          await this.$confirm('修改栏目类型将清空栏目下的原有内容数据，确认修改？', '系统提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          saveCatalog()
        } catch (e) {
        }
      } else {
        if (this.contentType !== this.catalogLinkInfo.contentType) {
          try {
            await this.$confirm('修改栏目内容类型将清空栏目下的原有内容数据，确认修改？', '系统提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            })
            saveCatalog()
          } catch (e) {
            // util.showErrorNotification(e)
          }
        } else {
          saveCatalog()
        }
      }
    },
    /** 撤销 */
    // async catalogRollBackClickHandler() {
    //   try {
    //     let res = await axios.put('/ui/contentcore/siteoperation/rollback')
    //     util.showResponseMessage(res.data)
    //   } catch (e) {
    //     util.showErrorNotification(e)
    //     return
    //   }
    // },
    /** 生效 */
    // async catalogTakeEffectClickHandler() {
    //   try {
    //     let res = await axios.put('/ui/contentcore/siteoperation/takeeffect')
    //     util.showResponseMessage(res.data)
    //   } catch (e) {
    //     util.showErrorNotification(e)
    //     return
    //   }
    // },
    turnToPlatformTabs (tabName) {
      this.$emit('update:tabName', tabName)
    },
    /** 修改当前栏目类型 */
    catalogTypeChangeHandler (val) {
      this.$emit('update:type', val)
    },
    async loadCatalogLinkInfo () {
      this.dataLoading = true
      let res = await axios.get(`/ui/catalogs/${this.catalogId}`)
      this.dataLoading = false
      this.catalogLinkInfo = res.data.data
      this.catalogLinkInfo.type = 'Link'
      this.resourceDialogProps = {
        dataType: 'CatalogLogo',
        dataID: this.catalogId,
        resourceType: 'image',
        imageType: 'logo',
        catalogID: this.catalogId,
        imageWidth: 120,
        imageHeight: 120,
        inputType: 'radio'
      }
    },
    // 消息Dialog封装
    dialog (msg) {
      this.$confirm(msg, '系统提示', {
        confirmButtonText: '确定',
        showCancelButton: false,
        type: 'warning'
      })
    }
  }
}
