import Taro from "@tarojs/taro"

export const orderState = new Map([
    ['paid', '待发货'],
    ['sent', '已发货'],
    ['received', '已收货'],
])

export const orderStateNotice = new Map([
    ['paid', '请耐心等待'],
    ['sent', '请耐心等待'],
    ['received', ''],
])


export const successNotice = (content) => {
    Taro.showToast({ title: content, icon: 'success' })
}

export const errorNotice = (content) => {
    Taro.showToast({ title: content, icon: 'error' })
}


export const loading = (title = '加载中') => {
    Taro.showLoading({ title })
}

export const closeLoading = () => {
    Taro.hideLoading()
}


export const redirectTo = (url) => {
    Taro.redirectTo({ url })
}

export const navigateTo = (url) => {
    Taro.navigateTo({ url })
}

export const backTo = () => {
    Taro.navigateBack({ delta: 1 })
}

export const backOrGo = (url) => {
    Taro.getCurrentPages().length > 0 ? Taro.navigateBack() : redirectTo(url)

}


export function dateFormat(fmt, date) {
    let ret;
    const opt = {
        "Y+": date.getFullYear().toString(),        // 年
        "m+": (date.getMonth() + 1).toString(),     // 月
        "d+": date.getDate().toString(),            // 日
        "H+": date.getHours().toString(),           // 时
        "M+": date.getMinutes().toString(),         // 分
        "S+": date.getSeconds().toString()          // 秒
        // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    for (let k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
        };
    };
    return fmt;
}


export const formatDateByStr = (date) => {
    const d = new Date(date)
    return dateFormat('YYYY-mm-dd HH:MM', d)
}