//页面上需要包含隐藏字段


/* 打开弹出层 */
function showDiv(objId, divId, store_id, shop_id,goods_id,approve_way,allot_id) {
	// alert(approve_way);
	 var obj = document.getElementById(objId);	//打开位置
	
	 document.getElementById('app_store_id').value = store_id;
	 document.getElementById('app_shop_id').value = shop_id;
	 document.getElementById('approve_way').value = approve_way;
	 document.getElementById('app_goods_id').value = goods_id;
	 document.getElementById('app_allot_id').value = allot_id;
	
	 var user_id = document.getElementById('user_id').value;
	 var user_mobile = document.getElementById('user_mobile').value;
	 
	
	 if ( approve_way == 1 && user_id != '' ) {  //自动审批，直接发送
		   
			doSendVipTo(store_id, user_id, user_mobile, shop_id,goods_id,approve_way, allot_id);
			return ;
	 }

	 hs.htmlExpand(obj, { contentId: divId } );    	
}
function showRecommend(objId,divId,url,good_name,av_time) {

	var obj = document.getElementById(objId);	//打开位置
	hs.htmlExpand(obj, { contentId: divId } );    	
	 document.getElementById('recommend').innerText = url;
	 document.getElementById('good_name_1').innerText = good_name;
	 document.getElementById('av_time_1').innerText = av_time; /* */
}
function showVipForm() {
	var cbox = document.getElementById('ifRealVip');
	var vipInfo = document.getElementById('realVipInfo'); 
	if ( cbox.checked == true ) {
		vipInfo.style.display = "block";
		
	} else {
		vipInfo.style.display = "none";
	}	
}

/* 关闭弹出层 */
function closeIt(obj) { 
	
	 if (!obj )  obj = document.getElementById('closeId');
	
	 hs.close(obj); //关闭弹出页面	    	
}

function doGetVipAppInfo() {
	var store_id = document.getElementById('app_store_id').value ;
	var shop_id = document.getElementById('app_shop_id').value;
	var goods_id = document.getElementById('app_goods_id').value;
	var allot_id = document.getElementById('app_allot_id').value;
	var user_id = document.getElementById('user_id').value;
	var approve_way = document.getElementById('approve_way').value  ;
	var real_vip_code = document.getElementById('real_vip_code').value;
	var real_vip_owner = document.getElementById('real_vip_owner').value;
	var user_mobile = document.getElementById('user_mobile').value;
	
	var sqlParam = 'pass_status,create_time';  	
  	var table = "a.pass_status,a.create_time from imall_icard_vip_approve a";
  	var whereSQL = " and a.allot_id="+allot_id+" and a.shop_id="+shop_id+" and a.user_id="+user_id;

  	ajax("do.php?act=ajaxPubSQL","POST","sqlType=select&table="+table+"&whereSQL="+whereSQL+"&sqlParam="+sqlParam,function(data){
  		//document.getElementById('content').value = data; 
  		// alert(data);
  		if(data != 'nofound' ) { //查到记录
  			var cInfo = data.split('|');
  			var pass_status =  cInfo[0];  		
  			var create_time =  cInfo[1];  	
  			//alert(data);
  			if ( pass_status == 0 ) { //申请中  				
  				showMsg('alterMsg', '您的申请已经于['+create_time+']提交过，商户正在审核中! 请勿重复提交申请!', -1, -1);
  				return -1;
  			} else if ( pass_status == 2 ) { //不通过
  				showMsg('alterMsg', '您于['+create_time+']提交的申请未通过!', -1, -1);
  			} else if ( pass_status == 1 ) { // 通过
  				
  				doSendVipTo(store_id, user_id, user_mobile, shop_id,goods_id,approve_way,allot_id);
  				// showMsg('alterMsg', '申请的会员卡已经发送到你', -1, -1);
  			}
  			//alert(data);
  		} else {  // 没有申请过该记录
  			doVipCodeApp();
  			// showMsg('alterMsg', '该物品没有您的消费记录，无法评论!', -1, -1);
  		}
  	});
}



function doVipCodeApp(status) { //vip卡申请
	
	var store_id = document.getElementById('app_store_id').value ;
	var shop_id = document.getElementById('app_shop_id').value;
	var real_vip_code = document.getElementById('real_vip_code').value;
	var real_vip_owner = document.getElementById('real_vip_owner').value;
	var user_id = document.getElementById('user_id').value;
	var allot_id = document.getElementById('app_allot_id').value;
	var goods_id = document.getElementById('app_goods_id').value;
	var create_time = 'sysdate';

	if ( store_id =='' || shop_id == '' || user_id == '' ) {
		showMsg('alterMsg', "申请失败,请联系卡宝网站!", -1, -1);
		return ;
	}

	var pass_status = 0;
  	if ( status ) pass_status = status;
  	var sqlParam = 'shop_id|#|'+shop_id
  					+'|@|user_id|#|'+user_id
  					+'|@|store_id|#|'+store_id
  					+'|@|allot_id|#|'+allot_id
  					+'|@|create_time|#|'+create_time
  					+'|@|pass_status|#|'+pass_status
  					+'|@|real_vip_code|#|'+real_vip_code
  					+'|@|real_vip_owner|#|'+real_vip_owner
                   	+'|@|goods_id|#|'+goods_id;
  	var whereSQL = '';
  	var table = 'imall_icard_vip_approve';
   
  	ajax("do.php?act=ajaxPubSQL","POST","sqlType=insert&table="+table+"&whereSQL="+whereSQL+"&sqlParam="+sqlParam,function(data){
  		//document.getElementById('content').value = data;
  		//alert(data);
  		if( data >= 0 ) { //成功  			  			
  			showMsg('alterMsg', "操作成功,请耐心等待商户的回复!", -1, -1);  			
  			// closeIt();
  		} else {  				
  			showMsg('alterMsg', "操作失败,可能您已经申请过该卡或系统出错,请联系卡宝网站!", -1, -1);
  		}
  	});
  }

	function doSendVipTo(sid, uid, mobile, shopid,goods_id,approve_way,allot_id) {
		
		var urlParam = "sid="+sid+"&uid="+uid+"&mobile="+mobile+"&shopid="+shopid+"&goods_id="+goods_id+"&allot_id="+allot_id;	
		//alert(urlParam);
		ajax("icard.php?app=down_vip_icard","POST", urlParam ,function(data){
	  		//document.getElementById('content').value = data;
	  		//alert(data);
			//document.write(data);
	  		// showMsg('alterMsg', data , -1, -1);  
	  		if( data.indexOf('qrimgURL') > -1 ) { //成功返回图片路径			
	  			alert("操作成功,该卡已经通过短信发放给您，请查收!");	
	  			closeIt();
	  			if ( approve_way && approve_way == 1)	doVipCodeApp(3);	//自动审批往审批表插入3
	  			// 
	  		} else {  		
	  			alert("操作失败:"+data);			
	  			showMsg('alterMsg', "操作失败:"+data, -1, -1);
	  		}
	  	});
	}

	/* 显示提示信息  timeFlag = 1时，始终显示,  timeFlag != 1时 , 显示  paraTime毫秒 后自动消失*/
	function showMsg(tagId, msg, paraTime,  timeFlag) {	 
		  	  document.getElementById(tagId).innerHTML = msg ;
		      if ( timeFlag && timeFlag == -1 )  {
		        // 
		      } else {
		        var time = 2000;
		        if ( paraTime && paraTime != -1 ) time = paraTime;
			    setTimeout(function(){document.getElementById(tagId).innerHTML = ""; }, time);	
			  }  
	}
