<template>
	<view class="container">
		<div class="weui-tab__content" id="smb_view" style="display: block;display: none;">
			<div style="margin-top: 50px;">
				<div class=" weui-cells_form" style="border:none !important;">
					<img style="width: 30vw;margin: 0vh 30vw;" src="/mpwechat/img/success.png" alt="" srcset="">
					<div style="font-size: 20px;margin: 30px 0 20vh 0;">
						<label id="smbcontext"></label>
					</div>
					<div class="weui-btn-area"> <a onclick="knowCloseWindow()" href="javascript:"
							class="weui-btn weui-btn_primary">知道了</a></div>
				</div>
			</div>
		</div>

		<div class="weui-tab__content" id="form_checkuser" style="display: block;display: none;">
			<div style="margin-top: 50px;">
				<div class="weui-cells weui-cells_form">
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
						<div class="weui-cell__bd"> <input id="stuname" class="weui-input" type="text" required=""
								pattern="REG_IDNUM" maxlength="18" placeholder="请输入孩子/老师姓名" style="text-align:right;"
								emptytips="请输入孩子/老师姓名" notmatchtips="请输入孩子/老师姓名">
						</div>
						<div class="weui-cell__ft"> <i class="weui-icon-warn"></i> </div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">手机号</label></div>
						<div class="weui-cell__bd"> <input id="phonenumber" class="weui-input" type="tel" required=""
								pattern="^\d{11}$" maxlength="11" placeholder="请输入签约手机号" style="text-align:right;"
								emptytips="请输入签约手机号" notmatchtips="请输入签约手机号">
						</div>
						<div class="weui-cell__ft"> <i class="weui-icon-warn"></i> </div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label style="width: 69px;" class="weui-label">验证码</label></div>
						<div class="weui-cell__bd"> <input id="checkcode" style="width:100%;text-align: right;"
								class="weui-input" maxlength="6" type="number" required="" pattern="REG_VCODE"
								placeholder="输入验证码" tips="输入验证码"> </div>
						<div class="weui-cell__ft"> <i class="weui-icon-warn"> </i><a id="sendcodebtn"
								href="javascript:;" onclick="sendsmscode(this)"
								style="font-size: 15px;margin-left: 10px;">获取验证码</a>
						</div>
					</div>
				</div>

				<div class="weui-btn-area"> <a id="formSubmitBtn" onclick="subcheckuser(this)" href="javascript:"
						class="weui-btn weui-btn_primary">立即验证</a>
				</div>
			</div>
		</div>

		<div class="weui-tab__content" id="credit_view" style="display: block;display: none;">
			<div style="margin-top: 20px;padding-bottom:50px;">
				<div class="weui-cells">
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">学校</label></div>
						<div class="weui-cell__bd">
							<label id="schname"></label>
						</div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">姓名</label></div>
						<div class="weui-cell__bd">
							<label id="username"></label>
						</div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">用户类型</label></div>
						<div class="weui-cell__bd">
							<label id="usertype"></label>
						</div>
					</div>

					<div class="weui-cell" id="gcinfo_dev" style="display: none;">
						<div class="weui-cell__hd"><label class="weui-label">年级/班级</label></div>
						<div class="weui-cell__bd">
							<label id="gcinfo"></label>
						</div>
					</div>

					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">信用金金额</label></div>
						<div class="weui-cell__bd">
							<label id="amount"></label>
						</div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">缴费方式</label></div>
						<div class="weui-cell__bd">
							<label id="soutype"></label>
						</div>
					</div>
					<div class="weui-cell">
						<div class="weui-cell__hd"><label class="weui-label">缴费时间</label></div>
						<div class="weui-cell__bd">
							<label id="paytime"></label>
						</div>
					</div>
					<div class="weui-cell" id="outno_dev" style="display: none;flex-wrap: wrap;">
						<div class="weui-cell__hd"><label class="weui-label" style="width:65px;">商户单号</label></div>
						<div class="weui-cell__bd" style="font-size:11px;">
							<label id="outno"></label>
						</div>
					</div>
					<div class="weui-cell" id="wxno_dev" style="display: none;flex-wrap: wrap;">
						<div class="weui-cell__hd"><label class="weui-label" style="width:65px;">微信单号</label></div>
						<div class="weui-cell__bd" style="font-size:11px;">
							<label id="wxno"></label>
						</div>
					</div>
				</div>

				<div class="weui-btn-area">
					<a id="applyrefoundbtn" href="javascript:" onclick="subrefund(this)"
						class="weui-btn weui-btn_primary">申请退款</a>
				</div>
				<div id="norftext" class="weui-btn-area" style="display: none;">
					<a id="applyrefoundbtn" href="javascript:" onclick="norefound(this)"
						style="color:#348FF6;display: block;font-size:14px;position: fixed;text-align: center;bottom: 3vh;width: 100%;left: 0;">为什么我无法申请退款？</a>
				</div>
			</div>
		</div>
	</view>
