
import wxpay from '@service.wxpay';
import alipay from '@service.alipay';
import { getConfigOfWxH5Pay, getConfigOfWxAppPay, getConfigOfAliPay } from "../api";

export const pageLoad = (meta, fn, params) => {
  return new Promise(async (resolve, reject) => {
    if (meta.current_page) {
      if (meta.next_page_url) {
        let ret = await fn(params);
        resolve(ret);
      } else reject("last page");
    } else {
      console.log("first page");
      let ret = await fn(params);
      resolve(ret);
    }
  })
}
// 支付配置
export const configWxPay = async (params) => {
  //let payType = wxpay.getType();
  let ret = await useWxH5Pay(params);;

  // return;
  /* switch (payType) {
    case "APP":
      try {
        ret = await useWxAppPay(params);
      } catch (e) {
        ret = e;
      };
      break;
    case "MWEB":
      try {
        ret = await useWxH5Pay(params);
      } catch (e) {
        console.log("e", e);
        ret = e;
      };
      break;
    default: ret = "app not install or wx pay is not avaliable";
  } */

  return ret;
}

// h5支付
let useWxH5Pay = (params) => {
  return new Promise(async (resolve, reject) => {
    let config = await getConfigOfWxH5Pay(params);
    let extra = {
      mweb_url: config.mweb_url + "&redirect_url=" + encodeURIComponent("https://quickapp.leyuee.com/testp")
    }
    console.log(extra);
    wxpay.pay({
      prepayid: config.prepay_id,
      referer: "https://quickapp.leyuee.com",
      extra: extra,
      success: (data) => {
        console.log("success", data);
        // app中返回prepayid web中返回final_url
        resolve({ code: "9000", data: data, order: config.trade_no, message: "订单已经提交" });
      },
      fail: (data, code) => {
        console.log("error");
        console.log(data, code);
        reject({ code: code, data: data, message: data });
      },
      cancel: () => {
        console.log("user cancel");
        reject({ code: "6001", data: "", message: "user cancel" });
      }
    })
  })
}

// app支付
let useWxAppPay = (params) => {
  return new Promise(async (resolve, reject) => {
    let config = await getConfigOfWxAppPay(params);
    let extra = {
      app_id: config.appid,
      partner_id: config.partnerid,
      package_value: config.package,
      nonce_str: config.noncestr,
      time_stamp: config.timestamp + "",
      order_sign: config.sign
    }
    console.log(extra);
    console.log("prepayid ", config.prepayid);
    wxpay.pay({
      prepayid: config.prepayid,
      extra: extra,
      success: (data) => {
        console.log("success", data);
        // app中返回prepayid web中返回final_url
        resolve({ code: "9000", data: data, order: config.trade_no, message: "支付成功" });
      },
      fail: (data, code) => {
        console.log("error");
        console.log(data, code);
        reject({ code: code, data: data, message: data });
      },
      cancel: () => {
        console.log("user cancel");
        reject({ code: "6001", data: "", message: "user cancel" });
      }
    })
  })
}

// 配置支付宝支付
export const configAliPay = (params) => {
  return new Promise(async (resolve) => {
    let orderInfo = await getConfigOfAliPay(params);
    alipay.pay({
      orderInfo: orderInfo.order_info,
      callback: (ret) => {
        console.log("alipay cb", ret);
        resolve({ code: ret.resultStatus, order: orderInfo.trade_no, result: ret.result ? JSON.parse(ret.result) : "" })
      }
    })
  })
}