<?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>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

$CNickname = $_POST['CNickname'];
$CNArea = $_POST['CNArea'];
$CMArea = $_POST['CMArea'];
$CSArea = $_POST['CSArea'];
$CSource = $_POST['CSource'];
$CTel = $_POST['CTel'];
$CTel = trim($CTel);
$ChkGreen = $_POST['chkGreen'];
$ChkBlack = $_POST['chkBlack'];
$ChkRed = $_POST['chkRed'];
$CClass = $_POST['radClass'];
//$CMiss = $_POST['CMiss'];

$queryCond = "";
if($CNickname != null && $CNickname != "")
{
	if($queryCond == "")
	{
		$queryCond = "暱稱 : ".$CNickname;
	}
	else
	{
		$queryCond = $queryCond.",暱稱 : ".$CNickname;
	}
}

if($CClass != null && $CClass != "")
{
	if($queryCond == "")
	{
		$queryCond = "類別 : ".$CClass;
	}
	else
	{
		$queryCond = $queryCond.",類別 : ".$CClass;
	}
}

$CTempArea = "";
if($CNArea == "N:")
{
	if($CTempArea == "")
	{
		$CTempArea = "北部";
	}
	else
	{
		$CTempArea = $CTempArea." "."北部";
	}
}
elseif(str_indexof($CNArea,"Taipei") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "台北";
	}
	else
	{
		$CTempArea = $CTempArea." "."台北";
	}
}
elseif(str_indexof($CNArea,"Taoyuan") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "桃園";
	}
	else
	{
		$CTempArea = $CTempArea." "."桃園";
	}
}
elseif(str_indexof($CNArea,"Zhongli") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "中壢";
	}
	else
	{
		$CTempArea = $CTempArea." "."中壢";
	}
}

if($CMArea == "M:")
{
	if($CTempArea == "")
	{
		$CTempArea = "中部";
	}
	else
	{
		$CTempArea = $CTempArea." "."中部";
	}
}
elseif(str_indexof($CMArea,"Hsinchu") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "新竹";
	}
	else
	{
		$CTempArea = $CTempArea." "."新竹";
	}
}
elseif(str_indexof($CMArea,"Toufen") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "頭份";
	}
	else
	{
		$CTempArea = $CTempArea." "."頭份";
	}
}
elseif(str_indexof($CMArea,"Taichung") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "台中";
	}
	else
	{
		$CTempArea = $CTempArea." "."台中";
	}
}
elseif(str_indexof($CMArea,"Zhanghua") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "彰投";
	}
	else
	{
		$CTempArea = $CTempArea." "."彰投";
	}
}

if($CSArea == "S:")
{
	if($CTempArea == "")
	{
		$CTempArea = "南部";
	}
	else
	{
		$CTempArea = $CTempArea." "."南部";
	}
}
elseif(str_indexof($CSArea,"Tainan") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "台南";
	}
	else
	{
		$CTempArea = $CTempArea." "."台南";
	}
}
elseif(str_indexof($CSArea,"Kaohsiung") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "高雄";
	}
	else
	{
		$CTempArea = $CTempArea." "."高雄";
	}
}

if($CSArea == "E:")
{
	if($CTempArea == "")
	{
		$CTempArea = "東部";
	}
	else
	{
		$CTempArea = $CTempArea." "."東部";
	}
}
elseif(str_indexof($CSArea,"Hualien") >= 0)
{
	if($CTempArea == "")
	{
		$CTempArea = "花蓮";
	}
	else
	{
		$CTempArea = $CTempArea." "."花蓮";
	}
}

if($CTempArea != null && $CTempArea != "")
{
	if($queryCond == "")
	{
		$queryCond = "地區 : ".$CTempArea;
	}
	else
	{
		$queryCond = $queryCond.",地區 : ".$CTempArea;
	}
}

$CSourceName = "";
if($CSource == "W")
{
	$CSourceName = "微信";
}
elseif($CSource == "L")
{
  $CSourceName = "Line";
}
elseif($CSource == "G")
{
	$CSourceName = "遇見";
}
elseif($CSource == "T")
{
  $CSourceName = "電話";
}
elseif($CSource == "O")
{
	$CSourceName = "其他";
}
else
{
	$CSourceName = "全";
}

if($CSource != null && $CSource != "")
{
	if($queryCond == "")
	{
		$queryCond = "來源 : ".$CSourceName;
	}
	else
	{
		$queryCond = $queryCond.",來源 : ".$CSourceName;
	}
}

if($CTel != null && $CTel != "")
{
	if($queryCond == "")
	{
		$queryCond = "電話 : ".$CTel;
	}
	else
	{
		$queryCond = $queryCond.",電話 : ".$CTel;
	}
}

$ChkName = "";

if($ChkRed != null && $ChkRed == "R")
{
	if($ChkName == "")
		$ChkName = "紅";
	else
	  $ChkName = $ChkName.",紅";
}
if($ChkBlack != null && $ChkBlack == "B")
{
  if($ChkName == "")
		$ChkName = "黑";
	else
	  $ChkName = $ChkName.",黑";
}
if($ChkGreen != null && $ChkGreen == "G")
{
  if($ChkName == "")
		$ChkName = "綠";
	else
	  $ChkName = $ChkName.",綠";
}

if($ChkName != null && $ChkName != "")
{
	if($queryCond == "")
	{
		$queryCond = "電話類別  : ".$ChkName;
	}
	else
	{
		$queryCond = $queryCond.",電話類別  : ".$ChkName;
	}
}

