<?php
// +-----------------------------------------------------------------------------------
// | Software: [ThinkPHP framework]
// | Version: V4.0
// | Site: http://www.czreg.com
// +-----------------------------------------------------------------------------------
// | Author: 飘叶 <cds425@163.com>
// | Copyright (c) 2012-2014, http://www.czreg.com All Rights Reserved.
// | time:2014.11.08
// | explain:网站配置类
// +-----------------------------------------------------------------------------------

namespace Admin\Controller;
use Think\Controller;
class ConfigController extends AdminController {

    public function _initialize () {
        parent::_initialize();
        $this->leftmain=2;
    }

    //网站信息配置
    public function index(){
        $this->topmain=1;
        $config=M('Config');
        if(IS_POST){
            $data=I('post.');
            $id=I('get.id',1);
            $saveconfig = $config->where(array('id' =>$id))->save($data);
            if($saveconfig!==false){
                //$this->success('更改成功！',U('index'));
                $da['isType'] = "success";
                $da['isData'] = '更改成功！';
                exit(json_encode($da));
            }else{
                //$this->error('更改失败！');
                $da['isType'] = "warning";
                $da['isData'] = '更改失败';
                exit(json_encode($da)); 
            }
        }else{
            $this->list = $config->limit(1)->find();
            $this->display();
        }
    }

    //第三方信息配置
   public function auth(){
        $this->topmain=2;
        $webconfigMonde=M('outh_config');
        if(IS_POST){
            $data=array(
                'value' => I('post.value'),
            );
                $id=I('get.id');
                $savetitle="更新";
                $savewebconfig=$webconfigMonde->where(array('id'=>$id))->save($data);
            if($savewebconfig!==false){
                //$this->success($savetitle.'成功',U('auth'));
                $auth = $webconfigMonde->where(array('id'=>$id))->find();
                if($auth['type']==1){
                	if($auth['value']==1){
                		$data['value']="开启";
                	}else{
                		$data['value']="关闭";
                	}
                }
                echo "<script language=javascript>parent.showlog();parent.$('#vl".$id."').css('color','red'); ;parent.$('#vl".$id."').text('".$data['value']."');var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);</script>"; 
                exit;
            }else{
                $this->error($savetitle.'失败');
            }
        }else{
            //判断是否删除操作
            if(I('get.action')=="del"){
                $delwebconfig=$webconfigMonde->where(array('id'=>I('get.id')))->delete();
                if($delwebconfig!==false){
                    $this->success('删除成功',U('auth'));
                }else{
                    $this->error('删除失败');
                }
            }else{
                if(I('get.do')=='edit'){
                    $this->webconfig=$webconfigMonde->where('id='.I('get.id'))->find();
                    $this->display('authedit');
                    exit;
                }else{
                    $list=$webconfigMonde->select();
                    $this->list=$list;
                    $this->show();
                }
            }
        }
    }

    //管理员设置
    public function admin(){
    	$this->topmain=3;
        $adminMonde=M('Admin');
        if(IS_POST){
            if(I('get.id')!=''){
                if(I('post.loginpass')!=''){
                    $data['loginpass'] =md5(I('post.loginpass'));
                }
                $data['status']=I('post.status');
                $data['nickname']=I('post.nickname');
                $savetitle="更新";
                $saveflash=$adminMonde->where(array('id'=>I('get.id')))->save($data);
            }else{
                $rules = array(
                     array('loginname','','帐号已经存在！',0,'unique',1), // 在新增的时候验证name字段是否唯一
                );
                $data=array(
                    'loginname'  => I('post.loginname'),
                    'loginpass'  => md5(I('post.loginpass')),
                    'nickname'   => I('post.nickname'),
                    'status'     => I('post.status'),
                );
                $savetitle="新增";
                if (!$adminMonde->validate($rules)->create()){
                    $this->error($adminMonde->getError());
                    exit();
                }else{
                    $saveadmin=$adminMonde->add($data);
                }
            }

            if($saveadmin!==false){
                $auth=session(user_auth);
                if(I('get.id')!=''){
                    M('Adminlog')->add(array('adminid'=>$auth['id'],'timeline'=>time(),'value'=>$savetitle."管理员id:".I('get.id'),'createip'=>get_client_ip()));
                    echo "<script language=javascript>parent.showlog();var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index);</script>"; 
                    exit;
                }else{
                    M('Adminlog')->add(array('adminid'=>$auth['id'],'timeline'=>time(),'value'=>$savetitle."管理员id:".$saveadmin,'createip'=>get_client_ip()));
                    $da['isType'] = "success";
                    $da['isData'] = '新增成功！';
                    exit(json_encode($da));
                    //$this->success($savetitle.'成功',U('admin'));
                }
            }else{
                if(I('get.id')!=''){
                    $this->error($savetitle.'失败');
                }else{
                    $da['isType'] = "warning";
                    $da['isData'] = '新增失败！';
                    exit(json_encode($da));
                }   
            }
        }else{
            //判断是否删除操作
            if(I('get.action')=="del"){
                $deladmin=$adminMonde->where(array('id'=>I('get.id')))->delete();
                if($deladmin!==false){
                    $auth=session(user_auth);
                    M('Adminlog')->add(array('adminid'=>$auth['id'],'timeline'=>time(),'value'=>"删除管理员id:".I('get.id'),'createip'=>get_client_ip()));
                    $da['isType'] = "success";
                    $da['isData'] = '删除成功！';
                    exit(json_encode($da));
                }else{
                    $da['isType'] = "warning";
                    $da['isData'] = '删除失败！';
                    exit(json_encode($da));
                }
            }else{
                if(I('get.do')=='edit'){
                    $this->admin=$adminMonde->where('id='.I('get.id'))->find();
                    $this->display('adminedit');
                    exit;
                }
                $this->dodo=I('get.do');
                $list=$adminMonde->select();
                $this->list=$list;
                $this->show();
            }
        }
    }

    //管理日志
    public function adminlog(){
        $this->topmain=4;
        $log = M('Adminlog');
        $prefix=C('DB_PREFIX');
        if(I('get.action')=="truncate"){
            //
            $model =new \Think\Model();
            $sql = "truncate table ".$prefix."adminlog";
            $model->execute($sql);
            $this->success('清理成功',U('adminlog'));
            exit;
        }
        $p=empty($_GET['p'])?'1':$_GET['p'];
        $this->list = $log->field('loginname,timeline,createip,value')->join('__ADMIN__ ON __ADMIN__.id = __ADMINLOG__.adminid','LEFT')->order($prefix.'adminlog.id desc')->page($p.',10')->select();
        $count=$log->field('id')->count();
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数
        $Page->rollPage=5;//页码每页显示数量
        $Page->lastSuffix = false;//尾页是否显示总页数
        $Page->setConfig('header','条记录');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setconfig('first','首页');
        $Page->setconfig('last','尾页');
        $Page->setConfig('theme','<div class="digg">%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% </div>');
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->show();
    }
}