<template>
	<view class="main">
		<view class="form">
			<view class="form_group">
				<view class="lable">所属科室</view>
				<input :value="user.dept.deptName || '无科室'" disabled />
			</view>
			<view class="form_group" v-if="item.type == 0">
				<view class="lable">提醒标题</view>
				<input v-model="item.templateName" placeholder="请输入提醒标题" />
			</view>
			<view class="form_group" v-if="item.type == 1">
				<view class="lable">随访模板</view>
				<picker :range="templateList" range-key="title" @change="picker">
					<input placeholder="请选择" v-model="item.templateName" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
			</view>
			<view class="form_group">
				<view class="lable">指定患者</view>
				<picker @click="go()" disabled v-if="item.patientList">
					<input placeholder="请选择" :value="item.patientList.length > 0 ? '已选择：' + item.patientList.length + '人' : ''" :disabled="true" />
					<view class="icon more">&#xe62b;</view>
				</picker>
				<input :value="item.patientName" v-else />
			</view>
			<view class="form_group" v-if="item.type == 0">
				<view class="lable">提醒内容</view>
				<textarea v-model="item.op" placeholder="请输入提醒内容"></textarea>
			</view>
		</view>
		<button class="btn" @click="add()">确认</button>
	</view>
</template>

<script>
export default {
	data() {
		return {
			user: this.getUser(),
			item: { patientList: [], op: '根据您于本院做的体检结果异常,请到岑溪人民医院做复诊' },
			templateList: []
		};
	},
	onLoad(e) {
		this.item.type = e.type || 0;
		this.getTemplateList();
		this.getDeatil(e);
		uni.$on('selectUser', (res) => {
			this.item.patientList = res;
		});
	},
	methods: {
		getDeatil(e) {
			if (e.id) {
				this.http.request({
					url: '/work/record/detail/' + e.id,
					success: (res) => {
						this.item = res.data.data;
						this.item.patientName = e.patientName;
						uni.setNavigationBarTitle({ title: '编辑' });
					}
				});
			}
		},
		picker(e, tag) {
			this.item.templateName = this.templateList[e.detail.value].title;
			this.item.op = this.templateList[e.detail.value].op;
			this.$forceUpdate();
		},
		//随访模板
		getTemplateList() {
			this.http.request({
				url: '/work/record/template/list',
				success: (res) => {
					this.templateList = res.data.data;
				}
			});
		},
		//指定患者
		go() {
			uni.navigateTo({ url: '/pages/follow/doctor/selectUser?selected=' + JSON.stringify(this.item.patientList) });
		},
		add() {
			let rule = [{ name: 'templateName', checkType: 'notnull', errorMsg: this.item.type == 0 ? '提醒标题' : '请选择随访模板' }];
			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/record/edit' : '/work/record/add',
				method: 'POST',
				data: this.item,
				success: (res) => {
					uni.showModal({
						title: '提示',
						content: '操作成功',
						showCancel: false,
						success: (res) => {
							uni.$emit('record');
							uni.navigateBack();
						}
					});
				}
			});
		}
	}
};
</script>

<style lang="scss"></style>
