<script setup>
import moment from 'moment'
import {ref, watch} from 'vue'
import { showToast } from 'vant'
const props=defineProps({
    modelValue:{
        type:Boolean,
        default:false
    },
    prePublishTime:''
})
const emits=defineEmits(['update:modelValue','confirm'])

const minDSFBDate=new Date()
const minDSFBTime=ref(moment().add(2,'m').format('HH:mm:ss'))
const dsfbDate=ref([])//定时发布选中的日期
const dsfbTime=ref([])//定时发布选中的时间
watch(
    ()=>props.modelValue,
    (n)=>{
        if(n&&props.prePublishTime){
            dsfbDate.value=props.prePublishTime.split(' ')[0].split('-')
            dsfbTime.value=props.prePublishTime.split(' ')[1].split(':').slice(0,2)
        }
    }
)

watch(
    ()=>dsfbDate.value,
    ()=>{
        if(dsfbDate.value.join('-')==moment().format('YYYY-MM-DD')){//选中的今天
            minDSFBTime.value=moment().add(2,'m').format('HH:mm:ss')
        }else{
            minDSFBTime.value='00:00:00'
        }
    }
)
function handleClose(){
    emits('update:modelValue',false)
}
function onConfirmDSFBTime(){
    const time=`${dsfbDate.value.join('-')} ${dsfbTime.value.join(':')}:00`
    const now=moment().format('YYYY-MM-DD HH:mm:ss')
    if(moment(time).isBefore(now,'second')){
        showToast('定时发布不得早于当前时间')
        return
    }
    emits('confirm',time)
}

</script>

<template>
    <van-popup
        v-model:show="props.modelValue"
        position="bottom"
        round
    >
        <van-picker-group
            title="设置发送时间"
            :tabs="['选择日期', '选择时间']"
            next-step-text="下一步"
            @confirm="onConfirmDSFBTime"
            @cancel="handleClose"
        >
            <van-date-picker
                v-model="dsfbDate"
                :min-date="minDSFBDate"
            />
            <van-time-picker
                v-model="dsfbTime"
                :columns-type="['hour', 'minute']"
                :min-time="minDSFBTime"
            />
        </van-picker-group>
    </van-popup>
</template>