var mobile;
$(function(){
	 $("#text").html("产品预约");
	 function GetQueryString(name)
	  {
	       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	       var r = window.location.search.substr(1).match(reg);
	       if(r!=null)return  unescape(r[2]); return null;
	  }

	  var uid = GetQueryString("uid");
	  var productId =GetQueryString("productId");
    	if(uid==="null"||uid===""||uid===undefined||uid===null){
    		uid=0;
    	}
    $.ajax({
    	url:baseUrl+ '/api/product/subscribe.do',
        data: {
          uid:uid,
          productId:productId
        },
        beforeSend: function() {
            $$.am_toast.innerHTML = tipTpl.loading;
            return toast.show();
          },
          complete:function(){
          	return toast.hide();
          },
        success: function(data) {
        	var basic,user;
            basic = data.data.basic;
            user = data.data.user;
            $("#productName").val(basic.productName);
            $("#productId").val(productId);
            if(user!=null){
            	$("#uname").val(user.name).show();
            	$(".uname").show();
            	mobile=user.mobile;
            }else{
            	$(".uname").hide();
            }          
        }
    });
});


// Generated by CoffeeScript 1.8.0
$(function() {
  var loadData;
  function getarg(url){
  	  var arg=url.split("#");
  	  if(arg[1]!=null&&arg[1]!=undefined){
  		  arg=arg[1].split("=");
  		  return arg[1];
  	  }else return null;
  	};
  	function GetQueryString(name)
	  {
	       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	       var r = window.location.search.substr(1).match(reg);
	       if(r!=null)return  unescape(r[2]); return null;
	  };
  	var uid = GetQueryString("uid");
  	var productId =GetQueryString("productId");
	if(uid==="null"||uid===""||uid===undefined||uid===null){
		uid=0;
	}
  loadData = (function(_this) {
    return function() {
    		if (!isNotNull($("#username")[0], "名字")) {
    				return;
    	      } else if (!isMobile($("#phone")[0], "手机号")){
    	    	  	return;
    	      } else if (!isNumber($("#orderMount")[0],"预约金额")) {
    	    	  	return;
    	      } else if(!tooSmall($("#orderMount")[0],"预约金额")){
    	    	  return;
    	      }
      $.ajax({
        url: _this.baseUrl + '/api/order/add_order.do',
        type:"post",
        data: {
        	mobile:$("#phone").val(),
        	userName:$("#username").val(),
        	orderMount:$("#orderMount").val(),
        	productId:$("#productId").val(),
        	description:$("#description").val(),
        	introducerUid:uid,
        	orgin:4
        },
        beforeSend: function() {
            $$.am_toast.innerHTML = tipTpl.loading;
            return toast.show();
          },
          complete:function(){
          	return toast.hide(800);
          },
        success: function(obj) {
        	
          if (obj.status === 200) {
        	  //预约成功，短信通知理财师
        	  if(typeof(mobile!=undefined)&&mobile!="null"&&mobile!=""){
        		  var content=$("#username").val()+" "+$("#phone").val()+" 通过您分享的链接已经成功预约  "+$("#productName").val()+",请及时与他/她联系。【金客】"
        		  $.ajax({
            		  url:baseUrl+"/api/user/send_remind_sms.do",
            		  type:"post",
            		  data:{
            			 mobile:mobile,
            			 content:content
            		  },
            		  callback:function(){
            			  localStorage.clear();
            		  }
            	  });
        	  }else{
        	  }
        	 
        	  setTimeout(function(){
        		  toast.success({msg:"预约成功"});
        	  },1000);
        	  setTimeout(function(){
        		  location.href="./retweetedAppointmentSuc.html?uid="+uid+"&productId="+$("#productId").val()+"&productName="+escape($("#productName").val())+"&amount="+$("#orderMount").val()
        	  },2000);
          }
        }
      });
      return false;
    };
  })(this);

  $(function() {
    $(".toast").hide();	
    return $(".am-button").on("click", function() {
      return loadData();
    });
  });
});

