<?php
include("CheckSession.php");
?>
<?php

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

$AId = $_POST["AId"];

$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);
}
$AName = $myrow['AName'];
$querySqlCond = "";

//查詢條件
$QueryTel = $_POST["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."'";
}
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
if($_SESSION['rightLevel'] == "0")
{
	$querySql = "select * from Customer where (select count(*) from Customer,CMissContent where CCId = Customer.CId) = 1";
}
elseif($_SESSION['rightLevel'] == "1")
{
	$querySql = "select * from Customer,AdminUser where AFlag = '1' and CCreateAcc = AId And AId in (select AId from AdminUser where AGroupId = ".$_SESSION['groupId']." and (select count(*) from CMissContent where CCId = Customer.CId)=1";
}
elseif($_SESSION['rightLevel'] == "2")
{
	$querySql = "select * from Customer where CCreateAcc = AId and CCreateAcc = '".$AId."' and (select count(*) from CMissContent where CCId = Customer.CId)=1";
}

$querySql = $querySql.$querySqlCond." order by CCreateDateTime desc";
echo $querySql;
exit(0);


$db->query($querySql);



//產製檔案，組成電話字串
$TelStr = "";
$TelTempStr = "";
$TelCount = 0;
while($myrow = $db->fetch_array())
{  
  if((str_indexof($myrow['CMemo'],"21") >= 0)
     || (str_indexof($myrow['CMemo'],"43") >= 0)
     || (str_indexof($myrow['CMemo'],"44") >= 0)
     || (str_indexof($myrow['CMemo'],"46") >= 0)
     || (str_indexof($myrow['CMemo'],"47") >= 0))
  {
  	continue;
  }   
  if($myrow['CGreenTel'] != null && $myrow['CGreenTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CGreenTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CGreenTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }  
  if($myrow['CBlackTel'] != null && $myrow['CBlackTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CBlackTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CBlackTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }
  if($myrow['CRedTel'] != null && $myrow['CRedTel'] != "")
  {
    $TelCount = $TelCount + 1;
    if($TelTempStr == "")
  	{
  		$TelTempStr = $myrow['CRedTel'];
  	}
  	else
  	{
  		$TelTempStr = $TelTempStr.";".$myrow['CRedTel'];
  	}
  	if($TelCount % 20 == 0)
  	{
  		$TelStr = $TelStr.$TelTempStr."\r\n";
  		$TelTempStr = "";
  	}
  }
}
if($TelCount % 20 != 0)
	$TelStr = $TelStr.$TelTempStr;

date_default_timezone_set('Asia/Taipei');
$t=time();
$NowDate = date("YmdHis");
$fp = fopen($_FILECONFIG['UsedDir'].$_SESSION['username']."_".$NowDate.".txt", 'w');
fwrite($fp, $TelStr);
fclose($fp);

$fp = fopen($_FILECONFIG['BackUpDir'].$_SESSION['username']."_".$NowDate.".txt", 'w');
fwrite($fp, $TelStr);
fclose($fp);

$db->close_db();
$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
//寫入資料庫
$insertSql = "INSERT INTO FileData (CMemo, CNewFileName,COrgFileName,CCreateDateTime,CUpdateDateTime,CCreateAcc,CRealCreator)"
	              ."VALUES ('".$db->mysql_real_escape_string($_SESSION['username']."_".$NowDate)."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['username']."_".$NowDate.".txt")."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['username']."_".$NowDate.".txt")."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".Date('Y-m-d H:i:s')."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['userId'])."'"
	              .",'".$db->mysql_real_escape_string($_SESSION['userId'])."')";
	              
$db->query($insertSql);
$db->close_db();

echo '<script type="text/javascript">';
echo 'alert("製作檔案成功!!!");';
echo 'window.close();';
echo '</script>';
exit();
?>