<template>
	<view class="container">
		<rich-text class="rich_text" :nodes="htmlContent"></rich-text>
		<view class="bottom_view flex-row flex-between">
			<view v-if="isAgree&&isBottom" @tap="clickAgreeContract" class="click_view">
				{{ $t('本人已知晓上述内容') }}
			</view>
			<view v-if="!isAgree&&isBottom" class="click_view_gray">
				{{ $t('阅读合同倒计时') }}{{countdownTime}}{{ $t('秒') }}
			</view>
	
			<view v-if="orderSign == 1" @tap="clickWriteSign" class="write_sign" style="background-color: #ffffff;">
				<image class="write_sign_img" src="https://qiniu.bms16.com/appsrc%2Fhire%2Fwrite_sign.png" />
				<view class="sign_view">{{orderSignUrl == ''?$t('签名'):$t('重新签名')}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	const config = require('../../common/config.js');
	var common = require('../../common/common.js');
	var http = require('../../common/http.js');

	export default {
		data() {
			return {
				contarct_url: '',
				htmlContent: '',
				timerInterval: null,
				countdownTime: 3,
				isAgree: false,
				isBottom: true,
				orderSign: 0,
				orderSignUrl: ""
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			const contract_id = options.contract_id
			if (contract_id == 102 || contract_id == 270) {
				this.setData({
					isBottom: false
				})
			} else{
				this.loadUserInfo()
			}
			this.loadHtmlContent(contract_id)
			const me = this
			this.timerInterval = setInterval(function () {
				me.countdownTimefun()
			}, 1000)
		},
		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			const app = getApp();
			this.setData({
				orderSign:app.globalData.orderSign,
			    orderSignUrl:app.globalData.orderSignUrl
			})
		},
		methods: {
			clearTimer: function () {
				if (this.timerInterval == null) return
				clearInterval(this.timerInterval)
				this.timerInterval = null
			},
			
			loadUserInfo() {
				const me = this
				const accountInfo = wx.getAccountInfoSync() // 上报小程序账号信息
				http.postApi(config.API_USER_INFO, { appid: accountInfo.miniProgram.appId }, function (resp) {
					if (resp.data.code === 200) {
						const app = getApp();
						app.globalData.orderSign = resp.data.data.userInfo.order_sign
						app.globalData.orderSignUrl = resp.data.data.userInfo.order_sign_url
						me.setData({
							orderSign:app.globalData.orderSign,
							orderSignUrl:app.globalData.orderSignUrl
						})
					}
				})
			},
			
			countdownTimefun() {
				var countdownTime = this.countdownTime
				countdownTime--;
				if (countdownTime <= 0) {
					this.setData({
						isAgree: true
					})
					this.clearTimer();
				}
				this.setData({
					countdownTime: countdownTime
				})
			},
			
			loadHtmlContent(contract_id) {
				const me = this
				http.getApi(config.API_CONTRACT_CONTENT + "&contract_id=" + contract_id, {}, function (resp) {
					if (resp.data.code === 200) {
						me.setData({
							htmlContent: resp.data.data.data
						})
					}
				})
			},
			
			clickAgreeContract() {
				// if (this.orderSign == 1 && this.orderSignUrl == '') {
				// 	uni.showModal({
				// 		title: this.$t('提示'),
				// 		content: this.$t('您当前没有手写签名，是否要进行填写'),
				// 		showCancel: true,
				// 		success: function(res) {
				// 			if (res.confirm) {
				// 				uni.navigateTo({
				// 					url: '/pages/my/sign/sign'
				// 				});
				// 			}
				// 		}
				// 	});
				// 	return;
				// }
				
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2];
				// prevPage.setData({
				//     isAgree: true
				// });
				prevPage.isAgree = true
				uni.navigateBack({
					delta: 1
				});
			}
		}
	};
</script>

<style>
	@import './contract.css';
</style>