$querySql1 = "select CId,CNickname,CArea from Customer ";
$querySql = "";
$querySql = $querySql."where 1=1";
if($CNickname != null && $CNickname != "")
{
	$querySql = $querySql." and CNickname like '%".$CNickname."%'";
}

if($CClass != null && $CClass != "")
{
	$querySql = $querySql." and CClass like '%".$CClass."%'";
}

$queryAreaSql = "";
if($CNArea != null && $CNArea != "")
{
  if($queryAreaSql == "")
  {
  	$queryAreaSql = " CArea like '%".$CNArea."%'";
  }
  else
  {
  	$queryAreaSql = $queryAreaSql." or CArea like '%".$CNArea."%'";
  }	
}
if($CMArea != null && $CMArea != "")
{
  if($queryAreaSql == "")
  {
  	$queryAreaSql = " CArea like '%".$CMArea."%'";
  }
  else
  {
  	$queryAreaSql = $queryAreaSql." or CArea like '%".$CMArea."%'";
  }	
}
if($CSArea != null && $CSArea != "")
{
  if($queryAreaSql == "")
  {
  	$queryAreaSql = " CArea like '%".$CSArea."%'";
  }
  else
  {
  	$queryAreaSql = $queryAreaSql." or CArea like '%".$CSArea."%'";
  }	
}
if($queryAreaSql != "")
{
	$querySql = $querySql." and (".$queryAreaSql.")";
}

if($CSource != null && $CSource != "" && $CSource != "ALL")
{
	$querySql = $querySql." and CSource = '".$CSource."'";
}
if($CTel != null && $CTel != "")
{
  $queryTelSql = "";
  $querySql = $querySql." and (";
  if($ChkGreen != null && $ChkGreen == "G")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " CGreenTel like '%".$CTel."%'";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CGreenTel like '%".$CTel."%'";
    }  	
  }
  if($ChkBlack != null && $ChkBlack == "B")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " CBlackTel like '%".$CTel."%'";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CBlackTel like '%".$CTel."%'";
    }  	
  }
  if($ChkRed != null && $ChkRed == "R")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " CRedTel like '%".$CTel."%'";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CRedTel like '%".$CTel."%'";
    }  	
  }
  $querySql = $querySql.$queryTelSql;
  $querySql = $querySql." )";
}
else
{
  $queryTelSql = "";  
	if($ChkGreen != null && $ChkGreen == "G")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " and (CGreenTel <> ''";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CGreenTel <> ''";
    }  	
  }
  if($ChkBlack != null && $ChkBlack == "B")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " and (CBlackTel <> ''";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CBlackTel <> ''";
    }  	
  }
  if($ChkRed != null && $ChkRed == "R")
  {
    if($queryTelSql == "")
    {
    	$queryTelSql = " and (CRedTel <> ''";
    }
    else
    {
    	$queryTelSql = $queryTelSql." or CRedTel <> ''";
    }  	
  }
  if($queryTelSql != "")
  {
  	$querySql = $querySql.$queryTelSql;
    $querySql = $querySql." )";
  }
}
 
if($_SESSION['viewData'] == "0")
{
	$querySql = $querySql."and CCreateAcc = '".$_SESSION['userId']."'";
}

$querySqlCount = "select count(*) from Customer ".$querySql;

$querySql = $querySql." order by CCreateDateTime desc";

$db = new DBOperation();

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$db->query($querySqlCount);
$row = $db->fetch_row();
$db->close_db();
$numrows = $row[0];

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$querySql1 = $querySql1.$querySql;

$db->query($querySql1);

?>
<body>
<form name="form" method="post" action="CustomerInput.php">
<table border="0" cellspacing="1" cellspadding="1" width="95%">
<tr>
<td align="left" colspan="2">
<span class="t12"><b>查詢條件 : <?php echo $queryCond;?></b></span>
</td>
</tr>
<tr>
<td align="center" colspan="2">
&nbsp;
</td>
</tr>
<tr>
<td align="left">
<span class="t12b"><b>
<?php echo "總共".$numrows."筆資料";?></b></span>
</td>
<td align="right">
<input type="button" name="btnSub" id="btnSub" value="重新查詢" class="t12" onclick="javascript:location.href='CustomerQueryInput.php';"/>
</td>
</tr>
<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>
  <?php if($_SESSION['username'] == "admin") { ?>    
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">序號</span></td>
  <?php } ?>
  <td width="40%" bgcolor="#E1E9FB" align="center"><span class="t12r">暱稱</span></td>
  <td width="40%" 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." "."頭份";
			}
    }
  ?>     
	  <tr>
	  <?php if($_SESSION['username'] == "admin") { ?> 
	  <td bgcolor="#E6F2FF"><?php if($_SESSION['username'] == "admin") {echo $myrow['CId'];}?></td> 
	  <?php } ?>    
	  <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">	   
	  <span class="t12">
	  <a href="CustomerQueryResult.php?id=<?php echo $myrow['CId'];?>&CNickname=<?php echo $CNickname;?>&CNArea=<?php echo $CNArea;?>&CMArea=<?php echo $CMArea;?>&CSArea=<?php echo $CSArea;?>&CArea=<?php echo $CArea;?>&CSource=<?php echo $CSource;?>&CTel=<?php echo $CTel;?>&chkGreen=<?php echo $ChkGreen;?>&chkBlack=<?php echo $ChkBlack;?>&chkRed=<?php echo $ChkRed;?>">
	  <b>明細</b></a></span>	 
	  </td>
	  </tr>     
  <?php     
  }while($myrow = $db->fetch_array());
  echo "</table>"   ;     
  }
?>
</td>
</tr>
</table>
</form>
</body>
</html>
