import store from '@/store'
import { isJSON } from '@/utils'
// import { getPayResponse } from '@/utils/posUtils'
import { payValueCard, payPOS, payBScanC } from '@/api/pay'
import choosePay, { getPosPayChannel } from '@/utils/choosePay'

const log = require('electron-log')

// 获取支付渠道类型
export function getPayType (payChannel) {
  const payChannelList = store.state.app.payChannelList
  const target = payChannelList.find(i => i.name === payChannel)

  return (target && target.payType) || ''
}

export default (params, res, vue, callback) => {
  const logData = JSON.parse(JSON.stringify(params))

  try {
    if (logData?.member?.face?.face_img) {
      logData.member.face.face_img = '图片'
      logData.member.facePic = '图片'
    }

    if (logData?.member?.childList) {
      logData.member.childList.forEach(item => {
        if (item?.face?.face_img) {
          item.face.face_img = '图片'
        }
      })
    }
  } catch (e) {
    console.log(e)
  }

  log.info('订单参数', JSON.stringify(logData))

  const payType = getPayType(params.payChannel)
  log.warn(`发起 ${payType} - ${params.payChannel} 支付`)
  switch (getPayType(params.payChannel)) {
    case 'POS':
      choosePay('pay', params, vue).then(data => {
        if (data) {
          log.info('支付成功', data)
          vue.$message.success('支付成功')
          params.bocTradeNo = data

          try {
            if (isJSON(data)) {
              params.bocPosId = JSON.parse(data).terminal || JSON.parse(data).terminalID
              // 设置本地pos终端号
            } else {
            }
            // 更新终端号
            if (params.bocPosId) {
              store.commit('SET_TERMAINAL_NO', params.bocPosId)
            }
          } catch (e) {
            log.error(JSON.stringify(e))
          }
          vue.payStatus = 1
          callback && callback(params)
          const posParams = {
            id: res?.id,
            payChannel: getPosPayChannel(params.bocTradeNo),
            posId: params.bocPosId,
            tradeInfo: params.bocTradeNo,
            type: params.memberTrade ? 'MEMBER' : 'TICKET'
          }

          log.info('PayPOS参数', posParams)
          payPOS(posParams).then(res => {
            log.info('接口提交成功', res)
          }).catch(e => {
            log.error('接口提交失败', e)
          })
        } else {
          vue.payStatus = 2
          vue.submitDisable = false
        }
      }).catch(e => {
        vue.payStatus = 2
        vue.submitDisable = false
      })
      break
    case 'B_SCAN_C_PAY':
      payBScanC({
        qrcode: params.qrCode,
        id: res?.id,
        type: 'ORDER'
      }).then(res => {})
      break
    case 'VALUECARD':
      // vue.$refs.readValueCard.show()
      payValueCard({
        cardNo: params.qrCode,
        authcode: '',
        discount: params.discount,
        password: params.password,
        memberSystemId: params.memberSystemId,
        // cardNo: '001828986095',
        id: res?.data,
        type: params.memberTrade ? 'MEMBER' : 'TICKET'
      }).then(res => {}
      ).catch((e) => {
        vue.submitDisable = false
        window.clearTimeout(vue.timer)
      })
      break
    default:
      callback && callback(params)
  }
}
