define(['zepto', 'medtap', '../javaScript/beforeSubmit.js'], function($, medtap, beforeSubmit) {
	var detail = {
		articleId: medtap.getRequest('articleId'),
		agencyId: medtap.getRequest('agencyId'),
		newAgencyId: '',
		wechatId: medtap.getRequest('wechatId'),
		doctorId: medtap.getRequest('doctorId'),
		bookTitle: '',
		bookInfo: {},
		bookId: '',
		init: function() {
			detail.getDetail();
			//detail.getUserShareInfo();
			//判断当前用户是否绑定手机号
			//detail.checkUser();

		},
		bind: function() {

		},
		getDetail: function() {
			medtap.loading(1);
			$.ajax({
				url: 'https://testdevgw.medtap.cn/operation/article/getBookArticle',
				type: 'get',
				data: {
					articleId: detail.articleId
				},
				success: function(res) {
					medtap.loading(0);
					if (res.success == true) {
						var info = res.content.articleInfoItem,
							articleCategory = info.articleCategory,
							tag = info.tag,
							title = info.articleTitle,
							content = info.content,
							readCount = info.readCount,
							createTime = info.hasOwnProperty('createTime') ? info.createTime.split(" ") : ['', ''],
							theDay = createTime[0],
							categoryName = '';
						detail.bookId = info.articleCategory;
						detail.getBook();
						var html = '<div class="title"><h4>' + title + '</h4></div>' +
							'<div class="tag_1">' +
							'<p class="module">' + categoryName + '</p>' +
							(!!tag ? ('<div class="icon"></div><p class="disease">' + tag + '</p>') : '') +
							'<div class="time"><p class="day">' + theDay + '</p></div>' +
							'</div>' +
							'<div class="main">' +
							'<div class="text"><p>' + content + '</p></div>' +

							'</div>';

						$('.body').html(html);
						if (!detail.agencyId) {

						} else {
							detail.getDoctorQrCode();
						}
						detail.title = title;
						document.title = title;
						detail.shareForWx(info)
						//设置章节id
						if (info.preId <= 0) {
							$('.last').hide();
							$('.next').attr('data-id', info.nextId);
							$('.spanWarp').prepend('<div></div>');
							$('.spanWarp').on('click', '.go', function() {
								var id = $(this).attr('data-id');
								window.location.href = '../pages/articleDetail.html?articleId=' + id + '&wechatId=' + detail.wechatId +
									'&agencyId=' + detail.agencyId + '&bookTitle=' + detail.bookTitle;
							});
						} else if (info.nextId <= 0) {
							$('.next').hide();
							$('.last').attr('data-id', info.preId);
							$('.spanWarp').append('<div></div>');
							$('.spanWarp').on('click', '.go', function() {
								var id = $(this).attr('data-id');
								window.location.href = '../pages/articleDetail.html?articleId=' + id + '&wechatId=' + detail.wechatId +
									'&agencyId=' + detail.agencyId + '&bookTitle=' + detail.bookTitle;
							});
						} else {
							$('.last').attr('data-id', info.preId);
							$('.next').attr('data-id', info.nextId);
							$('.spanWarp').on('click', '.go', function() {
								var id = $(this).attr('data-id');
								window.location.href = '../pages/articleDetail.html?articleId=' + id + '&wechatId=' + detail.wechatId +
									'&agencyId=' + detail.agencyId + '&bookTitle=' + detail.bookTitle;
							});
						}
						$('.catalog').on('click', function() {
							window.location.href = '../pages/bookDetail.html?bookId=' + info.articleCategory + '&wechatId=' +
								detail.wechatId + '&agencyId=' + detail.agencyId + '&bookTitle=' + detail.bookTitle;
						})
					}
				}
			})
		},

		//微信分享配置
		shareForWx: function(info) {
			detail.bookInfo = JSON.stringify(info);
			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'
						]
					})
					//console.log(JSON.stringify(res))
				},
				error: function() {}
			});

			//分享配置
			wx.ready(function() {
				var _this = this;
				detail.getUserShareInfo();
				var shareInfo = JSON.parse(detail.bookInfo);
				var temp2 = shareInfo.articleImg.replace(/(https:\/\/cdn.iplusmed.com\/\/)|(https:\/\/cdn.iplusmed.com\/)/g,
					'');
				var title = shareInfo.articleTitle,
					content = $('.text').text().trim().substring(0, 60),
					linkUrl = 'https://testdevgw.medtap.cn/wechat/lung/oauth_stepEnd?type=DISEASE_MANUAL_FREE_E_BOOK&articleId=' + detail.articleId,
					imgUrl = (temp2.length > 0) ? info.articleImg : 'https://cdn.iplusmed.com/static/images/user_logo.png';
				//分享给朋友
				wx.onMenuShareAppMessage({
					title: '向你推荐了《' + detail.bookDetail + '》',
					desc: '推荐章节《' + title + '》', // 分享描述
					link: linkUrl,
					imgUrl: imgUrl, // 分享图标
					type: 'link', // 分享类型,music、video或link，不填默认为link
					dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
					success: function() {
						if (!(/iphone|ipad/gi).test(navigator.userAgent)) {
							medtap.winPop('分享成功');
						}
					},
					cancel: function() {
						if (!(/iphone|ipad/gi).test(navigator.userAgent)) {
							medtap.winPop('取消分享');
						}
					}
				});
				//分享到朋友圈
				wx.onMenuShareTimeline({
					title: '向你推荐了《' + detail.bookDetail + '》', // 分享标题
					link: linkUrl,
					imgUrl: imgUrl, // 分享图标
					success: function() {
						if (!(/iphone|ipad/gi).test(navigator.userAgent)) {
							medtap.winPop('分享成功');
						}
					},
					cancel: function() {
						if (!(/iphone|ipad/gi).test(navigator.userAgent)) {
							medtap.winPop('取消分享');
						}
					}
				});
			});

		},
		checkUser: function() {
			$.ajax({
				url: 'https://testdevgw.medtap.cn/wechat/user/getWechatUser',
				type: 'get',
				headers: medtap.getHeaders(),
				contentType: "application/json",
				data: medtap.setToken(),
				success: function(res) {
					var info = res.content.wechatUser;
					if (info.hasOwnProperty('userId') && !!info['userId']) {
						$('.mod').hide();
						$('.body').css('overflow', 'scrocll');
						$('.body').css('height', 'auto');
					} else {
						$('.mod').show();
						$('.body').css('height', window.innerHeight);
						$('.body').css('overflow', 'hidden');
						$('.mod').on('click', function() {
							beforeSubmit.config({
								wechatId: detail.wechatId,
								getInfo: true,
								success: function() {
									if (detail.doctorId != '') {
										detail.attentionDoctor();
									} else {
										medtap.jzz(1);
										setTimeout(function() {
											parent.location.reload();
										}, 2000)
									}
								}
							}).init();
						})
					}
				}
			})
		},
		getBook: function() {
			medtap.submitAjax({
				url: 'https://testdevgw.medtap.cn/common/category/getCategoryByNode',
				type: 'get',
				async: true,
				data: {
					articleCategory: detail.bookId
				},
				success: function(res) {
					if (res.success == true) {
						var info = res.content.category;
						detail.bookDetail = info.content;
					}
				}
			})
		}

	}
	detail.init();
	detail.bind();
})
