<?php
defined('IN_YUNYECMS') or exit('No permission.');
core::load_class('base',false);
core::load_fun('content');
core::load_fun('tree');
core::load_fun('shop');
class membergroup_goods extends base {
	function __construct() {
	 parent::__construct();
	 $this->db->tablename = 'membergroup_goods';
	 }
	//首页
	 public function index(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('my_membergroup_goods')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
		$token=ugetcookie("loginrnd");
		$cfg=$this->cfg;
		$lang=$this->lang; 
		islogin(); 
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('membergroup_goods'),'url'=>url("member/membergroup_goods/index"))
			);		 
		$userid=usafestr(ugetcookie("userid"));
		$member=$this->member;
		 $pagesize=20;
		 $sqlquery="select * from `#yunyecms_membergroup_goods`  ";
		 $where=" where status=1  ";
		 $sqlcnt=" select count(*) from `#yunyecms_membergroup_goods` ";
		 $order=" order by `ordernum` asc,`addtime` desc ";
		  if(isset($_REQUEST)){
		   if(!empty($_REQUEST["searchkey"])){
		        $searchkey=usafestr(trim($_REQUEST["searchkey"]));
		        $where=$where." and ( `goods_title`  like '%{$searchkey}%' )";
			  }
		 }
		 $pagearr=$this->db->pagelist($sqlcnt,$sqlquery,$where,$order,$pagesize);
		 if($pagearr["count"]!=0){
			 $list=$pagearr["query"];
			 foreach($list as $key=>$var){
				   $list[$key]["content"]=uhtmlspecialchars_decode($var["content"]);
				   $list[$key]["content_en"]=uhtmlspecialchars_decode($var["content_en"]);
				   $list[$key]["membergroupname"]=GetMemberGroupName($var["groupid"]);
				    if(!empty($list[$key]["time_limit"])){
							 $list[$key]["time_limit"]=unserialize($list[$key]["time_limit"]);
					}	
			 }
			 $page=$pagearr["page"];
		 }
        include tpl('membergroup_goods','member');
	}

	 public function show(){
        $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$cfg=$this->cfg;
		$lang=$this->lang;
		if($this->member){
			$token=ugetcookie("loginrnd");
		 }
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('member_membergroup_goods'),'url'=>url("member/notice/index"))
			);	
		if(!empty($_REQUEST["id"])){
		$id = intval(usafestr($_REQUEST['id']));
		if(!is_numeric($id)){
		  messagebox(Lan('error_parameter'));		
			 }
		$row=$this->db->find("select * from `#yunyecms_membergroup_goods`  where id={$id}");
		if(empty($row)){
		      messagebox(Lan('error_parameter'));		
		}
		$row["content"]=uhtmlspecialchars_decode($row["content"]);
		$row["content_en"]=uhtmlspecialchars_decode($row["content_en"]);
		$prevsql='';
		$nextsql='';
		$prevsql=" addtime>=".$row['addtime']." and id<>".$id."  ";
		$nextsql=" addtime<=".$row['addtime']."  and id<>".$id."  ";	
		$prev=$this->db->find("select * from `#yunyecms_membergroup_goods`  where {$prevsql} order by addtime asc limit 0,1");
	    if($prev){
			$prev["url"]=url("member/notice/show",array("id"=>$prev["id"]));
		}		 
		$next=$this->db->find("select * from `#yunyecms_membergroup_goods`  where {$nextsql}  order by addtime desc limit 0,1");
		if($next){
			$next["url"]=url("member/notice/show",array("id"=>$next["id"]));
		}		 
		$this->db->query("update  `#yunyecms_membergroup_goods` set hits=hits+1  where id={$id}");
		$row['time']=udate($row['addtime']);
			$seo['title']=$row["title"].'-'.$seo['title'];
			$seo["keywords"]=(empty($row["keywords"])?$row["title"]:$row["keywords"]).','.$seo["keywords"];
			$seo["description"]=(empty($row["description"])? strcut(trim(strip_tags($row["content"])),200):$cat["description"]);			 
		 }else{
		      messagebox(Lan('error_parameter'));		
		}
        include tpl('notice_show','member');
	}	
	
	
	
 function pay() {
	    $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('cart')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>"会员升级",'url'=>url("member/membergroup_goods/pay"))
			);	
		islogin(); 
		//checktoken(trim($_POST["token"]));
		$member=$this->member;
		$userid=$this->member['id'];
	     if(empty($member["username"])){
               messagebox(Lan('improve_info_first'),url('member/member/myinfo'));
			   exit;
		 }		
			if(!empty($_REQUEST['orderprice'])){
				$totalamount=$_REQUEST['orderprice'];
				if(!is_numeric($totalamount)){
					messagebox(Lan('goods_parm_error'));		
				 }
			 }else{
				 messagebox(Lan('goods_parm_error'));		
			 }
			if(!empty($_REQUEST['goods_id'])){
				$goods_id=$_REQUEST['goods_id'];
				if(!is_numeric($goods_id)){
					messagebox(Lan('goods_parm_error'));		
				 }
				 $membergoods=$this->db->find("select * from `#yunyecms_membergroup_goods`  where goods_id={$goods_id}");
			 }else{
				 messagebox(Lan('goods_parm_error'));		
			 }
	 
	         $time_limit=$membergoods['time_limit'];
	         $time_limit=unserialize($time_limit);

			if(!empty($_REQUEST['timelimit_id'])){
				$timelimit_id=$_REQUEST['timelimit_id'];
				if(!is_numeric($timelimit_id)){
					messagebox(Lan('goods_parm_error'));
				 }
			 }else{
				 messagebox(Lan('goods_parm_error'));		
			 }
			  if(empty($_REQUEST['timelimit_name'])){
					 messagebox(Lan('goods_parm_error'));		
			   }else{
			   $timelimit_name=usafestr($_REQUEST["timelimit_name"]);
			   }
	          //验证价格
	             $realprice=0;
	             foreach($time_limit as $var){
					 if($var['id']==$timelimit_id){
						$realprice=$var['price']; 
					 }
				 }
	             if($totalamount!=$realprice){
				      messagebox(Lan('goods_parm_error'));		
				 }


				 if(!empty($_REQUEST['paymode'])){
					$paymode=$_REQUEST['paymode'];
					if(!is_numeric($paymode)){
					 messagebox("请选择支付方式");		
					 }
				 }else{
					 messagebox("请选择支付方式");		
				 }	 
			 
	          //验证价格结束
	          $curtime=time();
			  $sn = makesn();
			  $data['sn'] = $sn;
			  $data['userid'] = $userid;
			  $data['goods_id'] = $goods_id;
			  $data['title'] = $membergoods["goods_title"];
			  $data['timelimit_id'] = $timelimit_id;
			  $data['timelimit_name'] = $timelimit_name;
			  $data['money'] = $realprice;
			  $data['paymode'] = $paymode;
			  $data['name'] =$member["name"];
			  $data['address'] =$member["address"];
			  $data['mobile'] =empty($member["mobile"])?$member["phone"]:$member["mobile"];
			  $data['addtime'] = $curtime;
			  $data['status'] = 0;
			  $data['updatetime'] = $curtime;
              $insertid = $this->db->insert($data,'membergroup_orders',true);
			  if($insertid !== false){
				$msgcontent['mod']=ROUTE_M;
				$msgcontent['act']=ROUTE_A;
				$msgcontent['ctrl']=ROUTE_C;
				$msgcontent['table']="membergroup_orders";
				$msgcontent['id']=$insertid;
			    $msgcontent=serialize($msgcontent);
				$msgtitle=sprintf(lan('orders_success_msg'), $sn, $member['username'],$member["name"],$member["mobile"]);
			    $remsg=savemsg($msgtitle,4,$msgcontent,$insertid);
				 $emailcfg= $this->cfg;
				 $to=$emailcfg['email']['toemail'];
				 $name= $member["name"];
				 $subject="会员升级订单-{$membergoods["goods_title"]}-$timelimit_name-{$member["mobile"]}-$sn";
				 $body="订单标题:{$membergoods["goods_title"]}<br/>";
				 $body=$body."订单类型:$timelimit_name<br/>";
				 $body=$body."订单号:$sn<br/>";
				 $body=$body."会员:{$member['mobile']}<br/>";
				 $body=$body."姓名:{$member['name']}<br/>";
				 $body=$body."手机号:{$member['mobile']}<br/>";
				 $body=$body."金额 :$totalamount<br/>";
				 send_mail($emailcfg['email'],$to, $name, $subject, $body);					  
				 redirect(url('member/membergroup_goods/onlinepay',array("orderid"=>$insertid)));
              }else {
		        messagebox(Lan('error_checkout'),"back",'warn');
             }
    }	
	
	
 function onlinepay() {
	    $seo['title']=$this->lang["seotitle"];
        $seo['keywords']=$this->lang["seokey"];
        $seo['description']=$this->lang["seodesc"];
		$seostr=Lan('online_pay')."-".Lan('member_center');
        $seo['title']="{$seostr}-{$seo['title']}";
        $seo['keywords']="{$seostr}-{$seo['keywords']}";
	    $breadcumb=array('0'=>array('title'=>Lan('member_center'),'url'=>url("member/member/index")),
              '1'=>array('title'=>Lan('online_pay'),'url'=>url("shop/cart/pay"))
			);	
		$cfg=$this->cfg;
		$lang=$this->lang; 
	    islogin(); 
		$userid=usafestr(ugetcookie("userid"));
		$token=ugetcookie("loginrnd");
		$member=$this->member;
	    if(empty($member["username"])){
            messagebox(Lan('improve_info_first'),url('member/member/myinfo'));
			exit;
		 }	 
	    if(!empty($_REQUEST['orderid'])){
		    $orderid=$_REQUEST['orderid'];
			$orderid=usafestr(trim($orderid));
			if(!is_numeric($orderid)){
		       messagebox(Lan('order_parm_error'));		
             }
		 }else{
		       messagebox(Lan('order_parm_error'));		
		 }

	    $orderitem=$this->db->find("select * from `#yunyecms_membergroup_orders` where userid={$member["id"]} and id=$orderid");
	    if(empty($orderitem)){
			messagebox(Lan('order_not_exsit'));
		}

		$orderdata['body']="{$lang['sitename']}订单-{$orderitem['sn']}";
		$orderdata['attach']="姓名：{$orderitem['name']}-手机：{$orderitem['mobile']}}";
		$orderdata['out_trade_no']=$orderitem['sn'];
		$orderdata['total_fee']=intval($orderitem['money']*100);
		// $orderdata['total_fee']=1;
		$orderdata['out_trade_no']=$orderitem['sn'];
	   if($orderitem['paymode']==2){
	     if(is_mobile()){
	          	   if($orderitem['money']>=0){
                      require_once YUNYECMS_EXTEND.'wxpay'.DIRECTORY_SEPARATOR.'wxpay.php'; 
                      $wxpay=new wxpay();
                      $wxpay_para=$wxpay->jsapi($orderdata); 
                  } 
	       }else{
		   require_once YUNYECMS_EXTEND.'wxpay'.DIRECTORY_SEPARATOR.'wxpay_native.php'; 
		   $wxpay=new wxpay_native();
		   $wxpay_para=$wxpay->nativepay($orderdata); 
	       }	   
	    }	 
        include tpl('onlinepay','member');
     }	
		
	
	private function check_exist($id) {
			  $id = trim($id);
			  if(empty($id)||empty($id)){
				 return false;
				 }else{
				  if(!is_numeric($id)){
						  return false;
					 }
				  $cnt=$this->db->GetCount("select count(*) from `#yunyecms_membergroup_goods` where    `id`= {$id}");
				  if($cnt>=1){
					 return true;
				  }else{ return false;}			 
			  }
		  }		
}

?>