<template>
    <!-- 全局日期控件 -->
    <view class="date">
        <up-datetime-picker
            :show="show"
            v-model="defaultValue"
            :mode="mode"
            @confirm="confirm"
            closeOnClickOverlay
            @close="show = false"
            @cancel="show = false"
            :cancelText="cancelText"
            :confirmText="confirmText"
            :cancelColor="cancelColor"
            :confirmColor="confirmColor"
        ></up-datetime-picker>
        <view class="date-content" :class="getPickerClass" @tap="show = true" :style="getStyle">
            <text class="date-value">{{ getLabelValue }}</text>
            <view class="date-icon">
                <view class="date-icon-close" v-if="clearable && !showPlaceholder && !disabled" @tap.stop="clear">
                    <up-icon name="close-circle-fill" color="#c0c4cc" size="36rpx"></up-icon>
                </view>
                <global-icon icon="calendar" color="#86909C"></global-icon>
            </view>
        </view>
    </view>
</template>

<script setup>
import { computed, ref, watch } from 'vue'
import Day from 'mocp/utils/dayjs'

const es = defineEmits(['update:modelValue', 'change'])
const ps = defineProps({
    //日历模式date,time,year-month
    mode: {
        type: String,
        default: 'date'
    },
    //是否时间戳
    timestamp: {
        type: Boolean,
        default: true
    },
    //显示为空的value值
    emptyValue: {
        type: [String, Number],
        default: ''
    },
    modelValue: {
        type: [String, Number],
        default: ''
    },
    clearable: {
        type: Boolean,
        default: false
    },
    placeholder: {
        type: String,
        default: '请选择日期'
    },
    disabled: {
        type: Boolean,
        default: false
    },
    pickAlign: {
        type: String,
        default: 'left'
    },
    format: {
        type: String,
        default: 'YYYY/MM/DD'
    },
    valueFormat: {
        type: String,
        default: 'YYYY/MM/DD HH:mm:ss'
    },
    cancelText: {
        type: String,
        default: '取消'
    },
    confirmText: {
        type: String,
        default: '确认'
    },
    cancelColor: {
        type: String,
        default: '#909193'
    },
    confirmColor: {
        type: String,
        default: '#165dff'
    }
})
//获取下拉框样式
const getStyle = computed(() => {
    let pickAlign = 'flex-start'
    if (ps.pickAlign == 'center') {
        pickAlign = 'center'
    } else if (ps.pickAlign == 'right') {
        pickAlign = 'flex-end'
    }
    return {
        justifyContent: pickAlign
    }
})
//下拉框显示的内容
const labelValue = ref('')
const getLabelValue = computed(() => {
    if (labelValue.value) {
        return Day(labelValue.value).format(ps.format)
    } else {
        return ps.placeholder
    }
})
//获取下拉框class
const showPlaceholder = computed(() => {
    return !labelValue.value || labelValue.value == ps.emptyValue
})
const getPickerClass = computed(() => {
    return {
        disabled: ps.disabled,
        placeholder: showPlaceholder.value
    }
})
const show = ref(false)
//设置初始值
const defaultValue = ref()
watch(
    () => ps.modelValue,
    () => {
        if (ps.modelValue != 0 && ps.modelValue != ps.emptyValue) {
            if (ps.timestamp) {
                labelValue.value = parseInt(ps.modelValue)
                defaultValue.value = parseInt(ps.modelValue)
            } else {
                labelValue.value = ps.modelValue
                defaultValue.value = ps.modelValue
            }
        } else {
            if (ps.timestamp) {
                defaultValue.value = Day().valueOf()
            } else {
                defaultValue.value = Day().format(ps.valueFormat)
            }
        }
    },
    { immediate: true }
)
//点击确定按钮
const confirm = (e) => {
    show.value = false
    labelValue.value = e.value
    defaultValue.value = e.value
    if (ps.timestamp) {
        es('update:modelValue', defaultValue.value)
        es('change', defaultValue.value)
    } else {
        es('update:modelValue', Day(defaultValue.value).format(ps.valueFormat))
        es('change', Day(defaultValue.value).format(ps.valueFormat))
    }
}
//点击清空按钮
const clear = () => {
    labelValue.value = ''
    defaultValue.value = Day().valueOf()
    es('update:modelValue', ps.emptyValue)
    es('change', ps.emptyValue)
}
</script>
<style lang="scss" scoped>
.date {
    flex: auto;
    &-content {
        display: flex;
        align-items: center;
        line-height: 48rpx;
        &.placeholder {
            .date-value {
                color: $mocp-text-3;
            }
        }
        &.disabled {
            background: #f5f7fa;
        }
    }
    &-value {
        color: $mocp-text-4;
        margin-right: 8rpx;
    }
    &-icon {
        display: flex;
        align-items: center;
        &-close {
            margin-left: 4rpx;
        }
    }
}
</style>
