export function toast(options) {
    uni.showToast({
        icon: options.icon || 'none',
        mask: options.mask || true,
        title: options.title || 'title'
    })
}
toast.prototype.success = (message) => toast({ icon: 'success', title: message })
toast.prototype.error = (message) => toast({ icon: 'error', title: message })


const install = (Vue, options = {}) => {

    const prefix = options.prefix || '/pages/views/'

    /* tabbar 切换 */
    const switchTab = (url, success, fail) => {
        uni.switchTab({
            url: `/pages/tabBar/${url}/${url}`,
            success: success,
            fail: fail
        })
    }

    /* 保留当前页面跳转 */
    const navigateTo = (url, success, fail) => {
        uni.navigateTo({
            url: `${prefix}${url}`,
            success: success,
            fail: fail
        })
    }

    /* 关闭当前页面跳转 */
    const redirectTo = (url, success, fail) => {
        uni.redirectTo({
            url: `${prefix}${url}`,
            success: success,
            fail: fail
        })
    }

    /* 关闭所有页面跳转 */
    const reLaunchTo = (url, success, fail) => {
        uni.reLaunch({
            url: `${prefix}${url}`,
            success: success,
            fail: fail
        })
    }

    /* 关闭当前页面，返回上一页面或多级页面 */
    const navigateBack = (delta, success, fail) => {
        uni.navigateBack({
            delta: delta || 1,
            success: success,
            fail: fail
        })
    }

    /* 设置页面刷新标识 */
    const addRefreshType = (mark = '') => {
        uni.setStorageSync(`REFRESH_TYPE_${mark.toUpperCase()}`, true)
    }

    /* 获取页面刷新标识并判断是否需要刷新当前页面 */
    const checkRefreshType = (mark = '') => {
        const key = `REFRESH_TYPE_${mark.toUpperCase()}`
        const type = uni.getStorageSync(key)
        uni.removeStorageSync(key)
        return type
    }

    Vue.prototype.$toast = (message) => toast({ title: message })
    Vue.prototype.$toast.success = (message) => toast({ icon: 'success', title: message })
    Vue.prototype.$toast.error = (message) => toast({ icon: 'error', title: message })

    Vue.prototype.$router = {
        switchTab,
        navigateTo,
        redirectTo,
        reLaunchTo,
        navigateBack,
        addRefreshType,
        checkRefreshType
    }
}


export default install
