<template>
  <el-dialog
    :title="$t('admin.assignRole')"
    v-model="visible"
    :width="800"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-container>
      <el-main class="nopadding">
        <div>
         
          <el-button
            v-for="item in RoleItems"
            :key="item.roleName"
            :type="item.type"
            :plain="item.type === 'success'||item.roleCode == selectCode ? false : true"
            color="#66b1ff"
            @click="handleTagSelected"
          >
            {{ item.roleName }}
          </el-button>
        </div>
      </el-main>
    </el-container>

    <template #footer>
      <el-button @click="visible = false">{{
        $t("dialogBox.cancelButtonText")
      }}</el-button>
      <el-button type="primary" :loading="isSaveing" @click="submit()">{{
        $t("dialogBox.saveButtonText")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      selectCode: "",
      RoleItems: [],
      adminRoles: [],
    };
  },
  methods: {
    open(seletcCode) {
      this.visible = true;
      this.initRoles();
      this.selectCode = seletcCode
      return this;
    },
    setData(selectCode) {
      this.selectCode = selectCode;
    },
    submit() {
      this.$emit("success", this.selectCode);
      this.visible = false;
    },
    async initRoles() {
      this.adminRoles = await this.$API.adminManage.adminRoleSelectList.post();
      if (this.adminRoles != null && this.adminRoles.length > 0) {
        this.adminRoles.map((item) => {
          var temp = JSON.parse(JSON.stringify(item)); // 为了防止错误发生，采用深拷贝
          var tagTemp = {
            type: "info",
            roleName: temp.Value,
            roleCode: temp.Key,
            selectStatus: "dark",
          };
          this.RoleItems.push(tagTemp);
        });
      }
    },
    handleTagSelected(a) {
      var curRoleName = a.target.innerText;
      this.handlerFilterTag(curRoleName);
    },
    handlerFilterTag(curRoleName) {
      this.selectCode = "";
      if (this.RoleItems != null && this.RoleItems.length > 0) {
        var newRoleItems = [];
        this.RoleItems.map((item) => {
          var temp = JSON.parse(JSON.stringify(item)); // 为了防止错误发生，采用深拷贝
          if (curRoleName === temp.roleName) {
            temp.type = "success";
            this.selectCode = temp.roleCode;
          } else {
            temp.type = "info";
          }
          newRoleItems.push(temp);
        });
        this.RoleItems = newRoleItems;
      }
    },
    //搜索
    upsearch() {
      this.$refs.table.upData(this.search);
    },
    //刷新当前页
    refresh() {
      this.$refs.table.refresh();
    },
  },
};
</script>

<style>
.tag-group__title {
  font-size: 16px;
  color: #303133;
  font-weight: 700;
}
.tag-group__label {
  margin: 5px;
  height: 38px;
  line-height: 36px;
  min-width: 100px;
  text-align: center;
  cursor: pointer;
}
</style>
