<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: ,v1 10:39 2008-7-27 Leilei $
*/

require("../../include/header.php");

html_start("广告排行 - VeryIDE");
?>

<!--body start-->
<?php
	

	//module
	require("config.php");
	require("function.php");
	require("naver.php");
	
	if( !loader_cache($appid,"table.adsarea") ){
		//更新分类数组缓存
		cache_table($appid,$mysql,VI_DBMODPRE."adsarea",'<option value=\'{table:id}\'>{table:name}</option>');		
	}

	//mysql open
	reader_mysql();

	//'***********************

	$sel = GetQuery("sel");
	
	$q = GetQuery("q");
	$s = GetQuery("s");

	$parent = GetQuery("parent");
	$type = GetQuery("type");
	
	$full = GetNumber("full",-1);
	$sel = GetQuery("sel");
?>
	<div id="search">
	    <form name="formSeacrh" id="formSeacrh" method="get" action="?">
          <span class="action">
          	
            <label>
            <select name="parent" id="parent" onChange="/*filterSelect('type',Mo("select[name=parent]").value(),true);*/Mo('#formSeacrh').submit();" ignore="true">
            	<optgroup label="筛选分类.." />
                <option value="0">全部分类..</option>
                <?php
					foreach($VICACHE["mod.adsarea"] as $key){
						if($key["parent"]=="0"){
							echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
						}
					}
				?>
            </select>
            </label>
            
            <label>
            <select name="type" id="type" onChange="Mo('#formSeacrh').submit();" ignore="true">
            	<optgroup label="筛选位置.." />
                <option value="0">全部位置..</option>
                <?php
					foreach($VICACHE["mod.adsarea"] as $key){
						if($key["parent"]==$parent){
							echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
						}
					}
				?>
            </select>
            </label>
          
            <select name="full" id="full" onChange="Mo('#formSeacrh').submit();" ignore="true">
            	<optgroup label="筛选收款.." />
                <option value="-1">所有收款..</option>
                <option value="0"><?php echo $Modules["adsview"]["full"][0];?></option>
                <option value="1"><?php echo $Modules["adsview"]["full"][1];?></option>
            </select>
         </span>

            <select name="s" id="s">
				<option value="a_name" checked>名称</option>
				<option value="a_link">链接</option>
				<option value="a_tag">标签</option>
				<option value="area">地区</option>
				<option value="a_contract">合同号</option>
				<option value="a_custom">客户名</option>
            </select>
            含有
		    <input name="q" type="text" class="text" title="请输入关键字" id="q" value="<?php echo $q;?>">
		    <input name="sel" type="hidden" id="sel" value="<?php echo $sel;?>">
		    <button class="go" type="submit"></button>
		</form>
        
		<script type="text/javascript">
		 Mo("#s").value("<?php echo $s;?>");setSelect("full","<?php echo $a_full;?>");
		
		Mo.reader(
			function(){
				
				Mo("#type").value("<?php echo $type;?>");
				Mo("#parent").value("<?php echo $parent;?>");
				
				/*
				var tpid=getSelect("type").attr["pid"];

				if(tpid){
					tpid=tpid.value;
					Mo("select[name=parent]").value(tpid);
					filterSelect("type",tpid,true);
				}else{
					Mo("#parent").value("<?php echo $parent;?>");
					filterSelect("type","<?php echo $parent;?>",true);
				}
				*/
				
				/*Mo("#type").value("<?php echo $type;?>");*/
				
				/*滚动表头*/
				window.setInterval(function(){
					Mo.Thead("thead");
				},100);
			}
		);
		
       </script>
	</div>
  
	<!--up-->
	<?php
		
	$sql="SELECT * FROM ".VI_DBMODPRE."adsview where 1=1 ";
	
	if ($q && $s){
		$sql.=" and  ".$s." like '%".$q."%'";
	}
	
	if ($parent){
		$sql.=" and  parent ='".$parent."'";
	}
	
	if ($type){
		$sql.=" and  type ='".$type."'";
	}
	
	
	//款项筛选
	switch($full){
		case 0:
			$sql.=" and  ar > pr";
		break;
		
		case 1:
			$sql.=" and  ar = pr";
		break;
	}
	
	//广告筛选
	switch($sel){
		//按点击排序
		case "order-click-ratio":
			$sql.=" order BY (a_click/a_view) desc";
		break;
	
		//按点击排序
		case "order-click-ratio-asc":
			$sql.=" order BY (a_click/a_view) asc";
		break;
	
		//按点击排序
		case "order-click":
			$sql.=" order BY a_click desc";
		break;
		
		//按点击排倒序
		case "order-click-asc":
			$sql.=" order BY a_click asc";
		break;
		
		//按展示排序
		case "order-view":
			$sql.=" order BY a_view desc";
		break;
		
		//按展示排倒序
		case "order-view-asc":
			$sql.=" order BY a_view asc";
		break;
		
		default:
			$sql.=" order BY id desc";
	}
	
	//查询数据库_总记录数
	$result=mysql_query(str_replace("*","count(type) as count",$sql),$mysql);
	$row=mysql_fetch_assoc($result);
	$row_count=$row["count"];
	
	$Project["queries"]++;
	
	//查询数据库_设置相应字段
	//$sql=str_replace("*","parent,type,id,name,start,expire,status,ar,pr,contract,file,link",$sql);

	//分页参数
	$page=GetPage("page");
	$page_start=$Global["pagesize"]*($page-1);
	$sql=$sql." limit $page_start,".$Global["pagesize"];	
	
	
	//分页链接
	$url="?sel=".$sel."&s=".$s."&q=$q&sel=$sel&type=$type&full=$full&page=";	
	
	echo '
		<table width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
		  <tr class="title" id="thead">
			<td>序号'.( $sel=='' ? loader_image("icon/down.png"):'').'</td>
			<td>广告名称</td>
			<td>合同号</td>
			<td>点击'.( $sel=='order-click' ? loader_image("icon/up.png"):'') . ( $sel=='order-click-asc' ? loader_image("icon/down.png"):'').'</td>
			<td>展示'.( $sel=='order-view' ? loader_image("icon/up.png"):'') . ( $sel=='order-view-asc' ? loader_image("icon/down.png"):'').'</td>
			<td>点击率'.( $sel=='order-click-ratio' ? loader_image("icon/up.png"):'') . ( $sel=='order-click-ratio-asc' ? loader_image("icon/down.png"):'').'</td>
			<td>开播时间</td>
			<td>停播时间</td>
			<td>收款情况</td>
			<td>状态</td>
			<td>广告数据</td>
		  </tr>
		 ';
		 
		$result=mysql_query($sql,$mysql);
		
		$Project["queries"]++;
		 
		$colors=array("#eee","");
		$i=1;
		 
		while($row=mysql_fetch_assoc($result)){
			$a_id=$row["a_id"];
			
			$a_contract=$row["a_contract"];
			$a_custom=$row["a_custom"];
			
			$u_id=$row["u_id"];
			$a_link=$row["a_link"];
			$status=$row["status"];
			$a_name=$row["a_name"];
			$a_file=$row["a_file"];
			$parent=$row["parent"];
			$type=$row["type"];
			
			$a_width=$row["a_width"];
			$a_height=$row["a_height"];
			
			$a_date=$row["a_date"];
			$a_default=$row["a_default"];
			//$a_full=$row["a_full"];
			//$a_engage=$row["a_engage"];
			$a_contact=$row["a_contact"];
			
			$ar=$row["ar"];
			$pr=$row["pr"];
			
			//统计信息
			$a_view=$row["a_view"]*$Modules["adsview"]["multiple"];
			$a_click=$row["a_click"]*$Modules["adsview"]["multiple"];
			
			//默认广告
			if($a_default){
				$a_default='<span class="text-yes">是</span>';
			}else{
				$a_default="否";
			}
			
			$a_start=$row["a_start"];
			$expire=$row["expire"];
			$end=$row["end"];
			
			
			//广告投放天数
			if ($status){
				//正常状态
				$l_days=DateDiff("d",$a_start,$expire);
			}else{
				//已过期
				$l_days=DateDiff("d",$a_start,$end);
			}
			

			//到期倒记时
			$e_days=DateDiff("d",mktime(0, 0, 0, date("m"), date("j"), date("Y")),$expire);
			
			echo '
			  <tr class="line">
			  <td>'.$i.'</td>
			   <td><script>Mo.Tools("RsFile","'.$row["file"].'","");</script><a href="'.$row["link"].'" target="_blank" title="'.$row["link"].'">'.format_substr($row["name"],22).'</a></td>
			  <td>'.$row["contract"].'</td>
			   <td>'.number_format($a_click).'</td>
				<td>'.number_format($a_view).'</td>
				<td><span class="text-yes">'.(round($a_click/($a_view+1),2)*100).'%</span></td>
				<td>'.date("y-m-d",$row["start"]).'</td>
				<td>';
				//echo $e_days;
				if ($e_days<=$Modules["adsview"]["expire"] && $e_days>-1){
					echo '<span class="text-no">'.date("y-m-d",$expire).'</span>';
				}else{
					echo date("y-m-d",$expire);
				}
				
				echo '
				</td>
				<td>';
				
				if($pr < $ar){
					echo '<span class="text-no">'.$Modules["adsview"]["full"][0].'</span>';
				}else{
					echo '<span class="text-yes">'.$Modules["adsview"]["full"][1].'</span>';
				}
				
				echo '</td><td>';
				
				//广告状态
				if($expire+86400>time()){
					echo $Modules["adsview"]["status"][$status];
				}else{
					echo $Modules["adsview"]["status"][4];
				}
				
				echo '</td>
				<td>
					<a href="javascript:_adWindow(\'ad.info\','.$a_id.',\''.$t_key.'\');">详情</a>
					/
					<a href="javascript:_adWindow(\'ad.analyze\','.$a_id.',\''.$t_key.'\');">效果</a>
				</td>
			  </tr>
			';
			  
			$i++;
		}
		
		echo '</table>';
			
	//mysql close
	reader_mysql(true);
			
	?>
    
    <div id="saving">
		<?php
            echo GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
        ?>
    </div>

<!--body end-->

<?php html_close();?>