<template>
  <ht-sidebar-dialog
    title="分配门户管理员"
    :visible="dialogVisible"
    width="1040px"
    :before-close="handleClose"
    close-on-click-modal
  >
    <ht-table
      v-if="dialogVisible"
      @load="loadData"
      :data="data"
      :pageResult="pageResult"
      :selection="true"
      :show-export="false"
      :show-custom-column="false"
      :default-querys="defualtQuerys"
      :quick-search-props="[
        {prop: 'user_name_', label: '姓名'},
        {prop: 'account_', label: '账号'}
      ]"
      ref="tenantAuthTable"
      @selection-change="handleSelectionChange"
    >
      <template v-slot:toolbar>
        <el-button-group>
          <el-button
            size="small"
            type="primary"
            @click="showUserDialog()"
            icon="el-icon-plus"
            >加入用户</el-button
          >
          <ht-delete-button
            :url="roleDeleteUrl"
            :htTable="$refs.tenantAuthTable"
            pk="userId"
            parameter="userIds"
            :ignore-tenant="true"
            :disabled="isDisabled"
            >删除</ht-delete-button
          >
        </el-button-group>
      </template>
      <template>
        <ht-table-column type="index" width="50" align="center" label="序号" />
        <ht-table-column prop="id" label="主键" :sortable="true" hidden />
        <ht-table-column
          prop="username"
          label="用户名称"
          width="300"
          :sortable="true"
          :show-overflow-tooltip="true"
        />
        <ht-table-column prop="account" label="账号" :sortable="true" />
      </template>
    </ht-table>
    <ht-user-dialog
        v-show="choseAddAdmin"
      ref="eipUserDialog"
      @on-confirm="userDialogOnConfirm"
      append-to-body
    />
  </ht-sidebar-dialog>
</template>
<script>
import EipUserDialog from '@/components/dialog/EipUserDialog.vue'

export default {
  props: {},
  components: {
    // eslint-disable-next-line vue/no-unused-components
    EipUserDialog
  },
  computed: {
    roleDeleteUrl: function() {
      return `${window.context.portal}/portal/portalSysLayoutAuth/v1/removeBatch?layoutId=${this.layoutId}`
    }
  },
  data() {
    return {
      dialogVisible: false,
      isSubmit: true,
      data: [],
      pageResult: {
        page: 1,
        pageSize: 50,
        total: 0
      },
      defualtQuerys: [],
      tenantTypeId: '',
      layoutId: '',
      selectedList: [],
      isDisabled: true,
      choseAddAdmin: false
    }
  },
  watch: {
    selectedList: {
      handler(val) {
        this.isDisabled = val.length < 1 ? true : false
      },
      deep: true
    }
  },
  created() {},
  methods: {
    handleClose() {
      this.dialogVisible = false
    },
    /**
     *
     * @param {tenantTypeId}   对应的typeId
     * @param {layoutId}    id
     */
    showDialog(tenantTypeId, layoutId) {
      this.$store.dispatch('tenant/actionTenantId', null)
      this.defualtQuerys = []
      this.tenantTypeId = tenantTypeId
      //   this.defualtQuerys.push({property: 'auth.TYPE_ID_', value: tenantTypeId})

      this.layoutId = layoutId
      this.defualtQuerys.push({property: 'LAYOUT_ID_', value: layoutId})

      this.dialogVisible = true
    },
    dialogCancle() {
      this.dialogVisible = false
    },
    loadData(param, cb) {
      this.$http
        .post('${portal}/portal/portalSysLayoutAuth/v1/query', param)
        .then(
          resp => {
            let response = resp.data
            this.data = response.rows
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total
            }
          },
          error => {
            reject(error)
          }
        )
        .finally(() => cb())
    },
    showUserDialog() {
      this.choseAddAdmin = true;
      this.$refs.eipUserDialog.showDialog()
    },
    userDialogOnConfirm(selection) {
      if (!selection || selection.length == 0) {
        this.$message.error('请至少选择一个用户')
        return
      }
      //准备参数
      let sysLayoutAuths = []
      for (let user of selection) {
        sysLayoutAuths.push({ layoutId: this.layoutId, userId: user.id, username: user.fullname, account: user.account })
      }

      this.$http
        .post('${portal}/portal/portalSysLayoutAuth/v1/save', { sysLayoutAuths })
        .then(result => {
          if (result.data.state) {
            this.$message({
              message: result.data.message,
              type: 'success',
              showClose: true
            })
            this.$refs.tenantAuthTable.load()
            return
          }
          this.$message.error(result.data.message || '设置管理员失败')
        })
    },
    handleSelectionChange(selection) {
      this.selectedList = selection
    }
  }
}
</script>
<style scoped>
div[aria-invalid='true'] >>> .el-input__inner,
div[aria-invalid='true'] >>> .el-input__inner:focus {
  border-color: #f56c6c;
}
</style>
