<template>
	<view class="main">
		<view class="bgm">
			<view class="form_group">
				<view class="lable">标题</view>
				<input v-model="item.title" placeholder="请输入标题" />
			</view>
			<view class="form_group">
				<view class="lable">分类</view>
				<picker :range="type" range-key="dictLabel" @change="picker($event, 'type')">
					<input placeholder="请选择" v-model="item.type" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
			</view>
			<leditor ref="editor" v-model="item.content"></leditor>
			<view class="form_group">
				<view class="lable">状态</view>
				<picker :range="state" @change="picker($event, 'state')">
					<input placeholder="请选择" :value="item.state == 0 ? '启用' : '停用'" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
			</view>
		</view>
		<button class="btn" @click="add()">确认</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			item: { state: 0 },
			type: [],
			state: ['启用', '停用']
		};
	},
	onLoad(e) {
		this.getType();
		if (e.id) {
			this.http.request({
				url: '/work/knowledge/detail/' + e.id,
				success: (res) => {
					this.item = res.data.data;
					this.$refs.editor.setContents();
					uni.setNavigationBarTitle({ title: '编辑知识库' });
				}
			});
		}
	},
	methods: {
		picker(e, tag) {
			if (tag == 'type') {
				this.item.type = this.type[e.detail.value].dictLabel;
			} else {
				this.item[tag] = e.detail.value;
			}
			this.$forceUpdate();
		},
		getType() {
			this.http.request({
				url: '/app/common/type/knowledge_type',
				loading: 'false',
				success: (res) => {
					this.type = res.data.data;
				}
			});
		},
		add() {
			let rule = [
				{ name: 'title', checkType: 'notnull', errorMsg: '请输入标题' },
				{ name: 'type', checkType: 'notnull', errorMsg: '请选择分类' },
				{ name: 'content', checkType: 'notnull', errorMsg: '请输入内容' }
			];
			if (!this.verify.check(this.item, rule)) {
				uni.showModal({ content: this.verify.error, showCancel: false });
				return false;
			}
			this.http.request({
				url: this.item.id ? '/work/knowledge/edit' : '/work/knowledge/add',
				method: 'POST',
				data: this.item,
				success: (res) => {
					uni.showModal({
						title: '提示',
						content: '操作成功',
						showCancel: false,
						success: (res) => {
							uni.$emit('knowledge');
							uni.navigateBack();
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
.bgm {
	border-radius: 5px;
	overflow: hidden;
}
.btn {
	margin-top: 35px;
}
</style>
