import request from './request';

export const bundleList = params => {
  return request({
    url: `bundle/findAll`,
    data: params,
    method: 'post'
  }).then(res => res.data);
};
export const bundleById = params => {
  return request({
    url: `bundle/${params.id}`,
    method: 'get'
  }).then(res => res.data);
};

export const customerBundleList = params => {
  return request({
    url: `token/customerBundle/findAll`,
    method: 'post',
    data: params
  }).then(res => res.data);
};

export const cancelOrder = params => {
  return request({
    url: `token/customerBundle/cancel/${params.id}`,
    method: 'get'
  }).then(res => res.data);
};
export const orderDetail = params => {
  return request({
    url: `token/customerBundle/detail/${params.id}`,
    method: 'get'
  }).then(res => res.data);
};

export const getQRCode = params => {
  return request({
    url: `token/customerBundle/genCode`,
    method: 'POST',
    data: params
  }).then(res => res.data);
};

/**
 * 调用后台支付接口, 并唤醒微信支付
 * @param {*} params
 */
export const wxPay = params => {
  return request({
    url: `token/customerBundle/buy`,
    method: 'POST',
    data: params
  }).then(res => {
    const result = res.data;
    if (result.status === '200') {
      const _data = result.data;
      return new Promise(function(resolve, reject) {
        wx.requestPayment({
          timeStamp: _data.timeStamp,
          nonceStr: _data.nonceStr,
          package: _data.package,
          signType: _data.signType,
          paySign: _data.paySign,
          success(res) {
            res.status = '200';
            res.tradeNo = _data.tradeNo;
            res.orderBundleId = _data.orderBundleId;
            resolve(res);
          },
          fail(res) {
            console.log(res);
            res.orderBundleId = _data.orderBundleId;
            reject(res);
          }
        });
      });
    } else {
      return result;
    }
  });
};

export const testPay = params => {
  return request({
    url: `token/customerBundle/virtualPay/${params.id}`,
    method: 'get'
  }).then(res => res.data);
};

export const getPayStatus = params => {
  return request({
    url: `token/customerBundle/queryBankPayStatus`,
    method: 'post',
    data: params
  }).then(res => res.data);
};
