<?php
namespace Site\Controller;

class ServerController extends BaseController{

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        if(empty(PID)){
            $this->redirect('Media/index/index');
        }
    }

    public function index($p=1){
                $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row = 10;
        $map['promote_id'] = PID;
        $map['status'] = 1;
        $game=D("SiteGame")->field('id,game_name')->distinct(true)->where($map)->select();
        foreach ($game as $k => $v) {
            if(empty($v['id'])){
                unset($game[$k]);
            }
            if(!empty($v['id'])){
            $gameName[$k] = $v['id'];
            }
        }
        $SiteServer = D('SiteServer');
        if(!empty($gameName)){
        $map['game_id'] = array('in',implode(',',$gameName));
        }
        $map['start_time'] = array('lt',time());
        $data = $SiteServer->where($map)->page($page,$row)->select();
        foreach ($data as $k => $v) {
            $game_id=get_site_game_id2($v['game_id']);
            $gameId=get_site_game_game_id( $v['game_id']);
            if($game_id != 0){
               $data[$k]['game_id']= $gameId;
               $data[$k]['game_dow_url'] ='';
            }else{
                $data[$k]['game_dow_url'] = get_site_game_dow_url($v['game_id']);
            }

               $data[$k]['site_game_id']= $v['game_id'];
        }
        $count = $SiteServer->field('id')->where($map)->select();
        $count =count($count);

         if($count > $row){
            $page = new \Think\Page($count, $row);
            $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
            $this->assign('_page', $page->show());
        }

        $this->assign('server',$data);
        $this->meta_title = "区服列表"; 
        $this->display();

    }
}