<?php

namespace Home\Controller;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class SiteGiftController extends BaseController {

    //站点申请
    public function index($p=0,$game_name='',$gift_name='',$recommend_status=''){
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row = 10;

        $model = D('SiteGift');
        $map['promote_id'] = PID;
        if($game_name != ''){
            $map['game_name'] = array('like','%'.$game_name.'%');
            $parameter['game_name'] = $game_name;
        }
        if($gift_name != ''){
            $map['gift_name'] = array('like','%'.$gift_name.'%');
            $parameter['gift_name'] = $gift_name;
        }
        if($recommend_status != ''){
            $map['recommend_status'] = array('like','%'.$recommend_status.'%');;
            $parameter['recommend_status'] = $recommend_status;
        }
        $data = $model->where($map)->order('create_time desc')->page($page,$row)->select();
        /* 查询记录总数 */
        $count = $model->where($map)->count();
        //分页

				$parameter['p']=I('get.p',1);
				$parameter['row']=I('get.row');
				
				
				$page = set_pagination($count,$row,$parameter);
				if($page) {$this->assign('_page', $page);}
				
        $this->assign('p',$p);
        $this->assign('data',$data);
        $this->meta_title = "礼包列表";
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $model = D('SiteGift');
            $res = $model->saveData();
            if($res !== false){
                $this->success('添加成功!');
            }else{
                $this->error('添加失败:'.$model->getError());
            }
        }else {
            $map['promote_id'] = PID;
            $map['pack_url'] = ['neq', ''];
            $game = D('SiteGame')->get_promote_data();
            // var_dump($game);die;
            $this->assign('game', $game);
            $this->meta_title = "添加礼包";
            $this->display();
        }
    }

    public function edit($id=0){
        $model = D('SiteGift');
        if(IS_POST){
            $p = I("post.p",1,'intval');
            $res = $model->saveData();
            if($res !== false){
                $this->success('保存成功!',U('SiteGift/index',array('p'=>$p)));
            }else{
                $this->error('保存失败:'.$model->getError());
            }
        }else {
            $p = I("get.p",1,'intval');
            $this->assign('p',$p);
            $map['promote_id'] = PID;
            $map['pack_url'] = ['neq', ''];
            
            $game = D('SiteGame')->where($map)->select();
            $data = $model->find($id);
            $map['game_id'] = $data['site_game_id'];
						$game_source = D('SiteGame')->field('game_source')->where(array('id'=>$data['site_game_id']))->find();
						$data['game_source'] = $game_source['game_source'];
            $serverName = M('SiteServer','tab_')->field('id,server_name')->where($map)->select();
            $this->assign('serverNames',$serverName);
            $this->assign('data',$data);
            $this->assign('game', $game);

            $this->meta_title = "编辑礼包";
            $this->display();
        }
    }

    public function del($id){
        $model = D('SiteGift');
        $map['id'] = $id;
        $map['promote_id'] = PID;
        $res = $model->where($map)->delete();
        if($res){
        	$this->ajaxReturn(array('status'=>1,'msg'=>'操作成功'));
        }else{
        	$this->ajaxReturn(array('status'=>0,'msg'=>'操作失败'));
        }
    }
    public function get_server(){
        if(IS_AJAX){
            $game_id = $_POST['game_id'];
            $map['game_id']= $game_id;
						$map['promote_id'] = PID;
						$map['server_version'] = $_POST['sv'];
            $serverName = M('SiteServer','tab_')->field('id,server_name')->where($map)->select();
            $this->ajaxReturn(array('status'=>1,'data'=>$serverName));
        }
    }

}