<template>
	<view class="content">
		<image class="indexTop" src="/static/index_top.png" mode="scaleToFill"></image>
		<view class="indexTips">
			<view class="indexTips-name">欢迎使用盘债计算器</view>
			<view class="indexTips-text">请填写以下内容 <image class="indexTips-warm" @click="warmClick" src="/static/warm.png" mode="widthFix"></image></view>
		</view>
		<view class="indexCont">
			<view class="indexCont-white">
				<view class="indexUser" v-if="userLogin">
					<block v-if="inviteUser.user_id != ''">
						<view class="indexUser-left">
							<view class="indexUser-head"><image :src="inviteUser.avatar ? inviteUser.avatar : '/static/user_default.png' " mode="aspectFill"></image></view>
							<view class="indexUser-text">
								<view class="indexUser-name">
									{{inviteUser.realname ? inviteUser.realname : '--'}} <text class="indexUser-number">{{inviteUser.username ? inviteUser.username : '--'}}</text>
								</view>
								<view class="indexUser-tips">
									详细业务咨询请联系我
								</view>
							</view>
						</view>
						<image class="indexUser-tel" @click="callPhone" src="/static/tel.png" mode="aspectFill"></image>
					</block>
					<block v-else>
						<view class="indexUser-left">
							<view class="indexUser-head"><image :src="serviceData.avatar" mode="aspectFill"></image></view>
							<view class="indexUser-text">
								<view class="indexUser-name">
									{{serviceData.realname}} <text class="indexUser-number">{{serviceData.username}}</text>
								</view>
								<view class="indexUser-tips">
									详细业务咨询请联系客服
								</view>
							</view>
						</view>
						<image class="indexUser-tel" @click="callPhone" src="/static/tel.png" mode="aspectFill"></image>
					</block>
				</view>
				<view class="list">
					<block v-for="(item, index) in serviceArr" :key="index">
						<view class="listName">
							机构{{index + 1}}
							<view class="list-remove" @click="onRemove(index)" v-if="index > 0">
								<image src="/static/removeIcon.png" mode="widthFix"></image>删除
							</view>
						</view>
						<view class="listItem">
							<view class="listItem-name">
								{{item.titleName}}
							</view>
							<view class="listItem-picker">
								<block v-if="objectArray != ''">
									<picker :disabled="!canSettle" :range="objectArray" :value="item.pickerIndex" :range-key="'title'" class="listItem-picker-add" @change="($event) => item.pickerIndex = $event.detail.value">
										<view class="uni-input" v-if="objectArray[item.pickerIndex]">{{objectArray[item.pickerIndex].title}}</view>
									</picker>
								</block>
								<view class="listItem-picker-add" v-else>请先登录</view>
								<image src="/static/basic_down.png" class="listItem-picker-img" mode="widthFix"></image>
							</view>
						</view>
						<view class="listItem">
							<view class="listItem-name">
								{{item.priceName}}
							</view>
							<view class="listItem-picker">
								<input v-if="userLogin" type="number" :disabled="!canSettle" :value="item.priceValue" placeholder="请输入欠款金额" placeholder-class="placeholderClass" @input="($event) => item.priceValue = $event.detail.value" />
								<view class="listItem-picker-add" v-else>请先登录</view>
							</view>
						</view>
					</block>
				</view>
				<view class="listAdd">
					<view class="listAdd-btn" @click="addPush">
						<image src="/static/add.png" mode="widthFix"></image>添加新机构
					</view>
				</view>
			</view>
		</view>
		
		<view class="listSubmit">
			<view class="listSubmit-btn" :class="{active: !canSettle}" @click="onSubmit" v-if="userLogin">
				{{canRemark}}<block v-if="canSettle"><block v-if="numberCount.have_count > 0 && numberCount.have_count <= 5">(剩余次数{{numberCount.have_count}})</block></block>
			</view>
			<view class="listSubmit-btn" @click="onLogin" v-else>
				请先登录
			</view>
		</view>
		
		<!-- 漂浮窗 -->
		<view class="showy" v-if="userLogin">
			<navigator hover-class="none" url="/pages/index/custom" class="showy-item" v-if="isSalesman">
				<image class="showy-item-img" src="/static/record.png" mode="widthFix"></image>
				<view class="showy-item-name"><text>客户</text><text>管理</text></view>
			</navigator>
			<view class="showy-item" @click="isImgLay = true">
				<image class="showy-item-img" src="/static/share.png" mode="widthFix"></image>
				<view class="showy-item-name">分享</view>
				<view class="showy-item-number" v-if="numberCount.share_balance > 0">+{{numberCount.share_add}}次</view>
			</view>
			<view class="showy-item">
				<image class="showy-item-img" src="/static/record.png" mode="widthFix"></image>
				<view class="showy-item-name" @click="historyClick"><text>历史</text><text>记录</text></view>
				<view class="showy-item-number" v-if="numberCount.use_count > 0">{{numberCount.use_count}}条</view>
			</view>
		</view>
		
		<!-- 邀请好友弹出 -->
		<view class="postertBack" v-if="isImgLay"></view>
		<view class="postertCont postertContTop" v-if="isImgLay">
			<view class="poster-back">
				<image class="poster-img" src="/static/share_back.png" mode="widthFix"></image>
				<view class="poster-Cont">
				    <view class="poster-Cont-code">
						<image :src="canShare.qrcode" mode="widthFix"></image>
					</view>
				    <view class="poster-Cont-url">
						<view class="nowrap poster-url-number" @click="copyUrl(canShare.url)">地址：{{canShare.url}}/</view>
						<view class="poster-url-copy">复制</view>
				    </view>
					<view class="poster-Cont-btn">
						<view class="poster-Cont-down" @click="shareClick">保存图片，分享好友<image class="poster-Cont-down-img" src="/static/hand.png" mode="widthFix"></image></view>
					</view>
				</view>
				<image class="sign-img-btn" src="/static/close.png" @click="isImgLay = false" mode="widthFix"></image>
			</view>
		</view>
		
		<!-- 长按保存 -->
		<view class="postertBack" v-if="isImgLong"></view>
		<view class="postertCont" v-if="isImgLong">
			<view class="poster-back">
				<image v-if="isImgLong" class="poster-img" :src="posterImg" mode="widthFix"></image>
				<view class="poster-Cont-btn" style="margin-top: 40rpx; text-align: center;">
					<view class="poster-Cont-down">请长按图片，保存本地</view>
					<image class="poster-img-btn" src="/static/close.png" @click="isImgLong = false" mode="widthFix"></image>
				</view>
			</view>
		</view>
		
		<!-- 海报canvas -->
		<vue-canvas-poster :widthPixels="1000" :painting="paintings" @success="saveSuccess" @fail="saveFail"></vue-canvas-poster>
		
		<!-- 提交缓冲阶段 -->
		<view class="pack-center pages-hint grey" v-if="paySuccess">
		    <image src="/static/loadingGif.gif"></image>
		    <view>计算结果中...</view>
		</view>
		
		<!-- 是否确定提交计算 -->
		<view class="tipsBack" v-if="generalShow"></view>
		<view class="tipsCont" v-if="generalShow">
			<view class="tipsWhite">
				<image class="tipsCont-img" src="/static/tips.png" mode="widthFix"></image>
				<view class="tipsWhite-top">
					<view class="tipsWhite-name">
						计算提示
					</view>
					<view class="tipsWhite-text">
						请确认提交的数据是否准确？确定后进行提交
					</view>
				</view>
				<view class="tipsWhite-btn">
					<view class="tipsWhite-btn-go" @click="generalShow = false">
						暂不提交
					</view>
					<view class="tipsWhite-btn-go" @click="judgeGeneral">
						立即提交
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { VueCanvasPoster } from 'vue-canvas-poster'
	import { authFollow, wechatCode } from '@/apis/interfaces/authUrl'
	import { Init, Debt } from '@/apis/interfaces/index'
	import h5Copy from '@/js_sdk/junyi-h5-copy/junyi-h5-copy/junyi-h5-copy.js'
	export default {
		components: {
		    VueCanvasPoster
		},
		data() {
			return {
				userLogin  : false, // 是否登录
				canRemark  : '', // 按钮文字
				canSettle  : false, // 是否计算
				canShare   : '',  // 分享数据
				serviceData: '',  // 客服信息
				inviteUser : '',  // 用户信息
				serviceArr : [],
				blurValue  : '',
				numberCount: '',    // 计算次数
				haveCount  : '',    // 用户计算
				objectArray: [],    // 机构列表
				isSalesman : '',    // 客户管理
				paySuccess : false, // 计算结果
				generalShow: false,  // 是否提交
				
				// 海报
				isImgLay   : false, // 是否显示图片弹出层
				isImgLong  : false,// 长按保存图片
				paintings  : {
				    width: "375px",
				    height: "375px",
				    borderRadius: "10px",
				    background: "#ffffff",
				    views: [
				        //海报背景
				        {
				            type: "image",
				            url: "/static/share_downback.png",
				            css: {
				                top: "0",
				                left: "0",
				                width: "375px",
				                height: "375px",
				                mode: 'aspectFill'
				            },
				        },
				        
				        //二维码
				        {
				            type: "image",
				            url: "",
				            css: {
				                top: "60px",
				                left: "60px",
				                width: "255px",
				                height: "255px",
				                mode: 'aspectFill'
				            },
				        }
				    ],
				},
				posterImg	: "", //生成的海报图片路径
			}
		},
		
		onLoad(options) {
			// 邀请码数据
			if(options != '') {
				this.$store.commit('setInvite', options.invite)
				this.$store.commit('setShareUser', options.share_user)
				this.$store.commit('setLockId', options.lock_id)
			}
		},
		
		onShow() {
		    if(this.$store.getters.getToken) {
		        this.userLogin = true
				
				// 获取计算前置接口
				this.preUrl()
				return
		    }
			this.userLogin = false
		},
		
		created() {
			// 新增卡片
			this.serviceArr.push({
				titleName  : '机构名称',
				priceName  : '欠款金额',
				pickerIndex: 0,
				priceValue : '',
			})
		},
		
		methods: {
			// 计算前置接口
			preUrl() {
				Init().then(res => {
					this.serviceData = res.service
					this.isSalesman  = res.is_salesman
					this.inviteUser  = res.invite_user
					this.canShare    = res.share
					this.canSettle   = res.can_settle
					this.canRemark   = res.can_remark
					this.numberCount = res.count
					this.haveCount  = res.count.have_count - 1
					this.objectArray = res.institution
					this.paintings.views[1].url  = res.share.qrcode
				}).catch(err => {
					this.userLogin = false
					uni.showToast({
						title: err.message,
						icon: "none" 
					})
				})
			},
			
			// 新增表单数据
			addPush(){
				if(this.$store.getters.getToken) {
					// 是否可以计算
				    if(this.canSettle) {
						if(this.haveCount != 0) {
							this.serviceArr.push({
								titleName  : '机构名称',
								priceName  : '欠款金额',
								pickerIndex: 0,
								priceValue : '',
							})
							this.haveCount --
						} else {
							uni.showToast({
								title: '抱歉，没有次数啦',
								icon: "none" 
							})
						}
				    	return
				    }
				    
				    uni.showToast({
				    	title: this.canRemark,
				    	icon: "none" 
				    })
					return
				}
				uni.showToast({
					title: '请先登录',
					icon: "none" 
				})
			},
			
			// 移出选项
			onRemove(index){
				this.serviceArr.splice(index, 1)
				this.haveCount ++
			},
			
			// 提交订单数据-确认
			onSubmit(){
				// 已完善信息
				if(this.$store.getters.getPassInfo != 'false') {
					if(this.canSettle) {
						this.generalShow = true
						return
					}
					uni.showToast({
						title: this.canRemark,
						icon: "none" 
					})
					return
				}
				
				// 完善个人信息
				uni.navigateTo({
				    url: '/pages/index/perfect'
				})
				
			},
			
			// 提交订单数据
			judgeGeneral() {
				this.generalShow = false
				
				let SubmitAdd = this.serviceArr.map(obj => {
					return {
						institution_id	: this.objectArray[obj.pickerIndex].id,
						total			: obj.priceValue,
					}
				})
				Debt({
					invite	 : this.$store.getters.getInvite,
					lock_id	 : this.$store.getters.getLockId,
					data     : SubmitAdd
				}).then(res => {
					this.paySuccess = true
					
					this.serviceArr.splice(0,this.serviceArr.length);//清空数组
					this.serviceArr.push({
						titleName  : '机构名称',
						priceName  : '欠款金额',
						pickerIndex: 0,
						priceValue : '',
					})
					
					
					// 跳转结果页面
					setTimeout(()=>{
						this.paySuccess = false
						uni.navigateTo({
							url: '/pages/result/result'
						})
					},3000)
					
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none" 
					})
				})
			},
			
			// 微信授权登录
			onLogin() {
				authFollow({
					url: 'http://debtwww.douhuofalv.com/pages/webview/webview',
					scopes: ''
				}).then(res => {
					window.location.href = res
				}).catch(err => {
					uni.showToast({
						title: err.message,
						icon: "none" 
					})
				})
			},
			
			// 查看历史记录
			historyClick() {
				uni.navigateTo({
				    url: '/pages/result/history'
				})
			},
			
			// 拨打电话
			callPhone(e) {
				uni.makePhoneCall({
					phoneNumber: this.inviteUser.username
				})
			},
			
			// 查看温馨提示
			warmClick() {
				uni.showModal({
					title: '温馨提示',
					content: '新用户可免费计算2个机构，每邀请1名用户完成计算可增加1个机构计算份额。用户计算机构份额上限为5个。',
					showCancel: false,
					success: res=> {
						if (res.confirm) {
							console.log('用户点击确定');
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			
			// 复制订单号
			copyUrl(val) {
			    const result = h5Copy(val)
				if (result === false) {
					uni.showToast({
						title:'不支持',
					})
					return
				}
				uni.showToast({
				   title:'复制成功',
				   icon:'none'
				})
			},
			
			// 下载海报
			shareClick (){
				this.isImgLay = false
				this.isImgLong = true
				
			},
			saveSuccess(src) {
			    this.posterImg = src;
			},
			saveFail(err) {},
		}
	}
</script>

<style>
	.indexTop {
		width: 100%;
		height: 440rpx;
	}
	
	.indexTips {
		position: absolute;
		top: 0;
		left: 0;
		padding: 60rpx 40rpx 0;
		box-sizing: border-box;
		color: #fff;
	}
	
	.indexTips-name {
		font-weight: 600;
		font-size: 44rpx;
	}
	
	.indexTips-text {
		margin-top: 20rpx;
		font-size: 28rpx;
		display: flex;
	}
	
	.indexTips-warm {
		width: 34rpx;
		margin-left: 15rpx;
		margin-top: 2rpx;
		
	}
	.indexCont {
		width: 100%;
		padding: 30rpx;
		box-sizing: border-box;
		position: absolute;
		margin-top: 200rpx;
		top: 0;
		left: 0;
		border-bottom: 140rpx solid transparent;
	}
	
	.indexCont-white {
		height: 100%;
		background: white;
		border-radius: 20rpx;
	}
	
	.indexUser {
		display: flex;
		border-bottom: 4rpx solid #f7faff;
		padding: 40rpx 30rpx;
		box-sizing: border-box;
	}
	
	.indexUser-left {
		display: flex;
		flex: 1;
	}
	
	.indexUser-head {
		width: 110rpx;
		height: 110rpx;
		border-radius: 100%;
		background-color: #e5ecf6;
		border: 6rpx solid #e5ecf6;
		box-sizing: border-box;
	}
	
	.indexUser-head image {
		border-radius: 100%;
		width: 100%;
		height: 100%;
	}
	
	.indexUser-text {
		width: calc(100% - 150rpx);
		padding: 0 20rpx;
		box-sizing: border-box;
	}
	
	.indexUser-name {
		font-weight: 600;
		display: flex;
		font-size: 34rpx;
		line-height: 58rpx;
	}
	
	.indexUser-tips {
		font-size: 28rpx;
	}
	
	.indexUser-number {
		font-size: 30rpx;
		font-weight: normal;
		padding-left: 20rpx;
		box-sizing: border-box;
		line-height: 62rpx;
		color: #666666;
		display: inline-block;
	}
	
	.indexUser-tel {
		width: 60rpx;
		height: 60rpx;
		margin-top: 30rpx;
	}
	
	.list {
		padding: 20rpx 30rpx;
		box-sizing: border-box;
	}
	
	.listName {
		color: #0c58d2;
		font-weight: 600;
		font-size: 34rpx;
		margin: 30rpx 0 ;
		position: relative;
	}
	
	
	.list-remove {
		background-color: #FBE7EE;
		display: inline-block;
		position: absolute;
		padding: 0 20rpx;
		font-size: 26rpx;
		font-weight: normal;
		line-height: 44rpx;
		color: #d4155a;
		border-radius: 50rpx;
		top: 0;
		right: 0;
	}
	
	.list-remove image {
		width: 24rpx;
		height: 24rpx;
		margin-right: 10rpx;
		vertical-align: -2rpx;
	}
	
	.listItem-name {
		margin: 30rpx 0;
	}
	
	.listItem-picker {
		background-color: #f7faff;
		border-radius: 15rpx;
		height: 90rpx;
		line-height: 90rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: relative;
	}
	
	.listItem-picker input {
		display: block;
		height: 100%;
		background-color: transparent;
	}
	
	.listItem-picker-img {
		width: 30rpx;
		position: absolute;
		right: 30rpx;
		top: 30rpx;
	}
	
	.listAdd {
		text-align: center;
		color: #0c58d2;
		padding: 40rpx 0 40rpx;
	}
	
	.listAdd-btn image {
		width: 30rpx;
		vertical-align: middle;
		margin-right: 10rpx;
	}
	
	.listSubmit {
		position: fixed;
		width: 100%;
		bottom: 0;
		left: 0;
		background-color: #f7faff;
		text-align: center;
		height: 120rpx;
	}
	
	.listSubmit-btn {
		width: 90%;
		display: inline-block;
		background-color: #0352c8;
		text-align: center;
		line-height: 90rpx;
		border-radius: 100rpx;
		color: #fff;
	}
	
	.listSubmit-btn.active {
		background-color: #dcdcdc;
		color: #666;
	}
	
	/* 漂浮窗 */
	.showy {
		position: fixed;
		right: 0;
		bottom: 15%;
		background-color: #fff;
		box-shadow: 0 0 20rpx rgba(3, 96, 217, .1);
		z-index: 999;
		border-radius: 60rpx 0 0 60rpx;
		padding: 30rpx 0;
	}
	
	.showy-item {
		text-align: center;
		padding: 20rpx 30rpx 10rpx 35rpx;
		font-size: 26rpx;
		position: relative;
	}
	
	.showy-item-number {
		position: absolute;
		right: 5rpx;
		top: 0;
		background-color: #ff7171;
		color: #fff;
		font-size: 22rpx;
		border-radius: 80rpx;
		padding: 0 10rpx;
		transform: scale(0.83);
		line-height: 40rpx;
	}
	
	.showy-item-img {
		width: 44rpx;
	}
	
	.showy-item-name text {
		display: block;
	}
	
	.postertBack {
	    width: 100vw;
	    height: 100vh;
	    position: fixed;
	    background-color: rgba(0,0,0,.6);
	    left: 0;
	    top: 0;
	    z-index: 999;
	}
	
	.postertCont {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-box-pack: center;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		z-index: 1000;
		padding: 0 10%;
		box-sizing: border-box;
	}
	
	.postertContTop {
		top: -200rpx;
	}
	
	.poster-back {
		position: relative;
		padding-top: 100%;
		width: 100%;
	}
	
	.poster-img, .poster-Cont {
		position: absolute;
		width: 100%;
		height: 100%;
		left: 0;
		top: 0;
	}
	
	.poster-Cont {
		padding: 50rpx;
		box-sizing: border-box;
		color: #fff;
	}
	
	.poster-Cont-code {
		background: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		box-sizing: border-box;
	}
	
	.poster-Cont-code image {
		width: 100%;
	}
	
	.poster-Cont-url {
		display: flex;
		padding: 30rpx 0 40rpx;
	}
	
	.poster-url-number {
		flex: 1;
		padding-right: 20rpx;
		box-sizing: border-box;
	}
	
	.poster-url-copy {
		font-weight: 600;
	}
	
	.poster-Cont-btn {
		text-align: center;
	}
	
	.poster-Cont-down {
		display: inline-block;
		width: 80%;
		background-color: #fff;
		border-radius: 80rpx;
		line-height: 90rpx;
		color: #051437;
		font-weight: 600;
	}
	
	.poster-Cont-down-img {
		width: 40rpx;
		margin-left: 15rpx;
		vertical-align: -8rpx;
		animation: bounce-down 2s linear infinite;
	}
	
	
	@keyframes bounce-down {
	    25% {
	        transform: translateX(-4px);
	    }
	
	    50%,
	    100% {
	        transform: translateX(0);
	    }
	
	    75% {
	        transform: translateX(4px);
	    }
	}
	
	.sign-img-btn {
		position: absolute;
		left: calc(50% - 35rpx);
		width: 70rpx;
		top: calc(100% + 240rpx);
		z-index: 999;
	}
	
	.poster-img-btn {
		width: 70rpx;
		display: block;
		margin: 30rpx auto 0;
	}
	
	
	.grey {
	    background-color: #f9f9f9;
	    z-index: 999999;
	}
	
	/* 是否确认提交 */
	.tipsBack {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0;
		top: 0;
		z-index: 9999;
		background-color: rgba(0, 0, 0, .8);
	}
	.tipsCont {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-box-pack: center;
		position: fixed;
		left: 0;
		right: 0;
		top: 0;
		bottom: 0;
		z-index: 10000;
		padding: 0 10%;
		box-sizing: border-box;
	}
	
	.tipsWhite {
		background-color: #ffffff;
		border-radius: 20rpx;
		overflow: hidden;
	}
	
	.tipsWhite-top {
		padding: $padding;
		box-sizing: border-box;
		text-align: center;
	}
	
	.tipsCont-img {
		width: 100%;
	}
	
	.tipsWhite-name {
		text-align: center;
		color: #111111;
		font-size: 34rpx;
		font-weight: 600;
		margin-bottom: 15rpx;
	}
	
	.tipsWhite-text {
		font-size: 30rpx;
		color: #666666;
		line-height: 48rpx;
		padding: 0 50rpx;
		box-sizing: border-box;
	}
	
	.tipsWhite-btn {
		display: flex;
		padding: 30rpx 10rpx;
		box-sizing: border-box;
	}
	
	.tipsWhite-btn-go {
		flex: 2;
		color: #fff;
		margin: 0 15rpx;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border: 2rpx solid #F6F6F6;
		background-color: #007df5;
		border-radius: 20rpx;
	}
	
	.tipsWhite-btn-go:first-child {
		color: #333333;
		background-color: #ffffff;
		border: 2rpx solid #cccccc;
	}
</style>
