<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

require '../../include/loader.php';

html_start("广告报告 - VeryIDE");
?>

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

	//module
	require 'naver.php';

	//连接数据库
	System :: connect();

	if( !Cached :: loader($appid,"table.adsarea") ){
		//更新分类数组缓存
		Cached :: table( $appid, 'mod:adsarea', array( 'jsonde' => array('config') ));		
	}
	
	//***********************
	
	$sel = getgpc("sel");

	$q = getgpc('q');
	$s = getgpc("s");

	$filter = getgpc("filter");
	$parent = getgpc("parent");
	$type = getgpc("type");
	
	$action = getgpc('action');
	
	$list = getgpc('list');
	
	$full = getnum("full",-1);
	$owner = getnum("owner",0);
	
	//日期筛选
	$start = getgpc('start');
	$end = getgpc('end');

	//时间
	if ($start && $end){
	
		//开始时间
		$e_array=explode("-",$start);
		$time1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
		
		//到期时间
		$e_array=explode("-",$end);
		$time2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
	
	}
	
	switch($action){
		
		case "create":
			
			//生成广告
			Adsview :: create("SELECT * FROM `mod:adsarea` WHERE parent>0");
			
			//缓存广告缓存
			Cached :: rows( $appid, "SELECT * FROM `mod:adsview`");
			
			$_G['project']['message']="已生成所选广告！";
			
		break;
	}
	
	?>

	<?php    
    echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$_G['product']['version']);    
    ?>
    
    <div id="search">
    
    <form name="formSeacrh" id="formSeacrh" method="get" action="?">
      <span class="action">
        <label>
        <select name="filter" id="filter" onchange="Mo('#formSeacrh').submit();" ignore="true">
            <optgroup label="所在分类" />
            <option value="">全部分类</option>
            <option value="dateline">新增广告</option>
            <option value="modify">更改广告</option>
        </select>
        </label>
        
        <label>
        <select name="parent" id="parent" onchange="Mo('#type').value(0);Mo('#formSeacrh').submit();" ignore="true">
            <optgroup label="所在频道" />
            <option value="0">全部频道</option>
            <?php
                foreach($_CACHE['adsview']['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($_CACHE['adsview']['adsarea'] as $key){
                    if($key["parent"]==$parent && $parent){
                        echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
                    }
                }
            ?>
        </select>
        </label>
        
       	<input type="button" value="我的广告" onclick="location.href='?s=account&q=<?php echo urlencode($_SESSION['Manager']['account']);?>';" class='cancel'>
     
        <input type="button" value="下载报告" onclick="location.href=location.href.replace('value.php','report.php');" class='button'>
       </span>

        <select name="s" id="s">
            <option value="name" checked>名称</option>
            <option value="id">ID</option>
            <option value="link">链接</option>
            <option value="tags">标签</option>
            <option value="zone">地区</option>
            <option value="contract">合同号</option>
            <option value="custom">客户名</option>
            <option value="account">用户</option>
        </select>
        <input name="q" type="text" class="text" title="请输入关键字" id="q" size="15" value="<?php echo $q;?>">
        
        <!--时间节点-->
        <input name="start" type="text" class="date" id="start" value="<?php echo $start;?>" size="10" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日" onchange="Mo('#formSeacrh').submit();">
        -
        <input name="end" type="text" class="date" id="end" value="<?php echo $end;?>" size="10" readonly="true" onclick="return showCalendar(this.id,'y-mm-dd');" title="年月日" onchange="Mo('#formSeacrh').submit();">
        <!--时间节点-->
        
        <button class="go" type="submit"></button>
    </form>
    
    </div>
    <script type="text/javascript">
	Mo("#s").value("<?php echo $s;?>");	
	Mo("#filter").value("<?php echo $filter;?>");
    
    Mo.reader(
        function(){
            Mo("#type").value("<?php echo $type;?>");
            Mo("#parent").value("<?php echo $parent;?>");
        }
    );
    </script>

    <?php
    
    $sql="SELECT * FROM `mod:adsview` WHERE 1=1";
    
    if ($q && $s){
        
        if( strpos($s,"id") !== false ){
            $sql.=" and  `".$s."` = '".$q."'";
        }else{
            $sql.=" and `".$s."` like '%".$q."%'";
        }     
        
    }
    
    if ($parent){
        $sql.=" and  parent ='".$parent."'";
    }
    
    if ($type){
        $sql.=" and  type ='".$type."'";
    }
    
    if ($owner){
        $sql.=" and  owner ='".$owner."'";
    }
    
    //广告筛选
    switch($filter){
        
        //新增广告
        case "dateline":
			
			if( $time1 && $time2 ){			
				$sql.=" and  dateline >=".$time1." and dateline <= ".$time2;			
			}
			
        break;
        
        //修改广告
        case "modify":
			
			if( $time1 && $time2 ){			
				$sql.=" and  modify >=".$time1." and modify <= ".$time2;			
			}
			
        break;
        
        //全部类型
        default:
			
			if( $time1 && $time2 ){			
				$sql.=" and  dateline >=".$time1." and dateline <= ".$time2." or ( modify >=".$time1." and modify <= ".$time2.")";			
			}
			
        break;
		
    }
    
    //if ($ect) $sql.= $ect;
    
    $sql.=" order BY id desc";
    
    //echo $sql;
    
    
    //查询数据库_总记录数
    $result=mysql_query(str_replace("*","count(type) as count",$sql),$mysql);
    $row=mysql_fetch_assoc($result);
    $row_count=$row["count"];
    
    

    //分页参数
    $page=getpage("page");
    $page_start=$_G['setting']['global']['pagesize']*($page-1);
    $sql=$sql." limit $page_start,".$_G['setting']['global']['pagesize'];	
    
    $result = System :: $db -> getAll( $sql );
    
    //分页链接
    $url="?sel=".$sel."&s=".$s."&q=$q&e=$e&type=$type&parent=$parent&full=$full&owner=$owner&start=$start&end=$end&page=";
    
    ?>
    
    <table width="100%" border="0" cellpadding="1" cellspacing="1" id="table" class="table">
    
        <tr class="title" id="thead">
            <td>ID</td>
            <td>所在位置</td>
            <td>广告名称</td>
            <td>开始时间</td>
            <td>失效时间</td>
            <td>共投放</td>
            <td>停止时间</td>
            <td>添加时间</td>
            <td>修改时间</td>
            <td>创建者</td>
            <td>状态</td>
        </tr>			
        
        <?php
        
        $i=0;
        
        //今天
        $today = date("Y-m-d"); 
         
        foreach( $result as $row ){
            
            //到期倒记时
            $days=DateDiff("d",mktime(0, 0, 0, date("m"), date("j"), date("Y")),$row['expire']);

            echo '
              <tr class="'.zebra( $i, array( "line" , "band" ) ).'" pid="'.$row['id'].'" edit="adsview.edit.php?action=edit&id='.$row['id'].'&jump={jump}">
                <td>
                    '.$row['id'].'
                </td>
                <td>
                    <a href="?parent='.$row["parent"].'">'.$_CACHE['adsview']['adsarea'][$row["parent"]]["name"].'</a> &gt; <a href="?type='.$row["type"].'">'.$_CACHE['adsview']['adsarea'][$row["type"]]["name"].'</a>
                </td>
                <td id="tips-'.$row['id'].'">';
                    
                    if( $row['link'] ){
                        echo '<a href="'.$row['link'].'" target="_blank" title="'.$row['link'].'">'.format_substr($row['name'],22).'</a>';
                    }else{
                        echo '<span>'.format_substr($row['name'],22).'</span>';
                    }
                    
                echo '</td>';
                
                echo '<td>'.date("y-m-d",$row['start']).'</td>';
                
                echo '<td>';
                    if ( $days<=$_G['module']['adsview']["expire"] && $days>-1 ){
                        echo '<span class="text-no">'.date("y-m-d",$row['expire']).'</span>';
                    }else{
                        echo date("y-m-d",$row['expire']);
                    }
                echo '</td>
                <td class="text-yes">                
                    '.FormatDateDiff($row['start'],$row['expire']).'
                </td>
                <td class="text-key">                
                    '.( $row["end"] ? date("y-m-d H:i",$row["end"]) : '' ).'
                </td>
                <td>                
                    '.date("y-m-d H:i",$row['dateline']).'
                </td>
                <td class="text-yes">
                   '.( $row['modify'] ? date("y-m-d H:i",$row['modify']) : '' ).'
                </td>
				<td>
					<a href="?s=account&q='.urlencode($row['account']).'">'.$row['account'].'</a>
				</td>
				';
                
                echo '<td id="mark-'.$row['id'].'">';
                    //广告状态
                    if( $row['expire']>time() ){
                        echo $_G['module']['adsview']["state"][$row['state']];
                    }else{
                        echo $_G['module']['adsview']["state"][4];
                    }
					
                echo '</td>
              </tr>
            ';
              
        }
        
        ?>
    </table>
            
	<?php
	
	//关闭数据库
	System :: connect();
			
	?>
    
    <div id="saving">
		<?php
            echo multipage($page,$row_count,$_G['setting']['global']['pagesize'],$url,"page");
        ?>
    </div>

<!--body end-->

<?php html_close();?>