<?
// 기본 설정 로딩
require_once 'config.php';
require_once 'function.php';
require_once 'lib.php';
?>

<?
$query = "select servicedb.service_code, servicedb.client_code,servicedb.service_line,servicedb.server_code,servicedb.service_ip,servicedb.service_open_date,servicedb.coupon,servicedb.coupon_use,clientdb.client_code,clientdb.client_name From servicedb Natural Join clientdb where servicedb.service_line NOT IN ( 'vpn', 'test','soloLine','substitution' ) and Client_Code not in ('C219','C116','C012','C497','C464','C526') and service_ip not like '27.125.204%' order by clientdb.client_name asc";
$coupon_data = @mysql_query($query, $db_connect) or die($db_q_error);
$total = mysql_num_rows($coupon_data);

$list_no = (!$_GET[ea]) ? '50' : $_GET[ea];

if ($list_no > $total) {$list_no = $total;}
$total_page = ($total != '0') ? ceil($total / $list_no) : '0';

$page = $_GET[curPage];

if (!$page) {$page = 1;} elseif ($page >= $total_page) {$page = $total_page;} else { $page = $page;}

$next_page = ($page-'1') * $list_no;
$next_no = $next_page + $list_no;

$query = "select servicedb.service_code, servicedb.client_code,servicedb.service_line,servicedb.server_code,servicedb.service_ip,servicedb.service_open_date,servicedb.coupon,servicedb.coupon_use,clientdb.client_code,clientdb.client_name From servicedb Natural Join clientdb where servicedb.service_line NOT IN ( 'vpn', 'test','soloLine','substitution' ) and Client_Code not in ('C219','C116','C012','C497','C464','C526') and service_ip not like '27.125.204%' order by clientdb.client_name,service_open_date asc limit " . $next_page . "," . $next_no;

//$query="select servicedb.service_code, servicedb.client_code,servicedb.service_line,servicedb.server_code,servicedb.service_ip,servicedb.service_open_date,servicedb.coupon,clientdb.client_code,clientdb.client_name From servicedb Natural Join clientdb where servicedb.service_line NOT IN ( 'vpn', 'test','soloLine','substitution' ) and Client_Code not in ('C219','C116','C012') order by service_code asc";

$coupon_data = @mysql_query($query, $db_connect) or die($db_q_error);
$total_coupon = mysql_query("select sum(coupon),sum(coupon_use) from servicedb", $db_connect) or die($db_q_error);
$total_coupon = mysql_fetch_assoc($total_coupon);
?>
	   			<div class="table-responsive" id="table">전체 남은 수량 : <?=$total_coupon['sum(coupon)'];?> / 전체 사용 수량 : <?=$total_coupon['sum(coupon_use)']?><br><br>
	   				<input type="hidden" id="token">
					<table class="table table-bordered table-hover table-striped" style="text-align:center;">
						<thead>
							<tr>
								<th style="text-align:center;">No</th>
								<th style="text-align:center;">발급쿠폰</th>
								<th style="text-align:center;">남은수량</th>
								<th style="text-align:center;">사용완료</th>
								<th style="text-align:center;">고객명</th>
								<th style="text-align:center;">서비스코드</th>
								<th style="text-align:center;">장비명</th>
								<th style="text-align:center;">서버IP</th>
								<th style="text-align:center;">서비스개시일</th>
								<th style="text-align:center;">회선종류</th>
							</tr>
						</thead>
						<tbody>
<?
for ($i = $next_page; $i < $next_no; $i++) {
    $coupon = mysql_fetch_assoc($coupon_data);
    $num = ($total) - $i;

    $server_date = explode('-', $coupon['service_open_date']);
    $server_timestamp = mktime('0', '0', '0', $server_date[1], $server_date[2], $server_date[0]);

    $timestamp = mktime('0', '0', '0', '18', '9', '2015');
//echo $timestamp.$server_timestamp; // 기준일
    $default = $timestamp - 32676480; // 1432721520 <- 기준점에서 1년전 타임스탬프
    ?>
		<tr onclick="location.href='/IdcCouponUseMK.cup?client_code=<?=$coupon[client_code]?>'" style="cursor: pointer;">
			<td><?=$num?></td>
			<td><?=$coupon[coupon] + $coupon[coupon_use]?></td>
			<td><strong><font color=blue><?=$coupon[coupon]?></font></strong><?// if ( $server_timestamp < $default ) { echo "<font color=blue>5</font>"; } else { echo "<font color=red>3</font>";}?></td>
			<td><?=$coupon[coupon_use]?></td>
			<td><?=$coupon[client_name]?></td>
			<td><?=$coupon[service_code]?></td>
			<td><?=$coupon[server_code]?></td>
			<td><?=$coupon[service_ip]?></td>
			<td><?=$coupon[service_open_date]?></td>
			<td><?=$coupon[service_line]?></td>
		</tr>
<?}?>
						</tbody>
						<tfoot>
						</tfoot>
					</table>
				</div>

<div align='center'>
<?
$setup_dir = "http://" . $_SERVER['HTTP_HOST'] . ":6752";
$page_scale = 10;
$page3 = floor(($total_page-'1') / $page_scale);
$n_page = floor(($page-'1') / $page_scale);

if ($n_page > 0) {

    $p_start = (($n_page - 1) * $page_scale)+'1';
    $link = "<a onfocus=blur() href='" . $setup_dir . "/IdcCouponListMK.cup?ea=$_GET[ea]&curPage=" . $p_start . "'>";
    $link .= "<<";
    $link .= "</a>";
    echo $link . " ";
}
$is = ($n_page * $page_scale)+'1';
for ($i = $is; $i < $is + $page_scale; $i++) {

    if ($i < $total_page+'1') {
        if ($i == $page) {$ib = "<b>$i</b>";} else { $ib = $i;}
        $link = "<a onfocus=blur() href='" . $setup_dir . "/IdcCouponListMK.cup?ea=$_GET[ea]&curPage=" . $i . "'>";
        $link .= $ib;
        $link .= "</a>";
        echo $link . " ";
    }
}
if ($n_page < $page3) {
    $link = "<a onfocus=blur() href='" . $setup_dir . "/IdcCouponListMK.cup?ea=$_GET[ea]&curPage=" . $i . "'>";
    $link .= ">>";
    $link .= "</a>";
    echo $link;
}

?>
</div>
<?
//DB닫기
mysql_close($db_connect);
?>
