<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$where=" where a.uniacid=:uniacid ";
if(!empty($_GPC['keywords'])){
    $where.=" and a.company_name LIKE  concat('%', :name,'%') ";
    $data[':name']=$_GPC['keywords'];   
}
if(!empty($_GPC['time'])){
   $start=strtotime($_GPC['time']['start']);
   $end=strtotime($_GPC['time']['end']);
  $where.=" and a.sh_time >={$start} and a.sh_time<={$end} ";

}
if(!empty($_GPC['state'])){
   $start=strtotime($_GPC['time']['start']);
   $end=strtotime($_GPC['time']['end']);
  $where.=" and a.state ={$_GPC['state']}";

}
$data[':uniacid']=$_W['uniacid'];
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$type=isset($_GPC['type'])?$_GPC['type']:'all';
$sql="SELECT a.*,b.days as typename,c.type_name FROM ".tablename('zhtc_yellowstore'). " a"  . " left join " . tablename("zhtc_yellowset") . " b on b.id=a.rz_type left join " . tablename("zhtc_yellowtype") . " c on c.id=a.type_id ".$where. " ORDER BY a.sort ASC,a.id DESC";
$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('zhtc_yellowstore'). " a" ." left join " . tablename("zhtc_yellowset") . " b on b.id=a.rz_type  left join " . tablename("zhtc_yellowtype") . " c on c.id=a.type_id ".$where,$data);
$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='delete'){
    $res=pdo_delete('zhtc_yellowstore',array('id'=>$_GPC['id']));
    if($res){
         message('删除成功！', $this->createWebUrl('yellowstore'), 'success');
        }else{
              message('删除失败！','','error');
        }
}
if($_GPC['op']=='tg'){
  $rst=pdo_get('zhtc_yellowstore',array('id'=>$_GPC['id']));
  $time=pdo_get('zhtc_yellowset',array('id'=>$rst['rz_type']));
  $newtime=$time['days']*24*60*60;
    $res=pdo_update('zhtc_yellowstore',array('state'=>2,'sh_time'=>time(),'dq_time'=>time()+$newtime),array('id'=>$_GPC['id']));
    if($res){
         message('通过成功！', $this->createWebUrl('yellowstore'), 'success');
        }else{
          message('通过失败！','','error');
        }
}
if($_GPC['op']=='jj'){
    $res=pdo_update('zhtc_yellowstore',array('state'=>3,'sh_time'=>time()),array('id'=>$_GPC['id']));
    if($res){
         message('拒绝成功！', $this->createWebUrl('yellowstore'), 'success');
        }else{
              message('拒绝失败！','','error');
        }
}


if(checksubmit('export_submit', true)) {
  if($_GPC['time']['start']==$_GPC['time']['end']){
        $dcstart=strtotime(date('Y-m-d 00:00:00'));
        $dcend=strtotime(date('Y-m-d 23:59:59'));
        $note=$_GPC['time']['start']."黄页数据";
    }else{
        $dcstart=strtotime($_GPC['time']['start']." 00:00:00");
        $dcend=strtotime($_GPC['time']['end']." 23:59:59");

        $note=$_GPC['time']['start']."至".$_GPC['time']['end']."黄页数据";
    }
  $count = pdo_fetchcolumn("SELECT COUNT(*) FROM". tablename("zhtc_yellowstore")." WHERE uniacid={$_W['uniacid']} and state=2 and sh_time>={$dcstart} and sh_time<={$dcend}");
  $pagesize = ceil($count/5000);
        //array_unshift( $names,  '活动名称'); 

  $header = array(
    'id'=>'帖子id',
    'logo' => 'logo图片', 
    'company_name' => '公司名称', 
    'company_address' => '公司地址', 
    'coordinates' => '公司坐标',
    'link_tel' => '联系电话', 
    'dq_time' => '到期时间', 
    'typeName' => '一级分类',
    'nTypeName' => '二级分类',
    'cityname' => '所属城市', 
    'sh_time' => '入驻时间'
    );

  $keys = array_keys($header);
  $html = "\xEF\xBB\xBF";
  foreach ($header as $li) {
    $html .= $li . "\t ,";
  }
  $html .= "\n";
for ($j = 1; $j <= $pagesize; $j++) {
    $sql = "select a.*,b.type_name as typeName,c.name as nTypeName from " . tablename("zhtc_yellowstore")."  a"  . " left join " . tablename("zhtc_yellowtype")." b on a.type_id=b.id left join " . tablename("zhtc_yellowtype2")." c on a.type2_id=c.id  WHERE a.uniacid={$_W['uniacid']} and a.state=2 and a.sh_time>={$dcstart} and a.sh_time<={$dcend} limit " . ($j - 1) * 5000 . ",5000 ";
    $list = pdo_fetchall($sql);            
	if (!empty($list)) {
	    $size = ceil(count($list) / 500);
	    for ($i = 0; $i < $size; $i++) {
	      $buffer = array_slice($list, $i * 500, 500);
	      $user = array();
	      foreach ($buffer as $k =>$row) {
	     	$row['logo']=str_replace(',','#',$row['logo']);
	     	$row['coordinates']=str_replace(',','#',$row['coordinates']);
	        $row['dq_time']=date('Y-m-d H:i:s',$row['dq_time']);
	        $row['sh_time']=date('Y-m-d H:i:s',$row['sh_time']);
	        $row['cityname']=$row['cityname']?:'全国';
	        foreach ($keys as $key) {
	          $data5[] = $row[$key];
	        }
	        $user[] = implode("\t ,", $data5) . "\t ,";
	        unset($data5);
	      }
	      	$html .= implode("\n", $user) . "\n";
	    }
	}
}
  
  header("Content-type:text/csv");
  header("Content-Disposition:attachment; filename=".$note.".csv");
  echo $html;
  exit();
}
include $this->template('web/yellowstore');