<?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 type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,win_width,win_height) { 
  var PosX = ((screen.width-win_width)/100)*99; 
  var PosY = ((screen.height-win_height)/100)*99;
  features = "status=no,toolbar=no,scrollbars=yes,resizable=yes,width=750,Height=550,top="+PosY+",left="+PosX;; 
  var newwin = window.open(theURL,winName,features); 
} 
//-->
</script>
<SCRIPT language="JavaScript">
function checkDelete(id)
{
	if(confirm("確定要刪除嗎????"))
	{
     location.href = "CustomerBuyTimesQueryDeleteDeal.php?id=" + id;
	}
}

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

</SCRIPT>
<?php
require_once("class/DBConfig.php");
require_once("class/DBOperation.php");

$db = new DBOperation();

if($_SESSION['QueryTimeSql'] == "")
{
	
	$queryCond = "";
	$querySql = "";
	$querySpecSql = "(1=1 ";
	$querySql = $querySql." where (1=1 ";
	
	$queryAidSql = "";
	$AId = $_POST["AId"];
	if($AId != null && $AId != "")
	{
		$AId = split ('[&.-]', $AId);
		if($queryCond == "")
		{
			$queryCond = "誰的客戶 : ".$AId[1];
		}
		else
		{
			$queryCond = $queryCond.",誰的客戶 : ".$AId[1];;
		}		
		$queryAidSql = " Customer.CCreateAcc =".$AId[0];
	}
	if($queryAidSql != "")
	{
	  $querySql = $querySql." And (";
		$querySql = $querySql.$queryAidSql;
		$querySql = $querySql." )";
		
		$querySpecSql = $querySpecSql." And (";
		$querySpecSql = $querySpecSql.$queryAidSql;
		$querySpecSql = $querySpecSql." )";
	}
	
	
	$queryCostOneSql = "";
	$CCostOne = $_POST['CCostOne'];	
	$CCostTimes = $_POST['CCostTimes'];	
	if($CCostOne != null && $CCostOne != "")
	{
		if($queryCond == "")
		{
			$queryCond = "只消費".$CCostTimes."次";
		}
		else
		{
			$queryCond = $queryCond.",只消費".$CCostTimes."次";
		}		
		$queryCostOneSql = " Customer.ConTimes =".$CCostTimes;
	}
	if($queryCostOneSql != "")
	{
	  $querySql = $querySql." And (";
		$querySql = $querySql.$queryCostOneSql;
		$querySql = $querySql." )";
		
		$querySpecSql = $querySpecSql." And (";
		$querySpecSql = $querySpecSql.$queryCostOneSql;
		$querySpecSql = $querySpecSql." )";
	}
	
	$queryAreaSql = "";
	$chkTaipei = $_POST['chkTaipei'];
	$queryAreaCond = "";
	if($chkTaipei != null && $chkTaipei != "")
	{
	  if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 台北";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",台北";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%N:Taipei%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%N:Taipei%'";
		}	
	}
	$chkTaoyuan = $_POST['chkTaoyuan'];
	if($chkTaoyuan != null && $chkTaoyuan != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 桃園";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",桃園";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%N:Taoyuan%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%N:Taoyuan%'";
		}	
	}
	$chkZhongli = $_POST['chkZhongli'];
	if($chkZhongli != null && $chkZhongli != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 中壢";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",中壢";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%N:Zhongli%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%N:Zhongli%'";
		}	
	}
	$chkHsinchu = $_POST['chkHsinchu'];
	if($chkHsinchu != null && $chkHsinchu != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 新竹";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",新竹";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%M:Hsinchu%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%M:Hsinchu%'";
		}	
	}
	$chkTaichung = $_POST['chkTaichung'];
	if($chkTaichung != null && $chkTaichung != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 台中";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",台中";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%M:Taichung%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%M:Taichung%'";
		}	
	}
	$chkZhanghua = $_POST['chkZhanghua'];
	if($chkZhanghua != null && $chkZhanghua != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 彰投";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",彰投";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%M:Zhanghua%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%M:Zhanghua%'";
		}	
	}
	$chkTainan = $_POST['chkTainan'];
	if($chkTainan != null && $chkTainan != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 台南";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",台南";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%S:Tainan%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%S:Tainan%'";
		}	
	}
	$chkKaohsiung = $_POST['chkKaohsiung'];
	if($chkKaohsiung != null && $chkKaohsiung != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 高雄";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",高雄";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%S:Kaohsiung%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%S:Kaohsiung%'";
		}	
	}
	
	$chkHualien = $_POST['chkHualien'];
	if($chkHualien != null && $chkHualien != "")
	{
		if($queryAreaCond == "")
		{
			$queryAreaCond = "地區 : 花蓮";
		}
		else
		{
			$queryAreaCond = $queryAreaCond.",花蓮";
		}
		
		if($queryAreaSql == "")
		{
			$queryAreaSql = " CArea like '%E:Hualien%'";
		}
		else
		{
			$queryAreaSql = $queryAreaSql." or CArea like '%E:Hualien%'";
		}	
	}
	
	if($queryAreaCond != "")
	{
		if($queryCond == "")
		{
			$queryCond = $queryAreaCond;
		}
		else
		{
			$queryCond = $queryCond.",".$queryAreaCond;
		}
	}
	if($queryAreaSql != "")
	{
	  $querySql = $querySql." And (";
		$querySql = $querySql.$queryAreaSql;
		$querySql = $querySql." )";
		
		$querySpecSql = $querySpecSql." And (";
		$querySpecSql = $querySpecSql.$queryAreaSql;
		$querySpecSql = $querySpecSql." )";
	}
	
	if($_SESSION['rightLevel'] == "0")
	{
		$querySql = $querySql." and CCreateAcc = AId)";
	}
	elseif($_SESSION['rightLevel'] == "1")
	{
		$querySql = $querySql." and CCreateAcc = AId And AId in (select AId from AdminUser where AFlag = '1' and AGroupId = ".$_SESSION['groupId']."))";
	}
	elseif($_SESSION['rightLevel'] == "2")
	{
		$querySql = $querySql." and CCreateAcc = AId And CCreateAcc = '".$_SESSION['userId']."')";
	}	
	$querySql = $querySql." order by CCreateDateTime desc";	
	$_SESSION['QueryTimeSql'] = $querySql;
	$_SESSION['QueryTimeCond'] = $queryCond;	
}
else
{
	$querySql = $_SESSION['QueryTimeSql'];
	$queryCond = $_SESSION['QueryTimeCond'];
}
 
