<template>
  <div class="roleDialog" v-show="roleVisible" @mouseleave="mouseleaveHandle()">
    <div class="role-list">
      <div
        class="role-item"
        v-for="(item, index) in memberRoleList"
        :key="item.id"
        @click="confirmHandle(index)"
        :class="{ active: currentIndex === index }"
      >
        <div>
          <div class="role-name" :title="item.roleName">{{ item.roleName }}</div>
          <div class="org-name" :title="item.orgName">{{ item.orgName }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from 'vuex'
export default {
  name: 'roleDialog',
  props: {
    roleVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      currentIndex: 0
    }
  },
  computed: {
    ...mapState('user', ['memberRoleList', 'roleDetail'])
  },
  methods: {
    ...mapActions('tree', ['refreshTreeData']),
    ...mapMutations('user', ['setCurrentRoleDetail']),
    ...mapMutations('tree', ['SETCURRENRTNODE']),
    ...mapActions('menu', ['getUserMenus', 'getUserButtons']),
    mouseleaveHandle() {
      this.$emit('update:roleVisible', false)
    },
    async confirmHandle(index) {
      this.currentIndex = index
      // 切换角色后更新信息
      const roleDetail = this.memberRoleList[this.currentIndex]
      const { roleId, applyId } = roleDetail
      // 更新权限
      await this.getUserButtons({ roleId, applyId })
      // 更新菜单
      await this.getUserMenus({ roleId, applyId })
      // 更新当前角色信息
      await this.setCurrentRoleDetail(roleDetail)
      // 更新组织树的初始节点信息
      const { orgCode: partyOrgCode, orgId: id, orgName: partyOrgName, orgType: partyOrgType } = roleDetail
      await this.SETCURRENRTNODE({ partyOrgCode, id, partyOrgName, partyOrgType })
      // 更新树
      this.refreshTreeData()
      this.$emit('update:roleVisible', false)
    },

    closeHandle() {
      this.$emit('update:roleVisible', false)
    }
  },
  mounted() {
    this.currentIndex = this.memberRoleList.findIndex(v => v.id === this.roleDetail.id)
  }
}
</script>

<style lang="scss" scoped>
.role-list {
  font-family: PingFang SC-常规体, PingFang SC;
  font-weight: normal;
  margin: 0;
  padding: 0;
  height: 400px;
  overflow-y: auto;
  .role-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px;
    cursor: pointer;
    position: relative;
    background: #f7f8fa;
    margin-bottom: 16px;
  }
  .role-name {
    max-width: 260px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    height: 20px;
    font-size: 16px;
    font-weight: 600;
    color: #333333;
    line-height: 20px;
  }
  .org-name {
    margin-top: 8px;
    max-width: 260px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    height: 14px;
    font-size: 14px;
    line-height: 14px;
    color: #666666;
  }
  .active {
    background-color: #dc1a36;
    .role-name {
      color: #fff;
    }
    .org-name {
      color: #fff;
    }
  }
  .active::after {
    content: '';
    width: 24px;
    height: 24px;
    background-image: url('@/assets/images/base/select.svg');
    background-size: 100% 100%;
    margin-right: 20px;
  }
}
.roleDialog {
  width: 374px;
  height: 440px;
  padding: 20px;
  background-color: #fff;
  right: 16px;
  z-index: 10;
  top: 72px;
  position: absolute;
  border: 1px solid rgba(0, 0, 0, 0.12);
  box-shadow: 7px 12px 15px 1px rgba(0, 0, 0, 0.12);
  border-radius: 8px 8px 8px 8px;
  opacity: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;

  .footer {
    background-color: #fff;
    display: flex;
  }
}
</style>
