<?php 
global $wpdb;
$wpdb->erphpact = $wpdb->prefix.'erphpdown_activation';

$activation_table_name = $wpdb->prefix.'erphpdown_activation';
$activation_sql = "CREATE TABLE ".$activation_table_name." (
		id bigint(15) NOT NULL AUTO_INCREMENT,
		num varchar(100),
		uid int(10) DEFAULT '0',
		pid int(10) DEFAULT '0',
		usetime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
		status int(1) DEFAULT '0' NOT NULL,
		ctime datetime,
		UNIQUE KEY id (id)
		);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($activation_sql);


function erphpact_admin_pagenavi($total_count, $number_per_page=15){

	$current_page = isset($_GET['paged'])?$_GET['paged']:1;

	if(isset($_GET['paged'])){
		unset($_GET['paged']);
	}

	$base_url = add_query_arg($_GET,admin_url('admin.php'));

	$total_pages	= ceil($total_count/$number_per_page);

	$first_page_url	= $base_url.'&amp;paged=1';
	$last_page_url	= $base_url.'&amp;paged='.$total_pages;
	
	if($current_page > 1 && $current_page < $total_pages){
		$prev_page		= $current_page-1;
		$prev_page_url	= $base_url.'&amp;paged='.$prev_page;

		$next_page		= $current_page+1;
		$next_page_url	= $base_url.'&amp;paged='.$next_page;
	}elseif($current_page == 1){
		$prev_page_url	= '#';
		$first_page_url	= '#';
		if($total_pages > 1){
			$next_page		= $current_page+1;
			$next_page_url	= $base_url.'&amp;paged='.$next_page;
		}else{
			$next_page_url	= '#';
		}
	}elseif($current_page == $total_pages){
		$prev_page		= $current_page-1;
		$prev_page_url	= $base_url.'&amp;paged='.$prev_page;
		$next_page_url	= '#';
		$last_page_url	= '#';
	}
	?>
	<div class="tablenav bottom">
		<div class="tablenav-pages">
			<span class="displaying-num">每页 <?php echo $number_per_page;?>，共 <?php echo $total_count;?>个项目 </span>
			<span class="pagination-links">
				<a class="first-page <?php if($current_page==1) echo 'disabled'; ?>" title="前往第一页" href="<?php echo $first_page_url;?>">«</a>
				<a class="prev-page <?php if($current_page==1) echo 'disabled'; ?>" title="前往上一页" href="<?php echo $prev_page_url;?>">‹</a>
				<span class="paging-input">第 <?php echo $current_page;?> 页，共 <span class="total-pages"><?php echo $total_pages; ?></span> 页</span>
				<a class="next-page <?php if($current_page==$total_pages) echo 'disabled'; ?>" title="前往下一页" href="<?php echo $next_page_url;?>">›</a>
				<a class="last-page <?php if($current_page==$total_pages) echo 'disabled'; ?>" title="前往最后一页" href="<?php echo $last_page_url;?>">»</a>
			</span>
		</div>
		<br class="clear">
	</div>
	<?php
}

function getErphpActivation($id){
	global $wpdb;
	$result = $wpdb->get_row("select * from $wpdb->erphpact where id = '".$id."'");
	return $result;
}

function getErphpActStatus($id){
	global $wpdb;
	$result = $wpdb->get_row("select * from $wpdb->erphpact where id = '".$id."'");
	if($result->status == 1){
		return '已发放，用户ID：'.get_user_by('id',$result->uid)->user_login.'，发放日期：'.$result->usetime;
	}else{
		return '无';
	}
}

function doErphpAct($uid,$pid){
	global $wpdb;
	$result = $wpdb->get_row("select * from $wpdb->erphpact where status = 0 and pid=$pid limit 1");
	$wpdb->query("update $wpdb->erphpact set status=1,usetime='".date("Y-m-d H:i:s")."',uid=$uid where id=".$result->id);
	return $result->num;
}
