<?php
if ( !defined( "SAXUE_CORE_INCLUDE" ) ) {
		include "../../core.php";
} 
saxue_checkpower( 'memberbill' );
$id = intval( $_REQUEST['id'] );
if ( empty( $id ) ) {
		saxue_printfail( LANG_ERROR_PARAMETER );
}
include SAXUE_ROOT_PATH . "/model/member_bill.php";
$bill_handler = &saxuememberbillhandler :: getinstance( "saxuememberbillhandler" );
$bill = $bill_handler -> get( $id );
if ( !is_object( $bill ) ) {
		saxue_printfail( '提现记录不存在' );
} 
$row = $bill -> getvars( "n" );
if ( $row['status'] == 0 ) {
		include SAXUE_ROOT_PATH . "/model/member_users.php";
		$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
		$users = $users_handler -> get( $row['uid'] );
		if ( !is_object( $users ) ) {
				saxue_printfail( LANG_NO_USER );
		} 
		include SAXUE_ROOT_PATH . "/model/member_account.php";
		$account_handler = &saxuememberaccounthandler :: getinstance( "saxuememberaccounthandler" );
		$account = $account_handler -> get( $row['uid'] );
		if ( !is_object( $account ) ) {
				saxue_printfail( '会员提现帐号不存在' );
		} 
		$useramount = $users -> getvar( 'money' );
		if ( $row['amount'] > $useramount ) {
				$erramount = 1;
		} else {
				$erramount = 0;
		}
		if ( $row['account'] != $account -> getvar( 'account' ) ) {
				$erraccount = 1;
		} else {
				$erraccount = 0;
		}
		if ( isset( $_POST['dosubmit'] ) ) {
				$reason = trim( $_POST['reason'] );
				$status = intval( $_POST['status'] );
				if ( $status == 1 ) {
						if ( $erraccount == 1 ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '提现帐号不一致' ) ) );
						}
						if ( $erramount == 1 ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '会员余额不足' ) ) );
						}
						if ( !$users_handler -> changemoney( $row['uid'], -$row['amount'], 2 ) ) {
								exit( json_encode( array( 'flag' => 0, 'msg' => '更改会员资金失败' ) ) );
						}
				}
				$bill -> setvar( 'status', $status );
				$bill -> setvar( 'reason', $reason );
				$bill -> setvar( 'paytime', SAXUE_NOW_TIME );
				if ( !$bill_handler -> insert( $bill ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => LANG_ERROR_DATABASE ) ) );
				} 
				exit( json_encode( array( 'flag' => 1 ) ) );
		}
}
include SAXUE_ADMIN_PATH . "/header.php";
if ( $row['status'] == 0 ) {
		$saxueTpl -> assign( "useramount", $useramount );
		$saxueTpl -> assign( "erramount", $erramount );
		$saxueTpl -> assign( "erraccount", $erraccount );
		$saxueTpl -> assign_by_ref( "account", $account -> getvars( "n" ) );
}
$saxueTpl -> assign_by_ref( "row", $row );
$saxueTpl -> setcaching( 0 );
$saxueTset['saxue_contents_template'] = SAXUE_WEB_PATH . "/member/templates/bill_hand.html";
include SAXUE_ADMIN_PATH . "/footer.php";