apiready = function(){
	var ctrl = {
		init : {},
		bind : {},
		selected : null,

		GYNumbers : [],
		DYNumbers : [],
		BYQNumbers : [],
		ZLPNumbers : [],
		anothersNumbers : [],

		getNumbers : {},
		renderNumbers : {},
		sendNumber : {}
	}

	ctrl.init = function(){
		ctrl.bind();
		ctrl.getNumbers();
	}

	ctrl.bind = function(){
		//切换调度号
		$("#list").on("touchend", ".number", function(){
			var number = $(this).data("number");
			var name = $(this).data("name");

			ctrl.selected = { number : number, name:name};

			$(".number").removeClass("active");
			$(this).addClass("active");
		})

		//确定
		$("#ok").on("touchend", function(){
			ctrl.sendNumber(ctrl.selected);
			api.closeDrawerPane();
		})

		//取消
		$("#cancel").on("touchend", function(){
			api.closeDrawerPane();
		})

		//监听主页请求调度号的消息
		api.addEventListener({
	        name:'energyAskNumber'
        },function(ret,err){
        	ctrl.init();
        });

        //如果没有初始化 初始化
        api.addEventListener({
	        name:'viewappear'
        },function(ret,err){
        	if(!ctrl.selected){
        		ctrl.init();
        	}
        });
	}

	//获取调度号
	ctrl.getNumbers = function(){
		var url = "/ems/rest/ddnum/list";

		var param = {
			prId : $api.getStorage('powerRoom').prId,
			flag : 1,	// 吃查询能耗点的调度号
		}

		$api.get(url,param, callback);

		function callback(ret, err){
			ctrl.GYNumbers = [];
			ctrl.DYNumbers = [];
			ctrl.BYQNumbers = [];
			ctrl.ZLPNumbers = [];
			ctrl.anothersNumbers = [];
			if(!err){
				if(ret.code == "200"){

					var list = ret.body;
					var length = list.length;

					for(var i=0; i<length; i++){

						switch(list[i].location){
							case 1 : ctrl.GYNumbers.push(list[i]);break;
							case 2 : ctrl.DYNumbers.push(list[i]);break;
							case 3 : ctrl.BYQNumbers.push(list[i]);break;
							case 4 : ctrl.ZLPNumbers.push(list[i]);break;
							default : ctrl.anothersNumbers.push(list[i]);
						}

					}

					ctrl.renderNumbers();

				}else{
					$api.toast({"msg": "获取数据失败"});
				}
			}else{
				$api.toast({"msg": "网络请求失败"});
			}
		}
	}

	//渲染调度号列表
	ctrl.renderNumbers = function(){

		var length = ctrl.GYNumbers.length;
		var content = '';
		var classes = [{name : "高压侧",numbers : ctrl.GYNumbers},
					   {name : "低压侧",numbers : ctrl.DYNumbers},
					   {name : "变压器",numbers : ctrl.BYQNumbers},
					   {name : "直流屏",numbers : ctrl.ZLPNumbers},
					   {name : "其他",numbers : ctrl.anothersNumbers}
					  ];

		for(var i=0; i<5; i++){

			var numbers = classes[i].numbers;
			var length = numbers.length;
			if(length > 0){

				if(!ctrl.selected){

					ctrl.selected = { number : classes[i].numbers[0].ddNum, name:classes[i].numbers[0].deivceName}

				}

				//分类名称
				content += '<div class="class-name">'+ classes[i].name +'</div>'
				for(var j=0; j<length; j++){

					//调度号
					content += '<div class="number" data-number="'+numbers[j].ddNum+'" data-name="'+ numbers[j].deivceName +'">'+'('+numbers[j].ddNum+')&nbsp;&nbsp;'+ numbers[j].deivceName +'</div>'

				}

			}
		}

		if(!ctrl.selected){

			ctrl.sendNumber(null);

		}else{

			$("#list").html(content);
			ctrl.sendNumber(ctrl.selected);
			$(".number").eq(0).addClass("active");

		}

	}

	//发送调度号选中的事件
	ctrl.sendNumber = function(selected){
		api.sendEvent({
	        name : 'energySelectedNumber',
	        extra : selected,
        });
	}

	ctrl.bind();

}
