import util from '@src/common/util.js'
import LogoPath from '../contentcore/components/LogoPath.vue'
import PathPicker from '../contentcore/components/PathPicker.vue'
import TemplatePicker from '../contentcore/TemplatePicker.vue'
import CatalogCleanButton from './components/CatalogCleanButton.vue'
import CatalogPublishButton from './components/CatalogPublishButton.vue'
export default {
  components: {
    'logo-path': LogoPath,
    'path-picker': PathPicker,
    'template-picker': TemplatePicker,
    'c-clean-button': CatalogCleanButton,
    'c-publish-button': CatalogPublishButton
  },
  props: {
    dimensionId: [Number, String],
    dimensionCatalogId: [Number, String],
    dimensionCatalogInnerCode: String,
    tabName: String
  },
  data () {
    return {
      saveLoading: false,
      applyLoading: false,
      dimensionCatalogBasicInfo: {logoFile: ''} /** 栏目基本信息 */,
      resourceDialogProps: {}/** 上传logo图片对象 */
    }
  },
  watch: {
    tabName (val) {
      this.dimensionCatalogBasicInfo = {}
      if (val === 'basic') {
        this.loadDimensionCatalogBasicInfo()
      } else {
        this.$refs['dimension-catalog-info'].resetFields()
      }
    },
    dimensionCatalogId: function(val) {
      this.dimensionCatalogBasicInfo = {}
      if (val && this.tabName === 'basic') {
        this.loadDimensionCatalogBasicInfo()
      } else {
        this.$refs['dimension-catalog-info'].resetFields()
      }
    }
  },
  methods: {
    /** 保存栏目 */
    async saveClickHandler () {
      let catalogName = this.dimensionCatalogBasicInfo.name
      let alias = this.dimensionCatalogBasicInfo.alias
      let path = this.dimensionCatalogBasicInfo.path
      if (!catalogName || !alias || !path) {
        this.$alert('不能为空', '系统提示', {
          type: 'warning'
        })
        return
      }
      this.dimensionCatalogBasicInfo.name = this.dimensionCatalogBasicInfo.name.replace(
        '\t',
        '  '
      )
      this.dimensionCatalogBasicInfo.name = this.dimensionCatalogBasicInfo.name.replace(
        '/\r\b\f/g',
        ''
      )
      this.saveLoading = true
      try {
        await util.validateForm(this.$refs['dimension-catalog-info'])
        let res = await axios.put(
          `/ui/dimesion/${this.dimensionId}/catalog/${this.dimensionCatalogId}`,
          this.dimensionCatalogBasicInfo
        )
        this.saveLoading = false
        util.showMessage(res)
      } catch (e) {
        this.saveLoading = false
        // util.showErrorNotification(e)
      }
    },
    /** 发布栏目弹框回调 */
    async afterPulishCallback (taskID) {
      await util.showProgress(taskID, `正在发布栏目[${this.dimensionCatalogBasicInfo.name}]`)
    },
    async applyTemplateToChildren (typeID) {
      let opts = {
        ID: this.dimensionCatalogId,
        field: typeID,
        value: this.dimensionCatalogBasicInfo.indexTemplate
      }
      try {
        this.applyLoading = true
        let res = await axios.put(
          `/ui/dimesion/${this.dimensionId}/catalog/applytemplatetochildren`,
          opts
        )
        this.applyLoading = false
        util.showMessage(res)
      } catch (e) {
        this.applyLoading = false
        // util.showErrorNotification(e)
      }
    },

    async loadDimensionCatalogBasicInfo () {
      let res = await axios.get(
        `/ui/dimesion/${this.dimensionId}/catalog/${this.dimensionCatalogId}`
      )
      try {
        if (res.data.status === 1) {
          this.dimensionCatalogBasicInfo = res.data.data
          this.resourceDialogProps = {
            dataType: 'DimensionCatalogLogo',
            dataID: this.dimensionCatalogId,
            resourceType: 'image',
            imageType: 'logo',
            catalogID: 0,
            imageWidth: 120,
            imageHeight: 120,
            inputType: 'radio'
          }
        }
      } catch (e) {
        // util.showErrorNotification(e)

      }
    }
  }
}
