<template>
	<view class="condition">
		<view class="title">请选择追踪条件（可多选）</view>
		<view class="select-btn" v-if="theme.id === 0">
			<text>选择门诊或急诊</text>
			<view>
				<button :class="{active: condition.depType === 1}" 
					@click="selectBtnHandle(1)">门诊</button>
				<button :class="{active: condition.depType === 2}" 
					@click="selectBtnHandle(2)">急诊</button>
			</view>
		</view>
		<tm-trees :options="condition.options"
		 :defaultOpen="condition.defaultOpen" 
		 :defaultChecked="condition.conditionIds"
		 :parentCheckble = "false"	
		 @checked-keys="checkedHandle"></tm-trees>
	</view>
</template>

<script>
	import { mapState } from "vuex";
	import {conditionOptions,editCondition} from "./utils.js";
	
	export default {
		computed: {
			...mapState({
				condition: state => state.creatingSituations.condition,
				needReload: state => state.creatingSituations.needReload,
				theme: state => state.creatingSituations.theme,
				editConfig: state => state.creatingSituations.editConfig
			})
		},
		created: function(){
			// 编辑的时候不用获取数据
			if(this.editConfig && this.editConfig.theme.id === this.theme.id) {
				this.$store.commit({
					type: 'creatingSituations/comChangeState', 
					key: 'condition', 
					data: this.editConfig.condition
				});
			} else if(this.needReload) {
				if(this.theme.id != undefined||this.theme.id != null){
					  this.getOptions(this.theme.id === 1 ? 1 :this.condition.depType);
				}
				
			}
		},
		methods: {
			checkedHandle: function(conditionIds,condition) {
				this.myCommit('conditionIds', conditionIds);
				this.myCommit('checkedItems', condition);
			},
			selectBtnHandle: function(depType) {
				this.myCommit('depType', depType);
				this.getOptions(depType);
			},
			/**
			 * 获取树形节点数据
			 */
			getOptions: function(depType) {
				this.checkedHandle([]); // 切换后清空选中
				const {id} = this.theme;
				if(id === 0||id == 1) {
					//普通+个案
					this.$store.dispatch({
						type: 'creatingSituations/commActions',
						key: 'conditions',
						data: { depType, type: id }
					}).then((data)=> {
						if(data) {
							if(data.length > 0) {
								this.myCommit('defaultOpen', [data[0].id]);
							}
							this.myCommit('options', conditionOptions(data));
						} 
					});
				} else if(id == 2) {
					//自查督查
					this.$store.dispatch({
						type: 'creatingSituations/commActions',
						key: 'getTypeLists',
						data: { filter:1,situationId:this.theme.situationId}
					}).then((data)=> {
					
						if(data) {
							if(data.length > 0) {
								this.myCommit('defaultOpen', [data[0].id]);
							}
							this.myCommit('options', conditionOptions(data));
						} 
					});
				}else {
					uni.showModal({
					  title: '温馨提示',
					  content: '请先选择主题！',
					  showCancel: false
					});
				}
			},
			/**
			 * 更新condition数据
			 * @param {Object} key 要更新的属性
			 * @param {Object} value 值
			 */
			myCommit: function(key, value) {
				let data = {...this.condition};
				data[key] = value;
				this.$store.commit({
					type: 'creatingSituations/comChangeState', 
					key: 'condition', 
					data
				});
			}
		}
	}
</script>

<style lang="less">
	.select-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 15rpx;
		padding: 0 25rpx;
		width: 100%;
		height: 87.5rpx;
		font-size: 22.5rpx;
		line-height: 33.75rpx;
		color: #525866;
		background-color: #fff;
		
		view {
			display: flex;
			flex-direction: row;
			
			button {
				border: 0;
				border-radius: 25rpx;
				padding: 0 46.25rpx;
				height: 50rpx;
				line-height: 50rpx;
				color: #292C33;
				background-color: #EBEFF7;
				
				&:first-child {
					margin-right: 20rpx;
				}
				&::after {
					border: 0;
				}
				&.active {
					color: #fff;
					background-color: #3377FF;
				}
			}
		}
	}
</style>
