var config = require('./config.js');
var http = require('./http.js');
var common = require('./common.js');
var storage = require('./storage.js');
function login(callBack) {
    if (!callBack) {
        callBack = function () {};
    }
    var userInfo = storage.getUserInfo();
    if (!userInfo) {
        // uni.login({
        //     success: (res) => {
                //const accountInfo = uni.getAccountInfoSync();
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                var postData = {
                    appid: "wxddbcc3709026525e"
                };
                http.getRequest(config.API_USER_LOGIN, postData, function (res) {
                    console.log(res);
                    if (res.data.code == 200) {
                        storage.setUserInfo(res.data.data.baseInfo);
                        uni.setStorage({
                            key: config.STORAGE_USER_TOKEN,
                            data: res.data.data.baseInfo.token
                        });
                        callBack(res.data.data.baseInfo);
                    } else {
                        common.alert('登录提示', res.data.msg);
                        callBack(null);
                    }
                });
        //     }
        // });
    }
    callBack(userInfo);
}

// 登录从本地找openid 没有的话从本地加载
function getUserOpenId() {
    if (!uni.getStorageSync(config.STORAGE_OPEN_ID)) {
        uni.login({
            success: (res) => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                var postData = {
                    wx_code: res.code
                };
                http.postApi(config.API_GET_OPEN_ID, postData, function (res) {
                    if (res.data.code == 200) {
                        uni.setStorageSync(config.STORAGE_OPEN_ID, res.data.data.wx_session_msg.openid);
                        if (res.data.data.token) {
                            uni.setStorage({
                                key: config.STORAGE_USER_TOKEN,
                                data: res.data.data.token
                            });
                        }
                    }
                });
            }
        });
    }
}
function getUserInfo(callBack) {
    var userInfo = storage.getUserInfo();
    if (userInfo) {
        if (userInfo.is_new == 0) {
            if (callBack) {
                callBack(userInfo);
            }
            return;
        }
        uni.getUserInfo({
            success: function (res) {
                console.log(res);
                // 上报
                http.postApi(config.API_REPORT_USER_INFO, res.userInfo, function (rs2) {
                    if (rs2.data.code == 200) {
                        storage.setUserInfo(rs2.data.data.baseInfo);
                        callBack(rs2.data.data.baseInfo);
                    } else {
                        common.alert('登录提示', rs2.data.msg);
                    }
                });
            },
            fail: function (res) {
                console.log(res);
                common.alert('请授权', '商城需要获取您的用户信息', function () {
                    uni.openSetting({
                        success: function (rs) {}
                    });
                });
            },
            complete: function (res) {}
        });
    }
    login(callBack);
}

// 支付
function wxPay(order_sn, payParams, callBack) {
    common.loading();
    uni.requestPayment({
        timeStamp: payParams.timeStamp,
        nonceStr: payParams.nonceStr,
        package: payParams.package,
        signType: payParams.signType,
        paySign: payParams.paySign,
        success: function (rs) {
            uni.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 1500
            });
            callBack(true);
            // http.postApi(config.API_USER_PAY_SUCCESS, { order_sn: order_sn }, function (rs2) {
            //   console.log(rs2);
            //   wx.hideLoading();
            //   if (rs2.data.code == 200) {
            //     // 跳转到订单页面
            //     callBack(true);
            //   } else {
            //     common.alert('提示', rs2.data.msg);
            //   }
            // })
        },
        fail: function (res) {
            console.log(res);
            // 支付失败  / 未完成支付
            // 跳转到订单页面
            common.simpleToast('未完成支付');
            callBack(false);
        },
        complete: function (res) {
            uni.hideLoading();
        }
    });
}

// 取消订单
function cancelOrder(order_sn, callBack) {
    http.postApi(
        config.API_USER_CANCEL_ORDER,
        {
            order_sn: order_sn
        },
        function (res) {
            if (res.data.code == 200) {
                callBack();
            } else {
                common.alert('提示', res.data.msg);
            }
        }
    );
}
function loadCarNum() {
    http.postApi(config.API_USER_CART_NUM, {}, function (response) {
        if (response.data.code === 200) {
            if (response.data.data.cart_num) {
                uni.setTabBarBadge({
                    index: 2,
                    text: response.data.data.cart_num
                });
            } else {
                uni.removeTabBarBadge({
                    index: 2
                });
            }
        }
    });
}
module.exports = {
    login: login,
    getUserInfo: getUserInfo,
    wxPay: wxPay,
    cancelOrder: cancelOrder,
    loadCarNum: loadCarNum,
    getUserOpenId: getUserOpenId
};
