<?php
$message_status = array();
$status_page = (!empty($_GET['status'])) ? $_GET['status'] : '';
$search_str = (!empty($_GET['s'])) ? sanitize_text_field($_GET['s']) : '';
$per_page = 15;
if (!empty($_GET['remove']) && is_numeric($_GET['remove'])) $message_status = yascode_vip::remove_subscriber($_GET['remove']);
$paged = (!empty($_GET['paged']) && is_numeric($_GET['paged'])) ? $_GET['paged'] : 1;
$subscribers = yascode_vip::get_subscribers($per_page, $paged, $status_page, $search_str);
if (!empty($_GET['status'])) $message_status['status'] = $_GET['status'];
if (!empty($_GET['msg'])) $message_status['text'] = $_GET['msg']; ?>
<div class="wrap subscribers-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['status'])) ? $message_status['status'] : $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' => 'subscribers'), admin_url('admin.php')); ?>" class="<?php echo (empty($status_page)) ? 'current' : ''; ?>" aria-current="page">همه <span class="count">(<?php echo $subscribers['count_all']; ?>)</span></a> |</li>
                <li class="active"><a href="<?php echo add_query_arg(array('page' => 'subscribers', 'status' => 'active'), admin_url('admin.php')); ?>" class="<?php echo (!empty($status_page) && $status_page == 'active') ? 'current' : ''; ?>">فعال <span class="count">(<?php echo $subscribers['count_active']; ?>)</span></a> |</li>
                <li class="deactive"><a href="<?php echo add_query_arg(array('page' => 'subscribers', 'status' => 'expired'), admin_url('admin.php')); ?>" class="<?php echo (!empty($status_page) && $status_page == 'expired') ? 'current' : ''; ?>">منقضی <span class="count">(<?php echo $subscribers['count_expired']; ?>)</span></a></li>
            </ul>

            <form id="vips-discount-filter" method="GET" action="<?php echo esc_url(add_query_arg('page', 'subscribers', admin_url('admin.php'))); ?>">
                <input type="hidden" name="page" value="subscribers">
                <input type="hidden" name="status" value="<?php echo $status_page; ?>">
                <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="deactive">غیرفعال کردن اشتراک</option>
                            <option value="active">فعال کردن اشتراک</option>
                        </select>
                        <input type="submit" class="button action" value="اجرا">
                    </div>
                    <p class="alignleft 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>
                    <br class="clear">
                </div>
                <table class="wp-list-table widefat fixed striped membershiplevels vip-discount">
                    <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 class="manage-column column-subscriber column-primary">مشترک</th>
                            <th class="manage-column column-status">وضعیت</th>
                            <th class="manage-column column-expiration">تاریخ انقضا</th>
                            <th class="manage-column column-renewed">بروز شده</th>
                            <th class="manage-column column-created">تاریخ ایجاد</th>
                        </tr>
                    </thead>
                    <tbody id="the-list" class="ui-sortable">
                        <?php if (!empty($subscribers['items'])) :
                            foreach ($subscribers['items'] as $subscriber) :
                                $ID = (!empty($subscriber->ID)) ? $subscriber->ID : '';
                                $user_id = (!empty($subscriber->user_id)) ? $subscriber->user_id : 0;
                                $expiration_date = (!empty($subscriber->expiration_date)) ? $subscriber->expiration_date : '';
                                $status_subscriber = (!empty($subscriber->status)) ? $subscriber->status : '';
                                $updated_at = (!empty($subscriber->updated_at)) ? $subscriber->updated_at : '';
                                $created_at = (!empty($subscriber->created_at)) ? $subscriber->created_at : '';
                                $user_data = get_userdata($user_id);
                                $update = (!empty($updated_at)) ? jdate('d / m / Y', strtotime($updated_at)) : '';
                                $date = (!empty($created_at)) ? jdate('d / m / Y', strtotime($created_at)) : '';
                                $expire_date = (!empty($expiration_date)) ? jdate('d / m / Y', strtotime($expiration_date)) : 'نامحدود';
                                $status_s = (!empty($status_subscriber)) ? 'فعال' : 'منقضی'; ?>
                                <tr>
                                    <th scope="row" class="check-column"><input type="checkbox" name="select_subs[]" 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="edit"><a href="<?php echo admin_url('/user-edit.php?user_id=' . $user_id); ?>" target="_blank">ویرایش</a> | </span>
                                            <span class="delete"><span class="trash"><a href="<?php echo add_query_arg(array('page' => 'subscribers', 'remove' => $ID), admin_url('admin.php')); ?>" class="delete_discount">پاک کردن</a></span> | </span>
                                            <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="time column-time" data-colname="وضعیت"><?php echo $status_s; ?></td>
                                    <td class="expire column-expire" data-colname="تاریخ انقضا"><?php echo $expire_date; ?></td>
                                    <td class="updated column-updated" data-colname="بروز شده"><?php echo $update; ?></td>
                                    <td class="updated column-updated" data-colname="ثبت شده در"><?php echo $date; ?></td>
                                </tr>
                        <?php endforeach;
                        endif ?>
                    </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 class="manage-column column-subscriber column-primary">مشترک</th>
                            <th class="manage-column column-status">وضعیت</th>
                            <th class="manage-column column-expiration">تاریخ انقضا</th>
                            <th class="manage-column column-renewed">بروز شده</th>
                            <th class="manage-column column-created">تاریخ ایجاد</th>
                        </tr>
                    </tfoot>
                </table>
            </form>

            <div class="tablenav bottom">
                <div class="tablenav-pages"><span class="displaying-num"><?php echo $subscribers['count']; ?> مورد</span>
                    <?php
                    $big = 999999999;
                    echo paginate_links(array(
                        'base' => str_replace($big, '%#%', get_pagenum_link($big, false)),
                        'total' => $subscribers['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>