// 路由配置
const app = getApp();

const req = require("./request.js"); // 获取页面模板配置


const getHomeTemplateConfig = (data, success) => {
  req.getRequest('/api/template/getTemplate', data, success);
}; // 获取门店列表


const getMerchant = (data, success) => {
  req.getRequest('/api/merchant/list', data, success);
}; // 获取小程序配置


const getConfing = (data, success) => {
  req.g('/api/config', data, success);
}; // 门店详情


const getMerdetail = (data, success) => {
  req.getRequest('/api/merchant/detail', data, success);
}; // 解析二维码


const getParams = (data, success) => {
  req.getRequest('/api/code/params', data, success);
}; // 产品分类列表


const getCategory = (data, success) => {
  req.getRequest('/api/category/list', data, success);
}; // 获取banner图


const getBanner = (data, success) => {
  req.getRequest('/api/banner', data, success);
}; // 获取产品列表


const getProductList = (data, success) => {
  req.getRequest('/api/product/list', data, success);
}; // 获取小店产品列表


const getShopeList = (data, success) => {
  req.getRequest('/api/shopProduct/list', data, success);
}; // 获取活动产品


const getAcitve = (data, success) => {
  req.getRequest('/api/activity/list', data, success);
}; // 获取活动产品  拼团 砍价


const getAcitveNew = (data, success) => {
  req.getRequest('/api/activity/listNew', data, success);
}; // 取消活动产品预约


const cancelActivityRemind = (data, success) => {
  req.postRequest('/api/activity/cancel/remind', data, success);
}; // 活动产品预约


const addActivityRemind = (data, success) => {
  req.postRequest('/api/activity/remind', data, success);
}; // 获取专题


const getSpecial = (data, success) => {
  req.getRequest('/api/special/list', data, success);
}; // 加入购物车


const addShopCart = (data, success) => {
  req.getRequest('/api/purchase/cart', data, success);
}; // 查询用户余额


const extendNew = (data, success) => {
  req.postRequest('/api/rrLogs/extendNew', data, success);
}; // 余额充值策略


const balance = (data, success) => {
  req.getRequest('/api/rrLogs/list', data, success);
}; //余额购买


const remainder = (data, success) => {
  req.postRequest('/api/rrLogs/rechargeDecision', data, success);
}; // 查询余额明细


const balanceLogs = (data, success) => {
  req.postRequest('/api/rrLogs/balanceLogs', data, success);
}; // 生成微信统一下单订单信息


const membersPay = (data, success) => {
  req.postRequest('/api/members/weixin/pay', data, success);
}; //余额充值是否成功接口


const balanceStare = (data, success) => {
  req.postRequest('/api/rrLogs/check', data, success);
}; // vip创建订单购买


const vip = (data, success) => {
  req.postRequest('/api/levelStrategy/buyNowStrategy', data, success);
}; // vip支付方法回调


const vipCheck = (data, success) => {
  req.postRequest('/api/levelStrategy/check', data, success);
}; // 会员购买公共方法


const vipPay = (id, status, money, success) => {
  membersPay({
    id: id,
    status: status
  }, json => {
    console.log(json);

    if (json.type === 2) {
      //调用后台判断订单是否支付成功
      let i = 0;
      let timer = setInterval(() => {
        vipCheck({
          strategyId: id
        }, res => {
          if (res.isSuccess || i === 4) {
            clearInterval(timer);
            success.call(this, res.isSuccess);
          } else {
            i++;
          }
        }, true);
      }, 200); // success.call(this);

      return false;
    }

    uni.requestPayment({
      timeStamp: json.timeStamp,
      nonceStr: json.nonceStr,
      package: json.packages,
      signType: json.signType,
      paySign: json.sign,
      success: function () {
        let i = 0;
        let timer = setInterval(() => {
          vipCheck({
            strategyId: id
          }, res => {
            // console.log(i);

            if (res.isSuccess || i === 4) {
              clearInterval(timer);
              success.call(this, res.isSuccess);
            } else {
              i++;
            }
          }, true);
        }, 200);
      },
      fail: function (res) {// vipCheck( { strategyId: id }, data => {
        //   app.navigateTo('product/pay/pay?isSuccess=false&vip=true&money='+money+'&id='+id)
        //   success.call(this,data.isSuccess)
        // })
      }
    });
  });
}; // 余额支付


const balancePay = (id, status, money, success) => {
  let from = {
    id: id,
    status: status
  };
  membersPay(from, json => {
    // console.log(json);

    if (json.type === 2) {
      //调用后台判断订单是否支付成功
      let i = 0;
      let timer = setInterval(() => {
        balanceStare({
          strategyId: id
        }, res => {
          if (res.isSuccess || i === 4) {
            clearInterval(timer);
            success.call(this, res.isSuccess);
          } else {
            i++;
          }
        }, true);
      }, 200); // success.call(this);

      return false;
    }

    uni.requestPayment({
      timeStamp: json.timeStamp,
      nonceStr: json.nonceStr,
      package: json.packages,
      signType: json.signType,
      paySign: json.sign,
      success: function () {
        let i = 0;
        let timer = setInterval(() => {
          balanceStare({
            logsId: id
          }, res => {
            // console.log(i);

            if (res.isSuccess || i === 4) {
              clearInterval(timer);
              success.call(this, res.isSuccess);
            } else {
              i++;
            }
          }, true);
        }, 200);
      },
      fail: function (res) {// vipCheck( { strategyId: id }, data => {
        //   app.navigateTo('product/pay/pay?isSuccess=false&vip=true&money='+money+'&id='+id)
        //   success.call(this,data.isSuccess)
        // })
      }
    });
  });
};
/**
 * 
 * @param {*分享类型 1分享好友 2分享到朋友圈 11保存产品海报 12分享个人海报} type 
 * @param {*分享人id或者访问者的用户id} uid 
 * @param {*页面及参数} page 
 * @param {*门店id} merchantId 
 * @param {*小店id} shopId 
 * @param {*分享信息类型  1 产品 2 文章 3 专题  5 团购活动  6 秒杀  7 拼团 8 新客特价 9 优惠砍价 10 赛事活动} shareInfoType 
 * @param {*活动id_产品id} bindId 
 */


const share = (type, uid, page, merchantId, shopId, shareInfoType, bindId) => {
  let from = {
    type: type,
    page: page,
    shareInfoType: shareInfoType,
    bindId: bindId
  };

  if (!uid) {} else {
    from.uid = uid;
  }

  if (!shopId) {} else {
    from.shopId = shopId;
  }

  if (!merchantId) {} else {
    from.merchantId = merchantId;
  }

  req.postRequest('/api/recordShare', from, res => {
    console.info(res);
  });
};

module.exports = {
  getHomeTemplateConfig: getHomeTemplateConfig,
  getMerchant: getMerchant,
  getConfing: getConfing,
  getMerdetail: getMerdetail,
  getParams: getParams,
  getBanner: getBanner,
  getCategory: getCategory,
  getProductList: getProductList,
  getShopeList: getShopeList,
  getAcitve: getAcitve,
  cancelActivityRemind: cancelActivityRemind,
  addActivityRemind: addActivityRemind,
  getAcitveNew: getAcitveNew,
  getSpecial: getSpecial,
  addShopCart: addShopCart,
  extendNew: extendNew,
  balance: balance,
  remainder: remainder,
  balanceLogs: balanceLogs,
  membersPay: membersPay,
  balanceStare: balanceStare,
  vipPay: vipPay,
  vip: vip,
  vipCheck: vipCheck,
  balancePay: balancePay,
  share: share
};