<template>
	<div class="question-single question-subjective">
		<div class="exersices-content">
			<IconText :text="$t('survey.question')" :color="'#2d8cf0'" :icon="'ios-create'" style="margin-bottom:15px;"></IconText>
			<div ref="singleEditor" style="text-align:left"></div>
		</div>
	</div>
</template>
<script>
	import E from 'wangeditor'
	import IconText from '@/components/evaluation/IconText.vue'
	export default {
		components: {
			IconText
		},
		props: ['editInfo'],
		data() {
			return {
				options: [...new Array(2).keys()], // 默认四个选项
				existOptions: [...new Array(2).keys()],
				initFlag: true,
				trueIndex: 0,
				editSingleInfo: {},
				stemEditor: null,
				stemContent: '',
				optionsContent: [],
				optionEditors: [],
				defaultConfig: {
					uploadImgShowBase64: true,
					menus: this.$tools.wangEditorMenuSimple
				}
			}
		},
		created() {},
		methods: {
			resetContent() {
				console.log('重置')
				this.options = [...new Array(2).keys()],
					this.optionsContent = []
				this.optionEditors.forEach(i => {
					i.txt.clear()
				})
				this.stemEditor.txt.clear()
			},
			onRichTextClick(e) {
				this.$parent.onRichTextClick(e)
			},
			// 保存试题 获取最新选项数据
			doSave() {
				this.$emit('onSave', {
					stemContent: this.stemContent,
					optionsContent: []
				})
			},
		},
		mounted() {
			let stemEditor = new E(this.$refs.singleEditor)
			stemEditor.config.onchange = (html) => {
				this.stemContent = html
			}
			this.$editorTools.initSimpleEditor(stemEditor)
			stemEditor.create()
			this.stemEditor = stemEditor
			
			if (this.editInfo && this.editInfo.question) {
				console.log('进入判断题Mounted编辑')
				this.editSingleInfo = JSON.parse(JSON.stringify(this.editInfo))
				this.stemContent = this.editSingleInfo.question
				this.$nextTick(() => {
					this.stemEditor.txt.html(this.editSingleInfo.question)
				})
			}
		},
		watch: {
			editInfo: {
				handler(newValue, oldValue) {
					if (newValue) {
						this.editSingleInfo = JSON.parse(JSON.stringify(newValue))
						if (Object.keys(newValue).length > 0) {
							this.stemContent = this.editSingleInfo.question
							this.$nextTick(() => {
								this.stemEditor.txt.html(this.editSingleInfo.question)
							})
						}
					}
				},
				// immediate:true
			},

		}
	}
</script>
<style lang="less">
	@import "./BaseSingle.less";
</style>

<style>
	.question-subjective .w-e-text-container{
		height: 300px !important;
	}
	
</style>
