<?php
// +----------------------------------------------------------------------
// | Sunnyi 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.sunnyi.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 商易网络(admin@sunnyi.cn)
// +----------------------------------------------------------------------

//会员提现
class UcUnchargeAction extends UserCenterAction {
	public function index()
	{
		//初始化分页
	    	$page = intval($_REQUEST[C("VAR_PAGE")]);
	    	if($page==0)
	    	$page = 1;
	    	
	    	$result = D("UserUncharge")->getUnchargeList(intval($_SESSION['user_id']),$page);

	    	$this->assign("uncharge_list",$result['list']);
	    	//分页
			$page = new Page($result['total'],sunnyiC("PAGE_LISTROWS"));   //初始化分页对象 		
			$p  =  $page->show();
	        $this->assign('pages',$p);
	        //end 分页  	        
			    	
	        $this->display("Page:user_center_frame");
	}
	
	public function insert()
	{
		if(sunnyiC("TOKEN_ON")&&!M()->autoCheckToken($_POST))
		{
			$this->error(L("TOKEN_ERROR"));
		} 
		if(intval($_REQUEST['money'])<=0)
		{
			$this->error(L("MONEY_ERROR"));
		}
		$_POST['money'] = toDbPrice($_POST['money']);
		
		$user_money = D("User")->where("id=".intval($_SESSION['user_id']))->getField("money");
		$total_uncharge_money = D("UserUncharge")->where("user_id=".intval($_SESSION['user_id'])." and status=0")->sum("money");
		
		if($_POST['money']+$total_uncharge_money>$user_money)
		{
			$this->error(L("OVER_UNCHARGE"));
		}
		$data['sn'] = date('ymdhis', gmtTime()).rand_string(2,0);
		$data['user_id'] = intval($_SESSION['user_id']);
		$data['money'] = floatval($_POST['money']);
		$data['create_time'] = gmtTime();
		$id = D("UserUncharge")->add($data);
		if($id)
		{
			$this->error(L("SUBMIT_SUCCESS"));
		}
		else 
		{
			$this->error(L("SUBMIT_ERROR"));
		}			
	}
	
	public function modify()
	{
		$id=intval($_REQUEST['id']);
		$uncharge_info = D("UserUncharge")->where("status=0 and id=".$id)->find();
		if(!$uncharge_info)
		{
			$this->error(L("SUBMIT_ERROR"));
		}

		$currency_item = Session::get("sunnyi_currency");
		$uncharge_info['money'] = $uncharge_info['money']*$currency_item['radio'];
		
		$this->assign('uncharge_info',$uncharge_info);
		    	
	    $this->display("Page:user_center_frame");
	}
	
	
	public function update()
	{
		if(sunnyiC("TOKEN_ON")&&!M()->autoCheckToken($_POST))
		{
			$this->error(L("TOKEN_ERROR"));
		} 
		if(intval($_REQUEST['money'])<=0)
		{
			$this->error(L("MONEY_ERROR"));
		}
		$_POST['money'] = toDbPrice($_POST['money']);
		
		$id=intval($_POST['id']);
		$user_money = D("User")->where("id=".intval($_SESSION['user_id']))->getField("money");
		$total_uncharge_money = D("UserUncharge")->where("user_id=".intval($_SESSION['user_id'])." and status=0 and id<>".$id)->sum("money");
		
		if($_POST['money']+$total_uncharge_money>$user_money)
		{
			$this->error(L("OVER_UNCHARGE"));
		}
		
		$data['id'] = $id;
		$data['money'] = floatval($_POST['money']);
		$rs = D("UserUncharge")->save($data);
		$this->success(L("MODIFY_SUCCESS"));
		
	}
		
}
?>