<?php
$message_status = array();
$per_page = 15;
if (!empty($_GET['remove']) && is_numeric($_GET['remove'])) $message_status = yascode_vip::remove_discount($_GET['remove']);
$paged = (!empty($_GET['paged']) && is_numeric($_GET['paged'])) ? $_GET['paged'] : 1;
$discounts = yascode_vip::get_discounts($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' => 'discounts', 'discount' => '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_discounts_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="title" class="manage-column column-name column-primary">کد تخفیف</th>
                            <th scope="col" id="percentage" class="manage-column column-time">درصد تخفیف</th>
                            <th scope="col" id="expiration" class="manage-column column-Price">تاریخ انقضا</th>
                            <th scope="col" id="plan" class="manage-column column-discount-price">پلن</th>
                            <th scope="col" id="date" class="manage-column column-discount-price">تاریخ ثبت</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        if (!empty($discounts['items'])) {
                            foreach ($discounts['items'] as $discount) {
                                $ID = (!empty($discount->ID)) ? $discount->ID : '';
                                $created_at = (!empty($discount->created_at)) ? $discount->created_at : '';
                                $expire_at = (!empty($discount->expiration_date)) ? $discount->expiration_date : '';
                                $date = (!empty($created_at)) ? jdate('d / m / Y', strtotime($created_at)) : '';
                                $expire = (!empty($expire_at)) ? jdate('d / m / Y', strtotime($expire_at)) : '';
                                $title = (!empty($discount->title)) ? $discount->title : '';
                                $percentage = (!empty($discount->percentage)) ? $discount->percentage : 0;
                                $plans = (!empty($discount->plan)) ? $discount->plan : ''; ?>
                                <tr>
                                    <th scope="row" class="check-column"><input type="checkbox" class="req-id" name="select_discounts[]" 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' => 'discounts', 'discount' => $ID), admin_url('admin.php')); ?>" target="_blank"><?php echo $title; ?></a></strong>
                                        <div class="row-actions">
                                            <span class="edit"><a href="<?php echo add_query_arg(array('page' => 'discounts', 'discount' => $ID), admin_url('admin.php')); ?>">ویرایش</a> | </span>
                                            <span class="delete"><span class="trash"><a href="<?php echo add_query_arg(array('page' => 'discounts', '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="description column-description" data-colname="درصد تخفیف"><?php echo $percentage . '%'; ?></td>
                                    <td class="description column-description" data-colname="تاریخ انقضا"><?php echo (!empty($expire)) ? $expire : 'ندارد'; ?></td>
                                    <td class="description column-description" data-colname="پلن خاص"><?php echo (!empty($plans)) ? $plans : 'همه'; ?></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="title" class="manage-column column-name column-primary">کد تخفیف</th>
                            <th scope="col" id="percentage" class="manage-column column-time">درصد تخفیف</th>
                            <th scope="col" id="expiration" class="manage-column column-Price">تاریخ انقضا</th>
                            <th scope="col" id="plan" class="manage-column column-discount-price">پلن</th>
                            <th scope="col" id="date" class="manage-column column-discount-price">تاریخ ثبت</th>
                        </tr>
                    </tfoot>

                </table>
            </form>

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