(function($) {
	//0并行分支,1手动分支,2条件分支 

	Vue.component('user-select', {
		template: '\
    			<section>\
					<input type="hidden" id="backId" value="" />\
					<input type="hidden" id="backType" value="" />\
					<input type="hidden" id="isMultiDept" value="" />\
					<template v-for="(dept,index) in depts">\
						<a href="#" @click="getParentGroup(dept.id,index)">\
							<label v-if="index > 0">></label>\
							{{dept.name}}\
						</a>\
					</template>\
					<ul class="mui-table-view">\
						<li class="mui-table-view-cell" v-for="groupUser in groupUsers" v-if="groupUser.type == \'dept\'">\
							<a class="mui-navigate-right" href="javascript:void(0)" @click="getChildGroup(groupUser.id,groupUser.text);">{{groupUser.text}}</a>\
						</li>\
					</ul>\
					<template v-if="multiple == \'y\'">\
						<div class="mui-table-view-cell mui-checkbox" style="padding:5px;" v-for="groupUser in groupUsers" v-if="groupUser.type == \'user\'">\
							<label style="width: 60%;padding:5px;">{{groupUser.text}}</label>\
							<template v-if="ifChecked(groupUser)">\
								<input name="radio1" type="checkbox" :value="groupUser.id" @click="incrementHandler(groupUser,multiple,$event)" checked>\
							</template>\
							<template v-else>\
								<input name="radio1" type="checkbox" :value="groupUser.id" @click="incrementHandler(groupUser,multiple,$event)"  >\
							</template>\
						</div>\
					</template>\
					<template v-else>\
						<div class="mui-table-view-cell mui-radio" style="padding:5px;" v-for="groupUser in groupUsers" v-if="groupUser.type == \'user\'">\
							<label style="width: 60%;padding:5px;">{{groupUser.text}}</label>\
							<template v-if="ifChecked(groupUser)">\
								<input name="radio1" type="radio" :value="groupUser.id" @click="incrementHandler(groupUser,multiple,$event)">\
							</template>\
							<template v-else>\
								<input name="radio1" type="radio" :value="groupUser.id" @click="incrementHandler(groupUser,multiple,$event)"  >\
							</template>\
						</div>\
					</template>\
				</section>\
    		',
    	//multiple:是否多选(y:是,n:否)
		props: ['groupUsers', 'depts','multiple'],
		methods: {
		    incrementHandler: function (user,multiple,event) {
		    	var checked = event.target.checked;
		    	if(multiple == 'y'){//多选
			    	if(checked){//选中则添加人员
			    		vmdepts.curUser.push(user);
			    	}else{//取消则删除人员
			    		for(var i = 0;i<vmdepts.curUser.length;i++){
			    			if(vmdepts.curUser[i].id == user.id){
			    				vmdepts.curUser.splice(i,1);//移除
			    			}
			    		}
			    	}
		    	}else{
		    		if(checked){//选中则添加人员
			    		vmdepts.curUser.splice(0,1,user);
			    	}
		    	}
//		      	this.$emit('increment');
		    },
			ifChecked:function (user){
//				alert(vmdepts.curIds+"====="+user.id+"====="+vmdepts.curIds.indexOf(user.id));
    			if(vmdepts.curIds.indexOf(user.id)> -1){
    				return true;
    			}
				return false;
			}
	  	},
		//组件渲染之前
		created: function() {},
		//组件渲染之后
		mounted: function() {}
	});

})(jQuery);

var vmdepts = new Vue({
	el: '#personData',
	data: {
		curIds : "",
		groupUsers: [],
		depts: [{
			id: API.UNIT_ID,
			name: '联系人'
		}],
		curUser: [],
		//0人员userId,1人员groupxuserId
		isMultiDept : 0		
	}
});


function choosePerson(params) {
	var backId = params.backId,backType = params.backType,isMultiDept = params.isMultiDept;
	$("#backId").val(backId);
	$("#backType").val(backType);
	if(isMultiDept){
		vmdepts.isMultiDept = isMultiDept;
	}
	getParentGroup(API.UNIT_ID,0);
	viewApi.go('#personComponent');
};

function selectUser() {
	var backId = $("#backId").val();
	var backType = $("#backType").val(); 
	var userIds = "";
	var userNames = "";
	for(var i = 0;i<vmdepts.curUser.length;i++){
		if(userIds  == ""){
			userIds += vmdepts.curUser[i].id;
		}else{
			userIds += "," + vmdepts.curUser[i].id;
		}
		if(userNames  == ""){
			userNames += vmdepts.curUser[i].text;
		}else{
			userNames += "," + vmdepts.curUser[i].text;
		}
		
	}
	$("#" + backId).val(userNames);
	$("#" + backId).attr("userId", userIds);
	if("bpmFormUser" == backType){//流程表单人员选择控件,表单值回填特殊处理
		var formBackId = backId.replace("_name","");
		$("#" + formBackId).val(userIds+"|"+userNames);
	}
	
	viewApi.back();
}

function getParentGroup(groupId, index) {

	console.log(vmdepts.depts)
	vmdepts.depts.splice(index+1,vmdepts.depts.length);
	getGroupUser(groupId);
}

function getChildGroup(groupId, groupName) {
	var dept = {}
	dept.id = groupId;
	dept.name = groupName;
	vmdepts.depts.push(dept);
	getGroupUser(groupId);
}

//获取人员选择列表信息
function getGroupUser(groupId) {
	var userIds = "";
	var backId = $("#backId").val();
	if($("#"+backId).attr("userId")){
		userIds = $("#"+backId).attr("userId")
	}
	vmdepts.curIds = userIds;
	vmdepts.curUser = [];
	mui('#person-scroll').scroll().scrollTo(0, 0);
	console.log(groupId);
	var state = app.getState(); //获取登陆信息
	var staffId = state.user.useId;
	var isMultiDept = vmdepts.isMultiDept;
	mui.ajax(url, {
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		data: {
			'serviceId': 'eu_20180308loadUserByGroup',
			'params': '{staffId:"' + staffId + '",groupId:"' + groupId + '",isMultiDept:"'+isMultiDept+'"}'
		},
		timeout: 10000, //超时时间设置为10秒；
		headers: {
			'Accept': 'application/json'
		},
		success: function(data) {
			//服务器返回响应
			console.log("人员信息:" + JSON.stringify(data));
			if(data.returnCode == "1") {
				vmdepts.groupUsers = data.returnParams;
				for(var i = 0;i<vmdepts.groupUsers.length;i++){
					if(vmdepts.curIds.indexOf(vmdepts.groupUsers[i].id)> -1){
						vmdepts.curUser.push(vmdepts.groupUsers[i]);
					}
				}
			} else {
				mui.toast("获取人员信息错误");
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(xhr + "========" + type + "===========" + errorThrown);
		}
	});
};

function closeView(){
	viewApi.back();
}
