<template>
	<div class="writtingBottom">
        <!-- 文档停止解析 -->
        <div class="parsed" v-if="store.bottomProgressNum>0&&store.bottomProgressNum!=100&&store.writeProgressNum<10">
            <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.bottomProgressNum>=100">
            <div class="analysis" v-if="store.writeProgressNum>0&&store.writeProgressNum!=100"  @click="store.writeGenerateState">
                <div class="icon"></div>
                <span class="text-tip-2-bold">停止</span>
            </div>
            <div class="analysis" v-else-if="store.writeProgressNum>=100" @click="store.resetGenerateState">
                <img  src="@/assets/icons/aiBox/ai-logo-color.png" alt="">
                <span class="text-tip-1">重新上传</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">
                <!-- 如果store.writeProgressNum>=1 点击了写报 否则是思维导图已完成 -->
                <div class="login" v-if="store.writeProgressNum>=1&&store.writeProgressNum<100">
                    <el-progress  type="circle" :percentage="store.writeProgressNum" :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="login" v-else-if="store.writeProgressNum>=100">
                    <el-icon style="width: 24px;height: 24px;margin-right: 4px;"><CircleCheckFilled style="width: 24px;height: 24px;" class="var(--color-primary-35)" /></el-icon>
                    <span class="text-tip-2-bold"> 文档解析完成</span>
                </div>
                <div class="login" v-else-if="store.bottomProgressNum>=100">
                    <el-icon style="width: 24px;height: 24px;margin-right: 4px;"><CircleCheckFilled style="width: 24px;height: 24px;" class="var(--color-primary-35)" /></el-icon>
                    <!-- <el-progress  type="circle" :percentage="store.writeProgressNum" :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="processWriteLogRef" 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=()=>{
    store.isShowSteps = false

    emit("write");
}

const { renderedProcess, updateProcess, clearContent } = useStream();
const processContainerRef = ref(null);
// 监听 store.processLog 变化，更新步骤内容并滚动
watch(
	() => store.processLog,
	async (newLog) => {
        console.log(newLog)
		if (newLog !== undefined && newLog !== null) {
			await updateProcess(newLog, processContainerRef.value);
		}
	},
	{ immediate: true }
);

defineExpose({
	processContainerRef
});
const processWriteLogRef=ref(null)

watch(
	() => store.processWriteLog,
	async (newLog) => {
		if (newLog !== undefined && newLog !== null) {
            const lines = newLog.split('\n').filter(newLog => newLog.trim());
            const lastLine = lines[lines.length - 1];
			await updateProcess(lastLine?lastLine:'', processWriteLogRef.value);
		}
	},
	{ immediate: true }
);
</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: rgb(246, 250, 255);
            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;
            }
            img{
                width: 21px;
                height: 16px;
                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>