<template>
	<view class="container">
		<view class="gui-form-item gui-border-b" style="padding: 0 30rpx; padding-top: 20rpx;" v-if="type=='gmxz'">
			<text class="gui-form-label">须知模版</text>
			<view class="gui-form-body" style="text-align: right;">
				<picker mode="selector" range-key="name" :range="noticeList" @change="pickerNotice">
					<text class="gui-text gui-secondary-text"
						v-if="noticeIndex > -1">{{noticeList[noticeIndex].name}}</text>
					<text class="gui-text gui-secondary-text" v-else>选择模板，快速编辑</text>
				</picker>
			</view>

			<text class="gui-form-icon gui-icons gui-text-center gui-color-gray">&#xe601;</text>
		</view>

		<view class="page-body">
			<view class='wrapper'>
				<view class='toolbar' @tap="format" style="height: 80px;overflow-y: auto;">
					<view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold">
					</view>
					<view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi"
						data-name="align" data-value="center"></view>
					<view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi"
						data-name="align" data-value="right"></view>
					<view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height"
						data-name="lineHeight" data-value="2"></view>
					<!-- #ifndef MP-BAIDU -->
					<view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize"
						data-name="fontSize" data-value="24px"></view>
					<!-- #endif -->
					<view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color"
						data-name="color" data-value="#0000ff"></view>
					<view class="iconfont icon-date" @tap="insertDate"></view>
					<view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
					<view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie"
						data-name="list" data-value="ordered"></view>

					<view class="iconfont icon-undo" @tap="undo"></view>
					<view class="iconfont icon-redo" @tap="redo"></view>

					<view class="iconfont icon-fengexian" @tap="insertDivider"></view>
					<view class="iconfont icon-charutupian" @tap="insertImage"></view>
					<view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1"
						data-name="header" :data-value="1"></view>
					<view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao"
						data-name="script" data-value="sub"></view>
					<view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao"
						data-name="script" data-value="super"></view>

					<view class="iconfont icon-shanchu" @tap="clear"></view>

					<view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl"
						data-name="direction" data-value="rtl"></view>
				</view>

				<view class="editor-wrapper">
					<editor id="editor" class="ql-container" placeholder="开始输入..." show-img-size show-img-toolbar
						show-img-resize @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady"
						@input="eeeeee">
					</editor>

				</view>
				<!-- <view @click="aaaa">
					确定的
				</view> -->
				<!-- 	
				<view class="gui-margin-top-large gui-flex gui-rows gui-nowrap gui-justify-content-end">
					<button  class="gui-button " style="width:200rpx;"
						@click="aaaa">
						<text class="gui-icons gui-color-white gui-button-text">完成</text>
					</button>
				</view> -->

				<button type="default" class="gui-button gui-bg-blue"
					style="width: 90%;text-align: center;padding: 0rpx 0;text-align: center;background: linear-gradient(to bottom, #90178b, #590b6e);margin: 80rpx auto 0;color: #fff;border-radius: 40rpx;"
					@click="aaaa">
					<text class="gui-icons gui-color-white gui-button-text">完成</text>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import * as UploadApi from '@/api/upload'
	import * as GoodsApi from '@/api/goods/index.js'
	import * as PageApi from '@/api/page'
	export default {
		data() {
			return {
				noticeList: [],
				noticeIndex: -1,
				readOnly: false,
				formats: {},
				type: '',
				goodsId: 0,
				sb: ''
			}
		},
		onLoad(e) {
			this.type = e.type
			this.goodsId = e.goodsId
			this.getNoticeList()
		},

		onShow() {
			this.sb = uni.getStorageSync(this.type)

			console.log(this.sb)
		},
		methods: {

			getNoticeList() {
				let _this = this
				PageApi.getNoticeList()
					.then(result => {
						_this.noticeList = result.data.list

					})

			},

			pickerNotice(e) {
				this.noticeIndex = e.detail.value;

				this.sb = this.noticeList[e.detail.value].summary
				this.editorCtx.setContents({
					html: this.sb //this.EditGoodsDetail.content为赋值内容。    
				})


			},
			// getGoods() {
			// 	let _this = this
			// 	GoodsApi.shopdetail(_this.goodsId)
			// 		.then(result => {
			// 			if (_this.type == 'spxq') {
			// 				_this.sb = result.data.detail.content
			// 			}
			// 			if (_this.type == 'gmxz') {
			// 				_this.sb = result.data.detail.contents
			// 				console.log(result.data.detail.contents)
			// 			}
			// 		})
			// },
			eeeeee(e) {
				this.sb = e.detail.html
			},
			aaaa() {
				let _this = this
				// 页面B
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2]; //上一个页面
				console.log(_this.type)

				//prevPage[_this.type] = _this.sb
				if (_this.type == 'gmxz') {
					uni.setStorageSync('gmxz', _this.sb)
					prevPage['gmxz'] = _this.sb
				} else {
					uni.setStorageSync('spxq', _this.sb)
					prevPage['spxq'] = _this.sb
				}


				uni.navigateBack();
			},
			readOnlyChange() {
				this.readOnly = !this.readOnly
			},
			onEditorReady() {
				let _this = this
				// #ifdef MP-BAIDU
				this.editorCtx = requireDynamicLib('editorLib').createEditorContext('editor');
				// #endif

				// #ifdef APP-PLUS || MP-WEIXIN || H5
				uni.createSelectorQuery().select('#editor').context((res) => {
					_this.editorCtx = res.context

					// if (_this.goodsId > 0) {
					// 	GoodsApi.shopdetail(_this.goodsId)
					// 		.then(result => {
					// 			if (_this.type == 'spxq') {
					// 				_this.sb = result.data.detail.content
					// 			}
					// 			if (_this.type == 'gmxz') {
					// 				_this.sb = result.data.detail.contents
					// 				console.log(result.data.detail.contents)
					// 			}

					// 			_this.editorCtx.setContents({
					// 				html: _this.sb //this.EditGoodsDetail.content为赋值内容。    
					// 			})
					// 		})
					// }else{

					// }

					if (_this.type == 'spxq') {
						_this.sb = uni.getStorageSync('spxq')
						console.log(225)
					}
					if (_this.type == 'gmxz') {
						//_this.sb = result.data.detail.contents
						_this.sb = uni.getStorageSync('gmxz')



					}
					_this.editorCtx.setContents({
						html: _this.sb //this.EditGoodsDetail.content为赋值内容。    
					})



				}).exec()
				// #endif
			},
			undo() {
				this.editorCtx.undo()
			},
			redo() {
				this.editorCtx.redo()
			},
			format(e) {
				let {
					name,
					value
				} = e.target.dataset
				if (!name) return
				// console.log('format', name, value)
				this.editorCtx.format(name, value)
			},
			onStatusChange(e) {
				console.log(e)
				const formats = e.detail
				this.formats = formats
			},
			insertDivider() {
				this.editorCtx.insertDivider({
					success: function() {}
				})
			},
			clear() {
				uni.showModal({
					title: '清空编辑器',
					content: '确定清空编辑器全部内容？',
					success: res => {
						if (res.confirm) {
							this.editorCtx.clear({
								success: function(res) {}
							})
						}
					}
				})
			},
			removeFormat() {
				this.editorCtx.removeFormat()
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			// insertImage() {
			// 	uni.chooseImage({
			// 		count: 1,
			// 		success: (res) => {
			// 			this.editorCtx.insertImage({
			// 				src: res.tempFilePaths[0],
			// 				alt: '图像',
			// 				success: function() {}
			// 			})
			// 		}
			// 	})
			// }

			insertImage() {
				uni.chooseImage({
					count: 1,
					sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
					success: (res) => {
						console.log(res)
						var imageList = []
						imageList = res.tempFiles
						const files = []

						if (imageList.length) {
							const images = imageList.map(image => image)
							files.push({
								formDataIndex: 0,
								images
							})
						}
						
						console.log(imageList)

						UploadApi.goods_detail_image(files[0].images)
							.then(result => {
								//this.formData.uploaded = fileIds
								console.log(result)
								this.editorCtx.insertImage({
									src: result[0],
									alt: '图像',
									success: function() {
										console.log(
											'insert image success'
										)
									}
								})
							})
						// this.editorCtx.insertImage({
						// 	src: res.tempFilePaths[0],
						// 	alt: '图像',
						// 	success: function() {
						// 		console.log('insert image success')
						// 	}
						// })
					}
				})
			},


		}
	}
</script>

<style>
	@import "./editor-icon.css";

	.page-body {
		height: calc(100vh - var(--window-top) - var(--status-bar-height));
	}

	.wrapper {
		height: 100%;
	}

	.editor-wrapper {
		height: calc(100vh - var(--window-top) - var(--status-bar-height) - 200px);
		background: #e8e8e8;
	}

	.iconfont {
		display: inline-block;
		padding: 14px 14px;
		width: 36px;
		height: 36px;
		cursor: pointer;
		font-size: 24px;
	}

	.toolbar {
		box-sizing: border-box;
		border-bottom: 0;
		font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
	}

	.ql-container {
		box-sizing: border-box;
		padding: 12px 15px;
		width: 100%;
		min-height: 30vh;
		height: 100%;
		margin-top: 20px;
		font-size: 16px;
		line-height: 1.5;
	}

	.ql-active {
		color: #06c;
	}
</style>