<!-- 切换租户-子系统组件：选择租户及子系统，跳转到对应子系统。（默认选中当前租户、子系统，若租户只有一个，则不显示租户下拉列表） -->
<template>
  <el-dialog :title="$t('common.checkoutApp')" :close-on-click-modal="false" width="30%"  :visible.sync="dialogVisible" class="checkout-app-dialog">
    <div>
      <el-select v-model="tenantId" clearable :placeholder="$t('common.selectTenant')" v-if="isShow">
        <el-option v-for="item in tenants" :key="item.tenantId" :label="item.tenantName" :value="item.tenantId" ></el-option>
      </el-select>
      <el-select v-model="sysId" clearable :placeholder="$t('common.selectTenant')">
        <el-option v-for="item in system" :key="item.appId" :label="item.appName" :value="item.appId" ></el-option>
      </el-select>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">{{$t('common.cancel')}}</el-button>
      <el-button type="primary" @click="redirct">{{$t('common.checkout')}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import authcenterApi from '@/api/authcenterApi'
import { getUrlParams, getUrl } from '@/utils/devUtils'
import { getToken, getOrgId, getSysId } from '@/utils/auth'
export default {
  components: {
  },
  props: {
    // 控制dialog显示
    visible: {
      type: Boolean,
      default: function () {
        return false
      }
    }
  },
  data () {
    return {
      tenants: [],
      tenantId: null,
      sysId: null,
      system: [],
      sysInfo: null,
      isShow: true
    }
  },
  mounted () {
    this.$nextTick(function () {
      this.looadTenant()
    })
  },
  computed: {
    dialogVisible: {
      get () {
        return this.visible
      },
      set (value) {
        this.$emit('update:visible', value)
      }
    },
    curTenantId () {
      return getOrgId()
    },
    curSysId () {
      return getSysId()
    }
  },
  methods: {
    // 获取用户默认选择的appId
    getUserDefaultApp () {
      var $this = this
      var defaultAppKid = null
      if ($this.UtilJs.isNull($this.$store.state.user.user)) {
        $this.$store.dispatch('InitialAdminInfo').then(res => {
          defaultAppKid = $this.UtilJs.isNull(res) ? defaultAppKid : res.defaultAppKid
        })
      } else {
        defaultAppKid = $this.$store.state.user.user.defaultAppKid
      }
      return defaultAppKid
    },
    looadTenant () {
      // 加载租户信息
      var $this = this
      authcenterApi.tenant.getTenant().then(res => {
        // 设置租户信息列表并去除掉null的数据
        $this.tenants = res.data.filter(t => {
          return !$this.UtilJs.isNull(t)
        })
        $this.tenantId = $this.curTenantId
        $this.sysId = $this.curSysId
        // 若租户只有一个,不显示租户下拉框
        if ($this.tenants.length === 1) {
          $this.isShow = false
        }
      })
    },
    redirct () {
      var $this = this
      if ($this.tenantId === null || $this.sysId === null || $this.sysInfo === null) {
        $this.warning(this.$i18n.messages[this.$i18n.locale].common['pleaseSelectTenantApp'])
        return
      }
      let system = $this.sysInfo
      // 跳转到子系统指定的地址
      let rUrl = system.website
      if (rUrl !== null && rUrl !== null) {
        let rurlParas = getUrlParams(rUrl)
        let rparas = 'token=' + getToken() + '&tenantId=' + $this.tenantId + '&sysId=' + $this.sysId
        if (rurlParas !== null && rurlParas !== undefined) {
          Object.keys(rurlParas).forEach(it => {
            // 去除掉token、租户ID、子系统ID三个预留参数
            if (it !== 'token' && it !== 'tenantId' && it !== 'sysId') {
              rparas = rparas + rparas.length > 0 ? '&' : '' + it + '=' + rurlParas[it]
            }
          })
        }
        $this.$store.dispatch('FedLogout').then(res => {
          let newURI = getUrl(rUrl) + '?' + rparas
          window.open(newURI, '_self')
          $this.dialogVisible = false
        })
      } else {
        $this.warning('[' + system.appName + ']' + this.$i18n.messages[this.$i18n.locale].common['ReqUrlCanNotNull'])
      }
    }
  },
  watch: {
    tenantId (val, old) {
      if (this.UtilJs.isNull(val)) {
        return
      }
      var $this = this
      $this.sysId = null
      this.tenants.forEach(t => {
        if (t.tenantId === val) {
          // 设置子系统下拉框架选项列表
          $this.system = t.appLists
          // 若子系统中不存在当前子系统，则设为默认app/空
          let curApp = $this.system.filter(app => { return app.appId === $this.curSysId })
          let defaultAppKid = $this.getUserDefaultApp()
          if (!$this.UtilJs.isNull(defaultAppKid) && !$this.UtilJs.isEmpty(defaultAppKid)) {
            let defaultApp = $this.system.filter(app => { return app.appId === defaultAppKid })
            $this.sysId = ($this.UtilJs.isNull(curApp) || curApp.length <= 0) ? (($this.UtilJs.isNull(defaultApp) || defaultApp.length <= 0) ? null : defaultApp) : $this.curSysId
          } else {
            $this.sysId = ($this.UtilJs.isNull(curApp) || curApp.length <= 0) ? null : $this.curSysId
          }
        }
      })
    },
    sysId (val, old) {
      if (this.UtilJs.isNull(val)) {
        return
      }
      var $this = this
      this.system.forEach(it => {
        if (it.appId === val) {
          $this.sysId = it.appId
          $this.sysInfo = it
        }
      })
    }
  }
}
</script>

<style>

</style>
