/*******************************************
* global variables
*******************************************/
global $wpdb;
// the plugin base directory
global $rcp_base_dir;
// not used any more, but just in case someone else is
$rcp_base_dir = dirname(__FILE__);
// load the plugin options
$rcp_options = get_option('rcp_settings');
global $rcp_db_name;
$rcp_db_name = rcp_get_levels_db_name();
global $rcp_db_version;
$rcp_db_version = '1.5';
global $rcp_discounts_db_name;
$rcp_discounts_db_name = rcp_get_discounts_db_name();
global $rcp_discounts_db_version;
$rcp_discounts_db_version = '1.2';
global $rcp_payments_db_name;
$rcp_payments_db_name = rcp_get_payments_db_name();
global $rcp_payments_db_version;
$rcp_payments_db_version = '1.4';
/* settings page globals */
global $rcp_members_page;
global $rcp_subscriptions_page;
global $rcp_discounts_page;
global $rcp_payments_page;
global $rcp_settings_page;
global $rcp_reports_page;
global $rcp_export_page;
global $rcp_help_page;
 /**
  * Get things started
  *
  * @since   1.5
  */
 function __construct()
 {
     $this->db_name = rcp_get_discounts_db_name();
     $this->db_version = '1.2';
 }
    public function draw_page()
    {
        global $wpdb;
        $page = admin_url('/admin.php?page=rcp-gifts');
        ?>
		<div class="wrap">
			<?php 
        // get all discounts ids
        $discounts_db = rcp_get_discounts_db_name();
        $discount_ids = $wpdb->get_col("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_edd_rcp_gift_id';");
        $discount_ids = implode(',', $discount_ids);
        $discounts = $wpdb->get_results("SELECT * FROM {$discounts_db} WHERE id IN (" . $discount_ids . ");");
        ?>
			<h2><?php 
        _e('Restrict Content Pro Gifts', 'rcp-gifts');
        ?>
</h2>

			<table class="wp-list-table widefat fixed posts">
				<thead>
					<tr>
						<th class="rcp-discounts-id-col"><?php 
        _e('ID', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-name-col" ><?php 
        _e('Name', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-desc-col"><?php 
        _e('Description', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-code-col" ><?php 
        _e('Code', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-subscription-col" ><?php 
        _e('Subscription', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-amount-col"><?php 
        _e('Amount', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-type-col"><?php 
        _e('Type', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-status-col"><?php 
        _e('Status', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-uses-col"><?php 
        _e('Uses', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-uses-left-col"><?php 
        _e('Uses Left', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-expir-col" ><?php 
        _e('Expiration', 'rcp-gifts');
        ?>
</th>
						<th class="rcp-discounts-actions-col" ><?php 
        _e('Actions', 'rcp-gifts');
        ?>
</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th><?php 
        _e('ID', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Name', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Description', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Code', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Subscription', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Amount', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Type', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Status', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Uses', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Uses Left', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Expiration', 'rcp-gifts');
        ?>
</th>
						<th><?php 
        _e('Actions', 'rcp-gifts');
        ?>
</th>
					</tr>
				</tfoot>
				<tbody>

				<?php 
        if ($discounts) {
            $i = 1;
            foreach ($discounts as $key => $discount) {
                ?>
						<tr class="rcp_row <?php 
                if (rcp_is_odd($i)) {
                    echo 'alternate';
                }
                ?>
">
							<td><?php 
                echo $discount->id;
                ?>
</td>
							<td><?php 
                echo stripslashes($discount->name);
                ?>
</td>
							<td><?php 
                echo stripslashes($discount->description);
                ?>
</td>
							<td><?php 
                echo $discount->code;
                ?>
</td>
							<td>
								<?php 
                if ($discount->subscription_id > 0) {
                    echo rcp_get_subscription_name($discount->subscription_id);
                } else {
                    echo __('All Levels', 'rcp-gifts');
                }
                ?>
							</td>
							<td><?php 
                echo rcp_discount_sign_filter($discount->amount, $discount->unit);
                ?>
</td>
							<td><?php 
                echo $discount->unit == '%' ? __('Percentage', 'rcp-gifts') : __('Flat', 'rcp-gifts');
                ?>
</td>
							<td>
								<?php 
                if (rcp_is_discount_not_expired($discount->id)) {
                    echo rcp_get_discount_status($discount->id) == 'active' ? __('active', 'rcp-gifts') : __('disabled', 'rcp-gifts');
                } else {
                    _e('expired', 'rcp-gifts');
                }
                ?>
							</td>
							<td><?php 
                if ($discount->max_uses > 0) {
                    echo rcp_count_discount_code_uses($discount->code) . '/' . $discount->max_uses;
                } else {
                    echo rcp_count_discount_code_uses($discount->code);
                }
                ?>
</td>
							<td><?php 
                echo rcp_discount_has_uses_left($discount->id) ? 'yes' : 'no';
                ?>
</td>
							<td><?php 
                echo $discount->expiration == '' ? __('none', 'rcp-gifts') : date_i18n('Y-m-d', strtotime($discount->expiration));
                ?>
</td>
							<?php 
                do_action('rcp_discounts_page_table_column', $discount->id);
                ?>
							<td>
								<?php 
                if (rcp_get_discount_status($discount->id) == 'active') {
                    ?>
									<a href="<?php 
                    echo add_query_arg('deactivate_discount', $discount->id, $page);
                    ?>
"><?php 
                    _e('Deactivate', 'rcp-gifts');
                    ?>
</a> |
								<?php 
                } else {
                    ?>
									<a href="<?php 
                    echo add_query_arg('activate_discount', $discount->id, $page);
                    ?>
"><?php 
                    _e('Activate', 'rcp-gifts');
                    ?>
</a> |
								<?php 
                }
                ?>
								<a href="<?php 
                echo add_query_arg('delete_discount', $discount->id, $page);
                ?>
" class="rcp_delete_discount"><?php 
                _e('Delete', 'rcp-gifts');
                ?>
</a>
							</td>
						</tr>
					<?php 
                $i++;
            }
        } else {
            ?>
				<tr><td colspan="11"><?php 
            _e('No discount codes added yet.', 'rcp-gifts');
            ?>
</td>
				<?php 
        }
        ?>
			</table>
		</div>
	<?php 
    }