<template>
	<view class="check-map-detail">
		<view class="item" v-for="(item, index) in checkMap.actionItem.pointList" :key="index">
			<view class="icon-wrap" @click="openChildren(item.id)">
				<image :src="`../../static/list-${openItems.includes(item.id)
					?'close':'open'}.png`"></image>
			</view>
			<view class="content">
				<view class="content-title" @click="openChildren(item.id)">{{item.name}}</view>
				<view class="children" v-if="openItems.includes(item.id)">
					<view class="child" v-for="(child, n) in item.itemList" :key="n">
						<text>{{child.name}}</text>
						<view class="check-icon-wrap" @click="changeChecked(child.id)">
							<image :src="`../../static/check-${checkedIds.includes(child.id)
								?'checkbox':'no'}.png`"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<tm-simple-btn-group :options="botmBtnGroup"
			v-on:callback="btnClick" ></tm-simple-btn-group>
	</view>
</template>

<script>
	import { mapState } from "vuex";
	import {arrFilter} from "../../../utils/arrFilter.js";
	
	export default {
		data() {
			return {
				openItems: [],
				checkedIds: [],
			}
		},
		computed: {
			botmBtnGroup: function() {
				return [
					{id: 'cancle', label: '取消'},
					{id: 'ok', label: `已选${this.checkedIds.length}项，确定`}
				];
			},
			...mapState({
				checkMap: state => state.creatingSituations.checkMap,
			})
		},
		created:function(){
			// 默认全选中
			this.checkMap.actionItem.pointList.map((item)=>{
				this.openItems.push(item.id);
				item.itemList.map((ntem)=>{
					if(ntem.selectFlag === undefined || ntem.selectFlag) {
						this.checkedIds.push(ntem.id);
					}
				});
			});
		},
		methods: {
			openChildren: function(key) {
				this.openItems = arrFilter(key, this.openItems);
			},
			changeChecked: function(id) {
				this.checkedIds = arrFilter(id, this.checkedIds);
			},
			btnClick: function(id) {
				if(id === 'ok') {
					let pointList = [...this.checkMap.actionItem.pointList];
					pointList.map((item)=>{
						item.itemList.map((ntem)=>{
							ntem.selectFlag = this.checkedIds.includes(ntem.id) ? true : false;
						});
					});
					let list = [...this.checkMap.list];
					let index = list.findIndex(({id})=> id === this.checkMap.actionItem.id);
					list[index].pointList = pointList;
					this.myCommit('list', list);
				} 
				this.commit('showCheckMapDetail', false);
			},
			/**
			 * 更新condition数据
			 * @param {Object} key 要更新的属性
			 * @param {Object} value 值
			 */
			myCommit: function(key, value) {
				let data = {...this.checkMap};
				data[key] = value;
				this.commit('checkMap', data);
			},
			dispatch: function(key, data) {
				return this.$store.dispatch({type: 'creatingSituations/commActions', key, data});
			},
			commit: function(key,data) {
				this.$store.commit({type: 'creatingSituations/comChangeState', key, data});
			},
		}
	}
</script>

<style lang="less">
	.check-map-detail {
		padding-bottom: 75rpx;
		width: 100%;
		height: 100%;
		.item {
			display: flex;
			flex-direction: row;
			border-top: 1px solid #DADEE6;
			padding-left: 25rpx;
			width: 100%;
			background-color: #fff;
			.icon-wrap {
				padding-top: 28.12rpx;
				padding-right: 23.75rpx;
				width: 45rpx;
				height: inherit;
				image {
					width: 100%;
					height: 12.5rpx;
				}
			}
			.content {
				width: 100%;
				color: #292C33;
				line-height: 87.5rpx;
				.content-title {
					height: 87.5rpx;
					font-size: 22.5rpx;
					font-weight: 500;
				}
				.children {
					width: 100%;
					.child {
						display: flex;
						flex-direction: row;
						justify-content: space-between;
						border-top: 1px solid #DADEE6;
						height: 87.5rpx;
						.check-icon-wrap {
							display: flex;
							flex-direction: row;
							justify-content: center;
							align-items: center;
							width: 75rpx;
							height: 100%;
							image {
								width: 25rpx;
								height: 25rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
