<script setup>
import { showToast,showDialog } from 'vant';
import apiReport from '@/api/report'
import {useCachedViewsStore} from '@/store/modules/cachedViews'
import {usePublicSettingStore} from '@/store/modules/publicSetting'
const cachedViewsStore=useCachedViewsStore()
const publicSettingStore = usePublicSettingStore()

const props=defineProps({
    reportData:{
        default:''
    },
    waterMarkStr:{
        type:String,
        default:'',
    }
})

const emits=defineEmits(['close'])

// 确认发布报告
// type 1 仅发布 2发布且推送
function handleConfirmPublish(type){
    if(type==2&&props.reportData.MsgIsSend==1) return
    apiReport.reportPublish({
        ReportIds:props.reportData.Id.toString(),
        ReportUrl:generatePdfLinks(props.reportData.ReportCode)
    }).then(res=>{
        if(res.Ret!=200) return;
        cachedViewsStore.removeCaches('ReportList')
        if(type===2){
            handleReportMessageSend(true)
        }else{
            if(res.Data){
                showDialog({ title: '发布提示', message: res.Data }).then(()=>{})
            }else{
                showToast('发布成功')
            }
            emits('close','refresh')
        }
    })
}

function generatePdfLinks(Code){
    return `${publicSettingStore.publicSetting.ReportViewUrl}/reportshare_pdf?code=${Code}&flag=${props.waterMarkStr}`
}

// 推送消息
function handleReportMessageSend(publish){
    apiReport.reportMessageSend({
        ReportId:Number(props.reportData.Id)
    }).then(res=>{
        if(res.Ret===200){
            if(publish){
                showToast('发布且推送成功')
            }else{
                showToast('推送成功')
            }
            emits('close','refresh')
        }
    })
}
</script>

<template>
    <div class="publish-report-pop-box">
        <div class="title">发布提示</div>
        <p class="tips">{{reportData.PrePublishTime?'该报告已设置定时发布，是否立即发布报告并推送模板消息？':'是否立即发布报告，并推送模板消息？'}}</p>
        <div class="btns">
            <div :class="['btn blue',reportData.MsgIsSend===1?'disabled':'']" @click="handleConfirmPublish(2)">发布&推送</div>
            <div class="btn" @click="handleConfirmPublish(1)">仅发布</div>
            <div class="btn" @click="emits('close')" style="margin-bottom: 0;">取消</div>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.publish-report-pop-box{
    padding: 48px;
    .title{
        font-size: 32px;
        text-align: center;
        font-weight: 600;
        margin-bottom: 32px;
    }
    .tips{
        color: $font-grey;
        margin-bottom: 48px;
        font-size: 32px;
        line-height: 48px;
    }
    .btns{
        .btn{
            display: block;
            padding: 24px 0;
            line-height: 48px;
            border-radius: 12px;
            text-align: center;
            font-size: 32px;
            font-weight: 600;
            margin-bottom: 24px;
            background-color: #F2F3FF;
            color: $theme-color;
        }
        .blue{
            background-color: $theme-color;
            color: #fff;
        }
        .disabled{
            background-color: #a8b0fc;
        }
    }
}

@media screen and (min-width:$media-width){
    .publish-report-pop-box{
        padding: 24px;
        .title{
            font-size: 22px;
            margin-bottom: 16px;
        }
        .tips{
            margin-bottom: 24px;
            font-size: 22px;
            line-height: 48px;
        }
        .btns{
            .btn{
                padding: 12px 0;
                border-radius: 12px;
                line-height: 48px;
                font-size: 20px;
                margin-bottom: 12px;
            }
        }
    }
}
</style>