<template>
	<div class="writtingBottom">
        <div class="parsed" v-if="store.isGenerating">
            <div class="analysis" @click="store.resetGenerateState">
                <div class="icon"></div>
                <span class="text-tip-2-bold">停止</span>
            </div>
            <div class="progress">
                <div class="login">
                    <el-progress  type="circle" :percentage="store.bottomProgressNum" :width="24" :height="24" style="margin-right: 15px;" :show-text="false" color="rgb(5, 95, 194)"/>
                    <span class="text-tip-2-bold">文档翻译中</span>
                </div>
                <div class="text-tip-2" >
                    <div ref="processContainerRef" v-html="renderedProcess"></div>
                </div>
            </div>
        </div>
        <div class="parsed" v-else-if="store.isShowSteps&&store.bottomProgressNum>=100">
            <div class="analysis" v-if="store.isWriteStart"  @click="store.resetGenerateState">
                <div class="icon"></div>
                <span class="text-tip-2-bold">停止</span>
            </div>

            <div class="notAnalysis" v-else  @click="onWriteClick()" >
                <img src="@/assets/icons/aiBox/ai-logo.png" alt="">
                <span class="text-tip-1">智能写报</span>
            </div>

            <div class="progress">
                <div class="login">
                    <el-progress  type="circle" :percentage="store.bottomProgressNum" :width="24" :height="24" style="margin-right: 15px;" :show-text="false" color="rgb(5, 95, 194)"/>
                    <span class="text-tip-2-bold">文档翻译中</span>
                </div>
                <div class="text-tip-2" >
                    <div ref="processContainerRef" v-html="renderedProcess"></div>
                </div>
            </div>
        </div>
        <div class="parsed" v-else >
            <div class="notAnalysis" @click="onAnalysisClick()">
                <img src="@/assets/icons/aiBox/ai-logo.png" alt="">
                <span class="text-tip-1">文档解析</span>
            </div>
            <div class="notProgress">
                <img src="../assets/images/tips-icon.png" alt="">
                <span class="text-tip-2">内容由AI生成，无法确保真实准确，仅供参考</span>
            </div>
        </div>
	</div>
</template>

<script setup>
import { onMounted, onUnmounted, ref, nextTick ,watch} from "vue";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { useWrittingAsstaintStore } from "@/stores/writtingAsstaintStore";
import { useStream } from "@/hooks/useStream";
// 子组件直接获取Pinia Store（核心优化）
const store = useWrittingAsstaintStore();

const emit = defineEmits(["generate","write"]);

const onAnalysisClick=()=>{
    store.isShowSteps = !store.isShowSteps  
    emit("generate");


}
const onWriteClick=()=>{
    emit("write");
}
const { renderedProcess, updateProcess, clearContent } = useStream();
const processContainerRef = ref(null);
// 监听 store.processLog 变化，更新步骤内容并滚动
watch(
	() => store.processLog,
	async (newLog) => {
		if (newLog !== undefined && newLog !== null) {
			await updateProcess(newLog, processContainerRef.value);
		}
	},
	{ immediate: true }
);
defineExpose({
	processContainerRef
});
</script>
<style lang="scss" scoped>
.writtingBottom{
    width: 100%;
    background: #fff;
    box-shadow: 0px 0px 3px 0px #d2d2d2;
    position: relative;
    .parsed{
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 64px;
        .notAnalysis{
            margin-left: 22px;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: var(--color-primary-100);
            border-radius: 4px;
            width: 437px;
            height: 36px;
            color: #fff;
            cursor: pointer;
            img{
                width: 21px;
                height: 16px;
                margin-right: 12px;
            }
        }
        .notProgress{
            background-color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            color: rgba(132, 136, 142, 1);
            margin-right: 22px;
            img{
                width: 16px;
                height: 16px;
                margin-right: 8px;
            }
        }

        .analysis{
            border: 1px solid var(--color-primary-100);
            background-color: #fff;
            color: var(--color-primary-100);
            width: 437px;
            height: 36px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-left: 22px;
            cursor: pointer;
            .icon{
                width: 8px;
                height: 8px;
                border-radius: 2px;
                background-color: var(--color-primary-100);
                margin-right: 12px;
            }
        }
        .progress{
            display: flex;
            justify-content: center;
            align-items: center;
            color: var(--color-primary-100);
            margin-right: 22px;
            background-color: var(--color-primary-10);
            border-radius: 50px;
            height: 38px;
            padding: 0 12px;
            .login{
                margin-right: 10px;
                display: flex;
            }
        }
    }
    
}

</style>