import qs from 'qs'
import fn from './function'
import store from '../store/index'
import api from '../api/index'

export default {
    /**
     * 页面跳转
     * @param router
     * router.isTabBer 跳转到带底部导航的需要为true
     */
    push(router) {
        if (fn.getType(router) === 'string') {
            uni.navigateTo({
                url: router,
                fail: err => {
                    console.log(err)
                },
            })
        } else {
            let query = qs.stringify(router.query)
            let url = query ? router.path + '?' + query : router.path
            let isTabBer = router.isTabBer
            let isHasUrl = false

            for (let key in router.query) {
                if (router.query.hasOwnProperty(key)) {
                    let item = router.query[key]
                    if (fn.getType(item) === 'string' && item.indexOf('http') > -1) {
                        isHasUrl = true
                    }
                }
            }

            url = isHasUrl ? url : decodeURIComponent(url)

            if (isTabBer) {
                uni.switchTab({
                    url: url,
                    fail: err => {
                        console.log(err)
                    },
                })
            } else {
                uni.navigateTo({
                    url: url,
                    fail: err => {
                        console.log(err)
                    },
                })
            }
        }
    },

    /**
     * 页面重定向
     * @param router
     * router.isReLaunch 关闭其他页面需要为true
     */
    replace(router) {
        if (fn.getType(router) === 'string') {
            uni.redirectTo({
                url: router,
                fail: err => {
                    console.log(err)
                },
            })
        } else {
            let query = qs.stringify(router.query)
            let url = router.path + '?' + query
            let isReLaunch = router.isReLaunch
            let isHasUrl = false

            for (let key in router.query) {
                if (router.query.hasOwnProperty(key)) {
                    let item = router.query[key]
                    if (fn.getType(item) === 'string' && item.indexOf('http') > -1) {
                        isHasUrl = true
                    }
                }
            }

            url = isHasUrl ? url : decodeURIComponent(url)

            if (isReLaunch) {
                uni.reLaunch({
                    url: url,
                    fail: err => {
                        console.log(err)
                    },
                })
            } else {
                uni.redirectTo({
                    url: url,
                    fail: err => {
                        console.log(err)
                    },
                })
            }
        }
    },

    /**
     * 返回指定页面
     * @param index 后退的页面数
     */
    go(index) {
        index = index || 1
        uni.navigateBack({
            delta: index,
        })
    },

    /**
     * 返回上一页
     */
    back() {
        const self = this
        let page = getCurrentPages()

        if (page.length <= 1) {
            // 重定向到首页
            self.push({
                isTabBer: true,
                path: '/pages/index/index',
            })
        } else {
            uni.navigateBack({
                delta: 1,
            })
        }
    },

    /**
     * 获取path路径
     * @param path
     * @param query
     */
    getPath(path, query) {
        query = {
            ...query,
            'uid': store.state.user.user.id,
            // 'bid': store.state.user.boxId,
            shopId:store.state.common.activeShop.id
        }


        return '/' + path + '?' + decodeURIComponent(qs.stringify(query))
    },

    /**
     * 获取小程序吗参数
     * @param query
     * @returns {{}}
     */
    getScene(query = {}) {
        return new Promise(resolve => {
            if (query.shareId) {
                api.get('/Share/GetSharePath', {
                    id: query.shareId,
                }).then(res => {
                    resolve(JSON.parse(res.data.parameters))
                })
            } else {
                query = {
                    ...query,
                    'uid': store.state.user.userId,
                    'bid': store.state.user.boxId,
                }

                resolve(query)
            }
        })
    },
}
