<template>
	<!-- 自主签约 -->
	<view class="box wx-open">
		<view class="first-title">
			<text>电子合同签约</text>
			<view class="submit-btn" @tap="getSignInfo(true)">刷新</view>
		</view>
		<view class="tips">如需使用支付功能，请完成以下签约和授权</view>
		
		<view class="state">
			<text>签约状态</text>
			<view>{{ vdata.bizSignInfo.state || '异常[' + vdata.bizSignInfo.errInfo + ']' }}</view>
		</view>
		
		<template v-if="vdata.bizSignInfo.signUrl">
			<view class="second-title">电子合同签约地址和二维码：</view>
			<view style="margin-top: 20rpx;white-space: pre-wrap; word-wrap: break-word; color: #3981ff" @click="toContract()">
				{{ vdata.bizSignInfo.signUrl }}
			</view>
			<view style="display: flex;justify-content: center;margin-bottom: 20rpx">	
				<image :src="drawQRcode(vdata.bizSignInfo.signUrl)" class="wx-img" />
			</view>
		</template>
		
		<view class="state" style="margin-top: 40rpx;">
			<text style="white-space: nowrap; margin-right: 10rpx">商户确认状态</text>
			<view>{{ vdata.wxOpenInfo.state || '异常[' + vdata.wxOpenInfo.errInfo + ']' }}</view>
		</view>
		
		<template v-if="vdata.wxOpenInfo.signUrl">
			<view class="second-title">
				<text>微信开户意愿确认：</text>
				<view class="submit-btn" @tap="getWxInfo(true)">刷新</view>
			</view>
			<view style="color:#666666;font-size: 25rpx;margin-top: 20rpx">商户联系人使用已绑定银行卡的微信扫下面的二维码:</view>
	
			<view style="display: flex;justify-content: center;">
				<image v-if="vdata.wxOpenInfo.imgType == 'qrContent'" :src="drawQRcode(vdata.wxOpenInfo.signUrl)" class="wx-img" />
				
				<image v-else :src="'data:image/jpg;base64,' + vdata.wxOpenInfo.signUrl" mode="" class="wx-img" />
			</view>	
			<view style="color:#666666;font-size: 25rpx;margin-top: 20rpx">(温馨提示:自助认证不限制谁来操作认证,但建议是商户联系人进行认证,以免后期需要扫码找不到微信认证管理员)</view>
		</template>
		
	</view>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import JeePayForm from '@/components/applyComponents/JeePayForm.vue' // 通用左右结构布局
import JeepayUpLoad from '@/components/JeepayUpLoad/JeepayUpLoad.vue' // 图片上传
import qrcode from '@/util/qrcode.js'
import { $getMchApplymentChannelSignInfo, $getMchApplymentWxOpenInfo } from '@/http/apiManager.js'

const channelInfo = inject('channelInfo')

onMounted(() => {
	if (['2', '5'].includes(channelInfo.state)) {
		getWxInfo()
		getSignInfo()
	}
})

const vdata = reactive({
	bizSignInfo: {},  // 电子合同状态查询
	wxOpenInfo: {}, // 微信开户意愿信息
	isRefresh: false, // 是否点击了刷新按钮
})

// 电子合同状态查询
const getSignInfo = (flag = false) => {
	vdata.isRefresh = flag
	$getMchApplymentChannelSignInfo(channelInfo.applyId).then(({bizData}) => {
		vdata.bizSignInfo = bizData
		if (vdata.isRefresh) {
			uni.showToast({title: '已刷新', icon: 'none'})
			vdata.isRefresh = false
		}
	}).catch(err => vdata.isRefresh = false)
}

// 微信开户意愿查询
const getWxInfo = (flag = false) => {
	vdata.isRefresh = flag
	$getMchApplymentWxOpenInfo(channelInfo.applyId).then(({bizData}) => {
		vdata.wxOpenInfo = bizData
		if (vdata.isRefresh) {
			uni.showToast({title: '已刷新', icon: 'none'})
			vdata.isRefresh = false
		}
	}).catch(err => vdata.isRefresh = false)
}

// 跳转至电子合同页  盛付通 富友 会用到
const toContract = () => {
	uni.navigateTo({ url: '../../components/contract?url=' + vdata.bizSignInfo.signUrl })
}

// 制作图片
const drawQRcode = url => {
  return qrcode.drawImg(url, {
	  typeNumber: 4, // 密度
	  errorCorrectLevel: 'Q', // 纠错等级
	  size: 175, // 白色边框
  })
}

</script>

<style scoped lang="less">
.flex {
	display: flex;
	justify-content: space-between;
}
.box {
	height: auto;
	transition: .3s;
	position: relative;
	border-radius: 20rpx;
	background: #f5f6fc;
	padding: 10rpx 40rpx;
	box-sizing: border-box;
}

// 微信签约
.wx-open {
	margin-top: 30rpx;
	padding: 40rpx;
	.first-title {
		font-size: 28rpx;
		color: #18191d;
		font-weight: bold;
		display: flex;
		justify-content: space-between;
	}
	.tips {
		font-size: 25rpx;
		color: #7e7e80;
		margin: 20rpx 0 40rpx;
	}
	.state {
		margin-bottom: 30rpx;
		display: flex;
		justify-content: space-between;
	}
	.second-title {
		font-size: 27rpx;
		color: #000;
		display: flex;
		justify-content: space-between;
	}
	.wx-img {
		width: 300rpx;
		height: 300rpx;
		margin: 30rpx 0 20rpx ;
	}
}
.submit-btn {
	border-radius: 0.3125rem;
	background: #3981FF;
	color: #fff;
	padding: 0.15625rem 0.46875rem;
}
</style>