<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="processLogBox" :class="{isProcessLog:store.isProcessLog}">
                <div class="progress">
                    <div class="login">
                        <el-progress  type="circle" :percentage="store.bottomProgressNum" :stroke-width="4" :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" style="display: flex;align-items: center;">
                        <div class="processLogAlone" @click="progressAchieveClick">{{ storeProcessLogAlone }}</div>
                        <el-icon v-if="store.isProcessLog" @click="isProcessLogClick"><ArrowUpBold /></el-icon>
                        <el-icon v-else @click="isProcessLogClick"><ArrowDownBold /></el-icon>
                    </div>
                </div>
                <div class="processLogList" ref="processLogListRef">
                    <p>执行步骤：</p>
                    <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="processLogBox" :class="{isProcessLog:store.isProcessLog}">
                <div class="progress">
                    <!-- 如果store.writeProgressNum>=1 点击了写报 否则是思维导图已完成 -->
                    <div class="login" v-if="store.writeProgressNum>=1&&store.writeProgressNum<100">
                        <el-progress  type="circle" :percentage="store.writeProgressNum" :stroke-width="4" :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||store.bottomProgressNum>=100">
                        <img class="steps" style="" src="@/assets/icons/aiBox/steps.png" alt="">
                        <span class="text-tip-2-bold" v-if="store.writeProgressNum>=100" > 智能写报完成</span>
                        <span class="text-tip-2-bold" v-else-if="store.bottomProgressNum>=100"> 思维导图已完成</span>
                    </div>

                    <div class="text-tip-2" style="display: flex;align-items: center;">
                        <div class="processLogAlone"  @click="progressAchieveClick">{{ storeProcessLogAlone }}</div>
                        <el-icon v-if="store.isProcessLog" @click="isProcessLogClick"><ArrowUpBold /></el-icon>
                        <el-icon v-else @click="isProcessLogClick"><ArrowDownBold /></el-icon>
                    </div>
                    
                </div>
                <div class="processLogList" ref="processLogListRef">
                    <p>执行步骤：</p>
                    <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=()=>{
    store.isShowSteps = false
    emit("write");
}

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

defineExpose({
	processContainerRef
});
const processLogAloneRef=ref(null)
const storeProcessLogAlone=ref()
watch(
	() => store.processLogAlone,
	async (newLog) => {
        // console.log(newLog,1)
		if (newLog !== undefined && newLog !== null) {
            const lines = newLog.split('\n').filter(newLog => newLog.trim());
            const lastLine = lines[lines.length - 1];
            storeProcessLogAlone.value=lastLine
			// await updateProcess(lastLine?lastLine:'', processLogAloneRef.value);
		}
	},
	{ immediate: true }
);

const isProcessLogClick=()=>{
    store.isProcessLog=!store.isProcessLog
    nextTick(()=>{
        processLogListRef.value.scrollTo({
            top:processLogListRef.value.scrollHeight,
            behavior: 'smooth'
        });
    })
}
// 进度完成点击切换
const progressAchieveClick=  ()=>{
    if(store.writeProgressNum>=100&&storeProcessLogAlone.value=='点击查看'){ //写报
        console.log(store.headerTabType)
        store.headerTabType='message'
    }else if(store.bottomProgressNum>=100&&storeProcessLogAlone.value=='点击查看'){ //思维导图
        console.log(store.headerTabType)
        store.headerTabType='mind'
    }
 
            
}
</script>
<style lang="scss" scoped>

.writtingBottom{
    width: 100%;
    background: #fff;
    box-shadow: 0px 0px 3px 0px #d2d2d2;
    position: relative;
    padding: 0 22px;
    .parsed{
        display: flex;
        justify-content: space-between;
        // align-items: center;
        height: 64px;
        padding-top: 15px;
        .notAnalysis{
            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;
            align-items: center;
            justify-content: space-between;
            color: rgba(132, 136, 142, 1);
            
            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;
            align-items: center;
            justify-content: space-between;
            
            color: var(--color-primary-100);
            background-color: rgb(246, 250, 255);
            border-radius: 50px;
            height: 38px;
            padding: 0 12px;
            .login{
                margin-right: 10px;
                display: flex;
                align-items: center;
                .steps{
                    width: 16px;
                    height: 16px;
                    margin-right: 12px;
                }
                .text-tip-2-bold{
                }
            }
        }
        .processLogAlone{
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 300px;
            margin-right: 15px;
            cursor: pointer;
        }
    }
    // 总步骤条
    .processLogBox{
        // background-color: rgba(246, 250, 255, 1);
        border-radius: 5px;
        transition: all 0.3s ease;
        height: 300px;
        transform: translateY(0);
        .processLogList{
            display: none;
            padding: 0 40px;
            font-size: 14px;
            color: rgb(59, 65, 75);
            line-height: 22px;
            max-height: 240px;
            overflow-y: auto;
            width: 500px;
            font-size: 16px;
        }
    }
    .isProcessLog{
        transform: translateY(-270px);
        background: rgba(246, 250, 255, 1);
        .progress{
            background-color: rgba(246, 250, 255, 1);
        }
        .processLogList{
            display: block;
        }
    }
    
    
}

</style>