<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */

global $campoal_options;
global $post;
global $current_user;

// Check user is loggedin or not, if not redirect to login page
conikal_redirect_login();

$item_per_page_settings = isset($campoal_options['conikal_item_per_page_field']) ? $campoal_options['conikal_item_per_page_field'] : '';
$item_per_page 			= $item_per_page_settings != '' ? intval($item_per_page_settings) : 20;

$withdraw_paypal 	= isset($campoal_options['conikal_withdrawal_paypal_field']) && $campoal_options['conikal_withdrawal_paypal_field'] != '' ? $campoal_options['conikal_withdrawal_paypal_field'] : 1;
$withdraw_payoneer 	= isset($campoal_options['conikal_withdrawal_payoneer_field']) && $campoal_options['conikal_withdrawal_payoneer_field'] != '' ? $campoal_options['conikal_withdrawal_payoneer_field'] : 1;
$withdraw_bank 		= isset($campoal_options['conikal_withdrawal_bank_field']) && $campoal_options['conikal_withdrawal_bank_field'] != '' ? $campoal_options['conikal_withdrawal_bank_field'] : 1;
$withdraw_bitcoin 	= isset($campoal_options['conikal_withdrawal_bitcoin_field']) && $campoal_options['conikal_withdrawal_bitcoin_field'] != '' ? $campoal_options['conikal_withdrawal_bitcoin_field'] : 1;

$minimum_paypal 	= isset($campoal_options['conikal_minimum_withdrawal_paypal_field']) && $campoal_options['conikal_minimum_withdrawal_paypal_field'] != '' ? $campoal_options['conikal_minimum_withdrawal_paypal_field'] : 0;
$minimum_payoneer 	= isset($campoal_options['conikal_minimum_withdrawal_payoneer_field']) && $campoal_options['conikal_minimum_withdrawal_payoneer_field'] != '' ? $campoal_options['conikal_minimum_withdrawal_payoneer_field'] : 0;
$minimum_bank 		= isset($campoal_options['conikal_minimum_withdrawal_bank_field']) && $campoal_options['conikal_minimum_withdrawal_bank_field'] != '' ? $campoal_options['conikal_minimum_withdrawal_bank_field'] : 0;
$minimum_bitcoin 	= isset($campoal_options['conikal_minimum_withdrawal_bitcoin_field']) && $campoal_options['conikal_minimum_withdrawal_bitcoin_field'] != '' ? $campoal_options['conikal_minimum_withdrawal_bitcoin_field'] : 0;

$action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : '';

// get financial infomation
$paypal 	= get_user_meta($current_user->ID, 'financial_paypal', true);
$payoneer 	= get_user_meta($current_user->ID, 'financial_payoneer', true);
$bank 		= get_user_meta($current_user->ID, 'financial_bank', true);
$bitcoin 	= get_user_meta($current_user->ID, 'financial_bitcoin', true);

// convert financial infomation to object
$paypal_details 	= $paypal ? (object) $paypal : '';
$payoneer_details 	= $payoneer ? (object) $payoneer : '';
$bank_details 		= $bank ? (object) $bank : '';
$bitcion_details 	= $bitcoin ? (object) $bitcoin : '';

// balance
$balance = conikal_give_balance($current_user->ID);

// get currency format
$currency_position = give_get_option( 'currency_position', 'before' );
$currency_symbol = give_currency_symbol();

$cancel_id = isset($_GET['cancel_id']) ? sanitize_text_field($_GET['cancel_id']) : '';

// cancel withdrawal
if ($cancel_id) {
	wp_trash_post($cancel_id);
}

// user dashboard link
$user_dashboard_page = isset($campoal_options['conikal_page_dashboard_user_field']) ? $campoal_options['conikal_page_dashboard_user_field'] : 0;
$user_dashboard_page = ($user_dashboard_page ? get_permalink($user_dashboard_page) : '');

// get all withdraw
$withdrawals = conikal_all_withdrawls($current_user->ID, $item_per_page);
if($withdrawals->have_posts()) {
    $total = $withdrawals->found_posts;
} else {
    $total = 0;
}
?>

<div class="ui hidden divider"></div>
<div class="ui middle aligned grid">
	<div class="ten wide column">
		<h1 class="ui header submit-petition-title">
	        <div class="content">
	        	<?php esc_html_e('Withdrawals', 'campoal'); ?>
	        </div>
	    </h1>
	</div>
	<div class="six wide right aligned column">
		<button class="ui icon basic button" id="dashboard-sidebar-btn"><i class="sidebar icon"></i></button>
	</div>
</div>
<div class="ui hidden divider"></div>


