<?php
include("CheckSession.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="description" content="馬戲團" />
	<title>馬戲團</title>
	<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>

<script language="JavaScript" type="text/JavaScript">
	<!-- 
function MM_openBrWindow(theURL,winName,win_width,win_height) { 
  var PosX = ((screen.width-win_width)/2); 
  var PosY = ((screen.height-win_height)/2);
  features = "status=no,toolbar=no,scrollbars=yes,resizable=yes,width="+win_width+",Height="+win_height+",top="+PosY+",left="+PosX;; 
  var urlStr = theURL;
  var newwin = window.open(urlStr,winName,features); 
}
//-->
</script>

<?php

require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

$AId = $_POST["AId"];
$AId1 = $_GET["AId"];
if ($AId == null || $AId == "") {
	$AId = $AId1;
}

$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$querySql = "select * from AdminUser where AId = " . $AId . " and AFlag = '1'";

$db->query($querySql);
$myrow = $db->fetch_array();
$db->close_db();
if ($myrow == null) {
	echo '<script type="text/javascript">';
	echo 'alert("無此使用者!!!");';
	echo 'location.href="CustomerIndex1.php"';
	echo '</script>';
	exit(0);
}
$ARightLevel = $myrow['ARightLevel'];
$AName = $myrow['AName'];
$AGroupId = $myrow['AGroupId'];

//设定每一页显示的记录数
$pagesize = 20;

//取得记录总数，计算总页数用
$db = new DBOperation();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$querySql = "";

//if($_SESSION['rightLevel'] == "0")
//{
//  if($AId == $_SESSION['userId'])
//  {
//  	$querySql = "select count(*) from Customer";
//  }
//  elseif($ARightLevel == "1")
//  {
//  	$querySql = "select count(*) from Customer and CCreateAcc = AId And AId in (select AId from AdminUser where AGroupId = ".$AGroupId."))";
//  }	
//  elseif($ARightLevel == "2")
//  {
//  	$querySql = "select count(*) from Customer where CCreateAcc = '".$AId."'";
//  }
//}
//elseif($_SESSION['rightLevel'] == "1")
//{
//	if($AId == $_SESSION['userId'])
//  {
//  	$querySql = "select count(*) from Customer and CCreateAcc = AId And AId in (select AId from AdminUser where AGroupId = ".$AGroupId."))";
//  }
//  elseif($ARightLevel == "2")
//  {
//  	$querySql = "select count(*) from Customer where CCreateAcc = '".$AId."'";
//  }
//}
//elseif($_SESSION['rightLevel'] == "2")
//{
//	$querySql = "select count(*) from Customer where CCreateAcc = '".$AId."'";
//}

$querySql = "select count(*) from Customer where CCreateAcc = '" . $AId . "'";

$querySqlCond = "";

//查詢條件
$QueryTel = $_POST["QueryTel"];
$QueryTel = trim($QueryTel);
$ChkG = $_POST["ChkG"];
$ChkB = $_POST["ChkB"];
$ChkR = $_POST["ChkR"];
if ($QueryTel != null && $QueryTel != "") {
	if ($ChkG != null && $ChkG != "") {
		$querySqlCond = $querySqlCond . " And CGreenTel like '%" . $QueryTel . "%'";
	}
	if ($ChkB != null && $ChkB != "") {
		$querySqlCond = $querySqlCond . " And CBlackTel like '%" . $QueryTel . "%'";
	}
	if ($ChkR != null && $ChkR != "") {
		$querySqlCond = $querySqlCond . " And CRedTel like '%" . $QueryTel . "%'";
	}
	if (($ChkG == null || $ChkG == "") && ($ChkB == null || $ChkB == "") && ($ChkR == null || $ChkR == "")) {
		$querySqlCond = $querySqlCond . " And (CGreenTel like '%" . $QueryTel . "%' or CBlackTel like '%" . $QueryTel . "%' or CRedTel like '%" . $QueryTel . "%')";
	}
}

$CQueryNArea = $_POST["CQueryNArea"];
if ($CQueryNArea != null && $CQueryNArea != "" && $CQueryNArea != "ALL") {
	$querySqlCond = $querySqlCond . " And CArea like '%" . $CQueryNArea . "%'";
}
$CQuerySource = $_POST["CQuerySource"];
if ($CQuerySource != null && $CQuerySource != "" && $CQuerySource != "ALL") {
	$querySqlCond = $querySqlCond . " And CSource = '" . $CQuerySource . "'";
}

$querySql = $querySql . $querySqlCond;
//echo $querySql;
$db->query($querySql);
$row = $db->fetch_row();
$db->close_db();
$numrows = $row[0];

//?算???
$pages = intval($numrows / $pagesize);

if ($numrows % $pagesize) {
	$pages++;
}
$page = $_GET["page"];
//判????置与否，如??定??首?
if (!isset($page)) {
	$page = 1;
}
//判??到??
//if(isset($ys))
//{
//  if($ys>$pages)
//  {
//  	$page=$pages;
//  }
//  else
//  {     
//  	$page=$ys;
//  }
//}

//?算??偏移量     
$offset = $pagesize * ($page - 1);
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);

