<template>
  <div class="pane-up-wrap">
    <el-toolbar>
      <el-button @click="save" :loading="loading" :disabled="siteID <=0" :priv="`com.zving.cms.Site.ExtendAdmin.${siteID}`">
        <i class="fa fa-floppy-o" aria-hidden="true"></i>保存</el-button>
    </el-toolbar>
    <div class="pane-wrap site-extend">
    <el-form label-width="180px">
      <component :is="config" :extends="extendConfigs" :siteID="siteID" :disabled="!siteID" v-for="(config,index) in configs" :key="'key_'+index" ></component>
    </el-form>
    </div>
  </div>
</template>
<script>
import util from '@src/common/util.js'
import statSiteConfigExtend from '../../stat/components/SiteExtendAction.vue'
import recommendSiteConfigExtend from '../../recommend/components/SiteExtendAction.vue'
import advertiseSiteConfigExtend from '../../advertise/components/SiteExtendAction.vue'
import hotwordSiteConfigExtend from '../../wordmanage/components/SiteExtendAction.vue'
import memberSiteConfigExtend from '../../member/components/SiteExtendAction.vue'
import commentSiteConfigExtend from '@src/pages/comment/components/SiteExtendAction.vue'
import voteSiteConfigExtend from '../../vote/components/SiteExtendAction.vue'
import imagePlayerSiteConfigExtend from '../../imageplayer/components/SiteExtendAction.vue'
import gatherSiteConfigExtend from '../../gather/components/SiteExtendAction.vue'
import seoSiteConfigExtend from '../../seo/components/SiteExtendAction.vue'
import sitegroupSiteConfigExtend from '../../sitegroup/components/SiteExtendAction.vue'
import commonsiteSiteConfigExtend from '../../workspace/components/SiteExtendAction.vue'
import siteConfigExtend from './SiteExtendAction.vue'
export default {
  components: {
    statSiteConfigExtend,
    recommendSiteConfigExtend,
    advertiseSiteConfigExtend,
    hotwordSiteConfigExtend,
    memberSiteConfigExtend,
    commentSiteConfigExtend,
    voteSiteConfigExtend,
    imagePlayerSiteConfigExtend,
    gatherSiteConfigExtend,
    seoSiteConfigExtend,
    sitegroupSiteConfigExtend,
    commonsiteSiteConfigExtend,
    siteConfigExtend
  },
  props: {
    siteID: {
      type: Number,
      default() {
        return 0
      }
    }
  },
  data() {
    return {
      loading: false,
      extendConfigs: {},
      configs: {
        siteConfigExtend,
        statSiteConfigExtend,
        recommendSiteConfigExtend,
        advertiseSiteConfigExtend,
        hotwordSiteConfigExtend,
        memberSiteConfigExtend,
        commentSiteConfigExtend,
        voteSiteConfigExtend,
        imagePlayerSiteConfigExtend,
        gatherSiteConfigExtend,
        seoSiteConfigExtend,
        sitegroupSiteConfigExtend,
        commonsiteSiteConfigExtend
      }
    }
  },
  methods: {
    // 加载站点扩展配置
    async init() {
      this.loading = true
      try {
        let res = await axios.get(`/ui/sites/${this.siteID}/extends`)
        if (res.data.status === 1) {
          this.extendConfigs = res.data.data
          this.extendConfigs.publishPlatform = this.extendConfigs.publishPlatform ? this.extendConfigs.publishPlatform.split(',') : []
          this.extendConfigs.hotWordCatagory = this.extendConfigs.hotWordCatagory ? this.extendConfigs.hotWordCatagory.split(',') : []
          this.extendConfigs.articleAutoFormatRule = this.extendConfigs.articleAutoFormatRule
            ? this.extendConfigs.articleAutoFormatRule.split(',')
            : []
          if (this.extendConfigs.articleAutoPaged === 'Y') {
            if (this.extendConfigs.articleAutoPagedWords === '500') {
              this.extendConfigs.articleAutoPagedWords = {}
            } else {
              let arr = this.extendConfigs.articleAutoPagedWords.split(',')
              this.extendConfigs.articleAutoPagedWords = {}
              for (let i = 0; i < arr.length; i++) {
                let propArr = arr[i].split('=')
                Vue.set(this.extendConfigs.articleAutoPagedWords, propArr[0], propArr[1])
              }
            }
          }
        }
      } catch (e) {}
      this.loading = false
    },

    // 保存站点扩展信息
    async save() {
      let obj = Object.assign({}, this.extendConfigs)
      let publishPlatform = obj.publishPlatform
      let hotWordCatagory = obj.hotWordCatagory
      let articleAutoFormatRule = obj.articleAutoFormatRule
      obj.publishPlatform = ''
      obj.hotWordCatagory = ''
      obj.articleAutoFormatRule = ''
      for (let i = 0; i < publishPlatform.length; i++) {
        if (i === 0) {
          obj.publishPlatform += publishPlatform[i]
        } else {
          obj.publishPlatform += ',' + publishPlatform[i]
        }
      }
      for (let i = 0; i < hotWordCatagory.length; i++) {
        if (i === 0) {
          obj.hotWordCatagory += hotWordCatagory[i]
        } else {
          obj.hotWordCatagory += ',' + hotWordCatagory[i]
        }
      }
      for (let i = 0; i < articleAutoFormatRule.length; i++) {
        if (i === 0) {
          obj.articleAutoFormatRule += articleAutoFormatRule[i]
        } else {
          obj.articleAutoFormatRule += ',' + articleAutoFormatRule[i]
        }
      }
      if (obj.articleAutoPaged === 'Y') {
        let articleAutoPagedWords = obj.articleAutoPagedWords
        obj.articleAutoPagedWords = ''
        for (let i in articleAutoPagedWords) {
          obj.articleAutoPagedWords += i + '=' + articleAutoPagedWords[i] + ','
        }
        obj.articleAutoPagedWords = obj.articleAutoPagedWords.substring(0, obj.articleAutoPagedWords.lastIndexOf(','))
      }
      this.loading = true
      let res = await axios.put(`/ui/sites/${this.siteID}/extends`, { extends: obj })
      util.showResponseMessage(res.data)
      this.loading = false
    }
  }
}
</script>
