define(['zepto', 'medtap', '../javaScript/beforeSubmit.js'], function($,
	medtap, beforeSubmit) {
	var book = {
		bookId: medtap.getRequest('bookId'),
		title: medtap.getRequest('title'),
		wechatId: medtap.getRequest('wechatId'),
		agencyId: medtap.getRequest('agencyId') || '',
		bookTitle: medtap.getRequest('bookTitle'),
		doctorId: medtap.getRequest('doctorId'),
		newAgencyId: '',
		imgUrl: '',
		bookTitle: '',
		offset: 1,
		limit: 60,
		init: function() {
			document.title = book.title;
			book.getBookDetail();
			book.getArticleList();
		},
		//获取书籍介绍信息
		getBookDetail: function(bookId) {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/common/category/getCategoryByNode',
				type: 'get',
				async: false,
				data:{
					articleCategory: book.bookId
				},
				success: function(res) {
					if (res.success == true) {
						var bookDetail = res.content.category;
						book.bookTitle = bookDetail.content;

						if (bookDetail.picUrl == '' || bookDetail.picUrl == undefined || bookDetail.picUrl == null) {
							book.imgUrl = 'https://cdn.iplusmed.com/static/images/user_logo.png';
						} else {
							book.imgUrl = bookDetail.picUrl;
						}
						document.title = bookDetail.content;
						if (bookDetail.author == '' || bookDetail.author == undefined || bookDetail.author == null) {
							$('.bookAuthor').html()
						} else {
							$('.bookAuthor').html('主编：&nbsp;' + bookDetail.author);
						}
						$('.bookName').html(bookDetail.content);
						$('.bookDesc').html(bookDetail.desc);
						if (bookDetail.score == '' || bookDetail.score == undefined || bookDetail.score == null) {
							$('.hotDesc').html('热度值:9.8');
						} else {
							$('.hotDesc').html('热度值:' + bookDetail.score);
						}
						$('.bookProfile').attr('src', bookDetail.picUrl)
					}
				}
			})
		},
		//跳转至文章详情页面
		goArticleDetail: function(articleId) {
			$('.articleList').on('click', 'p', function() {
				var id = $(this).attr('data-id');
				medtap.pushWindow('../pages/articleDetail.html?articleId=' + id + '&wechatId=' + book.wechatId + '&bookTitle=' + book.bookTitle);
			})
		},
		//获取文章章节目录
		getArticleList: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/operation/article/listBookArticle',
				type: 'get',
				async: false,
				data: {
					limit: book.limit,
					offset: book.offset,
					articleCategory: book.bookId
				},
				success: function(res) {
					if (res.success == true) {
						var articleList = res.content.articleInfoList;
						var str = '';
						for (var i = 0; i < articleList.length; i++) {
							str += '<p data-id="' + articleList[i].articleId + '"><span class="articleTitle">' + articleList[i].articleTitle +
								'</span><span class="rightIcon"></span></p>'
						}
						$('.articleList').html(str);
						book.goArticleDetail();
					}
				}
			})
		},
		shareBook: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/wechat/lung/fetchWechatTicket',
				type: 'get',
				async: false,
				data:{
					url: window.location.href
				},
				success: function(data) {
					var res = data.content;
					wx.config({
						debug: false,
						appId: res.appId, // 必填，公众号的唯一标识
						timestamp: res.timestamp, // 必填，生成签名的时间戳
						nonceStr: res.nonceStr, // 必填，生成签名的随机串
						signature: res.signature, // 必填，签名，见附录1
						jsApiList: [
							'onMenuShareTimeline',
							'onMenuShareAppMessage'
						]
					})
				},
				error: function() {}
			});

			//分享配置
			wx.ready(function() {
				//分享给朋友
				wx.onMenuShareAppMessage({
					title: '向您推荐《' + book.bookTitle + '》，快打开看看吧',
					desc: '患教手册包含了科学的疾病知识，回答了许多常见的问题。建议患者和家属认真阅读。', // 分享描述
					link: 'https://testdevgw.medtap.cn/wechat/lung/oauth_stepEnd?type=DISEASE_MANUAL_WHOLE_EBOOK&bookId=' + book.bookId,
					imgUrl: book.imgUrl, // 分享图标
					type: 'link', // 分享类型,music、video或link，不填默认为link
					dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
					success: function() {
						
					},
					cancel: function() {
						
					}
				});
				//分享到朋友圈
				wx.onMenuShareTimeline({
					title: '向您推荐《' + book.bookTitle + '》，快打开看看吧', // 分享标题
					link: 'https://testdevgw.medtap.cn/wechat/lung/oauth_stepEnd?type=DISEASE_MANUAL_WHOLE_EBOOK&bookId=' + book.bookId,
					imgUrl: book.imgUrl, // 分享图标
					success: function() {
						
					},
					cancel: function() {
						
					}
				});
			});
		}
	}
	book.init();
})
