<?php
//DB연결
$server_root_path = $_SERVER['DOCUMENT_ROOT'];
require_once $server_root_path . "/database/mysqli_connection.php";


$query = "SELECT * FROM vpnid WHERE server_name='$HTTP_HOST' AND ( is_admin IS NULL OR is_admin='' ) ORDER BY no DESC";
$result = $db->query($query);
$total = mysqli_num_rows($result);


$output_data = "";
$output_data .= "<style>
.vpnid-list-wrap { width:100%; max-width:900px; margin:0 auto; font-family:'Segoe UI','Apple SD Gothic Neo','Malgun Gothic',Arial,sans-serif; }
.vpnid-list-table { width:100%; border-radius:14px; overflow:hidden; background:rgba(34,44,64,0.98); box-shadow:0 2px 16px rgba(40,60,120,0.13); border-collapse:separate; border-spacing:0; }
.vpnid-list-table thead tr { background:linear-gradient(90deg,#2e3a5a 60%,#5a6a8a 100%); }
.vpnid-list-table th, .vpnid-list-table td { font-size:1.08em; padding:15px 8px; text-align:center; }
.vpnid-list-table th { color:#e0eaff; font-weight:700; border-bottom:2px solid #3a4a6a; letter-spacing:0.1em; }
.vpnid-list-table td { color:#e0eaff; border-bottom:1px solid #2e3a5a; background:rgba(34,44,64,0.92); transition:background 0.15s; }
.vpnid-list-table tr:hover td { background:rgba(44,64,120,0.18); }
.vpnid-list-table .id-cell { color:#35d4c7; font-weight:700; font-family:monospace; }
.vpnid-list-table .expired { color:orangered; font-weight:700; }
.vpnid-list-table .expire-soon { background:orangered; color:#fff; font-weight:700; border-radius:5px; padding:2px 6px; }
.vpnid-list-table .memo-cell { color:#b2cfff; }
.vpnid-list-table .action-cell span { display:inline-block; margin:0 2px; padding:6px 14px; border-radius:6px; font-size:0.98em; font-weight:600; cursor:pointer; transition:background 0.15s, color 0.15s; }
.vpnid-list-table .action-cell .edit { background:#b2cfff; color:#2e3a5a; }
.vpnid-list-table .action-cell .edit:hover { background:#35d4c7; color:#fff; }
.vpnid-list-table .action-cell .del { background:#2e3a5a; color:#fff; }
.vpnid-list-table .action-cell .del:hover { background:orangered; color:#fff; }
.vpnid-list-header { display:flex; justify-content:space-between; align-items:center; margin-bottom:18px; }
.vpnid-list-header .add-btn { background:#35d4c7; color:#fff; font-size:1.05em; font-weight:600; border:none; border-radius:7px; padding:8px 22px; cursor:pointer; transition:background 0.15s; }
.vpnid-list-header .add-btn:hover { background:#2e3a5a; color:#fff; }
@media (max-width:600px) {
  .vpnid-list-wrap { max-width:100vw; }
  .vpnid-list-table th, .vpnid-list-table td { font-size:0.98em; padding:8px 2px; }
  .vpnid-list-header .add-btn { padding:7px 10px; font-size:0.97em; }
}
</style>";
$output_data .= "<div class='vpnid-list-wrap'>";
$output_data .= "  <div class='vpnid-list-header'>";
$output_data .= "    <button class='add-btn' onclick='list_id_popup_new();'>+ 아이디 추가</button>";
$output_data .= "    <span style='font-size:1.01em; color:#b2cfff; font-weight:400; letter-spacing:0.1em;'>총 <b style=\"color:#35d4c7; font-weight:700;\">$total</b>개</span>";
$output_data .= "  </div>";
$output_data .= "  <table class='vpnid-list-table'>";
$output_data .= "    <thead><tr>";
$output_data .= "      <th style='width:25%;'>등록/만료일시</th>";
$output_data .= "      <th style='width:25%;'>아이디</th>";
$output_data .= "      <th style='width:30%;'>메모</th>";
$output_data .= "      <th style='width:20%;'>기능</th>";
$output_data .= "    </tr></thead>";
$output_data .= "    <tbody>";

if ($total > 0) {
  while( $row = mysqli_fetch_array($result) ) {
	$member_id_data = $row['vpn_user_id'];
	$member_memo_data = $row['member_memo'];
	$member_id_expire_data = $row['member_id_expire'];
	$member_id_expire_data_short = substr($row['member_id_expire'],0,10);
	if ($member_id_expire_data == "") {
	  $member_id_expire_data = "0000-00-00 00:00:00";
	  $member_id_expire_data_short = "0000-00-00";
	}
	if (strtotime($member_id_expire_data_short . " 23:59:59") < strtotime(date("Y-m-d H:i:s"))) {
	  $expire_class = "expired";
	} else if (strtotime($member_id_expire_data_short . " 23:59:59" . "-1 days") < strtotime(date("Y-m-d H:i:s"))) {
	  $expire_class = "expire-soon";
	} else {
	  $expire_class = "";
	}
	$output_data .= "<tr>";
	$output_data .= "  <td><span style='font-size:0.98em; color:#b2cfff;'>" . substr($row['datetime'],0,19) . "</span><br><span class='".$expire_class."'>" . $member_id_expire_data . "</span></td>";
	$output_data .= "  <td class='id-cell'>" . htmlspecialchars($row['vpn_user_id']) . "</td>";
	$output_data .= "  <td class='memo-cell'>" . htmlspecialchars($row['member_memo']) . "</td>";
	$output_data .= "  <td class='action-cell'>";
	$output_data .= "    <span class='edit' onclick=\"list_id_popup_edit('".addslashes($member_id_data)."','".addslashes($member_id_expire_data_short)."','".addslashes($member_memo_data)."'); return false;\">변경</span>";
	$output_data .= "    <span class='del' onclick=\"list_id_popup_del('".addslashes($member_id_data)."','".addslashes($member_id_expire_data_short)."','".addslashes($member_memo_data)."'); return false;\">삭제</span>";
	$output_data .= "  </td>";
	$output_data .= "</tr>";
  }
} else {
  $output_data .= "<tr><td colspan='4' style='color:#b2cfff; font-size:1.08em; padding:38px 0; text-align:center;'>등록된 VPN 아이디가 없습니다.</td></tr>";
}
$output_data .= "    </tbody></table></div>";


if ($total > 0) {
	while( $row = mysqli_fetch_array($result) ) {
		$member_id_data = $row['vpn_user_id'];
		$member_memo_data = $row['member_memo'];
		$member_id_expire_data = $row['member_id_expire'];
		$member_id_expire_data_short = substr($row['member_id_expire'],0,10);
		if ($member_id_expire_data == "") {
			$member_id_expire_data = "0000-00-00 00:00:00";
			$member_id_expire_data_short = "0000-00-00";
		}

		if (strtotime($member_id_expire_data_short . " 23:59:59") < strtotime(date("Y-m-d H:i:s"))) {
			$member_id_expire_data_display = "<span style='color:orangered;'>" . $member_id_expire_data . "</span>";
		} else if (strtotime($member_id_expire_data_short . " 23:59:59" . "-1 days") < strtotime(date("Y-m-d H:i:s"))) {
			$member_id_expire_data_display = "<span style='background:orangered; color:#ffffff;'>" . $member_id_expire_data . "</span>";
		} else {
			$member_id_expire_data_display = "<span style='color:#ffffff;'>" . $member_id_expire_data . "</span>";
		}


		$output_data .= "<div style='width:50%; min-width:400px; max-width:600px; height:auto; display:flex; justify-content:center; align-items:center;; flex-direction:column;'>";
		$output_data .= "	<div style='width:100%; height:auto; display:flex; justify-content:center; align-items:center;; flex-direction:row; border:1px solid #333333;'>";
		$output_data .= "		<div style='width:27%; height:auto; padding:10px; text-align:center; border-right:1px solid #333333;'>" . substr($row['datetime'],0,19) . "<br>". $member_id_expire_data_display . "</div>";
		$output_data .= "		<div style='width:30%; height:auto; padding:10px; text-align:center; border:0; border-right:1px solid #333333; color:#35d4c7;'>" . $row['vpn_user_id'] . "</div>";
		$output_data .= "		<div style='width:30%; height:auto; padding:10px; text-align:center; border:0; border-right:1px solid #333333;'>" . $row['member_memo'] . "</div>";
		$output_data .= "		<div style='width:18%; height:auto; padding:10px 0px 10px; text-align:center; border:0;'><span style='background:#dddddd; color:#333333; padding:5px; cursor:pointer;' onclick='list_id_popup_edit(\"$member_id_data\",\"$member_id_expire_data_short\",\"$member_memo_data\"); return false;'>변경</span> <span  style='background:#333333; color:#ffffff; padding:5px; cursor:pointer;' onclick='list_id_popup_del(\"$member_id_data\",\"$member_id_expire_data_short\",\"$member_memo_data\"); return false;'>삭제</span></div>";
		$output_data .= "	</div>";
		$output_data .= "</div>";
	}
}

echo $output_data;
?>