<!-- 支付组件 -->
<template>
    <div>
        <pay-module :param_for_pay="param_for_pay" :reportLog="reportLearningLog" :payRelatedInfo="payRelatedInfo" />
        <pay_success
            v-if="isZeroPaySuccess"
            :store_code="store_code"
            :store_name="store_name"
            :is_show_code="isShowCode"
            :pay_gift_list="payGiftList"
            :right_type="rightType"
        >
        </pay_success>
    </div>
</template>

<script>
import jsTools from '@xiaoe/js-tools'
import pay_success from './pay_success.vue'

import { reportRecords } from '~publicAsset/libs/studyRecordReport.js'
// 通用支付组件
import { payModule } from '@xiaoe/xiaoe-common-pay'
import '@xiaoe/xiaoe-common-pay/lib/index/style.css'
export default {
    props: {
        param_for_pay: {
            default: () => {
                return {}
            },
        },
        payRelatedInfo: {
            default: () => {
                return {}
            },
        },
    },
    components: {
        pay_success,
        payModule,
    },
    computed: {
        /**
             * 在没有输入购买后的跳转链接前提下
             * 引导加群没有关注过，并且二维码地址不为空则显示购买成功弹框
                或者支付有礼有权益数据返回，则显示购买成功弹框
            */
        isShowPaySuccess() {
            if (this.isShowCode || (this.payGiftList && this.payGiftList.length > 0)) {
                return true
            } else {
                return false
            }
        },
    },
    data() {
        return {
            isZeroPaySuccess: false,

            store_code: '',
            store_name: '',
            subscribe: true, //是否已关注公众号
            isShowCode: false, //是否显示二维码
            rightType: '', //支付有礼的权益类型
            payGiftList: [], //支付有礼列表数据
        }
    },
    created() {
        console.log('支付独立模块接入')
    },
    watch: {
        param_for_pay: {
            handler(newval) {
                console.log('param_for_pay接入', newval)
                if (!(newval.hasOwnProperty('zero_call_back') && typeof newval.zero_call_back === 'function')) {
                    this.param_for_pay.zero_call_back = this.zero_pay_back
                }
            },
            deep: true,
        },
    },
    methods: {
        // 0元订单回调事件
        zero_pay_back(order_id) {
            let that = this
            that.attentionInfo(order_id, function() {
                //如果业务侧有传入支付后的跳转url,那么指向支付后的跳转
                if (that.param_for_pay.jump_data.back_url) {
                    let jump_url = that.param_for_pay.jump_data.back_url
                    if (jump_url.indexOf('?') === -1) {
                        jump_url = jump_url + `?paySuccess=1&order_id=${order_id}`
                    } else {
                        jump_url = jump_url + `&paySuccess=1&order_id=${order_id}`
                    }
                    eJump(`${jump_url}&isFirstDistributor=1`, true)
                } else if (that.isShowPaySuccess) {
                    //显示购买成功页弹框

                    that.isZeroPaySuccess = true
                } else {
                    let urlFlag = location.href
                    let date = new Date()
                    if (urlFlag.indexOf('?') > -1) {
                        urlFlag = urlFlag + `&paySuccess=1&${date.getTime()}`
                    } else {
                        urlFlag = urlFlag + `?paySuccess=1&${date.getTime()}`
                    }
                    eJump(`${urlFlag}&isFirstDistributor=1`, true)
                }
            })
        },
        // 公众号信息
        attentionInfo(order_id, callback) {
            let that = this
            let params_id = that.param_for_pay.resource_id
            let url = 'getSubscribedQr'
            if (that.param_for_pay.resource_type == 4) {
                params_id = params_id.split('/').pop()
            }
            if (this.reqType === 'alive') {
                url = '_alive/getSubscribedQr'
            }
            let params = {
                app_id: window.APPID,
                type: 0, //支付和邀请码页面传0  顶部传1
                resourceType: that.param_for_pay.resource_type,
                resourceId: params_id,
            }
            jsTools.request({ url, params }).then(data => {
                if (data && data.code == 0 && data.data) {
                    let subscribeConfig = data.data
                    that.store_code = subscribeConfig.wx_qr_url
                    that.store_name = subscribeConfig.wx_app_name
                    that.subscribe = subscribeConfig.subscribe
                    if (subscribeConfig.subscribe) {
                        that.isShowCode = false //关注过不弹窗
                    } else if (subscribeConfig.wx_qr_url) {
                        //没关注过且有二维码就弹窗
                        that.isShowCode = true
                    } else {
                        that.isShowCode = false
                    }
                    // 公共包是ajax请求，先嵌套回调
                    that.getPayGiftInfo(order_id, callback)
                } else {
                    // 公共包是ajax请求，先嵌套回调
                    that.getPayGiftInfo(order_id, callback)
                }
            })
        },
        /**
         * 1. 目前支付都跳空白页进行支付，因此支持学习中心的记录支持的时候，需要在购买成功后上报，学习进度
         * 2. 学习记录上报单品和专栏，且单品关联上级仅到专栏一级，专栏上报改目录展示下的一个资源
         * 3. 接口上报同学习记录上报接口。只是所有学习进度均为0
         * 4. 专栏目录为空不上报
         * */
        reportLearningLog() {
            let params_report = {
                resource_id: '',
                resource_type: '',
                learn_progress: 0,
                org_learn_progress: 0,
                product_id: '',
                // stay_time: reportData.stayTime
            }

            let { resource_type, product_id, resource_id } = this.param_for_pay

            // 单品的处理方式(单品类型) 专栏在其他项目
            if (['1', '2', '3', '4', '20'].indexOf(resource_type.toString()) > -1) {
                params_report['resource_type'] = resource_type
                params_report['resource_id'] = resource_id
                params_report['product_id'] = product_id || ''
                // 单品发送学习记录请求
                reportRecords(params_report, true)
            }
        },
        /**
         * 获取支付有礼数据，控制购买成功页的显示
         */
        getPayGiftInfo(order_id, callback) {
            try {
                let that = this
                let params = {
                    permit_id: order_id,
                }
                jsTools.request({ url: 'pay_gift/get_receive_detail', parmas }).then(data => {
                    if (data && data.code == 0 && data.data) {
                        let res = data.data
                        that.rightType = res.right_type || ''
                        that.payGiftList = res.list || []
                        if (callback && typeof callback === 'function') {
                            callback()
                        }
                    } else {
                        if (callback && typeof callback === 'function') {
                            callback()
                        }
                        console.log(data.msg)
                    }
                })
            } catch (error) {
                console.log(error)
            }
        },
    },
}
</script>

<style scoped></style>
