import sendReqMixin from './sendReqMixin'
import { useRouter } from "@/hooks/useRouter";

export default function () {
    const {sendReq} = sendReqMixin()
    const {push} = useRouter()

    //  请求数据前 请求完再显示所有组件
    function beforeGetData() {
        if (typeof uni !== 'undefined') {
            uni.getStorage({
                key: 'sendNum',
                success: function (res) {
                    const sendNum = res.data
                    uni.setStorage({key: 'sendNum', data: parseInt(sendNum) + 1})
                },
            })
        } else {
            const sendNum = localStorage.getItem('sendNum')
            localStorage.setItem('sendNum', parseInt(sendNum) + 1)
        }
    }

    //  请求数据后
    function afterGetData() {
        if (typeof uni !== 'undefined') {
            uni.getStorage({
                key: 'sendNum',
                success: function (res) {
                    const sendNum = res.data
                    uni.setStorage({key: 'sendNum', data: parseInt(sendNum) - 1})
                },
            })
        } else {
            const sendNum = localStorage.getItem('sendNum')
            localStorage.setItem('sendNum', parseInt(sendNum) - 1)
        }
    }

    // 判断url
    function jumpLink(linkObj) {
        console.log(linkObj, 'linkObj')
        var link = ''
        if (linkObj?.typeText) {
            switch (linkObj.typeText) {
                case '类别':
                    jumpCategory(linkObj.data.id)
                    break
                case '店辅':
                    jumpStore(linkObj.data)
                    break
                case '商品':
                    jumpProductDetail(linkObj.data)
                    break
                case '自定义':
                // router.push("/category");
                case '公告':
                    jumpNoticeDetail(linkObj.data)
                    break
                case '产品':
                    jumpProList()
                    break
                case '秒杀':
                    jumpSeckills()
                    break
                case '拼团':
                    jumpGroupWorks()
                    break
                case '折扣':
                    jumpDiscount()
                    break
            }
        } else if (linkObj?.selsectValue === '/index') {
            uni.navigateTo({
                url: `/root/index/index`
            })
        }
        return link
    }

    // 跳转到搜索
    function toSearch(key) {
        push({
            url: '/pages/search/search'
        }, {
            data: {key}
        })
    }

    // 跳转到类别主页
    function jumpCategory(id) {
        push({
            url: '/pages/goodsList/goodsList'
        }, {
            data: {sid: id}
        })
    }

    // 跳转到产品列表
    function jumpProList(item) {
        push({
            url: '/pages/goodsList/goodsList'
        })
    }

    // 跳转到店铺主页
    function jumpStore(item) {
        uni.navigateTo({
            url: `/pages_category_page1/store/index?storeId=${ item.shopId }`
        })
    }

    // 跳转到商品详情
    function jumpProductDetail(item) {
        push({url: '/pages/goodsDetail/goodsDetail'}, {data: {id:item.id,skuId:item.skuId}})
    }

    // 跳转到秒杀专区
    function jumpSeckills() {
        push({url: '/pages/seckilling/seckilling'})
    }

    // 跳转到拼团专区
    function jumpGroupWorks() {
        push({url: '/pages/groupBuy/groupBuy'})
    }

    // 跳转到折扣专区
    function jumpDiscount() {
        push({url: '/pages/discount/discount'})
    }

    // 跳转到会员专区
    function jumpVip() {
        uni.navigateTo({
            url: '/pages_category_page1/memberCenter/activityList',
            success: res => {
            }, fail: () => {
            }, complete: () => {
            }
        })
    }

    // 跳转组合支付
    function jumpCombination(item) {
        if (item.priceId) {
            uni.navigateTo({
                url: '/pages_category_page1/goodsModule/combination?priceId=' + item.priceId
            })
        } else {
            uni.showToast({
                title: '暂无活动',
                icon: "none"
            });
        }
    }

    // 跳转到公告详情
    function jumpNoticeDetail(item) {
        uni.navigateTo({
            url: '/pages_category_page2/userModule/messageDetail?noticeId=' + item.noticeId
        })
    }

    // 跳转到直播列表
    function jumpLive() {
        uni.navigateTo({
            url: '/pages_category_page2/livePage/index'
        })
    }

    // 加入购物车
    function addCart(id) {
        console.log(id)
    }

    return {
        beforeGetData,
        afterGetData,
        toSearch,
        sendReq,
        jumpLink,
        jumpCategory,
        jumpStore,
        jumpProductDetail,
        jumpSeckills,
        jumpGroupWorks,
        jumpDiscount,
        jumpVip,
        jumpNoticeDetail,
        addCart,
        jumpProList,
        jumpLive,
        jumpCombination
    }
}