<div class="ui accordion accord-withdrawals">
  	<div class="title">
    	<button class="ui primary small button"><?php esc_html_e('New Withdrawal', 'campoal') ?></button>
  	</div>
  	<div class="<?php echo esc_attr($withdrawals->have_posts() ? '' : 'active '); ?>content">
		<div class="ui grid">
			<div class="sixteen wide mobile eight wide computer column">
				<div class="ui grid secondary segment">
					<div class="ten wide middle aligned column">
						<?php esc_html_e('Current your Balance:', 'campoal') ?>
					</div>
					<div class="six wide right aligned column">
						<h2 class="ui primary header"><?php echo esc_html($balance ? give_currency_filter(give_format_amount($balance, array( 'sanitize' => false ))) : '') ?></h2>
					</div>
				</div>
				<div class="ui section divider"></div>
				<div id="withdraw-response" class="respon-message"></div>
				<div class="ui form">
					<h3 class="ui header"><?php esc_html_e('Amount', 'campoal') ?></h3>
					<div class="inline fields">
						<div class="field">
							<div class="ui radio fitted checkbox">
							  	<input type="radio" name="withdraw_type" value="amount" checked="checked">
							</div>
						</div>
						<div class="required field">
				      		<div class="ui <?php echo esc_attr($currency_position == 'after' ? 'right' : 'left') ?> labeled input">
							  	<?php if ($currency_position == 'before') { ?>
		                            <label for="withdraw_amount" class="ui basic label"><?php echo esc_html($currency_symbol) ?></label>
		                        <?php } ?>
							        <input type="number" placeholder="<?php esc_attr_e('Amount', 'campoal') ?>" name="withdraw_amount" id="withdraw_amount" value="<?php echo esc_attr($balance ? $balance : 0); ?>">
		                        <?php if ($currency_position == 'after') { ?>
		                            <label for="withdraw_amount" class="ui basic label"><?php echo esc_html($currency_symbol) ?></label>
		                        <?php } ?>
							</div>
			      		</div>
					</div>
					<div class="field">
						<div class="ui radio checkbox">
						  	<input type="radio" name="withdraw_type" value="all">
						  	<label><?php echo sprintf(esc_html__('All money available at %s', 'campoal'), date_i18n(get_option('date_format'), strtotime(current_time('Y-m-t')))) ?></label>
						</div>
					</div>
					<div class="field">
						<p>
							<?php esc_html_e('If you choose the All money option, you must still meet the minimum withdrawal amounts below. Your withdrawal request will not be processed unless it meets the minimum requirements.', 'campoal') ?>
						</p>
						<p>
							<?php esc_html_e('Please note that the All money request must be renewed each month. This means clicking it will not result in automatic withdrawals for future months.', 'campoal') ?>
						</p>
					</div>
				</div>
			</div>
			<div class="sixteen wide mobile eight wide computer column">
				<h3 class="ui header"><?php esc_html_e('Method', 'campoal') ?></h3>
				<div class="ui form">
					<div class="field">
						<?php if ($withdraw_paypal || $withdraw_payoneer || $withdraw_bank || $withdraw_bitcoin) { ?>
							<?php if (!$paypal && !$payoneer && !$bank && !$bitcoin) { ?>
								<div class="ui info message">
									<div class="header">
									    <?php esc_html_e('You have not set any payment methods yet.', 'campoal') ?>
									</div>
									<?php echo sprintf(esc_html__('Please %s to add new payment method.', 'campoal'), '<strong><a href="' . esc_url(add_query_arg(array('section' => 'financial'), $user_dashboard_page)) . '" data-bjax>' . esc_html__('click here', 'campoal') . '</a></strong>'); ?>
								</div>
							<?php } ?>
							<div class="accordion">
								<?php if ($withdraw_paypal && !empty($paypal)) { ?>
							  	<div class="title active">
							    	<div class="ui radio checkbox">
								        <input type="radio" name="withdraw_method" value="paypal" checked="checked">
								        <label><i class="paypal icon"></i><?php echo esc_html__('PayPal', 'campoal') . ' - ' . sprintf(esc_html__('Minimum %s', 'campoal'), give_currency_filter( give_format_amount( $minimum_paypal, array( 'sanitize' => false ) ) )) ?></label>
								    </div>
							  	</div>
							  	<div class="content active">
							    <p class="transition visibles">
							    	<?php echo esc_html($paypal ? __('Email:', 'campoal') . ' ' . $paypal_details->email : ''); ?>
							    </p>
							    <p class="transition visibles font tiny text grey">
									<?php esc_html_e('You will pay the fee yourself when receiving money via PayPal, which is deducted directly from the money you receive.', 'campoal') ?>
							    </p>
							  	</div>
							  	<?php }
							  	if ($withdraw_payoneer && !empty($payoneer)) { ?>
							  	<div class="title">
								    <div class="ui radio checkbox">
								        <input type="radio" name="withdraw_method" value="payoneer">
								        <label><i class="credit card alternative icon"></i><?php echo esc_html__('Payoneer', 'campoal') . ' - ' . sprintf(esc_html__('Minimum %s', 'campoal'), give_currency_filter( give_format_amount( $minimum_payoneer, array( 'sanitize' => false ) ) )) ?></label>
								    </div>
							  	</div>
							  	<div class="content">
							    	<p class="transition hidden">
							    		<?php echo esc_html($payoneer ? __('Email:', 'campoal') . ' ' . $payoneer_details->email : '') ?>
								    	<br>
								    	<?php echo esc_html($payoneer ? __('Card:', 'campoal') . ' ' . $payoneer_details->card : '') ?>
							    	</p>
							    	<p class="transition hidden font tiny text grey">
										<?php esc_html_e('You will pay the fee yourself when receiving money via Payoneer, which is deducted directly from the money you receive.', 'campoal') ?>
									</p>
							  	</div>
							  	<?php } 
								if ($withdraw_bitcoin && !empty($bitcoin)) { ?>
									<div class="title">
									  <div class="ui radio checkbox">
										  <input type="radio" name="withdraw_method" value="bitcoin">
										  <label><i class="bitcoin icon"></i><?php echo esc_html__('Bitcoin', 'campoal') . ' - ' . sprintf(esc_html__('Minimum %s', 'campoal'), give_currency_filter( give_format_amount( $minimum_bitcoin, array( 'sanitize' => false ) ) )) ?></label>
									  </div>
									</div>
									<div class="content">
									  <p class="transition hidden">
										  <?php echo esc_html($bitcoin ? __('Network:', 'campoal') . ' ' . $bitcion_details->network : '') ?>
										  <br>
										  <?php echo esc_html($bitcoin ? __('Address:', 'campoal') . ' ' . $bitcion_details->address : '') ?>
									  </p>
									  <p class="transition hidden font tiny text grey">
										  <?php esc_html_e('You will pay the fee yourself when receiving money via Bitcoin, which is deducted directly from the money you receive.', 'campoal') ?>
									  </p>
									</div>
								<?php } 
							  	if ($withdraw_bank && !empty($bank)) { ?>
							  	<div class="title">
							    	<div class="ui radio checkbox">
							        <input type="radio" name="withdraw_method" value="bank">
							        <label><i class="university icon"></i><?php echo esc_html__('Bank Transfer', 'campoal') . ' - ' . sprintf(esc_html__('Minimum %s', 'campoal'), give_currency_filter( give_format_amount( $minimum_bank, array( 'sanitize' => false ) ) )) ?></label>
							    	</div>
							  	</div>
							  	<div class="content">
							    	<p class="transition hidden">
							    		<h4><?php esc_html_e('Account holder', 'campoal') ?></h4>
							    		<?php echo esc_html($bank ? __('Full Name:', 'campoal') . ' ' . $bank_details->fullname : ''); ?>
								    	<br>
								    	<?php echo esc_html($bank ? __('Address:', 'campoal') . ' ' . $bank_details->user_address : ''); ?>
								    	<h4><?php esc_html_e('Bank infomation', 'campoal') ?></h4>
								    	<?php echo esc_html($bank ? esc_html__('Bank Name:', 'campoal') . ' ' . $bank_details->name : ''); ?>
								    	<br>
								    	<?php echo esc_html($bank ? __("Bank's Address:", 'campoal') . ' ' . $bank_details->address : ''); ?>
								    	<br>
								    	<?php echo esc_html($bank ? __('SWIFT code:', 'campoal') . ' ' . $bank_details->swift_code : ''); ?>
								    	<br>
								    	<?php echo esc_html($bank ? __("Account number:", 'campoal') . ' ' . $bank_details->account_number : ''); ?>
							    	</p>
							    	<p class="transition hidden font tiny text grey">
							    		<?php esc_html_e('We cannot be held responsible for delays, extra costs or financial loss that arises from being provided incorrect account information, so please ensure that you double check the details with your financial institution prior to submitting a request for a Bank Wire Transfer.', 'campoal') ?>
							    	</p>
							  	</div>
							  	<?php } ?>
							</div>
						<?php } ?>
					</div>
					<div class="ui message">
						<div class="field required">
		                    <label><?php esc_html_e('Confirm Password', 'campoal') ?></label>
		                    <input type="password" name="confirm_password" id="confirm_password" placeholder="<?php esc_attr_e('Enter your password', 'campoal') ?>">
		                </div>
		                <p>
			            <?php esc_html_e('* To protect your money when withdraw request is not from you. We require a confirmation with your password. Please enter your password in the field above.', 'campoal') ?>
			            </p>
		            </div>
					<div class="ui right floated header field">
						<button class="ui primary button" id="submitWithdrawBtn"><?php esc_html_e('Submit Request', 'campoal') ?></button>
					</div>
				</div>
			</div>
		</div>
		<div class="ui section divider"></div>
  	</div>
