<?php
$message_status = array();
$status_page = (!empty($_GET['status'])) ? $_GET['status'] : '';
$order_page = (!empty($_GET['order'])) ? $_GET['order'] : '';
$search_str = (!empty($_POST['s'])) ? sanitize_text_field($_POST['s']) : '';
$per_page = 15;
if (!empty($_GET['remove']) && is_numeric($_GET['remove'])) $message_status = yascode_vip::remove_transaction($_GET['remove']);
$paged = (!empty($_GET['paged']) && is_numeric($_GET['paged'])) ? $_GET['paged'] : 1;
$transactions = yascode_vip::get_all_payments($per_page, $paged, $status_page, $order_page, $search_str);
if (!empty($_GET['status'])) $message_status['status'] = $_GET['status'];
if (!empty($_GET['msg'])) $message_status['text'] = $_GET['msg']; ?>
<div class="wrap transactions-page">
    <h2>تراکنش ها</h2>
    <div id="poststuff">
        <div id="post-body">
            <div class="msg-body">
                <?php if (!empty($message_status) && is_array($message_status) && !empty($message_status['text'])) {
                    $status = (!empty($message_status['stats'])) ? $message_status['stats'] : $message_status[0];
                    $sts = ($status != 'nok') ? 'success' : 'error';
                    $text = (!empty($message_status['text'])) ? $message_status['text'] : ''; ?>
                    <div class="custom-notice notice-<?php echo $sts; ?>">
                        <p class="content"><?php echo $text; ?></p><button type="button" class="notice-dismiss"></button>
                    </div>
                <?php } ?>
            </div>

            <ul class="subsubsub">
                <li class="all"><a href="<?php echo add_query_arg(array('page' => 'transactions'), admin_url('admin.php')); ?>" class="<?php echo (empty($status_page)) ? 'current' : ''; ?>">همه <span class="count">(<?php echo $transactions['count_all']; ?>)</span></a> |</li>
                <li class="publish"><a href="<?php echo add_query_arg(array('page' => 'transactions', 'status' => 'success'), admin_url('admin.php')); ?>" class="<?php echo (!empty($status_page) && $status_page == 'success') ? 'current' : ''; ?>">موفق <span class="count">(<?php echo $transactions['count_success']; ?>)</span></a> |</li>
                <li class="publish"><a href="<?php echo add_query_arg(array('page' => 'transactions', 'status' => 'failed'), admin_url('admin.php')); ?>" class="<?php echo (!empty($status_page) && $status_page == 'failed') ? 'current' : ''; ?>">ناموفق <span class="count">(<?php echo $transactions['count_failed']; ?>)</span></a> |</li>
                <li class="publish"><a href="<?php echo add_query_arg(array('page' => 'transactions', 'status' => 'pending'), admin_url('admin.php')); ?>" class="<?php echo (!empty($status_page) && $status_page == 'pending') ? 'current' : ''; ?>">در انتظار <span class="count">(<?php echo $transactions['count_pending']; ?>)</span></a></li>
            </ul>

            <form method="post" action="" class="from_def">

                <p class="search-box">
                    <label class="screen-reader-text" for="post-search-input">جست‌وجو:</label>
                    <input type="search" id="post-search-input" name="s" value="">
                    <input type="submit" id="search-submit" class="button" value="جست‌وجو">
                </p>

                <div class="tablenav top" style="margin-bottom: 10px;">
                    <div class="alignleft actions bulkactions">
                        <label for="bulk-action-selector-top" class="screen-reader-text">انتخاب کار دسته جمعی</label>
                        <select name="action" id="bulk-action-selector-top">
                            <option value="-1">کارهای دسته‌جمعی</option>
                            <option value="remove">حذف</option>
                            <option value="successful">موفق شود</option>
                            <option value="unsuccessful">ناموفق شود</option>
                        </select>
                        <input type="submit" id="doaction" class="button action" value="اجرا">
                    </div>

                    <ul class="subsubsub" style="margin-top:3px">
                        <li class="all"><a href="<?php echo add_query_arg(array('page' => 'transactions'), admin_url('admin.php')); ?>" class="<?php echo (empty($order_page)) ? 'current' : ''; ?>">همه</a> |</li>
                        <li class="publish"><a href="<?php echo add_query_arg(array('order' => 'today')); ?>" class="<?php echo (!empty($order_page) && $order_page == 'today') ? 'current' : ''; ?>">امروز</a> |</li>
                        <li class="publish"><a href="<?php echo add_query_arg(array('order' => 'yesterday')); ?>" class="<?php echo (!empty($order_page) && $order_page == 'yesterday') ? 'current' : ''; ?>">دیروز</a> |</li>
                        <li class="publish"><a href="<?php echo add_query_arg(array('order' => 'week')); ?>" class="<?php echo (!empty($order_page) && $order_page == 'week') ? 'current' : ''; ?>">هفته</a> |</li>
                        <li class="publish"><a href="<?php echo add_query_arg(array('order' => 'month')); ?>" class="<?php echo (!empty($order_page) && $order_page == 'month') ? 'current' : ''; ?>">ماه</a> |</li>
                        <li class="publish"><a href="<?php echo add_query_arg(array('order' => 'year')); ?>" class="<?php echo (!empty($order_page) && $order_page == 'year') ? 'current' : ''; ?>">سال</a> |</li>
                        <li class="publish">
                            مورد موفق : <?php echo $transactions['count_total_success']; ?> عدد |
                        </li>
                        <li class="publish">
                            مبلغ کل : <?php echo number_format($transactions['count_total_come']); ?> تومان
                        </li>
                    </ul>
                </div>
                <table class="wp-list-table widefat fixed striped table-view-list posts">
                    <thead>
                        <tr>
                            <td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1">انتخاب همه</label>
                                <input id="cb-select-all-1" type="checkbox">
                            </td>
                            <th scope="col" id="user" class="manage-column column-user column-primary">کاربر</th>
                            <th scope="col" id="price" class="manage-column column-price">مبلغ</th>
                            <th scope="col" id="status" class="manage-column column-status"> وضعیت</th>
                            <th scope="col" id="discount_code" class="manage-column column-discount-code">کد تخفیف</th>
                            <th scope="col" id="discount_percentage" class="manage-column column-discount-percent">درصد تخفیف</th>
                            <th scope="col" id="transaction" class="manage-column column-transaction">تراکنش</th>
                            <th scope="col" id="gateway" class="manage-column column-gateway">درگاه</th>
                            <th scope="col" id="gateway_transaction" class="manage-column column-gateway-transaction">شناسه پرداخت</th>
                            <th scope="col" id="date" class="manage-column column-date">تاریخ</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (!empty($transactions['items'])) {
                            foreach ($transactions['items'] as $transaction) {
                                $ID = (!empty($transaction->ID)) ? $transaction->ID : '';
                                $user_id = (!empty($transaction->user_id)) ? $transaction->user_id : 0;
                                $user_data = get_userdata($user_id);
                                $created_at = (!empty($transaction->created_at)) ? jdate('H:i:s - Y/m/d', strtotime($transaction->created_at)) : '';
                                $price = (!empty($transaction->price)) ? number_format($transaction->price) : 0;
                                $stats = (!empty($transaction->status)) ? yascode_vip::get_payment_status_info($transaction->status) : array();
                                $discount = (!empty($transaction->discount)) ? $transaction->discount : 'ندارد';
                                $percentage = (!empty($transaction->percentage)) ? $transaction->percentage : 0;
                                $trans_id = (!empty($transaction->transaction)) ? $transaction->transaction : '';
                                $authority = (!empty($transaction->authority)) ? $transaction->authority : '';
                                $gateway_id = (!empty($transaction->gateway_id)) ? $transaction->gateway_id : '';
                        ?>
                                <tr>
                                    <th scope="row" class="check-column"><input type="checkbox" class="trans-id" name="select_transactions[]" value="<?php echo $ID; ?>"></th>
                                    <td class="name column-name has-row-actions column-primary" data-colname="نام">
                                        <strong><a class="row-title" href="<?php echo admin_url('user-edit.php?user_id=' . $user_id . ''); ?>" target="_blank"><?php echo (!empty($user_data->user_login)) ? $user_data->user_login : 'نامشخص'; ?></a></strong>
                                        <div class="row-actions">
                                            <span class="level_id"><span class="rcp-sub-id-col rcp-id-col">ID: <?php echo $ID; ?></span></span>
                                        </div>
                                        <button type="button" class="toggle-row"><span class="screen-reader-text">نمایش جزئیات بیشتر</span></button>
                                    </td>
                                    <td class="description column-description" data-colname="قیمت"><?php echo $price . ' تومان'; ?></td>
                                    <td class="description column-description" data-colname="وضعیت">
                                        <span class="status <?php echo (!empty($stats['class'])) ? $stats['class']  : ''; ?>"><?php echo (!empty($stats['name'])) ? $stats['name'] : ''; ?></span>
                                    </td>
                                    <td class="description column-description" data-colname="کد تخفیف"><?php echo $discount; ?></td>
                                    <td class="description column-description" data-colname="درصد تخفیف"><?php echo $percentage; ?></td>
                                    <td class="description column-description" data-colname="تراکنش"><?php echo $trans_id; ?></td>
                                    <td class="description column-description" data-colname="درگاه"><?php echo $gateway_id; ?></td>
                                    <td class="description column-description" data-colname="شناسه پرداخت"><?php echo $authority; ?></td>
                                    <td class="description column-description" data-colname="تاریخ"><?php echo $created_at; ?></td>
                                </tr>
                        <?php
                            }
                        }
                        ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1">انتخاب همه</label>
                                <input id="cb-select-all-1" type="checkbox">
                            </td>
                            <th scope="col" id="user" class="manage-column column-user column-primary">کاربر</th>
                            <th scope="col" id="price" class="manage-column column-price">مبلغ</th>
                            <th scope="col" id="status" class="manage-column column-status"> وضعیت</th>
                            <th scope="col" id="discount_code" class="manage-column column-discount-code">کد تخفیف</th>
                            <th scope="col" id="discount_percentage" class="manage-column column-discount-percent">درصد تخفیف</th>
                            <th scope="col" id="transaction" class="manage-column column-transaction">تراکنش</th>
                            <th scope="col" id="gateway" class="manage-column column-gateway">درگاه</th>
                            <th scope="col" id="gateway_transaction" class="manage-column column-gateway-transaction">شناسه پرداخت</th>
                            <th scope="col" id="date" class="manage-column column-date">تاریخ</th>
                        </tr>
                    </tfoot>

                </table>
            </form>

            <div class="tablenav bottom">
                <div class="tablenav-pages"><span class="displaying-num"><?php echo $transactions['count']; ?> مورد</span>
                    <?php
                    $big = 999999999;
                    echo paginate_links(array(
                        'base' => str_replace($big, '%#%', get_pagenum_link($big, false)),
                        'total' => $transactions['total_pages'],
                        'current' => max(1, $paged),
                        'format' => '&paged=%#%',
                        'prev_text' => '‹',
                        'next_text' => '›',
                        'type' => 'admin',
                        'show_all' => false,
                        'end_size' => 2,
                        'mid_size' => 3
                    ));

                    ?>
                    <br class="clear">
                </div>
            </div>

        </div>
    </div>
</div>