<?php
$message_status = array();
$per_page = 15;
if (!empty($_GET['remove']) && is_numeric($_GET['remove'])) $message_status = yascode_vip::remove_plan($_GET['remove']);
$paged = (!empty($_GET['paged']) && is_numeric($_GET['paged'])) ? $_GET['paged'] : 1;
$plans = yascode_vip::get_plans($per_page, $paged);
if (!empty($_GET['status'])) $message_status['status'] = $_GET['status'];
if (!empty($_GET['msg'])) $message_status['text'] = $_GET['msg']; ?>
<div class="wrap plans-page">
    <h2>اشتراک ویژه</h2>
    <a href="<?php echo add_query_arg(array('page' => 'subscriptions', 'plan' => 'new'), admin_url('admin.php')); ?>" class="page-title-action">پلن جدید</a>
    <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>
            <form method="post" action="" class="from_def">
                <div class="tablenav top">
                    <div class="alignleft actions bulkactions" style="float: right; margin-bottom: 15px;">
                        <label for="bulk-action-selector-top" class="screen-reader-text">انتخاب کار دسته جمعی</label>
                        <select name="action_item">
                            <option value="-1">کارهای دسته‌جمعی</option>
                            <option value="remove">حذف</option>
                        </select>
                        <input type="submit" name="select_plans_action" class="button action" value="اجرا">
                    </div>
                </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="name" class="manage-column column-name column-primary">عنوان</th>
                            <th scope="col" id="time" class="manage-column column-time">مدت زمان</th>
                            <th scope="col" id="price" class="manage-column column-Price"> قیمت</th>
                            <th scope="col" id="discount_price" class="manage-column column-discount-price">قیمت تخفیف خورده</th>
                            <th scope="col" id="date" class="manage-column column-date">تاریخ ثبت</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (!empty($plans['items'])) {
                            foreach ($plans['items'] as $plan) {
                                $ID = (!empty($plan->ID)) ? $plan->ID : '';
                                $created_at = (!empty($plan->created_at)) ? $plan->created_at : '';
                                $date = (!empty($created_at)) ? jdate('d / m / Y', strtotime($created_at)) : '';
                                $name = (!empty($plan->name)) ? $plan->name : '';
                                $time = (!empty($plan->time)) ? $plan->time : 0;
                                $price = (!empty($plan->price)) ? number_format($plan->price) : 0;
                                $discount_price = (!empty($plan->discount_price)) ? number_format($plan->discount_price) : 0; ?>
                                <tr>
                                    <th scope="row" class="check-column"><input type="checkbox" class="req-id" name="select_plans[]" 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 add_query_arg(array('page' => 'subscriptions', 'plan' => $ID), admin_url('admin.php')); ?>" target="_blank"><?php echo $name; ?></a></strong>
                                        <div class="row-actions">
                                            <span class="edit"><a href="<?php echo add_query_arg(array('page' => 'subscriptions', 'plan' => $ID), admin_url('admin.php')); ?>">ویرایش</a> | </span>
                                            <span class="delete"><span class="trash"><a href="<?php echo add_query_arg(array('page' => 'subscriptions', 'remove' => $ID), admin_url('admin.php')); ?>" class="delete_subscription">پاک کردن</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="description column-description" data-colname="مدت زمان"><?php echo $time; ?></td>
                                    <td class="description column-description" data-colname="قیمت"><?php echo $price . ' تومان'; ?></td>
                                    <td class="description column-description" data-colname="تخفیف خورده"><?php echo $discount_price . ' تومان'; ?></td>
                                    <td class="description column-description" data-colname="تاریخ ثبت"><?php echo $date; ?></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="name" class="manage-column column-name column-primary">عنوان</th>
                            <th scope="col" id="time" class="manage-column column-time">مدت زمان</th>
                            <th scope="col" id="price" class="manage-column column-Price"> قیمت</th>
                            <th scope="col" id="discount_price" class="manage-column column-discount-price">قیمت تخفیف خورده</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 $plans['count']; ?> مورد</span>
                    <?php
                    $big = 999999999;
                    echo paginate_links(array(
                        'base' => str_replace($big, '%#%', get_pagenum_link($big, false)),
                        'total' => $plans['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>