<template>
	<view class="app">
		<view class="items">
			<view class="item"
				:style="disease.find(e => e.diseaseName == item.diseaseName)?'background-color: #4C7BC9;color: #ffffff;':''"
				v-for='(item,index) in diseaselist' :key="index" @tap='choicedisease(item)'>
				{{item.diseaseName}}
			</view>
			<view class="other">
				<span>其他:</span>
				<u-input type="text" placeholder='请填写' v-model="diseaseother.diseaseName" />
			</view>
			<view class="btn" @tap='goinformation'>
				提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getDiseaseInfo
	} from '@/api/disease/index.js'
	export default {
		data() {
			return {
				diseaselist: [], //疾病列表
				disease: [],
				diseaseother: {
					diseaseName: '',
				},
			};
		},
		methods: {
			//选择疾病
			choicedisease(item) {
				if (this.disease.findIndex(e => e.diseaseName == item.diseaseName) == -1) {
					this.disease.push(item)
				} else {
					this.disease = this.disease.filter(e => e.diseaseName != item.diseaseName)
				}
				// if (this.disease.length > 1) {
				// 	this.disease = this.disease.filter(e => e.id != 9999)
				// }
			},
			//获取疾病列表
			diseaseinfo() {
				try {
					const value = uni.getStorageSync('patientId');
					if (value) {
						getDiseaseInfo(value).then(res => {
							this.diseaselist = res.data;
							// this.diseaselist.unshift({
							// 	id: 9999,
							// 	diseaseName: '无'
							// })
						})
					}
				} catch (e) {}
			},
			//跳转回信息完善页面
			goinformation() {
				if (this.diseaseother.diseaseName != '' || this.diseaseother.diseaseName) {
					this.disease.push(this.diseaseother)
				}
				this.diseaselist.forEach(e => {
					if (this.diseaseother.diseaseName == e.diseaseName) {
						this.disease = this.disease.filter(el => el.diseaseName != e.diseaseName)
						this.disease.push(e)
					}
				})
				uni.$emit('disease', {
					disease: JSON.stringify(this.disease)
				})
				uni.navigateBack({
					delta: 1
				});
			}
		},
		onShow() { //开局方法
			this.diseaseinfo()
		},
		onLoad(options) { //传参接受
			this.disease = JSON.parse(options.diseaseInfoList)
			console.log(this.disease)
		},
	}
</script>

<style lang="scss">
	@import './disease.scss';
</style>
