<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends salebase {

    
    public function action_index(){
        // $this->checkAuth('run');
        // $this->_getItems();
        $searchsql = "";
        if ($this->sgroupid>0) {
            $searchsql .= " AND v.groupid='".$this->sgroupid."'";
        }
        if (!empty($this->susername)) {
            $searchsql .= " AND lower(v.adminname) LIKE '%".strtolower($this->susername)."%'";
        }
        $model = parent::model('admin', 'sa');
        list($datacount, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
        $url = XRequest::getPhpSelf();
        $url .= '?c=admin&'.$this->_urlitem;
        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($datacount/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$datacount,
            'showpage'=>XPage::admin($datacount, $this->pagesize, $this->page, $url, 10),
            'itemurl'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'sgroupid'=>$this->sgroupid,
            'susername'=>$this->susername,
            'admin'=>$data,
        );
        unset($model);
        TPL::assign($var_array);
        TPL::display($this->cptpl.'admin/admin.tpl');
    }

    public function action_edit()
    {
        $id = XRequest::getInt('id');
        $this->_validID($id);
        $this->_getItems();
        $model = parent::model('admin', 'sa');
        list($datacount, $group) = $model->getroleList(array('page'=>$this->page, 'pagesize'=>$this->pagesize,));

        $data = $model->getData($id);
        if (empty($data)) {
            XHandle::halt('对不起，读取数据不存在！', '', 1);
        }
        else {
            parent::loadLib('mod');
            $var_array = array(
                'admin'=>$data,
                'id'=>$id,
                'comeurl'=>$this->_comeurl,
                'group'=>$group
            );
            TPL::assign($var_array);
            TPL::display($this->cptpl.'admin/edit.tpl');
        }
        unset($model);
    }


    public function action_saveedit() {
        $id = XRequest::getInt('id');

        $args = XRequest::getGpc(
            array(
                'password', 'confirmpassword', 'groupid', 'super', 'flag', 'memo'
            )
        );

        $args = $this->_validEdit($id, $args);
        if($args['groupid']=='0'){
           $args['super']=1;
        }
        $this->_getItems();
        $model = parent::model('admin', 'sa');
        $result = $model->doEdit($id, $args);
        if (true === $result) {
            $data['code']=1;
            $data['msg']="编辑成功";
        }
        else {
            $data['msg']="编辑失败";
        }
        echo json_encode($data);
        unset($model);
    }


    public function action_add() {
        $model = parent::model('admin', 'am');
        list($datacount, $data) = $model->getroleList(array('page'=>$this->page, 'pagesize'=>$this->pagesize,));
        $var_array = array(
            'data'=>$data,
        );
        unset($model);
        TPL::assign($var_array);
        TPL::display($this->cptpl.'admin/add.tpl'); 
    }

    public function action_saveadd() {
        $args = XRequest::getGpc(
            array('adminname', 'password', 'confirmpassword', 
                'groupid', 'super', 'flag', 'memo')
        );
        $args = $this->_validAdd($args);
        if($args['groupid']=='0'){
           $args['super']=1;
        }
        $model = parent::model('admin', 'sa');
        if (true === $model->doCheckUserName($args['adminname'])) {
            $data['msg']="对不起，该帐号已存在，请填写另外一个。";
            echo json_encode($data);die;
        }
        else {
            $result = $model->doAdd($args);
            if (true === $result) {
                $data['code']=1;
                $data['msg']="添加成功";
            }
            else {
                $data['msg']="添加失败";
            }
        }
        echo json_encode($data);
        unset($model);
    }

    public function action_del() {
        // $array_id = XRequest::getArray('id');
        // $ids = XRequest::getArray('id');
        // var_dump($ids);die;
        // $this->_validID($array_id);
        // $model = parent::model('admin', 'sa');
        // for($ii=0; $ii<count($array_id); $ii++){
        //     $id = intval($array_id[$ii]);
        //     var_dump($id);die;
        //     // if (true === $model->doCheckUid($id)){
        //     //     XHandle::halt('对不起，您不能删除自己的帐号！', '', 1);
        //     // }
        //     $result = $model->doDel($id);            
        // }
        
        // if (true === $result) {
        //     $data['code']=1;
        //     $data['msg']='删除成功';
        // }
        // else {
        //     $data['msg']='删除失败';
        // }
        // echo json_encode($data);
        // unset($model); 
    }








    private function _validAdd($array) {
        if (!XValid::isUserArgs($array['adminname'])) {
            $data['msg']="帐号格式不正确，只能由字母、数字、下横线、中文组成";
            echo json_encode($data);die;
        }
        if (!XValid::isLength($array['adminname'], 4, 16)){
            $data['msg']="帐号长度不正确，必须为4-16个字符";
            echo json_encode($data);die;
        }
        if (!XValid::isLength($array['password'], 4, 16)){
            $data['msg']="密码长度不正确，必须为4-16个字符";
            echo json_encode($data);die;
        }
        if ($array['confirmpassword'] != $array['password']){
            $data['msg']="确认密码不正确";
            echo json_encode($data);die;
        }
        $array['groupid'] = intval($array['groupid']);
        $array['super'] = intval($array['super']); 
        $array['flag'] = intval($array['flag']);
        unset($array['confirmpassword']);
        return $array;
    }


    private function _validEdit($id, $array) {
        if ($id<1) {
            $data['msg']="ID丢失";
            echo json_encode($data);die;
        }
        if (!empty($array['password'])) {
            if (!XValid::isLength($array['password'], 4, 16)){
                $data['msg']="密码长度不正确，必须为4-16个字符";
                echo json_encode($data);die;

            }
            if ($array['confirmpassword'] != $array['password']){
                $data['msg']="确认密码不正确";
                echo json_encode($data);die;
            }
            
            unset($array['confirmpassword']);
        }
        else {
            unset($array['password']);
            unset($array['confirmpassword']);
        }
        $array['groupid'] = intval($array['groupid']);
        $array['super'] = intval($array['super']); 
        $array['flag'] = intval($array['flag']); 
        return $array;
    }


    private function _validID($id) {
        if (empty($id)) {
            XHandle::halt('ID丢失，请选择要操作的ID', '', 1);
        }
    }

    private function _getItems() {
        $this->sgroupid = XRequest::getInt('sgroupid');
        $this->susername = XRequest::recArgs('susername');
        $this->_urlitem = "sgroupid=".$this->sgroupid."&susername=".urlencode($this->susername)."";
        $this->_comeurl = $this->_urlitem."&page=".$this->page."";
    }

   
}