//if($_SESSION['rightLevel'] == "0")
//{
//  if($AId == $_SESSION['userId'])
//  {
//  	$querySql = "select CId,CNickname,CArea from Customer";
//  }
//  elseif($ARightLevel == "1")
//  {
//  	$querySql = "select CId,CNickname,CArea from Customer and CCreateAcc = AId And AId in (select AId from AdminUser where AGroupId = ".$AGroupId."))";
//  }	
//  elseif($ARightLevel == "2")
//  {
//  	$querySql = "select CId,CNickname,CArea from Customer where CCreateAcc = '".$AId."'";
//  }
//}
//elseif($_SESSION['rightLevel'] == "1")
//{
//	if($AId == $_SESSION['userId'])
//  {
//  	$querySql = "select CId,CNickname,CArea from Customer and CCreateAcc = AId And AId in (select AId from AdminUser where AGroupId = ".$AGroupId."))";
//  }
//  elseif($ARightLevel == "2")
//  {
//  	$querySql = "select CId,CNickname,CArea from Customer where CCreateAcc = '".$AId."'";
//  }
//}
//elseif($_SESSION['rightLevel'] == "2")
//{
//	$querySql = "select count(*) from Customer where CCreateAcc = '".$AId."'";
//}

$querySql = "select CId,CNickname,CArea from Customer where CCreateAcc = '" . $AId . "'";

$querySql = $querySql . $querySqlCond . " order by CCreateDateTime desc limit $offset,$pagesize";


$db->query($querySql);
?>
<SCRIPT language="JavaScript">
	function checkDelete(id) {
		if (confirm("確定要刪除嗎????")) {
			location.href = "CustomerDeleteDeal.php?id=" + id + "&AId=" + document.all['AId'].value;
		}
	}

	function SendUpdate(id) {
		location.href = "CustomerInput.php?actionOp=Update&AId=" + document.all['AId'].value + "&AName=" + document.all['AName'].value + "&id=" + id;
	}

	function SendMake() {
		form1.target = "_blank";
		form1.action = "MakeTelFile.php";
		form1.submit();
	}

	function SendMakeOneTime() {
		form1.target = "_blank";
		form1.action = "MakeTelOneTimeFile.php";
		form1.submit();
	}

	function SendDel() {
		var cboxes = document.getElementsByName('chkId[]');
		var len = cboxes.length;
		var isDel = false;
		for (var i = 0; i < len; i++) {
			if (cboxes[i].checked) {
				isDel = true;
				if (confirm("確定要刪除嗎????")) {
					form1.action = "MultiCustomerDel.php";
					form1.submit();
				}
				break;
			}
		}
		if (!isDel) {
			alert("請勾選要刪除的資料!!!");
		}
	}

	function check_all(obj, cName) {
		var checkboxs = document.getElementsByName(cName);
		for (var i = 0; i < checkboxs.length; i++) {
			checkboxs[i].checked = obj.checked;
		}
	}
