<?php
	define('thisscript','withdraw');

	require_once 'global.php';	
	require_once 'class/Withdraw.php';	
	$withdraw = new Withdraw();
	
	if (empty($userid)){
		redirecting("index.php");
	}
	
	/* if (empty($useraccess['manage_account_bank'])){
		redirecting('index.php');
	} */
	
	/* sanitize post, get, request */
	$_POST['replayedit'] = ((empty($_POST['replayedit']))?"":$_POST['replayedit']);
	$_GET['getlist'] = ((empty($_GET['getlist']))?"":$_GET['getlist']);
	$_GET['keyword'] = ((empty($_GET['keyword']))?array():$_GET['keyword']);
	$_GET['field'] = ((empty($_GET['field']))?array():$_GET['field']);
	$_POST['cek'] = ((empty($_POST['cek']))?array():$_POST['cek']);
	$_POST['do'] = ((empty($_POST['do']))?"":$_POST['do']);
	$_GET['error'] = ((empty($_GET['error']))?"":$_GET['error']);		
	$_GET['actions'] = ((empty($_GET['actions']))?"":$_GET['actions']);
	$_GET['act'] = ((empty($_GET['act']))?"":$_GET['act']);
	$_GET['page'] = ((empty($_GET['page']))?"":$_GET['page']);
	$_GET['msg'] = ((empty($_GET['msg']))?"":$_GET['msg']);
	$_REQUEST['id'] = ((empty($_REQUEST['id']))?"":$_REQUEST['id']);

	/* end sanitize */
	
	$curstatus['amount'] = '';
	$curstatus['date'] = '';
	$curstatus['type'] = '';
	$curstatus['withdraw_to'] = '';
	
	$listtr = '';
	
	if (empty($general['showperpageadmin'])){
		$general['showperpageadmin'] = 20;
	}
	
	$pagelink = '';
	$listr = '';
	$listaction = '';
	$pagenavs = '';
	
	$user_db = $db->fetch_one("SELECT deposit, bonus_point, bonus_sharing, trading_account_pam_amount FROM user WHERE userid = '".$db->clean($userid)."'");
	$total_deposit = empty($user_db['deposit'])?0:$user_db['deposit'];
	$total_bonus = empty($user_db['bonus_point'])?0:$user_db['bonus_point'];
	$total_bonus_share = empty($user_db['bonus_sharing'])?0:$user_db['bonus_sharing'];
	$total_pam = empty($user_db['trading_account_pam_amount'])?0:$user_db['trading_account_pam_amount'];
	
	$minimal_withdrawal_amount = empty($transactionsetting['minimal_withdraw_amount'])?0:$transactionsetting['minimal_withdraw_amount'];

	if (!empty($_POST)){
		$do = $_POST['do'];
		if ($do == "addWithdraw"){
			$amount = (empty($_POST['amount']))?"":togglenumber($_POST['amount'], 'calculate');
			$type = (empty($_POST['type']))?"":$_POST['type'];
			$payment = (empty($_POST['payment']))?"":$_POST['payment'];
			$status = 1;
			
			$filesname = '';
			
			/* $check_db = $db->fetch_one("SELECT SUM(amount) total FROM withdraw WHERE userid = '".$db->clean($userid)."' AND status = 1 AND type = ".$db->clean($type));
			$total_withdraw = empty($check_db['total'])?0:$check_db['total']; */
			
			if(($amount > $total_bonus_share && $type == 1) /* || ($amount > $total_pam && $type == 2) || (($amount + $total_withdraw) > $total_bonus_share && $type == 3) */){
				redirecting('withdraw?act=addnew&msg=erramount');
			}
			
			$withdraw->saveWithdraw($amount, $type, $status, $payment);
			
			redirecting('withdraw?act=addnew&msg=success');
		}
		else if ($do == 'get_rate'){
			$amount = ((empty($_POST['amount'])) ? 0 : $_POST['amount']);
			
			$response = file_get_contents('https://bitpay.com/api/rates/idr');
			$bitcoin_amount = 0;
			$idr_amount = $transactionsetting['withdrawal_idr_rate'] * $amount;
			if (!empty($response)){
				$r = json_decode($response);
				if (!empty($r->rate)){
					$bitcoin_amount = $idr_amount / $r->rate;
					$bitcoin_amount = number_format($bitcoin_amount, 3);
				}
			}
			
			echo json_encode(array('bitcoin_amount' => $bitcoin_amount, 'idr_rate' => number_format($transactionsetting['withdrawal_idr_rate'], 0), 'idr_amount' => number_format($idr_amount, 0)));
			exit;
		}
	}
	
	
	$id = trim($_REQUEST['id']);
	if (!empty($id)){
		$withdraw->setId($id);
		$curstatus = $withdraw->getWithdrawDetail();
		if (empty($curstatus['withdraw_id']) || $curstatus['userid'] != $userid){
			redirecting('home');
		}
		
		$curstatus['amount'] = empty($curstatus['amount'])?0:number_format($curstatus['amount'], 2, $decimalseparator, $thousandseparator);
		$curstatus['date'] = empty($curstatus['date'])?'-':date('d-m-Y', strtotime($curstatus['date']));
		$curstatus['type'] = empty($curstatus['type'])?0:$curstatus['type'];
		switch ($curstatus['status']){
			case '1' : $status = 'Pending'; break;
			case '2' : $status = 'Success'; break;		
			case '3' : $status = 'Reject'; break;
		}

	}
	else{
	
		$keywords = $_GET['keyword'];
		$fields = $_GET['field'];
		$alls = $withdraw->searchWithdraw($keywords,$fields,'','','data', -1, 'member');
		$totalrows = sizeof($alls);
		$totalpgs = ceil($totalrows / $general['showperpage']);
		$pgs = handlepage($_GET['page'],$totalpgs);
		$dblast = $withdraw->searchWithdraw($keywords,$fields,'','','data',$pgs, 'member');
		
		$pagenavs = generatepagelinkgeneral($pgs,$totalrows,$general['showperpage'],'withdraw','','');

		if (sizeof($dblast) > 0){
			foreach ($dblast as $result){
				$cstatus = '';
				switch ($result['status']){
					case '1' : $cstatus = 'Pending'; break;
					case '2' : $cstatus = 'Success'; break;		
					case '3' : $cstatus = 'Reject'; break;	
				}
				
				$listr .= '
					<tr>
						<td height="25" align="left">
						<a href="withdraw?id='.$result['withdraw_id'].'">'.$result['transaction_number'].'</a></td>
						<td height="25" align="center">'.date('d M Y', strtotime($result['date'])).'</td>
						<td height="25" align="right">
						'.number_format($result['amount'], 2, $decimalseparator,$thousandseparator).'</td>
						<td height="25" align="center">
						'.htmlspecialchars($cstatus).'</td>		
						<td height="25" align="center">
						<a href="withdraw?id='.$result['withdraw_id'].'"><i class="fa fa-search"></i></a></td>							
					</tr>
				';
			}
		}
		else{
			$listr = '<tr><td colspan="5" align="center">NO DATA</td></tr>';
		}
			
	
	}
	
	$payment_list = '';
	if (!empty($id)){
		$payment_list = $curstatus['withdraw_to'];
	}
	else{
		$row = $db->fetch_one("SELECT a.bitcoin_acc as account_id, b.logo FROM user a ,bank b WHERE a.userid = '".$db->clean($userid)."' AND b.bank_id = 27");
		if(!empty($row['account_id'])){
			$payment_list .= '
				<div class="radio">
					<label><input type="radio" name="payment" id="payment_'.$row['account_id'].'" value="Bitcoin Account - '.$row['account_id'].'" class="payment-list">'.(!empty($row['logo'])?'<img src="'.$row['logo'].'" height="30">':$row['bankname']).'<br><b>'.$row['account_id'].'</b></label>
					<div style="display:none; padding-left:21px;">Withdrawal Amount : <span class="transfer-amount" id="transfer-amount-27">-</span></div>
				</div>
			';
		}
		
		$row = $db->fetch_one("SELECT a.bank_id as account_id, b.bankname, a.bank_account as rek_no, a.bank_account_name as rek_name, b.logo FROM user a INNER JOIN bank b ON b.bank_id = a.bank_id WHERE a.userid = '".$db->clean($userid)."'");
		if(!empty($row['account_id']) && !empty($row['rek_no']) && !empty($row['rek_name'])){
			$payment_list .= '
				<div class="radio">
					<label><input type="radio" name="payment" id="payment_'.$row['account_id'].'" value="'.$row['bankname'].' - '.$row['rek_no'].' a/n '.$row['rek_name'].'" class="payment-list">'.(!empty($row['logo'])?'<img src="'.$row['logo'].'" height="30">':$row['bankname']).' <br> <b>'.$row['rek_no'].'</b>'.(!empty($row['rek_name'])?' a/n <b>'.$row['rek_name'].'</b>':'').'</b></label>
					<div style="display:none; padding-left:21px;">Withdrawal Amount : <span class="transfer-amount" id="transfer-amount-'.$row['account_id'].'">-</span></div>
				</div>
			';
		}
	}

	if($_GET['msg'] == 'erramount'){
		$errmsg = 'Insufficient amount';
	}
	
	$now = date('d-m-Y');
	
	require_once "incl/global_template.php";
	
	$tmpl = gettemplate('withdraw');
	eval("\$template = \"$tmpl\";");
	echo $template;
?>