</div>

<?php if ($withdrawals->have_posts()) : ?>
<table class="ui very basic celled very padded striped table">
  	<thead>
    <tr>
    	<th>
    		<div class="ui grid computer only">
    			<div class="sixteen wide mobile one wide computer column"><?php esc_html_e('ID', 'campoal') ?></div>
    			<div class="sixteen wide mobile four wide computer column"><?php esc_html_e('Date', 'campoal') ?></div>
    			<div class="sixteen wide mobile three wide computer column"><?php esc_html_e('Type', 'campoal') ?></div>
    			<div class="eight wide mobile two wide computer column"><?php esc_html_e('Amount', 'campoal') ?></div>
    			<div class="eight wide mobile two wide computer column"><?php esc_html_e('Method', 'campoal') ?></div>
    			<div class="four wide mobile two wide computer column"><?php esc_html_e('Status', 'campoal') ?></div>
    			<div class="four wide mobile two wide computer column"><?php esc_html_e('Action', 'campoal') ?></div>
    		</div>	
    	</th>
  	</tr>
 	</thead>
  	<tbody>
  	<?php
		while( $withdrawals->have_posts() ) : $withdrawals->the_post();
        $id = get_the_ID();
        $date = get_the_date(get_option( 'date_format' ), $id);
        $amount = get_post_meta($id, 'withdraw_amount', true);
        $notes = get_post_meta($id, 'withdraw_notes', true);
        $method_type = conikal_withdraw_label($id);
        $status = get_post_status($id);
        $status_labels = conikal_get_withdraw_status($id);

        if ($amount) {
        	$amount = give_currency_filter( give_format_amount( $amount, array( 'sanitize' => false ) ) );
        } else {
        	$amount = '-';
        }
    ?>
    <tr>
    	<td>
	    	<div class="ui middle aligned grid">
	    		<div class="three wide mobile one wide computer column">
	    			<?php echo esc_html__('#', 'campoal') . esc_html($id) ?>
	    		</div>
	    		<div class="eight wide mobile four wide computer column">
			      	<?php echo esc_html($date); ?>
	    		</div>
	    		<div class="five wide mobile three wide computer column">
	    			<h4 class="ui header">
				        <?php echo esc_html($method_type->type) ?>
			      	</h4>
	    		</div>
	    		
	    		<div class="sixteen wide mobile two wide computer column">
	    			<h5 class="ui primary header">
					  	<?php echo esc_html($amount) ?>
					</h5>
	    		</div>
	    		<div class="six wide mobile two wide computer column">
	    			<?php echo esc_html($method_type->method); ?>
	    		</div>
	    		<div class="five wide mobile two wide computer column">
					<?php echo esc_html($status_labels->label) ?>
	    		</div>
	    		<div class="five wide mobile two wide computer column">
	    			<?php if ($status == 'publish') { ?>
	    				<a class="withdraw-notes" href="#" data-content="<?php echo esc_attr($notes ? $notes : __('The withdrawal amount has been transferred to your payment account successfully!', 'campoal')) ?>" data-position="top right"><i class="sticky note icon"></i><?php esc_html_e('Notes', 'campoal') ?></a>
	    			<?php } else { ?>
	    				<a href="<?php echo esc_url(add_query_arg(array('section' => 'withdrawals', 'cancel_id' => $id), $user_dashboard_page)) ?>"><i class="remove icon"></i><?php esc_html_e('Cancel', 'campoal') ?></a>
	    			<?php } ?>
	    		</div>
	    	</div>
    	</td>
    </tr>
    <?php endwhile; ?>
  	</tbody>
  	<tfoot>
    	<tr>
			<th colspan="5">
			<!-- PAGINATION -->
			<?php conikal_pagination_buttons(_n('User', 'Users', $total, 'campoal'), $withdrawals->max_num_pages, $total, $item_per_page) ?>
			</th>
		</tr>
	</tfoot>
</table>
<?php else :
    // display not found post notice
    conikal_not_found_post(__('No withdrawal found.', 'campoal'), __("We're sorry, but no withdrawal request has been sent yet.", 'campoal'));
endif; ?>
<?php wp_nonce_field('withdraw_ajax_nonce', 'securityWithdraw', true); ?>