const CONFIG = require("../config.js");
const plugin = requirePlugin('fm-plugin')
const { Parse } = plugin
// var Parse = getApp().Parse;
module.exports = {
    goNovaURL: (path) => {
        console.log(path);
        // 外部应用，检测Scheme：https:// http:// wxapp://
        if (path.indexOf("http") != -1) {
            let p = path.split('?')[1]
            let url = p.substring(p.indexOf('http')).split('&')[0]
            console.log(path);
            wx.navigateTo({
                url: path
            });
            return
        }

        if (path.indexOf("wxapp") != -1) {
            return
        }
        if (path.indexOf("/n") != -1) {
            wx.navigateTo({
                url: path
            });
            return
        }

        // 内部应用，截取path第一位为module，后续为路径，?为参数
        // var plist = path.split("/")
        // var rpath = path.split("?")[0]
        // var paramstr = path.split("?")[1]
        // if(type=="module"){
        //   return "comp"
        // }
        // if(type=="path"){
        //   return rpath
        // }
        // if(type=="tag"){ // 如："/nova-zhiliang/pages/my/index" 转为组件名 "nova-zhiliang-pages-my-index"
        //   var tag = rpath.split("/").slice(1).join("-");
        //   return tag
        // }
        // if(type=="params"){
        //   if(paramstr){
        //     return params2Json(paramstr)
        //   }else{
        //     return {}
        //   }
        // }

        // wx.showToast({
        //     title: '未设置跳转链接，暂不支持该跳转',
        //     icon: 'error',
        //     duration: 1000
        // })
        return
    },
    getBlockData: async (data) => {

        let className = ""
        if (data.className || (data.filter && data.filter.className)) {
            className = data.className || data.filter.className
        }

        let filter = data.filter || {}
        if (!data.src || data.src == "list") {
            return data.list
        }
        if (data.src && data.src == "filter" && className) {
            let query = new Parse.Query(className);
            query.equalTo("company", CONFIG.default.company)

            // 加载equalTo条件
            console.log(filter.equalTo, data.filter.className)
            let keyArr = Object.keys(filter)
            keyArr.forEach(key => {
                console.log(key)
                filter[key].forEach(f => {
                    if (key == 'include') {
                        query[key](f.key)
                    }
                    if (key != 'include') {
                        if (f.type == "Boolean" && f.isOpen) {
                            query[key](f.key, f.value)
                        }
                        if ((f.type == 'String' || f.type == 'select') && f.value) {
                            query[key](f.key, f.value)
                        }
                        if (f.type == 'Pointer' && f.value) {
                            query[key](f.key, f.value)
                        }
                        if (f.type == 'Array' && f.value && f.value.length > 0) {
                            query[key](f.key, f.value)
                        }
                    }
                })
            });
            switch (className) {
                case 'Lesson':
                    query.ascending('sort')
                    break
                case 'Article':
                    query.ascending('order')
                    break
                case 'RushGoods':
                    query.ascending('order')
                    break
                case 'ShopStore':
                    query.ascending('score')
                    break
                default:
                    query.ascending('index')
                    break
            }
            if (data.limit) {
                query.limit(data.limit)
            }
            let list = await query.find();
            console.log("getBlockData", list);
            return list.map(item => item.toJSON())
        }
        return []
    },
    async checkComponentsDataProperties(comp) {
        console.log(comp.data.options && comp.data.options.data)
        return;
    },
    // More for Delete
    // 跳转详情
    goGoodsDetail: (e) => {
        //        let userInfo = wx.getStorageSync("userLogin");
        //         if (userInfo == "") {
        //           util.loginNow();
        //           return
        //        }
        console.log(e)
        console.log(e.currentTarget.dataset.link, e.currentTarget.dataset.type)
        if (e.currentTarget.dataset.link && e.currentTarget.dataset.type == "video") {
            return
        }
        // 截取后端传过来的商品的objectId
        let arr = e.currentTarget.dataset.link.split("=")
        if (!arr[1]) {
            wx.navigateBack()
        }
        console.log(arr[1])
        // 根据objectId获取商品信息
        let goodsInfo = new Parse.Query("ShopGoods")
        goodsInfo.equalTo("objectId", arr[1])
        goodsInfo.first().then(res => {
            console.log(res)
            let data = res.toJSON()
            wx.navigateTo({
                url: '/nova-shop/pages/goods_detail/goods_detail',
                events: {
                    acceptGoodsDetail: function (goodsInfo) {
                        console.log(goodsInfo)
                    }
                },
                success: function (res) {
                    res.eventChannel.emit('acceptGoodsDetail', {
                        data
                    })
                }
            });
        })
        // console.log(this.data.blocks)

    },
    toStoreDetail: (e) => {
        let userInfo = wx.getStorageSync("userLogin");
        if (userInfo == "") {
            util.loginNow();
            return
        }
        let data = JSON.stringify(e.currentTarget.dataset.item)
        if (!data) {
            wx.navigateBack()
        }
        wx.navigateTo({
            url: '/nova-shop/pages/store/store?data=' + data,
        })
    },
    toArticleDetail: (e) => {
        //        let userInfo = wx.getStorageSync("userLogin");
        //         if (userInfo == "") {
        //           util.loginNow();
        //           return
        //        }
        wx.navigateTo({
            url: e.currentTarget.dataset.url,
        })
    },

};