<template>
    <button class="input-tool-item" type="button" :disabled="isPending || !sessionId" @click="handleClick">
        <img :src="sendEvaluateIcon" class="input-tool-icon" :alt="t('邀请评价')" />
        <span class="input-tool-label">{{ t('邀请评价') }}</span>
    </button>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import AIDeskCoreInstance from '@tencentcloud/ai-desk-core';
import toast from '../../../../../../utils/toast';
import sendEvaluateIcon from './sendEvaluateIcon.svg';
import { parseUnknownError } from '../../../../../../utils/parseUnknownError';

const props = defineProps<{
    sessionId: string;
}>();

const emit = defineEmits<{
    (event: 'sent'): void;
}>();

const { t } = useI18n();
const isPending = ref(false);

const handleClick = async () => {
    if (!props.sessionId || isPending.value) {
        return;
    }

    try {
        isPending.value = true;
        await AIDeskCoreInstance.sessionService.evaluateSession(props.sessionId);
        emit('sent');
    } catch (error) {
        console.error(error);
        toast.error(parseUnknownError(error))
    } finally {
        isPending.value = false;
    }
};
</script>

<style scoped>
.input-tool-item {
    display: inline-flex;
    flex: 0 0 70px;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 70px;
    height: 70px;
    padding: 0;
    border: 1px solid #E6E9F0;
    border-radius: 12px;
    background: #fff;
    box-sizing: border-box;
}

.input-tool-item:disabled {
    opacity: 0.4;
}

.input-tool-icon {
    display: block;
    width: 28px;
    height: 28px;
}

.input-tool-label {
    margin-top: 10px;
    color: #222;
    font-size: 12px;
    line-height: 16px;
    text-align: center;
    white-space: nowrap;
}
</style>