</SCRIPT>

<body>
	<form name="form1" method="post" action="CustomerIndex.php">
		<input type="hidden" value="<?php echo $AId; ?>" name="AId" id="AId" />
		<input type="hidden" value="<?php echo $AName; ?>" name="AName" id="AName" />
		<table border="0" cellspacing="1" cellspadding="1" width="95%">
			<!--
<tr>
<td align="center" colspan="2">
<span class="title"><b>客戶資料管理</b></span>
</td>
</tr>
-->
			<tr>
				<td align="center" colspan="2">
					&nbsp;
				</td>
			</tr>
			<?php
			if (str_indexof($_SESSION['rights'], "B1I") >= 0) {
			?>
				<tr>
					<td align="left" colspan="2">
						<input type="button" name="btnInput" id="btnSub" value="資料匯入" class="t10" onclick="MM_openBrWindow('CustomerFileIn.php?AId=<?php echo $AId; ?>&AName=<?php echo $AName; ?>','FileInWin',700,350)" )" />
						<input type="text" value="<?php if ($QueryTel != null && $QueryTel != "") echo $QueryTel; ?>" name="QueryTel" id="QueryTel" size="15" maxlength="20" style="font-size: 12.0pt;font-family: 微軟正黑體;Height:23px;" />

						<Select name="CQueryNArea">
							<Option Value="ALL" class="t12" <?php if ($CQueryNArea == null || $CQueryNArea == "") echo "selected"; ?>>全部地區</Option>
							<Option Value="N:Taipei" <?php if ($CQueryNArea == "N:Taipei") echo "selected"; ?>><span class="t12">台北</span></Option>
							<Option Value="N:Taoyuan" <?php if ($CQueryNArea == "N:Taoyuan") echo "selected"; ?>><span class="t12">桃園</span></Option>
							<Option Value="N:Zhongli" <?php if ($CQueryNArea == "N:Zhongli") echo "selected"; ?>><span class="t12">中壢</span></Option>
							<Option Value="M:Hsinchu" <?php if ($CQueryNArea == "M:Hsinchu") echo "selected"; ?>><span class="t12">新竹</span></Option>
							<Option Value="M:Taichung" <?php if ($CQueryNArea == "M:Taichung") echo "selected"; ?>><span class="t12">台中</span></Option>
							<Option Value="M:Zhanghua" <?php if ($CQueryNArea == "M:Zhanghua") echo "selected"; ?>><span class="t12">彰投</span></Option>
							<Option Value="S:Tainan" <?php if ($CQueryNArea == "S:Tainan") echo "selected"; ?>><span class="t12">台南</span></Option>
							<Option Value="S:Kaohsiung" <?php if ($CQueryNArea == "S:Kaohsiung") echo "selected"; ?>><span class="t12">高雄</span></Option>
							<Option Value="E:Hualien" <?php if ($CQueryNArea == "E:Hualien") echo "selected"; ?>><span class="t12">花蓮</span></Option>
							<Option Value="M:Toufen" <?php if ($CQueryNArea == "M:Toufen") echo "selected"; ?>><span class="t12">頭份</span></Option>
						</Select>
						<Select name="CQuerySource">
							<Option Value="ALL" <?php if ($CQuerySource == null || $CQuerySource == "") echo "selected"; ?>><span class="t12">全部來源</span></Option>
							<Option Value="W" <?php if ($CQuerySource == "W") echo "selected"; ?>><span class="t12">微信</span></Option>
							<Option Value="L" <?php if ($CQuerySource == "L") echo "selected"; ?>><span class="t12">Line</span></Option>
							<Option Value="G" <?php if ($CQuerySource == "G") echo "selected"; ?>><span class="t12">遇見</span></Option>
							<Option Value="T" <?php if ($CQuerySource == "T") echo "selected"; ?>><span class="t12">電話</span></Option>
							<Option Value="O" <?php if ($CQuerySource == "O") echo "selected"; ?>><span class="t12">其他</span></Option>
						</Select>
						<span class="t12">
							<input type="checkbox" name="ChkG" id="ChkG" value="G" <?php if ($ChkG == "G") echo "checked"; ?>>不傳
						</span>
						<span class="t12">
							<input type="checkbox" name="ChkB" id="ChkB" value="B" <?php if ($ChkB == "B") echo "checked"; ?>>黑
						</span>
						<span class="t12">
							<input type="checkbox" name="ChkR" id="ChkR" value="R" <?php if ($ChkR == "R") echo "checked"; ?>>紅
						</span>
						<input type="submit" name="btnQuery" id="btnQuery" value="查看" class="t10r" />

						<input type="button" name="btnMake" id="btnMake" value="製作" class="t10r" onclick="javascript:SendMake();" />

						<input type="button" name="btnMakeOneTime" id="btnMakeOneTime" value="製作消費一次檔案" class="t10r" onclick="javascript:SendMakeOneTime();" />

						<input type="button" name="btnMultiDel" id="btnMultiDel" value="多筆資料刪除" class="t10" onclick="javascript:SendDel();" />
						<br>
						<span class="t12">共 <?php echo $numrows; ?> 筆</span>
					</td>
				</tr>
			<?php
			}
			?>
			<tr>
				<td align="left" colspan="2" width="100%">
					<?php
					if ($myrow = $db->fetch_array()) {
						$i = 0;
					?>
						<table width="100%" border="1" cellspacing="2" cellpadding="2">
							<tr>
								<td colspan="7" align="center">
									<span class="t14"><?php echo $AName; ?></span>
								</td>
							</tr>
							<tr>
								<td width="2%" bgcolor="#E1E9FB" align="center">
									<span class="t12">全選</span>
									<br>
									<input type="checkbox" name="chkAll" id="chkAll" value="All" onclick="check_all(this,'chkId[]')">
								</td>
								<td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">序號</span></td>
								<td width="30%" bgcolor="#E1E9FB" align="center"><span class="t12r">暱稱</span></td>
								<td width="30%" bgcolor="#E1E9FB" align="center"><span class="t12r">地區</span></td>
								<td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">消費內容修改</span></td>
								<td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">基本資料修改</span></td>
								<td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">刪除</span></td>
							</tr>
							<?php
							$index = 0;
							do {
								$i++;
								$CTempArea = "";
								if (str_indexof($myrow['CArea'], "Taipei") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "台北";
									} else {
										$CTempArea = $CTempArea . " " . "台北";
									}
								}
								if (str_indexof($myrow['CArea'], "Taoyuan") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "桃園";
									} else {
										$CTempArea = $CTempArea . " " . "桃園";
									}
								}
								if (str_indexof($myrow['CArea'], "Zhongli") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "中壢";
									} else {
										$CTempArea = $CTempArea . " " . "中壢";
									}
								}
								if (str_indexof($myrow['CArea'], "Hsinchu") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "新竹";
									} else {
										$CTempArea = $CTempArea . " " . "新竹";
									}
								}
								if (str_indexof($myrow['CArea'], "Taichung") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "台中";
									} else {
										$CTempArea = $CTempArea . " " . "台中";
									}
								}
								if (str_indexof($myrow['CArea'], "Zhanghua") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "彰投";
									} else {
										$CTempArea = $CTempArea . " " . "彰投";
									}
								}
								if (str_indexof($myrow['CArea'], "Tainan") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "台南";
									} else {
										$CTempArea = $CTempArea . " " . "台南";
									}
								}
								if (str_indexof($myrow['CArea'], "Kaohsiung") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "高雄";
									} else {
										$CTempArea = $CTempArea . " " . "高雄";
									}
								}
								if (str_indexof($myrow['CArea'], "Hualien") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "花蓮";
									} else {
										$CTempArea = $CTempArea . " " . "花蓮";
									}
								}
								if (str_indexof($myrow['CArea'], "Toufen") >= 0) {
									if ($CTempArea == "") {
										$CTempArea = "頭份";
									} else {
										$CTempArea = $CTempArea . " " . "頭份";
									}
								}
								if ($CTempArea == "") {
									$CTempArea = "&nbsp;";
								}
							?>
								<tr>
									<td bgcolor="#E6F2FF">
										<input type="checkbox" name="chkId[]" id="chkId[]" value="<?php echo $myrow['CId']; ?>">
									</td>
									<td bgcolor="#E6F2FF"><?php echo $myrow['CId']; ?></td>
									<td bgcolor="#E6F2FF">
										<span class="t12">
											<?php echo $myrow['CNickname']; ?></span>
									</td>
									<td bgcolor="#E6F2FF">
										<span class="t12">
											<?php echo $CTempArea; ?></span>
									</td>
									<td bgcolor="#E6F2FF" align="center">
										<?php
										if (str_indexof($_SESSION['rights'], "B1U") >= 0 && $_SESSION['rightLevel'] != "2") {
										?>
											<span class="t12">
												<a href="javascript:void(0);" onclick="MM_openBrWindow('CMissContentIndex.php?CCId=<?php echo $myrow['CId']; ?>&CNickname=<?php echo $myrow['CNickname']; ?>','MissWin',600,550)" )">
													<b>修改</b></a></span>
										<?php
										} else
											echo "&nbsp;";
										?>
									</td>
									<td bgcolor="#E6F2FF" align="center">
										<?php
										if (str_indexof($_SESSION['rights'], "B1U") >= 0 && $_SESSION['rightLevel'] != "2") {
										?>
											<span class="t12">
												<a href="javascript:void(0);" onclick="SendUpdate('<?php echo $myrow['CId']; ?>');">
													<b>修改</b></a></span>
										<?php
										} else
											echo "&nbsp;";
										?>
									</td>
									<td bgcolor="#E6F2FF" align="center">
										<?php
										if (str_indexof($_SESSION['rights'], "B1D") >= 0) {
										?>
											<span class="t12">
												<a href="javascript:void(0);" onclick="checkDelete('<?php echo $myrow['CId']; ?>');">
													<b>刪除</b></a></span>
										<?php
										} else
											echo "&nbsp;";
										?>
									</td>
								</tr>
						<?php
							} while ($myrow = $db->fetch_array());
							echo "</table>";
						}
						//?示???     
						echo "<br><div align='left'>共有" . $pages . "頁 (" . $page . "/" . $pages . ")<br><br>";
						//?示分??     
						for ($i = 1; $i < $page; $i++)
							echo "&nbsp;<a href='CustomerIndex.php?page=" . $i . "&AId=" . $AId . "'>第" . $i . "頁</a>&nbsp;";
						echo "第" . $page . "頁";
						for ($i = $page + 1; $i <= $pages; $i++)
							echo "&nbsp;<a href='CustomerIndex.php?page=" . $i . "&AId=" . $AId . "'>第" . $i . "頁</a>&nbsp;";
						//echo "<br><br>";     
						//?示?到??     
						//echo "<form action='CustomerIndex.php' method='post'>";     
						//?算首?、上一?、下一?、尾?的??值     
						$first = 1;
						$prev = $page - 1;
						$next = $page + 1;
						$last = $pages;
						if ($page > 1) {
							//echo "&nbsp;<a href='CustomerIndex.php?page=".$first."'>首頁</a>&nbsp;";     
							//echo "&nbsp;<a href='CustomerIndex.php?page=".$prev."'>上一頁</a>&nbsp;";     
						}
						if ($page < $pages) {
							//echo "&nbsp;<a href='fenye.php?page=".$next."'>下一頁</a>&nbsp;";     
							//echo "&nbsp;<a href='fenye.php?page=".$last."'>尾頁</a>&nbsp;";     
						}
						//echo "到&nbsp;<input type=text name='ys' size='2' value=".$page.">&nbsp;頁";     
						//echo "<input type=submit name='Submit' value='Go'>";     
						//echo "</form>";     
						echo "</div>";
						?>
				</td>
			</tr>
		</table>
	</form>
</body>

</html>