<template>
	<view>
		<view class="questionBankAnswer-bottom-flex" @tap="note">
			<view class="iconfont icon-bianjisekuai"></view>
			<view>笔记</view>
		</view>
		<uni-popup ref="popup" type="bottom" radius="12rpx" background-color="#8c8989" color="#5A5B5C" width="90%">
			<view class="biji-content">
				<view class="popup-content">
					<text class="text">笔记</text>
					<view class="iconfont icon-cuo1" @tap="hide"></view>
				</view>
				<textarea :value="content" placeholder="请输入笔记" @input="setContent" class="popup-textarea" />
				<button type="primary" :loading="isSubmit" @tap="submit" class="popup-button">提交</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		props: {
			current_timu: {
				type: Object,
				default: function() {
					return {}
				},
			},
		},
		data() {
			return {
				content: '',
				isSubmit: false,
			}
		},
		methods: {
			setContent(e) {
				this.content = e.detail.value
			},
			async submit() {
				// if (this.content == '') return
				if (this.isSubmit) return
				this.isSubmit = true
				let res = await this.$myHttp.post({
					url: this.$myHttp.urlMap.addNote,
					data: {
						question_id: this.current_timu.id,
						content: this.content
					},
					needLogin: true
				})
				if (res.code == 1) {
					this.isSubmit = false
					uni.showToast({
						title: '保存成功'
					})
					this.$refs.popup.close()
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					this.isSubmit = false
				}
			},
			//获取之前的记录
			async getNote() {
				let res = await this.$myHttp.post({
					url: this.$myHttp.urlMap.getNote,
					data: {
						question_id: this.current_timu.id,
					},
					needLogin: true
				})
				if (res.code == 1) {
					if (res.data == null) {
						this.content = ''
					} else {
						if (this.current_timu.id == res.data.question_id) {
							this.content = res.data.content
						}
					}

				}
			},
			note() {
				if (this.current_timu.id != '' && this.current_timu.id != undefined) {
					this.$refs.popup.open('bottom')
					this.getNote()
				}
			},
			hide() {
				this.$refs.popup.close()
			}
		}
	}
</script>

<style>
	.popup-content {
		position: relative;
		background: #fff;
		z-index: 111;
		width: 100%;
		border-radius: 12px 12px 0 0;
		height: 42px;
		border-bottom: solid 1px #f1f1f1;
		text-align: center;
		line-height: 42px;
		color: #313131;
		font-size: 15px;
	}

	.popup-textarea {
		width: 92%;
		margin: 12px;
		height: 125px;
		font-size: 14px;
	}

	.biji-content {
		position: relative;
		background: #fff;
		border-radius: 12rpx 12rpx 0 0;
		max-height: 80vh;
		overflow-y: auto;
	}

	.popup-button {
		background: #3c7bfc !important;
		color: #fff;
		padding: 2px;
		margin: 0 auto 11px;
		border-radius: 43px;
		font-size: 16px;
		display: block !important;
		width: 95%;
	}

	.icon-cuo1 {
		position: absolute;
		color: #ccc;
		top: 0;
		right: 12px;
	}
</style>