<?php 

function bank_deposit_cb() {
	global $post_meta;
	$post = new post;
	$user = new user;
	if(!($token = verify_token($_POST[SALT]))) {
        do_action("_display_message", ["msg" => '<i class="fa fa-warning"></i> Sorry, it seems that you submitted your request through invalid token. Please try again later.']);
        return;
    }
    $user_meta = $user->get_user_meta($_POST['user_id']);
    $name_on_invoice = apply_filters("pm_name_on_invoice", get_fullname($user_meta), $user_meta);
    $invoice = new pm_invoice($_POST['user_id']);
    $invoice->set_invoice_by_number($_POST['in_ref']);
    $fee_meta = apply_filters("pm_fee_meta", $invoice->invoice);
    $existing = db_post_unique(["type" => "credit", "in_ref" => $_POST['in_ref'], "user_id" => $_POST['user_id']]);
    $bank_accounts = $post->get_post_by_meta_data(['type' => 'pm_bank_account', 'status' => 'active']);
    $banks = pm_bank_accounts_option();
	?>
    <!-- <div class="row"> -->
        <style type="text/css">
            .account-details.card h4 {
                font-size: 1.8rem;
            }
            .card.account-details:hover {
                background-color: #f7f7f7;
                box-shadow: 5px 5px 5px #ececec;
            }
        </style>
        <div class="panel panel-primary">
            <!-- <div class="panel-heading">
                <strong class="panel-title">Payment Details</strong>
            </div> -->
            <div class="panel-body">
                    <div class="col-md-6">
                        <h3 class="mt-3 mb-2 text-center">ACCOUNT DETAILS</h3>
                        <?php if (_count($bank_accounts)):
                            foreach ($bank_accounts as $bank) {
                            ?>
                                <div class="card mb-4 account-details">
                                    <div class="card-body">
                                        <div><h4>Account Number <span class="pull-right"><?php echo $bank['account_number']; ?></span></h4></div>
                                        <div><h4>Account Name <span class="pull-right"><?php echo $bank['account_name']; ?></span></h4></div>
                                        <div><h4>Account Type <span class="pull-right"><?php echo $bank['account_type']; ?></span></h4></div>
                                        <div><h4>Bank Name <span class="pull-right"><?php echo $bank['bank_name']; ?></span></h4></div>
                                        <?php if (trim($bank['instructions']) !== ""): ?>
                                            <div><h4 class="text-center">PAYMENT INSTRUCTION</h4></div>
                                            <div class="pl-3 pr-3 pt-3 pb-5"><?php echo $bank['instructions']; ?></div>
                                        <?php endif ?>
                                    </div>
                                </div><hr>
                        <?php 
                            }
                        endif; ?>
                    
                    <?php if ($existing && $existing['status'] == 'confirmed' && $invoice->status() == 'confirmed'): ?>
                        <div class="green"><strong>Your Payment has already been confirmed</strong></div>
                    <?php else: ?>
                    <h3 class="mt-5 text-center">UPLOAD YOUR PROOF OF PAYMENT</h3>
                    <?php 
                        do_action('drag_n_drop_uploader', ["ref" => "pop-".$_POST['in_ref'], "user_id" => $_POST['user_id'], "accepts" => "image/*,application/pdf", "max_files" => 3]);
                     ?>
                    <form id="submit-payment-form" action="contents/plugins/payment_module/ajax.php" method="POST" data-callback="disable" data-callbackid="#submit-bank_deposit" data-dontcallbackonload=1>
                        <input type="hidden" name="<?php echo SALT; ?>" value="<?php echo $_POST[SALT]; ?>">
                        <input type="hidden" name="amount" value="<?php echo $invoice->total(true); ?>">
                        <input type="hidden" name="user_id" value="<?php echo $_POST['user_id']; ?>">
                        <input type="hidden" name="in_ref" value="<?php echo $_POST['in_ref']; ?>">
                        <?php 
                        select(['label' => 'Bank Account Used', 'id' => 'bank_used', 'required' => 'required', 'hint' => 'This will help in easy reconciliation and tracing of fund'], $banks);
                        submit_btn("bank_deposit", "Submit Payment"); ?>
                    </form>
                    <?php endif; ?>
                </div>
            	<div class="col-md-6">
                    <table class="table table-bordered">
                        <tbody>
                            <tr>
                                <td>Name</td>
                                <td><?php echo $name_on_invoice ?></td>
                            </tr>
                            <tr>
                                <td>Payment receipt will be sent</td>
                                <td><?php echo $user_meta['email'] ?></td>
                            </tr>
                            <tr>
                                <td>Description</td>
                                <td><?php echo $invoice->description(); ?></td>
                            </tr>
                            <tr>
                                <td>Amount</td>
                                <td><?php echo toMoney($invoice->total(true)) ?></td>
                            </tr>
                            <tr>
                                <td>Status</td>
                                <td><?php echo $invoice->status(); ?></td>
                            </tr>
                        </tbody>
                    </table>
                    <div class="notice">
                        <div class="notice-heading"><h1 class="notice-title">How to Pay</h1></div>
                        <div class="notice-body">
                            <p>You are expected to deposit the exact amount due to the appropriate account from the list of account numbers provided on the right hand side of the document.</p>
                            <p>After successful payment which can either be through transfer or bank deposit, you are required to upload your evidence(s) of payment below and click on submit payment.</p>
                            <p class="_red">Note that if you did not submit payment, your payment process is not yet complete. Note also that submission of fake teller or fake evidences to claim payment is highly prohibited and punishable.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <!-- </div> -->
	<?php
    add_page_javascript('app/assets/js/page-scripts/submit.js');
    add_page_javascript('app/assets/js/page-scripts/js-functions.js');
}