define(['zepto','medtap'],function($,medtap){
	var main = {
		wechatId:medtap.getRequest('wechatId'),
		agencyId:medtap.getRequest('agencyId'),
		operationType:medtap.getRequest('operationType'),
		token:'',
		msgId:medtap.getRequest('wechatMsgId'),
		init:function(){
			main.getTokenByWechatId();
			
			if(!main.msgId){
				
			}else{
				main.msgRecord();
			}
		},
		bind:function(){
			$('.filterBox').on('click', 'p', function(){
				$('.filter').text('筛选:' + $(this).text());
				$('.filter, .filterBox').removeClass('on');
				var type = $(this).attr('key');
				main.getBook(type)
			});
			$('.filter').on('click', function(){
				var elFilter = $('.filter');
				!!elFilter.hasClass('on') 
					? $('.filter, .filterBox').removeClass('on')
					: $('.filter, .filterBox').addClass('on');
			});
			$('.lectureWrap').on('click',function(){
				window.location.href = 'https://mp.weixin.qq.com/mp/homepage?__biz=MzA3NTk5NjEwMQ==&hid=15&sn=97b35926097a72407b476e086c5dee82&scene=18'
			})
		},
		//获取患教手册内容
		getBook:function(type){
			medtap.loading(1);
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/common/category/listEduBook',
				type:'get',
				data:{
					type:type
				},
				success:function(res){
					if(res.success == true){
						medtap.loading(0);
						var bookList = res.content.categoryList;
						var str = '';
						for(var i = 0;i < bookList.length;i++){
							str += '<div class="bookItem" data-bookId="'+ bookList[i].node +'-'+ bookList[i].categoryId+'" data-title="'+bookList[i].content+'">'+
										'<div class="bookbanner"><img src="'+bookList[i].picUrl+'" ></div>'+
										'<div class="bookName">'+bookList[i].content+'</div>'+
									'</div>';
						}
						$('.bookList').html(str);
						main.goToBookDetail();
						
					}
				}
			})
		},
		//跳转至书籍详情
		goToBookDetail:function(){
			$('.bookList').on('click','.bookItem',function(){
				var bookId = $(this).attr('data-bookId');
				var title = $(this).attr('data-title');
				medtap.pushWindow('pages/bookDetail.html?bookId=' + bookId + '&title=' + title + '&wechatId=' + main.wechatId + '&agencyId=' + main.agencyId + '&operationType=' + main.operationType);
			})
		},
		
		getTokenByWechatId:function(){
			localStorage.wxLungToken = '';
			medtap.submitAjax({
				url:'https://testdevgw.medtap.cn/user/auth/token',
				type:'POST',
				async:false,
				contentType: "application/json",
				data:{
					wechatId:main.wechatId
				},
				success:function(res){
					if(res.success == true){
						var token = res.content.token;
						main.token = token;
						localStorage.setItem('wxLungToken',token);
						main.getBook('0');//初始化时获取患教手册
					}
				}
			})
		}
	}
	main.init();
	main.bind();
})