<template>
  <el-dialog
    title="租户下发"
    width="30%"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    append-to-body
  >
    <el-form
      v-loading="loading"
      element-loading-text="下发用户或应用过多时，租户下发的时间将会比较慢，请耐心等待"
    >
      <ht-form-item label="租户">
        <el-select multiple v-model="tenantIds">
          <el-option
            v-for="item in tenants"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </ht-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="dialogVisible = false" :loading="loading"
        >取消</el-button
      >
      <el-button type="primary" @click="confirm" :loading="loading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import request from '@/request'
export default {
  name: 'AppTenantPublishDialog',
  data() {
    return {
      dialogVisible: false,
      tenants: [],
      tenantIds: [],
      appIds: [],
      loading: false,
    }
  },
  created() {
    this.getTenants()
  },
  methods: {
    open(appIds) {
      this.dialogVisible = true
      this.appIds = appIds
      this.tenantIds = []
    },
    confirm() {
      let param = {
        appIds: this.appIds,
        tenantIds: this.tenantIds,
      }
      this.loading = true
      request
        .post(`${window.context.portal}/portal/sysApp/v1/tenant/publish`, param)
        .then((resp) => {
          if (resp.data.state) {
            this.$message.success(resp.data.message)
            this.dialogVisible = false
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    getTenants() {
      let param = {pageBean: {page: 1, pageSize: -1}}
      request
        .post(`${window.context.uc}/uc/tenantManage/v1/listJson`, param)
        .then((resp) => {
          this.tenants = resp.data.rows.filter((item) => item.id !== '-1') || []
        })
    },
  },
}
</script>

<style scoped></style>
