<template>
	<view v-if="detaile">
		<view class="user dflex">
			<image src="../../static/images/userimg.png" mode="aspectFill" class="userimg"></image>
			<view class="flex">
				<view class="username">欧衡</view>
				<view class="time">{{getDateTimeStamp(detaile.createDate)}}</view>
			</view>
		</view>
		<view class="title" v-if="detaile.title">{{ detaile.title }}</view>
		<block v-if="rootCode == 'hair_circle' || rootCode == 'marketing_script'" >
		<view class="brief">
			<rich-text :nodes="detaile.brief"></rich-text>
		</view>
		<view class="imabox dflex"v-if="detaile.params.imageList&&detaile.params.imageList.length>0">
			<image :src="item.url" v-for="(item,index) in detaile.params.imageList" :key="index" class="img" @click="previewImg(index)"></image>
		</view>
		</block>
		<view class="content" v-if="detaile.text">
			<view :class="'viewMore' + (viewMoress?' con-vheight':'')" :style="'max-height:' + height">
				<mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
			</view>
			<view class="view-more" @tap="viewMores()" v-if="viewMoress">查看全文</view>
			<!-- <view :class="'viewMore' + (viewMore?'':' con-vheight')">
				<mp-html :content="detaile.text" :lazy-load="true" @imgtap="choose"></mp-html>
			</view>
			<view class="view-more" @tap="viewMores()" v-if="!viewMore">查看全文</view> -->
		</view>
		<view class="operate ddflex">
			<view class="flex dflex">
				<view class="zan ddflex"><image src="../static/images/view.png"></image>{{detaile.browseCount}}</view>
				<view class="zan ddflex" @click="thumbs()"><image :src="'../../static/images/' + (detaile.isThumbs ? 'like1_h.png' : 'like1.png')"></image>{{detaile.thumbsCount}}</view>
			</view>
			<button class="share ddflex" hover-class="none" open-type="share"><image src="../static/images/share.png"></image>分享</button>
		</view>
		<view class="box" v-if="contentList && contentList.length > 0">
			<view class="tit ddflex">为您推荐</view>
			<view class="rec">
				<navigator :url="'/other/content/index?code=' + item.code + '&rootCode=' + rootCode" hover-class="none" class="li ddflex" v-for="(item,index) in contentList" :key="index" v-if="index < 3">
					<view class="fflex">
						<view class="rec-tit tovers">{{item.title}}</view>
						<view class="ddflex">
							<!-- <view class="author ddflex"><image src="../../static/pages/images/zbgw.png" mode="aspectFill"></image>欧衡</view> -->
							<view class="rec-time">{{item.createDate}}</view>
						</view>
					</view>
					<image :src="item.pic" mode="aspectFill" class="rec-img"></image>
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
const app = getApp();
const req = require('../../utils/request.js');
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
import mpHtml from "../../components/mp-html/components/mp-html/mp-html";
export default {
	components: {
		mpHtml,
	},
	data() {
		return {
			code: '',
			rootCode: '',
			detaile: '',
			type: 2, //详情类型：1 普通内容 2 自定义页面内容
			form: {
				page: 1,
				limit: 4,
			},
			contentList: [],
			height: 'initial',
			viewMoress: false,
			productList: [],
			merchant: {},
			bannerList: [],
			swiperCurrent: 0,
			sceneId: '',//分享码传入id
		};
	},

	onLoad: async function(options) {
		if(options.detail){
			let params = JSON.parse(decodeURIComponent(options.detail));
			console.log('params==',params)
			this.code = params.code;
			this.shareCode = params.shareCode
			this.rootCode = params.rootCode
			if(params.appId){
				req.setStorage('appId',params.appId)
			}
			// if(params.userId){
			// 	req.setStorage('pidCode', params.userId);
			// }
		}
		if(options.x_code_id){
			console.log('options==',options)
			this.sceneId = options.x_code_id
		}
		await this.loadCodeParams()
		await this.getDetail();
		this.getContentList();
		if(this.detaile.text){
			setTimeout(async res=>{
				await this.monitor();
			},500)
		}
	},
	onShow(){
	},

	methods: {
		monitor() {
			let that = this;
			let system = uni.getSystemInfoSync();
			let query = uni.createSelectorQuery();
			query.select('.viewMore').boundingClientRect(data=>{
				if(data){
					let height = data.height;
					if(height > system.windowHeight*2) {
						that.height = system.windowHeight *1.8 + 'px';
						that.viewMoress = true
					}
				}
			}).exec();
		},
		viewMores(){
			this.height = 'initial';
			this.viewMoress = false;
		},
		choose: function () {
		  let freshen = false;
		  this.$emit('freshen', {
		    detail: freshen
		  });
		},
		getDateTimeStamp(dateStr){
		 return util.getDateDiff(Date.parse(dateStr.replace(/-/gi,"/")));
		},
		loadCodeParams() {
			let _ts = this;
			return new Promise((resolve, reject) => {
				let form = {
					scene: _ts.sceneId
				};
				if (!_ts.sceneId) {
					resolve();
					return false;
				}
				req.getRequest(api.api_code_params, form, data => {
					let res = JSON.parse(data.scene);
					this.code = res.code;
					this.shareCode = res.shareCode
					this.rootCode = res.rootCode
					if(res.appId){
						req.setStorage('appId',res.appId)
					}
					resolve();
				});
			});
		},
		getDetail() {
			let that = this;
			let apiUrl = api.api_material_detail;
			return new Promise((resolve,reject)=>{
				req.getRequest(apiUrl,{ code: that.code,shareCode: that.shareCode }, res => {
					console.log('that.detaile==',that.detaile)
					if(res.brief){
						res.brief = res.brief.replace(/\r\n/g,"<br>")
						res.brief = res.brief.replace(/\n/g,"<br>");
						res.brief = res.brief.replace(/↵/g,"<br>");
					}
					that.detaile = res;
					resolve();
				},true);
			})
		},
		getContentList() {
			let form = this.form;
			form.rootCode = this.rootCode;
			req.getRequest(api.api_material_library,form,res => {
				this.contentList = res.list;
				for (var i = 0; i < this.contentList.length; i++) {
					if (this.contentList[i].code == this.code){
						this.contentList.splice(i,1);
					}
				}
			});
		},
		//预览图片
		previewImg(index) {
			//预览合同图片
			var src = this.detaile.params.imageList[index].url;
			let imgs = [];
			this.detaile.params.imageList.map(it=>{
				imgs.push(it.url)
			})
			uni.previewImage({
				current: src, // 当前显示图片的http链接
				urls: imgs
			});
		},
		thumbs(){
			req.postRequest(api.api_material_thumbs,{code: this.code,shareCode: this.shareCode},data=>{
				if(this.detaile.isThumbs){
					this.detaile.isThumbs = false
					this.detaile.thumbsCount--
				}else{
					this.detaile.isThumbs = true
					this.detaile.thumbsCount++
				}
			})
		}
	},
	filters: {
		/**
		 * 处理富文本里的图片宽度自适应
		 * 1.去掉img标签里的style、width、height属性
		 * 2.img标签添加style属性：max-width:100%;height:auto
		 * 3.修改所有style里的width属性为max-width:100%
		 * 4.去掉<br/>标签
		 * @param html
		 * @returns {void|string|*}
		 */
		formatRichText(html) {
			//控制小程序中图片大小
			let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
				match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
				match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
				match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
				return match;
			});
			newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
				match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
				return match;
			});
			// newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			newContent = newContent.replace(/<br[^>]*\/>/gi, '<p style="margin: 10px 0;"></p>');
			newContent = newContent.replace(/<br[^>]*\>/gi, '<p style="margin: 10px 0;"></p>');
			newContent = newContent.replace(/font-size:[^;]+;?/g,'');
			newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:inline-block;margin:12rpx auto;"');
			return newContent;
		}
	}
};
</script>
<style>
@import './index.css';
</style>
