var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var that;
var Interval;
var wechat;
var id;

Page({


startX: 0,
startY: 0,
isClear: false,

//-----------------------------
//	数据变量
//-----------------------------
data: {
	showModalStatus: true,
	pen: 3,
	color: '#FFFFFF',
	currentTab: 0,
	now: 1,//当前页码
	all: 10,//总页码
	height: 0,
	time: "",
	all_time: 0,
	Timer_state: 1,
	Answer_card: false,
	Draft_paper: false,
	false_analysis:false,
	stem_height: 150,
	jiexi:false,
	field:{
		analysis_text:"",
	},
	img:[],
	scroll:"scroll",
	index:0,
	drag_line:false,
},
	

//-----------------------------
//	当页面加载时
//-----------------------------
onLoad: function (options) {

	//定义变量
	that	=	this;
	id		=	options.id == undefined ? 269 : options.id;

},

onShow:function(){
	app.Check();
	wechat = wx.getStorageSync("wechat");
	that.Get_a_problem(id);
	that.get_config();
},


//-----------------------------
//	获取系统信息
//-----------------------------
get_config:function(){
	wx.getSystemInfo({
		success: function (res) {
			that.setData({
				height: res.windowHeight,
			})
		}
	});
	that.setData({
		option_box_height:Number(that.data.height)-Number(that.data.stem_height),
	})
},
//-----------------------------
//	获取问题详情
//-----------------------------
Get_a_problem:function(){
    wx.request({
		url: app.data.Home+'/index/Get_a_problem',
		data: {
			id:id,
			wechat_id:wechat.wechat_id,
		},
		success: function(res) {

			var	info	=	res.data;
			console.log(info);
			if (info.stem_text!=null){
				WxParse.wxParse('stem_text', "html", info.stem_text, that,0);
			}
			WxParse.wxParse('problem_text', "html",info.problem_text, that,0);
			
			that.setData({
				info:info,
			})
			that.check_coll();
		}
    })
},
//-----------------------------
//	点赞
//-----------------------------
on_zan:function(e){

	wx.showLoading({
		mask: true,
	})

	var a	=	e.currentTarget.dataset.a;
	var b	=	e.currentTarget.dataset.b;
	var id	=	e.currentTarget.dataset.id;

	wx.showLoading();
	wx.request({
		url: app.data.Home + '/index/analysis_on_zan',
		data: {
			id:id,
			wechat_id:wechat.wechat_id,
		},
		success: function (rs) {
			wx.hideLoading();
			if (rs.data == "Success") {
				var info = that.data.info;
				info.problem_analysis[b].analysis_zan		=	Number(info.problem_analysis[b].analysis_zan) +1;
				info.problem_analysis[b].analysis_zan_state	=	Number(1);
				that.setData({
					info: info,
				})
			}
			wx.hideLoading()
		}
	})
},
//-----------------------------
//	点赞
//-----------------------------
off_zan: function (e) {
	wx.showLoading({
		mask: true,
	})
	var a	=	e.currentTarget.dataset.a;
	var b	=	e.currentTarget.dataset.b;
	var id	=	e.currentTarget.dataset.id;
	wx.showLoading();
	wx.request({
		url: app.data.Home + '/index/analysis_off_zan',
		data: {
			id: id,
			wechat_id: wechat.wechat_id,
		},
		success: function (rs) {
			wx.hideLoading();
			if (rs.data=="Success") {
				var info	=	that.data.info;
				info.problem_analysis[b].analysis_zan		=	Number(info.problem_analysis[b].analysis_zan) - 1;
				info.problem_analysis[b].analysis_zan_state	=	Number(2);
				that.setData({
					info:info,
				})
			}
			wx.hideLoading();
		}
	})

},
//---------------------------------
//	图片预览
//---------------------------------
Picture_preview: function (res) {
	var src = res.currentTarget.dataset.src;
	var urls = res.currentTarget.dataset.urls;
	//图片预览
	wx.previewImage({
		current: src, // 当前显示图片的http链接
		urls: urls // 需要预览的图片http链接列表
	})
},



//-----------------------------
//	上一个
//-----------------------------
prev: function (e) {
	that.guodu();
	var index	=	that.data.index-1;
	if (index < 0) {
		return false;
	}
	this.setData({
		index:index,
		now:index+1,
	});
	if(that.data.false_analysis == false){
		that.Get_a_problem(that.data.list[index].problem_id);
	}else{
		that.Get_a_problem(that.data.score_wrong_problem[index]);
	}
},
//-----------------------
//	下一个
//-----------------------
next: function (e) {
	
	that.guodu();

	var index	=	that.data.index+1;
	if (index	==	(that.data.all) ) {
		return false;
	}
	this.setData({
		index:index,
		now:index+1,
	});
	if(that.data.false_analysis == false){
		that.Get_a_problem(that.data.list[index].problem_id);
	}else{
		that.Get_a_problem(that.data.score_wrong_problem[index]);
	}
},
//-----------------------
//	点击数字跳转页面
//-----------------------
Continue:function(e){
	that.guodu();
	var index	=	e.currentTarget.dataset.index;
	this.setData({
		index:index,
		now:index+1,
	});
	if(that.data.false_analysis == true){
		var all	=	that.data.list.length;
		that.setData({
			false_analysis: false,
			all: all,
		})
	}
	that.change_answer_card();
	that.Get_a_problem(that.data.list[index].problem_id);
},
//-----------------------
//	点击进入下一题
//-----------------------
click: function (res) {
	that.guodu();
	var eq			=	res.currentTarget.dataset.index;
	var option		=	res.currentTarget.dataset.select;
	var my_answer	=	that.data.my_answer;
	my_answer[eq]	=	option;
	var index	=	that.data.index+1;
	if (index	==	(that.data.all) ) {
		that.setData({
			my_answer:my_answer,
		})
		that.change_answer_card();
		return false;
	}
	this.setData({
		index:index,
		now:index+1,
		my_answer:my_answer,
	});
	if(that.data.false_analysis == false){
		that.Get_a_problem(that.data.list[index].problem_id);
	}else{
		that.Get_a_problem(that.data.score_wrong_problem[index]);
	}
},
//-----------------------------
//	究极的过度效果
//-----------------------------
guodu:function(){
	var animation = wx.createAnimation({
		timingFunction: "linear",
		delay: 0,
	});
	this.animation = animation;
	animation.opacity(0).translateY(-50).scale(0).step(150);
	this.setData({
		animationData: animation.export()
	})
	setTimeout(function () {
		animation.opacity(1).translateY(0).scale(1).step(100);
		this.setData({
			animationData: animation
		})
	}.bind(this),150);	
},
//-----------------------------
//	查看全部解析
//-----------------------------
all_analysis: function (res) {
	that.Get_a_problem(that.data.list[0].problem_id);
	that.change_answer_card();
	var all	=	that.data.list.length;
	that.setData({
		false_analysis: false,
		now: 1,
		index:0,
		all: all,
	})
},
//-----------------------------
//	查看错误解析
//-----------------------------
false_analysis:function(e){
	that.Get_a_problem(that.data.score_wrong_problem[0]);
	var all = that.data.score_wrong_problem.length;
	that.setData({
		false_analysis: true,
		now:1,
		index:0,
		all: all,
	})
	that.change_answer_card();
},
//-----------------------------
//	切换答题卡状态  开启 关闭
//-----------------------------
change_answer_card: function () {
	var card = that.data.Answer_card;
	if (card == true) {
		that.setData({
			Answer_card: false,
		})
	} else {
		that.setData({
			Answer_card: true,
		})
	}
},
//-----------------------------
//	草稿纸
//-----------------------------
shows: function () {
	console.log(that.data.Draft_paper);
	if (that.data.Draft_paper == false) {
		that.setData({
			Draft_paper: true,
		})
		this.util('open');
	} else {
		that.setData({
			Draft_paper: false,
			showModalStatus: false
		})
	}
},
util: function (currentStatu) {
	var animation = wx.createAnimation({
		duration: 200,
		timingFunction: "linear",
		delay: 0,
	});
	this.animation = animation;
	animation.opacity(0).rotateX(-100).step();
	this.setData({
		animationData: animation.export()
	})
	setTimeout(function () {
		animation.opacity(1).rotateX(0).step();
		this.setData({
			animationData: animation
		})
		if (currentStatu == "close") {
			this.setData({ showModalStatus: false });
		}
	}.bind(this), 200)
	// 显示  
	if (currentStatu == "open") {
		this.setData({
			showModalStatus: true
		});
	}
},
//-----------------------------
//	手机触摸开始
//-----------------------------
touchStart: function (e) {
	//得到触摸点的坐标
	this.startX = e.changedTouches[0].x
	this.startY = e.changedTouches[0].y
	this.context = wx.createContext()
	this.context.setStrokeStyle(this.data.color)
	this.context.setLineWidth(this.data.pen)
	this.context.setLineCap('round') // 让线条圆润
	this.context.beginPath()

},
//-----------------------------
//	触摸后移动
//-----------------------------
touchMove: function (e) {

	var startX1 = e.changedTouches[0].x
	var startY1 = e.changedTouches[0].y
	this.context.moveTo(this.startX, this.startY)
	this.context.lineTo(startX1, startY1)
	this.context.stroke()
	this.startX = startX1;
	this.startY = startY1;
	wx.drawCanvas({
		canvasId: 'myCanvas',
		reserve: true,
		actions: this.context.getActions()
	})
},
penSelect: function (e) {
	this.setData({ pen: parseInt(e.currentTarget.dataset.param) });
	this.isClear = false;
},
colorSelect: function (e) {
	this.setData({ color: e.currentTarget.dataset.param });
	this.isClear = false;
},
cleardraw: function (e) {
	this.setData({
		showModalStatus: false
	});
	this.setData({
		showModalStatus: true
	});
},
//-----------------------------
//	我要解析 显示隐藏
//-----------------------------
I_want_to_parse:function(e){
	var jiexi	=	that.data.jiexi;
	if (jiexi == false){
		that.setData({
			jiexi:true,
			problem_id: that.data.info.problem_id,
		})
	}else{
		that.setData({
			jiexi:false,
			problem_id:"",
		})
	}
},
//-----------------------------
//	打字
//-----------------------------
dazi:function(e){

	var value	=	e.detail.value;
	var name	=	e.currentTarget.dataset.name;
	that.data.field[name] = value;

	that.setData({
		field:that.data.field
	});
},
//--------------------------
//	 上传
//--------------------------
upload: function () {
	wx.showLoading({
		mask:true,
	})
	wx.chooseImage({
		count: 1, // 默认9
		sizeType: ['original', 'compressed'],
		sourceType: ['album', 'camera'],
		success: function (res) {
			var tempFilePaths = res.tempFilePaths
			wx.uploadFile({
				url: getApp().data.Home + "/Comment/upload",
				filePath: tempFilePaths[0],
				name: 'file',
				header: { 'content-type': 'multipart/form-data' },
				success: function (res) {
					wx.hideLoading();
					var len = that.data.img.length;
					if (len >= 3) {
						wx.showModal({
							title: app.data.error_title,
							content: '都说了不要超过三张了',
							showCancel: false,
						})
						return false;
					}
					if (len == 0) {
						that.data.img[0] = res.data;
						that.setData({
							img: that.data.img
						})
					} else {
						that.data.img[len] = res.data;
						that.setData({
							img: that.data.img
						})
					}
				}
			})
		}
	})
},
//-----------------------------
//	提交解析
//-----------------------------
submit:function(e){
	wx.showLoading({
		mask:true,
	})
	wx.request({
		url: app.data.Home + '/index/submit_analysis',
		data: {
			analysis_author: wechat.wechat_id,
			analysis_problem: that.data.problem_id,
			analysis_text: that.data.field.analysis_text == undefined ? "" : that.data.field.analysis_text,
			analysis_img: that.data.img == undefined ? "" : that.data.img,
		},
		method:"post",
		success: function (rs) {
			wx.hideLoading();
			if(rs.data == 'Success'){
				that.Get_a_problem(id);
				wx.showModal({
					content: '提交成功',
					showCancel: false,
					success: function (res) {
						that.Get_a_problem();
						that.setData({
							jiexi: false,
							field: {
								analysis_text: "",
							},
							img: [],
							problem_id:"",
						})
					}
				})
			}
		}
	})
},
//-----------------------------
//	判断收藏
//-----------------------------
check_coll:function(){
	var	id	=	that.data.info.problem_id;
	wx.request({
		url: app.data.Home + '/index/check_coll',
		data: {
			wechat_id:wechat.wechat_id,
			id:id,
		},
		success: function (rs) {
			if(rs.data == "Success"){
				that.setData({
					check_coll:true,
				})
			}else{
				that.setData({
					check_coll:false,
				})
			}
		}
	})
},
//-----------------------------
//	是否收藏
//-----------------------------
coll:function(){
	wx.showLoading({
		mask:true,
	})
	var	id	=	that.data.info.problem_id;
	wx.request({
		url: app.data.Home + '/index/coll',
		data: {
			wechat_id:wechat.wechat_id,
			id:id,
		},
		success: function (rs) {
			wx.hideLoading();
			if (rs.data == "Success") {
				that.setData({
					check_coll: true,
				})
				wx.showToast({
					title: '已收藏',
					icon: 'success',
					duration: 2000
				})
			} else {
				that.setData({
					check_coll: false,
				})
				wx.showToast({
					title: '已取消',
					icon: 'success',
					duration: 2000
				})
			}
		}
	})
},
//-----------------------------
//	页面分享
//-----------------------------
onShareAppMessage: function () {
	var id = that.data.info.problem_id;
	var Share = {
		title: '怼怼公考',
		desc: '怼怼公考',
		path: "/pages/problem/problem?id=" + id,
	};
	return Share;
},
//-----------------------------
//	重置
//-----------------------------
reset: function () {
	that.setData({
		jiexi: false,
		img: [],
		problem_id: "",
	})
},
//-----------------------------
//	过渡效果
//-----------------------------
drag_line: function () {
	var drag_line = that.data.drag_line;
	if (drag_line) {
		that.setData({
			drag_line: false,
		})
	} else {
		that.setData({
			drag_line: true,
		})
	}
	that.change();
},
//-----------------------------
//	究极的过度效果
//-----------------------------
change: function (state) {
	var animation = wx.createAnimation({
		timingFunction: "linear",
		delay: 0,
	});
	that.animation = animation;
	var drag_line = that.data.drag_line;
	var height = that.data.height - 129;

	if (drag_line) {
		animation.height(height).step(150);
		that.setData({
			animationData2: animation.export()
		})
	} else {
		animation.height(app.data.stem_height).step(150);
		that.setData({
			animationData2: animation.export()
		})
	}
},


//-----------------------------
//	页面分享
//-----------------------------
onShareAppMessage: function () {
	return app.Share();
},

})