<template>
  <zpa-form ref="Form">
    <zpa-form-group>
      <zpa-select
        v-model="id"
        label="可选租户"
        :labelWidth="120"
        :values="tenantList"
      />
    </zpa-form-group>
  </zpa-form>
</template>
<script>
import { queryTopUnit } from '@centit/api-admin/unit'
import { getUser, modifyUser } from '@centit/api-admin/user'
import { getUserInfo } from '@centit/api-auth'

export default {
  name: 'TenantSelect',

  data () {
    return {
      id: '',
      currentuser: '',
      tenantList: []
    }
  },

  computed: {},

  methods: {
    getTenantList () {
      queryTopUnit({ userCode: this.currentuser.userInfo.userCode }).then(
        res => {
          this.tenantList = res.map(m => {
            return {
              value: m.topUnit,
              text: m.unitName
            }
          })
        }
      )
    },

    submit () {
      getUser(this.currentuser.userInfo.userCode).then(user => {
        user.userInfo.topUnit = this.id
        const data = {
          ...user.userInfo,
          ...{
            userRank: user.userUnit.userRank,
            userStation: user.userUnit.userStation
          }
        }
        modifyUser(this.currentuser.userInfo.userCode, data)
        // location.reload()
      })
    }
  },
  mounted () {
    getUserInfo().then(res => {
      this.currentuser = res
      this.id = this.currentuser.userInfo.topUnit
      this.getTenantList()
    })
  }
}
</script>