</template>

<script>
	//公共参数
	let gHost = "https://" + window.location.host;
	if (window.location.protocol != "https:") {
		gHost = "http://" + window.location.host;
	}
	let sessionid = "";
	let binduserid = 0;
	let msgid = "";
	let curCount = 60;
	export default {
		name:"CreditRefund",
		data() {
			return {

			}
		},
		created: function() {
			console.log("created");
		},
		mounted: function() {
			let this_ = this
			this_.$nextTick(function() {
				let loading = weui.loading('玩命加载中.');
				let params = this_.parseUrl();
				if (!params.code || params.code == "" || !params.state || params.state == "") {
					loading.hide();
					weui.alert('请通过微信扫一扫学校退款二维码打开！', function() {
						console.log('ok');
					}, {
						title: '提示'
					});
					return;
				}
				let ua = navigator.userAgent.toLowerCase();
				if (ua.indexOf('micromessenger') == -1) {
					// 非微信浏览器H5支付
					loading.hide();
					weui.alert('请通过微信浏览器打开！', function() {
						console.log('ok');
					}, {
						title: '错误'
					});
					return;
				}
				console.log(params);
              $.ajax({
				url: gHost + '/mp_wechat/x/access_token',
				type: "POST",
				dataType: "json",
				contentType: "application/x-www-form-urlencoded",
				async: false,
				data: params,
				success: function(resp1) {
					if (resp1.code != "10000") {
						loading.hide();
						weui.alert(resp1.message, function() {
							console.log('ok');
							wx.closeWindow();
						}, {
							title: '错误'
						});
					} else {
						sessionid = resp1.results
						//查询联合关联关系
						$.ajax({
							url: gHost + '/mp_wechat/svmpwechat/QueryBindInfoByUnionid',
							type: "POST",
							dataType: "json",
							contentType: "application/x-www-form-urlencoded",
							async: false,
							data: {
								"mp_session_id": sessionid
							},
							success: function(resp2) {
								console.log(resp2)
								if (resp2.code != "10000") {
									loading.hide();
									weui.alert(resp2.message, function() {
										console.log('ok');
										wx.closeWindow();
									}, {
										title: '提示'
									});
								} else {
									if (resp2.results.islist) { //多人
										var myArray=new Array();
										for ( var i = 0; i <resp2.results.bindlist.length; i++){
											var item  = new Object();
											item.label = resp2.results.bindlist[i].username;
											item.value = resp2.results.bindlist[i].userid;
											myArray.push(item);
										}
									
										loading.hide();
										weui.picker(myArray, {
											defaultValue: [0],
											className: 'custom-classname',
											onChange: function onChange(result) {
												//console.log(item, index);
												//console.log(result[0].value);
											},
											onConfirm: function onConfirm(result) {
												console.log('confirm');
												console.log(result[0].value);
												
												binduserid = result[0].value;
												
												var loading2 = weui.loading('玩命加载中.');
												//查询待退款的信用金信息
												$.ajax({
													url: gHost + '/mp_wechat/svmpwechat/QueryCreditInfo',
													type: "POST",
													dataType: "json",
													contentType: "application/x-www-form-urlencoded",
													async: false,
													data: {
														"mp_session_id": sessionid,
														"userid": binduserid
													},
													success: function(resp3) {
														console.log(resp3)
														if (resp3.code != "10000") {
															loading2.hide();
															weui.alert(resp3.message, function() {
																console.log('ok');
																wx.closeWindow();
															}, {
																title: '提示'
															});
														} else {
															if (resp3.results.status == 1) {
																loading2.hide();
																$("#smbcontext").text("退款申请已提交，审核通过后，预计1-7个工作日通过商户退款或现金红包形式退款到当前微信账户！")
																$("#smb_view").show()
															} else {
																$("#schname").text(resp3.results.schoolname)
																$("#username").text(resp3.results.username)
																var utstr = resp3.results.usertype == "1" ? "学生" : "教职工";
																$("#usertype").text(utstr);
																if (resp3.results.usertype == "1") {
																	$("#gcinfo_dev").show();
																	$("#gcinfo").text(resp3.results.gradename + "/" + resp3.results.classname)
																}
																$("#amount").text("￥" + (resp3.results.amount / 100).toFixed(2))
																var soustr = resp3.results.sourcetype == "1" ? "现金缴纳" : resp3.results.sourcetype == "2" ?
																	"微信支付" : resp3.results.sourcetype == "3" ? "生活费抵扣" : "免交信用金"
																$("#soutype").text(soustr)
																$("#paytime").text(resp3.results.paytime)
																if (resp3.results.sourcetype != "2") {
																	$("#applyrefoundbtn").css("opacity", "0.3");
																	$("#applyrefoundbtn").css("pointer-events", "none");
																	$("#norftext").show();
																}
																if (resp3.results.outno != "") {
																	$("#outno").text(resp3.results.outno)
																	$("#outno_dev").show()
																}
																if (resp3.results.wxno != "") {
																	$("#wxno").text(resp3.results.wxno)
																	$("#wxno_dev").show()
																}
												
																loading2.hide();
																$("#credit_view").show();
															}
														}
													},
													error: function(err3) {
														loading2.hide();
														weui.alert(err3.toString(), function() {
															console.log('ok');
															wx.closeWindow();
														}, {
															title: 'ERROR'
														});
													}
												});
											},
											id: 'picker',
											title: '当前手机号存在多个关联用户，请选择其中一个'
										});
										return
									} else { //单人
										if (resp2.results.bindflag) {
											binduserid = resp2.results.userid;
											//查询待退款的信用金信息
											$.ajax({
												url: gHost + '/mp_wechat/svmpwechat/QueryCreditInfo',
												type: "POST",
												dataType: "json",
												contentType: "application/x-www-form-urlencoded",
												async: false,
												data: {
													"mp_session_id": sessionid,
													"userid": binduserid
												},
												success: function(resp3) {
													console.log(resp3)
													if (resp3.code != "10000") {
														loading.hide();
														weui.alert(resp3.message, function() {
															console.log('ok');
															wx.closeWindow();
														}, {
															title: '提示'
														});
													} else {
														if (resp3.results.status == 1) {
															loading.hide();
															$("#smbcontext").text("退款申请已提交，审核通过后，预计1-7个工作日通过商户退款或现金红包形式退款到当前微信账户！")
															$("#smb_view").show()
														} else {
															$("#schname").text(resp3.results.schoolname)
															$("#username").text(resp3.results.username)
															var utstr = resp3.results.usertype == "1" ? "学生" : "教职工";
															$("#usertype").text(utstr);
															if (resp3.results.usertype == "1") {
																$("#gcinfo_dev").show();
																$("#gcinfo").text(resp3.results.gradename + "/" + resp3.results.classname)
															}
															$("#amount").text("￥" + (resp3.results.amount / 100).toFixed(2))
															var soustr = resp3.results.sourcetype == "1" ? "现金缴纳" : resp3.results.sourcetype == "2" ?
																"微信支付" : resp3.results.sourcetype == "3" ? "生活费抵扣" : "免交信用金"
															$("#soutype").text(soustr)
															$("#paytime").text(resp3.results.paytime)
															if (resp3.results.sourcetype != "2") {
																$("#applyrefoundbtn").css("opacity", "0.3");
																$("#applyrefoundbtn").css("pointer-events", "none");
																$("#norftext").show();
															}
															if (resp3.results.outno != "") {
																$("#outno").text(resp3.results.outno)
																$("#outno_dev").show()
															}
															if (resp3.results.wxno != "") {
																$("#wxno").text(resp3.results.wxno)
																$("#wxno_dev").show()
															}

															loading.hide();
															$("#credit_view").show();
														}
													}
												},
												error: function(err3) {
													loading.hide();
													weui.alert(err3.toString(), function() {
														console.log('ok');
														wx.closeWindow();
													}, {
														title: 'ERROR'
													});
												}
											});
										} else {
											loading.hide();
											//没有任何绑定的人
											weui.confirm('此微信不是绑定该学生（教师）的微信，需要验证后进行退款！', function() {
												console.log('yes');
												$("#form_checkuser").show();
											}, function() {
												console.log('no');
												wx.closeWindow();
											}, {
												title: '提示'
											});
										}
									}
								}
							},
							error: function(err2) {
								loading.hide();
								weui.alert(err2.toString(), function() {
									console.log('ok');
									wx.closeWindow();
								}, {
									title: 'ERROR'
								});
							}
						});
					}
				},
				error: function(err) {
					loading.hide();
					weui.alert(err.toString(), function() {
						console.log('ok');
					}, {
						title: 'ERROR'
					});
				}
			});
			})
			console.log("mounted");
		},
		methods: {
			//获取验证码
			 sendsmscode(self) {
				curCount = 60;
				var phonestr = $("#phonenumber").val()
				if (phonestr == "") {
					weui.topTips('手机号码不能为空', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
				if (!isPoneAvailable(phonestr)) {
					weui.topTips('手机号码格式不正确', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
				var loading = weui.loading('请稍后');
			
				$.ajax({
					url: gHost + '/mp_wechat/svmpwechat/SendSMSAuthCode',
					type: "POST",
					dataType: "json",
					contentType: "application/x-www-form-urlencoded",
					async: false,
					data: {
						"mp_session_id": sessionid,
						"phone": phonestr
					},
					success: function(resp3) {
						console.log(resp3)
						if (resp3.code != "10000") {
							loading.hide();
							weui.alert(resp3.message, function() {
								console.log('ok');
								wx.closeWindow();
							}, {
								title: '提示'
							});
						} else {
							loading.hide();
							$("#sendcodebtn").css("pointer-events", "none");
							$("#sendcodebtn").css("color", "dimgrey");
							$("#sendcodebtn").html(curCount + "秒后重新获取");
							msgid = resp3.message;
							InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
			
						}
					},
					error: function(err3) {
						loading.hide();
						weui.alert(err3.toString(), function() {
							console.log('ok');
							wx.closeWindow();
						}, {
							title: 'ERROR'
						});
					}
				});
			},
			//验证信息
			 subcheckuser(self) {
				var phonestr = $("#phonenumber").val()
				if (phonestr == "") {
					weui.topTips('手机号码不能为空', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
				if (!isPoneAvailable(phonestr)) {
					weui.topTips('手机号码格式不正确', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
				var checkcode = $("#checkcode").val()
				if (checkcode == "") {
					weui.topTips('验证码不能为空', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
				var stuname = $("#stuname").val()
				if (stuname == "") {
					weui.topTips('姓名不能为空', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
			
				if (msgid == "") {
					weui.topTips('请先获取验证码', {
						duration: 3000,
						className: "custom-classname",
						callback: function callback() {
							console.log('close');
						}
					});
					return;
				}
			
				var loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/mp_wechat/svmpwechat/CheckSMSAuthCode',
					type: "POST",
					dataType: "json",
					contentType: "application/x-www-form-urlencoded",
					async: false,
					data: {
						"mp_session_id": sessionid,
						"phone": phonestr,
						"msgid": msgid,
						"code": checkcode,
					},
					success: function(resp3) {
						console.log(resp3)
						if (resp3.code != "10000") {
							loading.hide();
							weui.alert(resp3.message, function() {
								console.log('ok');
								wx.closeWindow();
							}, {
								title: '提示'
							});
						} else {
							//验证通过，查询人
							$.ajax({
								url: gHost + '/mp_wechat/svmpwechat/QueryUserIDByPhone',
								type: "POST",
								dataType: "json",
								contentType: "application/x-www-form-urlencoded",
								async: false,
								data: {
									"mp_session_id": sessionid,
									"phone": phonestr,
									"username": stuname,
								},
								success: function(resp4) {
									console.log(resp4)
									if (resp4.code != "10000") {
										loading.hide();
										weui.alert(resp4.message, function() {
											console.log('ok');
											wx.closeWindow();
										}, {
											title: '提示'
										});
									} else {
										if (resp4.results.bindflag) {
											binduserid = resp4.results.userid;
											//查询待退款的信用金信息
											$.ajax({
												url: gHost + '/mp_wechat/svmpwechat/QueryCreditInfo',
												type: "POST",
												dataType: "json",
												contentType: "application/x-www-form-urlencoded",
												async: false,
												data: {
													"mp_session_id": sessionid,
													"userid": binduserid
												},
												success: function(resp5) {
													console.log(resp5)
													if (resp5.code != "10000") {
														loading.hide();
														weui.alert(resp5.message, function() {
															console.log('ok');
															wx.closeWindow();
														}, {
															title: '提示'
														});
													} else {
			
														$("#form_checkuser").hide();
			
														if (resp5.results.status == 1) {
															loading.hide();
															$("#smbcontext").text("退款申请已提交，审核通过后，预计1-7个工作日通过商户退款或现金红包形式退款到当前微信账户！")
															$("#smb_view").show()
														} else {
															$("#schname").text(resp5.results.schoolname)
															$("#username").text(resp5.results.username)
															var utstr = resp5.results.usertype == "1" ? "学生" : "教职工";
															$("#usertype").text(utstr);
															if (resp5.results.usertype == "1") {
																$("#gcinfo_dev").show();
																$("#gcinfo").text(resp5.results.gradename + "/" + resp5.results.classname)
															}
															$("#amount").text("￥" + (resp5.results.amount / 100).toFixed(2))
															var soustr = resp5.results.sourcetype == "1" ? "现金缴纳" : resp5.results.sourcetype == "2" ?
																"线上支付" : resp5.results.sourcetype == "3" ? "钱包抵扣" : "免交信用金"
															$("#soutype").text(soustr)
															$("#paytime").text(resp5.results.paytime)
															if (resp5.results.sourcetype != "2") {
																$("#applyrefoundbtn").css("opacity", "0.3");
																$("#applyrefoundbtn").css("pointer-events", "none");
																$("#norftext").show();
															}
															if (resp5.results.outno != "") {
																$("#outno").text(resp5.results.outno)
																$("#outno_dev").show()
															}
															if (resp5.results.wxno != "") {
																$("#wxno").text(resp5.results.wxno)
																$("#wxno_dev").show()
															}
			
			
															$("#credit_view").show();
														}
														loading.hide();
													}
												},
												error: function(err5) {
													loading.hide();
													weui.alert(err5.toString(), function() {
														console.log('ok');
														wx.closeWindow();
													}, {
														title: 'ERROR'
													});
												}
											});
										} else {
											loading.hide();
											weui.alert("验证信息有误，请核实后重新进行验证！", function() {
												console.log('ok');
												wx.closeWindow();
											}, {
												title: '提示'
											});
										}
									}
								},
								error: function(err4) {
									loading.hide();
									weui.alert(err4.toString(), function() {
										console.log('ok');
										wx.closeWindow();
									}, {
										title: 'ERROR'
									});
								}
							});
			
						}
					},
					error: function(err3) {
						loading.hide();
						weui.alert(err3.toString(), function() {
							console.log('ok');
							wx.closeWindow();
						}, {
							title: 'ERROR'
						});
					}
				});
			},
			//申请退款
			subrefund(self) {
				if (binduserid < 1) {
					weui.alert("非法请求", function() {
						console.log('ok');
						wx.closeWindow();
					}, {
						title: '错误'
					});
					return
				}
				let loading = weui.loading('请稍后');
				$.ajax({
					url: gHost + '/mp_wechat/svmpwechat/ApplyCreditRefund',
					type: "POST",
					dataType: "json",
					contentType: "application/x-www-form-urlencoded",
					async: false,
					data: {
						"mp_session_id": sessionid,
						"userid": binduserid,
					},
					success: function(resp) {
						console.log(resp)
						if (resp.code != "10000") {
							loading.hide();
							weui.alert(resp.message, function() {
								console.log('ok');
								wx.closeWindow();
							}, {
								title: '提示'
							});
						} else {
							loading.hide();
							$("#credit_view").hide();
							$("#smbcontext").text("退款申请已提交，审核通过后，预计1-7个工作日通过商户退款或现金红包形式退款到当前微信账户！")
							$("#smb_view").show()
						}
					},
					error: function(err) {
						loading.hide();
						weui.alert(err.toString(), function() {
							console.log('ok');
							wx.closeWindow();
						}, {
							title: 'ERROR'
						});
					}
				});
			},
			//timer处理函数
			SetRemainTime() {
				if (curCount == 0) {
					window.clearInterval(InterValObj); //停止计时器
					$("#sendcodebtn").css("pointer-events", "");
					$("#sendcodebtn").css("color", "");
					$("#sendcodebtn").html("获取验证码");
				} else {
					curCount--;
					$("#sendcodebtn").html(curCount + "秒后重新获取");
				}
			},
			norefound(self) {
				weui.alert("信用金缴纳方式为现金缴纳的，仅支持线下现金退款，请联系学校管理员！", function() {
					console.log('ok');
					wx.closeWindow();
				}, {
					title: '无法申请退款'
				});
			},

			parseUrl() {
				var searchHref = window.location.search.replace('?', '');
				var params = searchHref.split('&');
				var returnParam = {};
				params.forEach(function(param) {
					var paramSplit = param.split('=');
					returnParam[paramSplit[0]] = paramSplit[1];
				});
				return returnParam;
			},
			// 判断是否为手机号
			isPoneAvailable(pone) {
				var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(pone)) {
					return false;
				} else {
					return true;
				}
			},
			//关闭窗口
			knowCloseWindow() {
				wx.closeWindow();
			}
		}
	}
</script>

<style scoped>
	.weui-tab__content {
		width: 86vw;
		margin-left: 7vw;
	}

	.weui-label,
	.weui-input {
		font-size: 15px;
	}

	.weui-btn {
		font-size: 17px !important;
		width: 60vw !important;
		z-index: 999;
		letter-spacing: 2px;
		line-height: 50px;
		height: 50px;
		padding: 0;
		position: fixed;
		bottom: 8vh;
		left: 20vw;
		margin: 0 !important;
	}

	.weui-icon-btn_close {
		display: none;
	}

	.weui-cell:before {
		left: 0;
	}

	#weui-picker-confirm {
		bottom: 20px !important;
	}

	.weui-cell {
		padding: 16px 0;
	}

	.weui-cell__bd {
		font-size: 14px;
		text-align: right;
		color: gray;
	}
</style>
