<template>
  <div class="jointRoomMenu">
    <pop-menu ref="popMenu" :menu="menu" />
  </div>
</template>

<script>
import PopMenu from '@/components/PopMenu';
import { DeviceMenu } from '@/scripts/ConstDic';
import { putJointTrainingUserkicked } from '@/api/chat';
import { putJointTrainingUserkickedNew} from '@/api/jointTraining';

export default {
    components: {
        PopMenu
    },
    props: {
        clickUserId: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            menu: [{
                label: this.$t('trainRoom.kickOutTheRoom'),
                handler: this.kicked
            }]
        };
    },
    computed: {
        userId() {
            return this.$store.state.user ? this.$store.state.user.id : '';
        },
        position() {
            return this.$store.state.menuOperation.menuPosition;
        },
        drawWay() {
            return this.$route.query.drawWay + '';
        }
    },
    watch: {
        '$store.state.menuOperation.menuCount': function (val) {
            if (this.$store.getters['menuOperation/checkDialogIsOpen'](DeviceMenu.JointRoom)) {
                this.doShow();
            } else {
                this.doClose();
            }
        }
    },
    mounted() {
        this.closeEvent();
    },
    methods: {
        closeEvent() {
            const self = this;
            window.onclick = function (e) {
                self.doClose();
            };
        },
        doShow() {
            this.closeEvent();
            if (this.userId != this.clickUserId) {
                if (this.$refs && this.$refs.popMenu) {
                    this.$refs.popMenu.resetShowPosition(this.position);
                }
            }
        },
        doClose() {
            if (this.$refs && this.$refs.popMenu) {
                this.$refs.popMenu.close();
            }
        },
        async kicked() {
            try {
                if (this.drawWay === 'true' ) {
                    await putJointTrainingUserkickedNew(this.clickUserId, this.$route.query.group);
                } else {
                    await putJointTrainingUserkicked(this.clickUserId, this.$route.query.group);
                }
            } catch (error) {
                console.error(error);
            }
        }
    }
};
</script>
