<!-- TODO tagUser  -->
<template>
    <div class="tagUserbox" v-if="defaultLabel">
      <a-select :default-value="defaultValue" v-model:value="defaultValue" class='tab' :autoFocus='false' style="border:0" @change="handleChange">
        <a-select-opt-group>
          <span slot="label">切换用户</span>
          <template v-for="(el, i) of options" >
            <template v-if='i<3'>
                <a-select-option :value='el.value' :key='i'>{{el.label}}</a-select-option>
            </template>
            <template v-if='i==3'>
                <a-select-option :value='-404' :key='i'>更多</a-select-option>
            </template>
          </template>

          <!-- <a-select-option v-for="(el, i) of options" :key="i" :value="el.value">{{el.label}}</a-select-option> -->
        </a-select-opt-group>

        
      </a-select>

      <a-modal
          title="请选择租户"
          :visible="visible"
          @ok="confirmRestart"
          @cancel="concelRestart"
          :confirmLoading="this.btnLoading.loading"
          okText="确认"
          cancelText="取消"
          destroyOnClose
          centered
          :maskClosable="false"
          :keyboard="false"
      >
        <a-form :form="form">
          <a-form-item v-bind="$store.state.modal_ItemLayout" label="租户">
            <a-select @change="handleAllChange"
                show-search
                optionFilterProp="children"
                v-decorator="['type', { rules: [{ required: true, message: '请选择租户' }] }]"
                placeholder="请选择租户">
              <a-select-option
                  v-for="(item, index) of tenantList"
                  :key="index"
                  :value="item.value"
                  :disabled="item.disabled">{{ item.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
</template>
<script>
import { findListItem } from '@/utils/common'
export default {

  name: 'tagUser',

  data () {
    return {
      options:[],
      oldValue:undefined,
      defaultValue:'',
      defaultLabel:'',
      visible:false,
      btnLoading: { loading: false },
      tenantList:[],
      chooseSwitchId:undefined
    };
  },

  computed: {
    
  },

  watch: {
    
  },
  created () {
    this.getSelect()
  },
  mounted(){
    // 获取状态
    this.$get({
        url: '/sysUser/getStatus',
        params: {}
      }).then(({ data }) => {

      });
  },
  methods: {
    confirmRestart(){
      if(!this.chooseSwitchId){
        this.$message.info(
          '请选择租户！',
          3
        );
        return
      }else{
        this.switchTenant(this.chooseSwitchId)
      }
    },
    concelRestart(){
      this.chooseSwitchId = undefined
      this.defaultValue = this.oldValue
      this.visible=false
    },
    refresh(){
      this.$router.go(0)
    },
    getSelect () {
       this.$get({
        url: '/user/selectTenantLoginLog',
        params: {}
      }).then(({ data }) => {
        this.options = data
        
        this.$get({
          url: '/user/getInfo',
          params: {}
        }).then(({ data }) => {
          this.defaultValue = data.tenantId.tenantId
          this.oldValue = data.tenantId.tenantId
          this.defaultLabel = data.tenantId.tenantName
          let item = {label:this.defaultLabel,value:this.defaultValue}
          this.$nextTick(() => {
            sessionStorage.setItem('TenantLoginLogid',item.value)
            sessionStorage.setItem('TenantLoginLogname',item.label)
          })
          let noitemOption = this.options.filter(i => i != this.options.find(y => y && y.value == this.defaultValue))
          noitemOption.unshift(item)
          this.options = noitemOption
        });

      });
    },
    handleAllChange(val){
      this.chooseSwitchId = val
    },
    finditem(list,val){
      return list.find(item => item&&item.value === val)
    },
    switchTenant(id){
      let that = this
      this.$post({
        url:'/user/switchTenant',
        params:{
          tenantId:id
        }
      }).then(data =>{
        this.$message.success(data.msg)
        setTimeout(() => {
          that.refresh()
        }, 500);
      }).catch(data =>{
        this.$message.error(data.data.msg);
        this.defaultValue = this.oldValue
      })
    },
    handleChange(val){
      if(val == '-404'){
        this.$get({
          url: '/user/selectAllTenantLoginLog',
          params: {}
        }).then(({ data }) => {
          this.tenantList = data
          this.visible = true
        });
        return
      }
      sessionStorage.setItem('TenantLoginLogid',val)
      this.switchTenant(val)
    }
  }
  
};
</script>
<style lang="scss">
  .tagUserbox{
    margin-top:10px;
    width:140px;
    .tab{
      width:140px;
      border:0px !important;
      .ant-select-selection{
        border:0;
      }
      ::v-deep .ant-select-focused{
        border:1px solid red;
      }
      .ant-select-open .ant-select-selection{
        border:1px solid red !important;
      }
    }
  }
</style>
