<script setup name="ReportPreview">
import {ref} from 'vue'
import { useRoute, useRouter } from "vue-router";
import apiReport from '@/api/report'
import AudioBox from './components/AudioBox.vue'
import {showToast} from 'vant'
import {reportManageBtn,useAuthBtn} from '@/hooks/useAuthBtn'
const {checkAuthBtn} = useAuthBtn()

const route=useRoute()
const router=useRouter()


// 获取报告详情
let reportInfo=ref(null)
async function getReportDetail(){
    const res=await apiReport.getReportDetail({ReportId:Number(route.query.id)})
    if(res.Ret===200){
        reportInfo.value=res.Data
        document.title=res.Data.Title
    }
}
if(route.query.id==-1){
    //新增编辑报告时的预览
    const data=JSON.parse(sessionStorage.getItem('reportPreData'))
    reportInfo.value=data
    document.title=data.Title
}else{
    getReportDetail()
}


//去编辑
async function goEdit(){
    // 先mark一下
    const markRes=await apiReport.reportMark({
        Status:1,
        ReportId:reportInfo.value.Id
    })
    if(markRes.Ret===200){
        if(markRes.Data.Status===1){
            showToast(markRes.Data.Msg || '该研报正在编辑，不可重复编辑')
            return
        }
    }else{
        showToast(markRes.ErrMsg || '未知错误，请稍后重试')
        return
    }

    if(['day','week'].includes(reportInfo.value.ChapterType)){
        router.push({
            path:"/report/chapter/list",
            query:{
                id:reportInfo.value.Id
            }
        })
    }else{
        // 研报
        router.push({
            path:'/report/edit',
            query:{
                id:reportInfo.value.Id
            }
        })
    }
}

</script>

<template>
    <div class="report-detail-page" v-if="reportInfo">
        <div class="top-stage-box" v-if="$route.query.id!=-1">
            <span class="stage">第{{reportInfo.Stage}}期 / {{reportInfo.Frequency}}</span>
            <img v-if="reportInfo.State==1&&checkAuthBtn(reportManageBtn.reportManage_reportEdit)" class="edit-icon" src="@/assets/imgs/report/icon_edit2.png" alt="" @click="goEdit">
        </div>
        <h1 class="report-title">{{reportInfo.Title}}</h1>
        <div class="auth-box">
            <span>{{reportInfo.Author}}</span>
            <span>{{reportInfo.PublishTime}}</span>
        </div>
        <!-- 晨报/周报 -->
        <template v-if="['day','week'].includes(reportInfo.ChapterType)">
            <div class="report-abstract" v-if="reportInfo.Abstract">摘要：{{reportInfo.Abstract}}</div>
            <ul class="chapter-list-wrap">
                <li class="chapter-item-box" v-for="item in reportInfo.ChapterList" :key="item.ReportChapterId">
                    <div class="type-box">
                        <span class="tag">{{item.TypeName}}</span>
                        <span>{{item.Title}}</span>
                    </div>
                    <!-- 音频 -->
                    <!-- <AudioBox :url="item.VideoUrl" v-if="item.VideoUrl"/> -->
                    <div class="report-html-wrap" v-html="item.Content"></div>
                </li>
            </ul>
        </template>
        <!-- 研报 -->
        <template v-else>
            <!-- 音频 -->
            <AudioBox :url="reportInfo.VideoUrl" v-if="reportInfo.VideoUrl"/>
            <div class="report-abstract" v-if="reportInfo.Abstract">摘要：{{reportInfo.Abstract}}</div>
            <div class="report-html-wrap" v-html="reportInfo.Content"></div>
        </template>
    </div>
</template>

<style lang="scss" scoped>
.report-detail-page{
    padding: 30px 34px;
    .report-title{
        margin: 30px 0;
        font-weight: 600;
        font-size: 42px;
        line-height: 56px;
    }
    .auth-box{
        display: flex;
        justify-content: space-between;
        font-size: $font-grey;
        font-size: 36px;
        padding-bottom: 40px;
        border-bottom: 1px solid $border-color;
        margin-bottom: 40px;
    }
    .report-abstract{
        font-size: 34px;
        line-height: 54px;
        margin: 40px 0;
    }
    .audio-box{
        margin: 40px 0;
    }
    .chapter-list-wrap{
        .chapter-item-box{
            padding-bottom: 20px;
            border-bottom: 1px dashed $border-color;
            margin-bottom: 20px;
            .type-box{
                display: flex;
                align-items: center;
                .tag{
                    display: inline-block;
                    padding: 10px;
                    font-size: 24px;
                    background: rgba(0, 82, 217, 0.1);
                    border-radius: 4px;
                    color: $theme-color;
                    margin-right: 20px;
                }
            }
        }
    }
}
.top-stage-box{
    .stage{
        display: inline-block;
        background-color: #F2F3FF;
        border-radius: 8px;
        height: 72px;
        line-height: 72px;
        padding: 0 20px;
        font-size: 28px;
    }
    .edit-icon{
        float: right;
        width: 70px;
        height: 70px;
    }
}

@media screen and (min-width:$media-width){
    .report-detail-page{
        max-width: 800px;
        margin: 0 auto;
        padding: 30px;
        .report-title{
            margin: 15px 0;
            font-size: 21px;
            line-height: 28px;
        }
        .auth-box{
            font-size: 18px;
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        .report-abstract{
            font-size: 17px;
            line-height: 27px;
            margin: 20px 0;
        }
        .chapter-list-wrap{
            .chapter-item-box{
                padding-bottom: 20px;
                margin-bottom: 20px;
                .type-box{
                    .tag{
                        padding: 5px;
                        font-size: 12px;
                        border-radius: 2px;
                        margin-right: 10px;
                    }
                }
            }
        }
    }
    .top-stage-box{
        .stage{
            border-radius: 4px;
            height: 36px;
            line-height: 36px;
            padding: 0 10px;
            font-size: 14px;
        }
        .edit-icon{
            width: 35px;
            height: 35px;
        }
    }
}
</style>