<template>
	<view>
		<!--mine/page/page.wxml-->
		<view class="title" v-if="title&&!isHetong">{{ title }}</view>
		<view class="" style="padding: 30rpx 30rpx 50rpx;" v-if="isHetong">
			<rich-text :nodes="params"></rich-text>
			<image style="width: 500rpx;height: 300rpx;" mode="aspectFit" :src="signUrl"></image>
		</view>
		<view class="content" v-else>
			<rich-text :nodes="params"></rich-text>
		</view>
	</view>
</template>

<script>
	// mine/page/page.js
	const app = getApp();
	const req = require('../../utils/request.js');
	const util = require('../../utils/util')

	export default {
		data() {
			return {
				title: '',
				isXieyi: false, //用户协议
				isYinsi: false, //隐私政策
				isHetong: false, //合同
				params: '',
				hetongId: null,
				signUrl:null
			};
		},

		components: {},
		props: {},
		onLoad: function(options) {
			this.id = options.id;
			if (options.isXieyi) {
				this.isXieyi = true;
			}
			if (options.isYinsi) {
				this.isYinsi = true;
			}
			if (options.isHetong) {
				this.isHetong = true;
			}

			this.title = options.title;
			if (options.hetongId) this.hetongId = options.hetongId
			// if (this.title) {
			// 	uni.setNavigationBarTitle({
			// 		title: this.title
			// 	});
			// }
			this.getData();
		},
		methods: {
			getData() {
				var that = this;
				let url = false;
				let form = {};
				if (that.isXieyi) {
					req.getRequest('/api/other/config', {}, function(res) {
						that.params = res.SERVICE_AGREEMENT;
					});
				} else if (that.isYinsi) {
					req.getRequest('/api/other/config', {}, function(res) {
						that.params = res.PRIVACY_AGREEMENT;
					});
				} else if (that.isHetong) {
					req.getRequest('/admin/v2/contract/' + this.hetongId, {}, (data)=> {
						data.content = data.content.replace(/{partyb}/, data.partyb);
						data.content = data.content.replace(/{card_no}/, data.partybCardNo);
						data.content = data.content.replace(/{partyb_mobile}/, data.partybPhone);
						if(data.leaseStart){
							data.leaseStart = data.leaseStart.split('-');
						}
						data.content = data.content.replace(/{hire_begin}/, data.leaseStart ? data.leaseStart[0] + '年' + data
							.leaseStart[1] + '月' + data.leaseStart[2] : '____');
						if (data.leaseEnd) {
							data.leaseEnd = data.leaseEnd.split('-');
						}
						let time = '';
						data.content = data.content.replace(/{hire_end}/, data.leaseEnd ? data.leaseEnd[0] + '年' +
							data.leaseEnd[1] + '月' + data.leaseEnd[2] : '____');
						if (data.leaseTime) {
							data.leaseTime = this.getTime(data.leaseTime);
							if (data.leaseTime[0] == 0 && data.leaseTime[1] == 0) time = '01分钟';
							if (data.leaseTime[0] > 0) {
								time += data.leaseTime[0] + '小时';
							}
							if (data.leaseTime[1] >= 0) {
								time += data.leaseTime[1] + '分钟';
							}
						}
						data.content = data.content.replace(/{hire_duration}/, time ? time : '____');
						data.content = data.content.replace(/{battery_model}/, data.batteryModel);
						data.content = data.content.replace(/{battery_num}/, data.batteryNum);
						data.content = data.content.replace(/{deposit}/, data.deposit);
						data.content = data.content.replace(/{hire_price}/, data.leaseMoney);
						data.content = data.content.replace(/{order_sign}/, '');
						that.params = data.content;
						that.signUrl = data.signUrl;
					});
				}
			},
			getTime(time) {
				return util.countDown(time);
			},
		}
	};
</script>
<style>
	@import './page.css';
</style>
