<template>
  <div>
    <el-toolbar>
      <el-button :loading="submitLoading" :disabled="catalogId===0" @click="saveCatalogConfigClickHandler" :priv="'com.zving.cms.Catalog.ExtendAdmin.'+catalogId">
        <i class="fa fa-floppy-o"></i>保存
      </el-button>
      <el-button  :disabled="catalogId===0" @click="apply2AllCatalogChildrenClickHandler" :priv="'com.zving.cms.Catalog.ExtendAdmin.'+catalogId">
        <i class="fa fa-cog"></i>应用到所有<span style="color:green;font-weight: bold;margin: 0 3px;">[{{catalogConfigExtends.contentTypeName || '混合'}}]</span>子栏目
      </el-button>
    </el-toolbar>
    <div class="margin-top-8 scroll catalog-extends-config">
      <el-form
        label-suffix="："
        v-loading="dataLoading"
        label-position="left"
        class="catalog-form"
        label-width="180px"
        :disabled="catalogId===0"
        v-model="catalogConfigExtends">
        <component :is="config" :extendsData="catalogConfigExtends" :catalog-id="catalogId" v-for="(config,index) in configs" :key="'key_'+index" ></component>
      </el-form>
    </div>
  </div>
</template>
<script>
import util from '@src/common/util.js'
import CatalogConfigExtend from './CatalogExtendAction.vue'
import WordCatalogConfigExtend from '../../wordmanage/components/CatalogExtendAction.vue'
import DimensionCatalogConfigExtend from '../../contentadvance/components/CatalogExtendAction.vue'
import MemberCatalogConfigExtend from '../../member/components/CatalogExtendAction.vue'
import ContentCatalogConfigExtend from '../../contentextend/components/CatalogExtendAction.vue'
import AtvertiseCatalogConfigExtend from '../../advertise/components/CatalogExtendAction.vue'
import ImagePlayerCatalogConfigExtend from '../../imageplayer/components/CatalogExtendAction.vue'
import GatherCatalogConfigExtend from '../../gather/components/CatalogExtendAction.vue'
import ContributeCatalogConfigExtend from '../../membercontribute/components/CatalogExtendAction.vue'
import CommonSiteCatalogConfigExtend from '../../workspace/components/CatalogExtendAction.vue'
import ArticleCatalogConfigExtend from '../../article/components/CatalogExtendAction.vue'
import MediaCatalogConfigExtends from '../../media/components/CatalogExtendAction.vue'
import CommentCatalogConfigExtend from '@src/pages/comment/components/CatalogExtendAction.vue'
import WorkflowCatalogConfigExtend from '../../contentworkflow/components/CatalogExtendAction.vue'
import SiteGroupCatalogConfigExtend from '../../sitegroup/components/CatalogExtendAction.vue'
import VoteCatalogConfigExtend from '../../vote/components/CatalogExtendAction.vue'
import BrowsePrivCatalogConfigExtend from '../../browsepriv/components/CatalogExtendAction.vue'
export default {
  components: {
    CatalogConfigExtend,
    WordCatalogConfigExtend,
    DimensionCatalogConfigExtend,
    MemberCatalogConfigExtend,
    ContentCatalogConfigExtend,
    AtvertiseCatalogConfigExtend,
    ImagePlayerCatalogConfigExtend,
    GatherCatalogConfigExtend,
    ContributeCatalogConfigExtend,
    CommonSiteCatalogConfigExtend,
    MediaCatalogConfigExtends,
    ArticleCatalogConfigExtend,
    CommentCatalogConfigExtend,
    WorkflowCatalogConfigExtend,
    SiteGroupCatalogConfigExtend,
    VoteCatalogConfigExtend,
    BrowsePrivCatalogConfigExtend
  },
  props: {
    catalogId: [String, Number],
    type: String,
    tabName: String
  },
  data() {
    return {
      dataLoading: false, //  数据加载
      catalogConfigExtends: {memberReaderRecordEnableForSite: 'N', contentTypeName: ''}, //  扩展配置项数据
      submitLoading: false,
      configs: {
        CatalogConfigExtend,
        WordCatalogConfigExtend,
        DimensionCatalogConfigExtend,
        MemberCatalogConfigExtend,
        ContentCatalogConfigExtend,
        AtvertiseCatalogConfigExtend,
        ImagePlayerCatalogConfigExtend,
        GatherCatalogConfigExtend,
        ContributeCatalogConfigExtend,
        CommonSiteCatalogConfigExtend,
        MediaCatalogConfigExtends,
        ArticleCatalogConfigExtend,
        CommentCatalogConfigExtend,
        WorkflowCatalogConfigExtend,
        SiteGroupCatalogConfigExtend,
        VoteCatalogConfigExtend,
        BrowsePrivCatalogConfigExtend
      }
    }
  },
  watch: {
    catalogId: function(val) {
      if (val && this.tabName === 'config') {
        this.loadCatalogConfigExtends()
      } else {
        this.catalogConfigExtends = { contentTypeName: '' }
      }
    },
    tabName: function(val) {
      if (val === 'config' && this.catalogId) {
        this.loadCatalogConfigExtends()
      } else {
        this.catalogConfigExtends = { contentTypeName: '' }
      }
    }
  },
  methods: {
    // 应用到所以类型栏目子目录
    async apply2AllCatalogChildrenClickHandler() {
      try {
        let res = await axios.put(
          `/ui/contentcore/catalogs/${this.catalogId}/extends/applytochildren`
        )
        util.showMessage(res)
      } catch (e) {
        util.showErrorNotification(e)
      }
    },
    // 校验是否为正整数
    isPositiveInteger(param) {
      return /(^[1-9]\d*$)/.test(param)
    },
    // 校验是否为非负整数
    isNonnegativeInteger(param) {
      return /(^[0-9]\d*$)/.test(param)
    },
    // 保存栏目扩展配置
    async saveCatalogConfigClickHandler() {
      if (this.catalogConfigExtends.cronPublishEnable === 'Y' && !this.isPositiveInteger(this.catalogConfigExtends.cronPublishPeriod)) {
        this.catalogConfigExtends.cronPublishPeriod = 24
        util.showError('定时发布执行周期只能为正整数')
        return false
      }
      if (this.catalogConfigExtends.offLineEnable === 'Y' && !this.isNonnegativeInteger(this.catalogConfigExtends.archiveTimeType)) {
        this.catalogConfigExtends.archiveTimeType = 0
        util.showError('归档期限只能为正整数')
        return false
      }
      if (this.catalogConfigExtends.commentEnable === 'N') {
        this.catalogConfigExtends.commentStartTime = ''
        this.catalogConfigExtends.commentEndTime = ''
      } else {
        if (this.catalogConfigExtends.commentStartTime) {
          if (this.catalogConfigExtends.commentEndTime) {
            if (this.catalogConfigExtends.commentStartTime > this.catalogConfigExtends.commentEndTime) {
              this.dialog('评论开始时间不能大于结束时间！')
              return
            }
          }
        } else {
          this.catalogConfigExtends.commentStartTime = ''
          this.catalogConfigExtends.commentEndTime = ''
        }
      }
      this.submitLoading = true
      try {
        let res = await axios.put(
          `/ui/contentcore/catalogs/${this.catalogId}/extends`,
          this.catalogConfigExtends
        )
        this.submitLoading = false
        util.showMessage(res)
      } catch (e) {
        this.submitLoading = false
        // util.showErrorNotification(e)
      }
    },
    // 加载栏目扩展配置
    async loadCatalogConfigExtends() {
      this.dataLoading = true
      let res = await axios.get(
        `/ui/contentcore/catalogs/${this.catalogId}/extends`
      )
      this.dataLoading = false
      this.catalogConfigExtends = res.data.data
    }
  }
}
</script>
<style>
.catalog-extends-config .catalog-form .el-form-item {
  padding: 0px 15px;
}
.catalog-form .long-label .el-form-item__label{
   width: 300px!important;
}
.catalog-form .middle-label .el-form-item__label{
  width: 200px!important;
}
.catalog-form .short-label .el-form-item__label{
  width: 120px!important;
}
.catalog-form .short-label .el-form-item__content {
  margin-left: 120px!important;
}
</style>