$querySqlCount = "select count(*) from Customer,AdminUser ".$querySql;

//echo $querySqlCount;

//exit(0);


$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 = "select * from Customer,AdminUser ".$querySql;
$db->query($querySql1);


if($queryCond == "")
{
	$queryCond = "無";
}
?>
<body onload="MM_preloadImages('images/LastPage1.jpg')">
<form name="form1" method="post" action="CustomerInput.php">
<input type="hidden" value="<?php echo $querySql; ?>" name="querySql" id="querySql" />
<table border="0" cellspacing="1" cellspadding="1" width="100%">
<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">
<?php
if(($_SESSION['rightLevel'] == "0" && (str_indexof($_SESSION['rights'],"B1I") >= 0)) ||
($_SESSION['rightLevel'] == "1" && (str_indexof($_SESSION['rights'],"B1I") >= 0)) ||
($_SESSION['rightLevel'] == "2" && (str_indexof($_SESSION['userRights'],"I") >= 0)))
{
?>
<input type="button" name="btnAdd" id="btnAdd" value="新     增" class="t12" onclick="MM_openBrWindow('CustomerQueryIndex1.php?AddTel=<?php echo $CQueryTel; ?>','AddWin',600,500)" style="WIDTH:150px;Height:30px;"/>
<?php
}
?>
&nbsp;
<input type="button" name="btnMake" id="btnMake" value="製做檔案" class="t12" onclick="javascript:SendMake();"/>
&nbsp;
<a href="javascript:location.href='CustomerBuyTimes.php';">
<img src="images/LastPage1.jpg" ID="ImgLogIn" width="118" height="25" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('ImgLogIn','','images/LastPage2.jpg',1)">
</a>
</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="8%" bgcolor="#E1E9FB" align="center"><span class="t12r">序號</span></td>
   <?php } ?>
  <td width="32%" bgcolor="#E1E9FB" align="center"><span class="t12r">地區</span></td>     
  <td width="32%" bgcolor="#E1E9FB" align="center"><span class="t12r">暱稱/其他</span></td>  
  <td width="10%" bgcolor="#E1E9FB" align="center"><span class="t12r">誰的客戶</span></td>  
  <td width="6%" bgcolor="#E1E9FB" align="center"><span class="t12r">查看</span></td>
  <td width="6%" bgcolor="#E1E9FB" align="center"><span class="t12r">新增</span></td> 
  <td width="6%" 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($CTempArea == null || $CTempArea == "")
    {
    	$CTempArea = "&nbsp;";
    }
    $SingleTel = "";
    if($SingleTel == "" && $myrow['CBlackTel'] != null && $myrow['CBlackTel'] != "")
    {
    	$SingleTel = $myrow['CBlackTel'];
    }
    if($SingleTel == "" && $myrow['CRedTel'] != null && $myrow['CRedTel'] != "")
    {
    	$SingleTel = $myrow['CRedTel'];
    }
    if($SingleTel == "" && $myrow['CGreenTel'] != null && $myrow['CGreenTel'] != "")
    {
    	$SingleTel = $myrow['CGreenTel'];
    }
  ?>     
	  <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 $CTempArea;?></span></td>	    
	  <td bgcolor="#E6F2FF">
	  <span class="t12">
	  <?php echo $myrow['CNickname'];?></span></td>
	  <td bgcolor="#E6F2FF">
	  <span class="t12">
	  <?php echo $myrow['AName'];?></span></td>	 
	  <td bgcolor="#E6F2FF" align="center">	   
	  <span class="t12">
	  <a href="CustomerQueryBuyTimesResultW.php?id=<?php echo $myrow['CId'];?>">
	  <b>明細</b></a></span>	 
	  </td>
	  <td bgcolor="#E6F2FF" align="center">
	  <?php
			if(($_SESSION['rightLevel'] == "0" && (str_indexof($_SESSION['rights'],"B1I") >= 0)) ||
($_SESSION['rightLevel'] == "1" && (str_indexof($_SESSION['rights'],"B1I") >= 0)) ||
($_SESSION['rightLevel'] == "2" && (str_indexof($_SESSION['userRights'],"I") >= 0)))
{
		?>
	  <span class="t12">	  
	  <a href="javascript:void(0);" onclick="MM_openBrWindow('CustomerQueryIndex1.php?AddTel=<?php echo $SingleTel; ?>','AddWin',600,500)">
	  <b>新增</b></a></span>
	   <?php
			}
			else
				echo "&nbsp;";
		?> 
	  </td>	  
	  <td bgcolor="#E6F2FF" align="center">
	  <?php
			if(($_SESSION['rightLevel'] == "0" && (str_indexof($_SESSION['rights'],"B1D") >= 0)) ||
($_SESSION['rightLevel'] == "1" && (str_indexof($_SESSION['rights'],"B1D") >= 0)) ||
($_SESSION['rightLevel'] == "2" && (str_indexof($_SESSION['userRights'],"D") >= 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>"   ;     
  }
?>
</td>
</tr>
</table>
</form>
</body>
</html>
