<template>
  <span>
    <a-select show-search
              option-filter-prop="children"
              :filter-option="filterOption"
              v-model="siteCode" @change="changeUser"  :style="'width:'+selectWidth">
      <a-select-option v-for="data in siteinfo" :key="data.code" :value="data.code" :info="data">
        {{ data.name }}
      </a-select-option>
    </a-select>

    <a-modal
      title="温馨提示"
      :visible="visible"
      :closable="false"
      :keyboard="false"
      :maskClosable="false"
      :footer="null"
      centered
      dialogClass="wp-tips"
    >
      <div style="text-align: center;line-height: 2">
         <p>您当前选择的是 WordPress 嵌套网站，需要在系统操作之前，请返回首页重新选择站点操作。本网站的产品类型是 {{ productType }}。</p>
          <router-link :to="{ path: '/websiteManage/dashboard/analysis' }">
            <a-button type="primary">返回首页</a-button>
          </router-link>
      </div>
    </a-modal>
  </span>

</template>

<script>
import { getAction, postAction } from '/@/api/manage/manage'
import { mapActions } from 'vuex'
import {store}  from '/@/store'
import { generateIndexRouter } from '/@/utils/util'
import { router } from '/@/router';

export default {
  data(){
    return{
      siteinfo:[],
      siteCode:'',
      selectSiteInfo:{},
      visible:false,
      productType:''
    }
  },
  props:{
    comProps:{},
    siteListUrl:{
      default:'/sys/api/getSiteListByUid'
    },
    selectWidth:{
      default:'300px'
    }
  },
  mounted() {
    this.getSiteInfo()
  },
  methods:{
    ...mapActions(['GetPermissionList']),
    //进入获取站点code
    getSiteInfo() {
      let route = this.$route.name
      let that = this
      getAction(that.siteListUrl).then(function (res) {
        if (res.code == 0) {
          that.siteinfo = res.data
          let isInSite = false
          for (let i in res.data){
            if(localStorage.getItem('siteCode') !== null && res.data[i].code === localStorage.getItem('siteCode')){
              isInSite = true
              that.selectSiteInfo = res.data[i]
            }
          }
          if (localStorage.getItem('siteCode') !== null && isInSite) {
            that.siteCode = localStorage.getItem("siteCode")
          }else {
            that.siteCode = res.data[0].code
            that.selectSiteInfo = res.data[0]
            localStorage.setItem("siteCode",res.data[0].code)
          }
          that.$nextTick(()=>{
            that.$emit('setSiteInfo',res.data,that.selectSiteInfo)
          })
          if(route == 'websiteManage-dashboard-analysis'){
            that.getPermissionList(that.siteCode)
          }
          // that.getAllInfo()
        } else {
          that.$message.error('站点获取失败，请刷新重试')
        }
      })
    },


    //改变站点
    changeUser(value,e) {
      let that = this
      let route = this.$route.name
      that.siteCode = value
      localStorage.setItem("siteCode",value)
      postAction('/adweb/adwebSiteManage/queryWordPressConfig', {  siteCode: value }).then(function (res) {
        if(res.code == 200 && res.result.wordpressSwitch && res.result.wordpressSwitch == 1
          && (route == 'pageManage-allPage'
            || route == 'pageManage-addPage'

            || route == 'website-enterpriseInfo-baseInfo'
            || route == 'website-enterpriseInfo-bannerAdv'
            || route == 'website-enterpriseInfo-customRevier'
            || route == 'website-enterpriseInfo-enterpriseintroduction'
            || route == 'company-advantage'
            || route == 'website-enterpriseInfo-projectApplication'
            || route == 'website-enterpriseInfo-honor'
            || route == 'website-productSummary-productList5684'

            || route == 'website-productSummary-category'
            || route == 'website-productSummary-productsTag'
            || route == 'product-draft'
            || route == 'website-productSummary-productList'
            || route == 'website-productSummary-downloadDocument'
            || route == 'website-productSummary-faq'
            || route == 'website-productSummary-productAdd'

            || route == 'website-newsSummary-category'
            || route == 'website-newsSummary-contentTag'
            || route == 'website-newsSummary-AdwebMaterialNewsList'
            || route == 'websit-newsSummary-addContent'
            || route == 'website-enterpriseInfo-enterprisehistory'
            || route == 'website-enterpriseInfo-FactoryEnvironment'
            || route == 'website-enterpriseInfo-serviceGuarantee'
            || route == 'website-enterpriseInfo-QuestionList'
            || route == 'website-enterpriseInfo-socialmedialinks'

            || route == 'website-productSummary-category1'
            || route == 'website-productSummary-productList1'
            || route == 'website-newsSummary-category1'
            || route == 'operate-blog-update')

        ){
          that.productType = res.result.productType
          that.visible = true;
        }else{
          that.getPermissionList(value)
          that.$emit('comMethods',value,e)
        }
      })
    },
    //站点搜索
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },

    getPermissionList(value){
      let that = this
      that.$store.dispatch('GetPermissionList', value).then(res => {
        const menuData = res.result.menu;
        if (menuData === null || menuData === "" || menuData === undefined) {
          return;
        }
        let constRoutes = [];
        constRoutes = generateIndexRouter(menuData);
        that.$store.dispatch('UpdateAppRouter',  { constRoutes }).then(() => {
          router.addRoutes(store.getters.addRouters)
        })
      })
    }


  }
}
</script>

<style lang="less">
.wp-tips{
  .ant-modal-header{
    padding: 10px;
  }
  .ant-modal-body{
    padding: 24px;
    p{
      margin-bottom: 10px;
    }
  }
}
</style>
