<?php
// +----------------------------------------------------------------------
// | WANCMS游戏平台管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.wancms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 顽皮蛋 <shf_l@163.com> <http://www.wancms.com>
// +----------------------------------------------------------------------
// | 修改时间: 2015/12/15
// +----------------------------------------------------------------------

namespace Phonegame\Controller;
use Common\Controller\AdminbaseController;
/**
 * 活动管理控制器
 * @author 顽皮蛋 <shf_l@163.com>
 */
class ActivitiesadminController extends AdminbaseController{
	/**
     * 活动管理
     * @author 顽皮蛋 <shf_l@163.com>
     */
	public function index(){
		$type_id = I('type_id');
        $title = I('title');
        $map['title']    =   array('like', '%'.(string)$title.'%');
        if($type_id)$map['type_id'] = $type_id;
        $count=M("m_activities")->where($map)->count();
        $page       = new \Think\Page($count,20);//实例化分页类 传入总记录数和每页显示的记录数
        /*条件*/
        $parameter['title'] = $title;
        $parameter['type_id'] = $type_id;
        foreach($parameter as $key=>$val){
            $page->parameter[$key]   =   urlencode($val);
        }
        $show  = $page->show();// 分页显示输出
        $lists = M("m_activities")
        ->where($map)
        ->order("addtime DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
        $this->assign('lists', $lists);
        $this->assign("page", $show);
        $this->assign('type_id', $type_id);
        $this->assign('title', $title);
        $this->display();
	}
	/**
     * 增加活动
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function add(){
        if(IS_POST){
            $_POST['btime'] = strtotime($_POST['btime']);
            $_POST['etime'] = strtotime($_POST['etime']);
            if (D("Common/MActivities")->create()){
                if (D("Common/MActivities")->add()!==false) {
                    $this->success("添加成功！", U("Activitiesadmin/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error(D("Common/MActivities")->getError());
            }
        }else{
        $this->assign('info', '');   
        $this->display();
        }
    }
    /**
     * 修改活动
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function edit(){
        if(IS_POST){
            $_POST['btime'] = strtotime($_POST['btime']);
            $_POST['etime'] = strtotime($_POST['etime']);
            if (D("Common/MActivities")->create()) {
                if (D("Common/MActivities")->save()!==false) {
                    $this->success("保存成功！", U("Activitiesadmin/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error(D("Common/MActivities")->getError());
            }
        }else{
        $id=  intval(I("get.id"));
        $info = M('m_activities')->where(array("id"=>$id))->find(); 
        $this->assign('info', $info);   
        $this->display('add');
        }    	
    }
    /**
     * 删除活动
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function del(){
        if(isset($_POST['ids'])){
            $ids = implode(",", $_POST['ids']);
            if (M('m_activities')->where("id in ($ids)")->delete()!==false) {
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        }else{
            $id = intval(I("get.id"));
            if (M('m_activities')->delete($id)!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }    	
    }
    /**
     * 开启、关闭
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function toggle(){
        if(isset($_POST['ids']) && $_GET["display"]){
            $ids = implode(",", $_POST['ids']);
            $data['status']=1;
            if (M('m_activities')->where("id in ($ids)")->save($data)!==false) {
                $this->success("开启成功！");
            } else {
                $this->error("开启失败！");
            }
        }
        if(isset($_POST['ids']) && $_GET["hide"]){
            $ids = implode(",", $_POST['ids']);
            $data['status']=0;
            if (M('m_activities')->where("id in ($ids)")->save($data)!==false) {
                $this->success("关闭成功！");
            } else {
                $this->error("关闭失败！");
            }
        }

    }


}