import { Toast } from 'vant'
import { createPayOrder, queryOrderState } from '../api/wx-jsapi'
import router from '../router'
import store from '../store'

export function wxPay(param) {
  createPayOrder(param).then((res) => {
    if (res.yjReqNo) {
      store.commit('SET_YJREQNO', null)
    }
    WeixinJSBridge.invoke(
      'getBrandWCPayRequest',
      {
        appId: res.appId,
        timeStamp: res.timeStamp,
        nonceStr: res.serialNo,
        package: 'prepay_id=' + res.prepayId,
        signType: res.signType,
        paySign: res.paySign,
        totalFee: res.totalFee,
      },
      function (res2) {
        console.log(res2)
        queryOrderState(res.tradeNo).then((res3) => {
          Toast.success(res3)
          router.push('/hospitalService')
        })
      }
    )
  })
}
